package com.huimv.receive.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.BaseVisiting;
import com.huimv.receive.entity.BaseWashoutPoint;
import com.huimv.receive.entity.dto.GetFlowListDto;
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.mapper.BaseVisitingMapper;
import com.huimv.receive.mapper.BaseWashoutPointMapper;
import com.huimv.receive.service.IBaseWashoutPointService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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;

    @Override
    public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        List<BaseVisiting> visitings = visitingMapper.selectList(new QueryWrapper<>());
        List<GetWashoutPointDto> washoutPointDtos = new ArrayList<>();
        if (ObjectUtil.isEmpty(visitings)) {
            return new Result(ResultCode.SUCCESS, washoutPointDtos);
        } else {
            for (BaseVisiting visiting : visitings) {
                Integer type = visiting.getId();
                List<BaseWashoutPoint> list = washoutPointMapper.selectList(new QueryWrapper<BaseWashoutPoint>()
                        .like("farm_ids", farmId).eq("visiting_type", 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());
                        washListDtos.add(washListDto);
                    }
                    GetWashoutPointDto getWashoutPointDto = new GetWashoutPointDto();
                    getWashoutPointDto.setFarmIds(farmId);
                    getWashoutPointDto.setNumber(list.size());
                    getWashoutPointDto.setVisitingType(type);
                    getWashoutPointDto.setWashListDtos(washListDtos);
                    washoutPointDtos.add(getWashoutPointDto);
                }
            }
        }
        return new Result(ResultCode.SUCCESS, washoutPointDtos);
    }

    @Override
    public Result add(HttpServletRequest httpServletRequest, List<GetWashoutPointDto> washoutPointDtos) {
        String farmIds = TokenSign.getFarmIds(httpServletRequest);
        //访问类型集合
        QueryWrapper<BaseWashoutPoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_ids",farmIds);
        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(farmIds);
                washoutPoint.setPointLevel(i);
                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("vistitType");
        List<BaseWashoutPoint> baseWashoutPointList = washoutPointMapper.listDest(farmId,vistitType);
        List<DestVo> destVoList = new ArrayList<>();
        for (BaseWashoutPoint baseWashoutPoint : baseWashoutPointList) {
            DestVo destVo = new DestVo();
            destVo.setId(baseWashoutPoint.getLocationId());
            destVo.setLocationName(baseWashoutPoint.getLocationName());
            destVoList.add(destVo);

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

}
