package com.huimv.receive.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultCode;
import com.huimv.receive.entity.BaseVisitingMid;
import com.huimv.receive.entity.BaseWashoutPoint;
import com.huimv.receive.mapper.BaseVisitingMidMapper;
import com.huimv.receive.mapper.BaseWashoutPointMapper;
import com.huimv.receive.service.IBaseVisitingMidService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-04
 */
@Service
public class BaseVisitingMidServiceImpl extends ServiceImpl<BaseVisitingMidMapper, BaseVisitingMid> implements IBaseVisitingMidService {

    @Autowired
    private BaseVisitingMidMapper midMapper;
    @Autowired
    private BaseWashoutPointMapper pointMapper;

    @Override
    public Result add(BaseVisitingMid baseVisitingMid) {
        Integer visitingType = baseVisitingMid.getVisitingType();
        String visitingName = baseVisitingMid.getVisitingName();
        Integer farmId = baseVisitingMid.getFarmId();
        QueryWrapper<BaseVisitingMid> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("visiting_type", visitingType).eq("visiting_name", visitingName).eq("farm_id", farmId);
        BaseVisitingMid visitingMid = midMapper.selectOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(visitingMid)) {
            return new Result(10001,"同类型的流程名称已存在！",false);
        } else {
            midMapper.insert(baseVisitingMid);
            return new Result(ResultCode.SUCCESS);
        }

    }

    @Override
    public Result update(BaseVisitingMid baseVisitingMid) {
        Integer visitingType = baseVisitingMid.getVisitingType();
        String visitingName = baseVisitingMid.getVisitingName();
        Integer farmId = baseVisitingMid.getFarmId();
        QueryWrapper<BaseVisitingMid> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("visiting_type", visitingType).eq("visiting_name", visitingName)
                .ne("id", baseVisitingMid.getId()).eq("farm_id", farmId);
        Integer count = midMapper.selectCount(queryWrapper);
        if (0 == count) {
            midMapper.updateById(baseVisitingMid);
//            QueryWrapper<BaseWashoutPoint> queryWrapper1 = new QueryWrapper<>();
//            queryWrapper1.eq("mid_id", baseVisitingMid.getId());
//            BaseWashoutPoint washoutPoint = pointMapper.selectOne(queryWrapper1);
//            if (ObjectUtil.isNotEmpty(washoutPoint)) {
//                washoutPoint.setMidName(baseVisitingMid.getVisitingName());
//                pointMapper.updateById(washoutPoint);
//            }
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(10001,"同类型的流程名称已存在！",false);
        }
    }
}
