package com.huimv.receive.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.receive.common.token.TokenSign;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultCode;
import com.huimv.receive.entity.BaseProcess;
import com.huimv.receive.entity.BaseVisitingMid;
import com.huimv.receive.entity.BaseWashoutPoint;
import com.huimv.receive.entity.dto.GetWashoutPointDto;
import com.huimv.receive.entity.dto.WashListDto;
import com.huimv.receive.entity.vo.DestVo;
import com.huimv.receive.entity.vo.WashOutVo;
import com.huimv.receive.mapper.*;
import com.huimv.receive.service.IBaseWashoutPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-06
 */
@Service
public class BaseWashoutPointServiceImpl extends ServiceImpl<BaseWashoutPointMapper, BaseWashoutPoint> implements IBaseWashoutPointService {

    @Autowired
    private BaseWashoutPointMapper washoutPointMapper;
    @Autowired
    private BaseVisitingMapper visitingMapper;
    @Autowired
    private SysAccountMultilevelMapper accountMultilevelMapper;
    @Autowired
    private BaseProcessMapper processMapper;
    @Autowired
    private BaseVisitingMidMapper midMapper;

    @Override
    public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        List<BaseVisitingMid> midList = midMapper.selectList(new QueryWrapper<BaseVisitingMid>().eq("farm_id", farmId));
//        List<BaseVisiting> visitings = visitingMapper.selectList(new QueryWrapper<>());
        List<GetWashoutPointDto> washoutPointDtos = new ArrayList<>();
        if (ObjectUtil.isEmpty(midList)) {
            return new Result(ResultCode.SUCCESS, washoutPointDtos);
        } else {
            for (BaseVisitingMid visiting : midList) {
                Integer type = visiting.getId();
                List<BaseWashoutPoint> list = washoutPointMapper.selectList(new QueryWrapper<BaseWashoutPoint>()
                        .eq("farm_ids", farmId).eq("mid_id", type));
                List<WashListDto> washListDtos = new ArrayList<>();
                if (list.size() != 0) {
                    for (BaseWashoutPoint point : list) {
                        WashListDto washListDto = new WashListDto();
                        washListDto.setAddressId(point.getLocationId());
                        washListDto.setFlowLists(point.getFlowList());
                        washListDto.setNextName(point.getTailLocationName());
                        washListDto.setPcrTime(point.getPcrTime());
                        washListDto.setRemark(point.getRemark());
                        washListDtos.add(washListDto);
                    }
                    GetWashoutPointDto getWashoutPointDto = new GetWashoutPointDto();
                    getWashoutPointDto.setFarmIds(farmId);
                    getWashoutPointDto.setNumber(list.size());
                    getWashoutPointDto.setVisitingType(visiting.getVisitingType());
                    getWashoutPointDto.setMidId(visiting.getId());
                    getWashoutPointDto.setMidName(visiting.getVisitingName());
                    getWashoutPointDto.setWashListDtos(washListDtos);
                    getWashoutPointDto.setRemark(list.get(0).getRemark());
                    washoutPointDtos.add(getWashoutPointDto);
                }
            }
        }
        return new Result(ResultCode.SUCCESS, washoutPointDtos);
    }

    @Override
    public Result add(HttpServletRequest httpServletRequest, WashOutVo washOutVo) {
        List<GetWashoutPointDto> washoutPointDtos = washOutVo.getWashoutPointDtos();
        String farmId = washOutVo.getFarmId();
        //如果当前存在进行中流程，则初始化流程配置不能修改或删除
        QueryWrapper<BaseProcess> processQueryWrapper = new QueryWrapper<>();
        processQueryWrapper.eq("farm_id", farmId).eq("process_type",0);
        List<BaseProcess> processes = processMapper.selectList(processQueryWrapper);
        if (processes.size() > 0) {
            return new Result(10001, "当前存在进行中流程，无法修改", true);
        }

        //访问类型集合
        QueryWrapper<BaseWashoutPoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_ids",farmId);
        washoutPointMapper.delete(queryWrapper);
        for (GetWashoutPointDto washoutPointDto : washoutPointDtos) {
            //流程集合
            List<WashListDto> washListDtos = washoutPointDto.getWashListDtos();
            int i = 1;
            for (WashListDto washListDto : washListDtos) {
                BaseWashoutPoint washoutPoint = new BaseWashoutPoint();
                washoutPoint.setCreateTime(new Date());
                washoutPoint.setCreateUser(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                washoutPoint.setFlowList(washListDto.getFlowLists());
                washoutPoint.setLocationId(washListDto.getAddressId());
                washoutPoint.setPcrTime(washListDto.getPcrTime());
                washoutPoint.setVisitingType(washoutPointDto.getVisitingType());
                washoutPoint.setTailLocationName(washListDto.getNextName());
                washoutPoint.setFarmIds(farmId);
                washoutPoint.setPointLevel(i);
                washoutPoint.setMidName(washoutPointDto.getMidName());
                washoutPoint.setMidId(washoutPointDto.getMidId());
                washoutPoint.setRemark(washoutPointDto.getRemark());
                washoutPointMapper.insert(washoutPoint);
                i++;
            }
        }
        return new Result(10000, "添加成功！", true);
    }

    @Override
    public Result delete(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");//访问类型
        String level = paramsMap.get("level");//洗消站等级
        washoutPointMapper.delete(new QueryWrapper<BaseWashoutPoint>().like("farm_ids", farmId)
                .eq("visiting_type", type).eq("point_level", level));
        return new Result(10000, "删除成功！", true);
    }

    @Override
    public Result listDest(Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String vistitType = paramsMap.get("midId");
        String type = paramsMap.get("type");
//        if ("0".equals(vistitType)){
//            vistitType=  "7";
//        }

        List<BaseWashoutPoint> baseWashoutPointList = washoutPointMapper.listDest(farmId,vistitType);
        List<DestVo> destVoList = new ArrayList<>();
        if ((StringUtils.isBlank(type ) || "0".equals(type)) ){
            DestVo destVo3 = new DestVo();
            destVo3.setId(0);
            destVo3.setLocationName(baseWashoutPointList.get(0).getRemark());
            destVoList.add(destVo3);
        }
        for (BaseWashoutPoint baseWashoutPoint : baseWashoutPointList) {
            DestVo destVo2 = new DestVo();

            destVo2.setId(baseWashoutPoint.getLocationId());
            destVo2.setLocationName(baseWashoutPoint.getTailLocationName());
            destVoList.add(destVo2);
        }
        return new Result(ResultCode.SUCCESS,destVoList);
    }

    @Override
    public Result listWash(Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String vistitType = paramsMap.get("midId");
        List<BaseWashoutPoint> points = washoutPointMapper.listWash(farmId, vistitType);
        List<DestVo> destVoList = new ArrayList<>();
        for (BaseWashoutPoint baseWashoutPoint : points) {
            DestVo destVo = new DestVo();
            destVo.setId(baseWashoutPoint.getLocationId());
            destVo.setLocationName(baseWashoutPoint.getLocationName());
            destVoList.add(destVo);
        }
        return new Result(ResultCode.SUCCESS,destVoList);
    }

}
