package com.yunfeiyun.agmp.fms.service.impl;

import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.enums.FmsMessageResVoType;
import com.yunfeiyun.agmp.common.enums.TaskStatus;
import com.yunfeiyun.agmp.common.enums.TaskType;
import com.yunfeiyun.agmp.common.exception.BizException;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.common.web.system.mapper.SysResMapper;
import com.yunfeiyun.agmp.fms.domain.*;
import com.yunfeiyun.agmp.fms.domain.resvo.*;
import com.yunfeiyun.agmp.fms.mapper.*;
import com.yunfeiyun.agmp.fms.util.DataAuthUtil;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsTaskListReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsTaskStopReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.SelectOfTssStatReqVo;
import com.yunfeiyun.agmp.fms.service.IFmsTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 农事任务Service业务层处理
 * 
 * @author 杨晓辉
 * @date 2023-05-19
 */
@Service
public class FmsTaskServiceImpl implements IFmsTaskService
{
    @Autowired
    private FmsTaskMapper fmsTaskMapper;
    @Autowired
    private FmsTaskpickMapper fmsTaskpickMapper;
    @Autowired
    private FmsTaskpatrolMapper fmsTaskpatrolMapper;
    @Autowired
    private FmsWarnMapper fmsWarnMapper;
    @Autowired
    private FmsTaskassetMapper fmsTaskassetMapper;
    @Autowired
    private FmsTaskmachineMapper fmsTaskmachineMapper;
    @Autowired
    private FmsTaskrcdMapper fmsTaskrcdMapper;
    @Resource
    private SysResMapper sysResMapper;
    @Resource
    private DataAuthUtil dataAuthUtil;

    /**
     * 查询农事任务
     * 
     * @param taskId 农事任务主键
     * @return 农事任务
     */
    @Override
    public FmsTask selectFmsTaskByTaskId(String taskId)
    {
        return fmsTaskMapper.selectFmsTaskByTaskId(taskId);
    }

    @Override
    public int selectCountFmsTaskByPlanId(String planId) {
        return fmsTaskMapper.selectCountFmsTaskByPlanId(planId);
    }

    @Override
    public int selectCountFmsTask(FmsTaskListReqVo reqVo) {
        return fmsTaskMapper.selectCountFmsTask(reqVo);
    }

    @Override
    public List<FmsTaskTypeCountResVo> selectCountFmsTaskByType(FmsTaskListReqVo reqVo) {
        return fmsTaskMapper.selectCountFmsTaskByType(reqVo);
    }

    @Override
    public List<FmsTaskSumTaskrcdHourResVo> selectSumTaskrcdHour(FmsTaskListReqVo reqVo) {
        return fmsTaskMapper.selectSumTaskrcdHour(reqVo);
    }

    @Override
    public FmsTaskResVo getInfoByTaskId(String taskId) {
        return fmsTaskMapper.getInfoByTaskId(taskId);
    }

    @Override
    public List<FmsTaskResVo> getInfoByPlanId(String planId) {
        return fmsTaskMapper.getInfoByPlanId(planId);
    }

    /**
     * 查询农事任务列表
     * 
     * @param fmsTask 农事任务
     * @return 农事任务
     */
    @Override
    public List<FmsTask> selectFmsTaskList(FmsTask fmsTask)
    {
        return fmsTaskMapper.selectFmsTaskList(fmsTask);
    }

    @Override
    public List<FmsTaskResVo> selectTaskList(FmsTaskListReqVo reqVo) {
        return fmsTaskMapper.selectTaskList(reqVo);
    }

    @Override
    public List<FmsTaskResVo> selectTaskListAll(FmsTaskListReqVo reqVo) {
        return fmsTaskMapper.selectTaskListAll(reqVo);
    }

    @Override
    public List<FmsTaskResVo> selectTaskListScreenData(FmsTaskListReqVo reqVo) {
        return fmsTaskMapper.selectTaskListScreenData(reqVo);
    }

    @Override
    public List<FmsTaskMessageResVo> selectAllTaskList(FmsTaskListReqVo reqVo) {
        return fmsTaskMapper.selectAllTaskList(reqVo);
    }

    /**
     * 新增农事任务
     * 
     * @param fmsTask 农事任务
     * @return 结果
     */
    @Override
    public int insertFmsTask(FmsTask fmsTask)
    {
        return fmsTaskMapper.insertFmsTask(fmsTask);
    }

    @Transactional
    @Override
    public int insertFmsTask(FmsTask fmsTask, FmsTaskpick fmsTaskpick, FmsTaskpatrol fmsTaskpatrol, FmsWarn fmsWarn) {
        int i = fmsTaskMapper.insertFmsTask(fmsTask);
        if (fmsTaskpick != null) {
            fmsTaskpickMapper.insertFmsTaskpick(fmsTaskpick);
        }
        if (fmsTaskpatrol != null) {
            fmsTaskpatrolMapper.insertFmsTaskpatrol(fmsTaskpatrol);
        }
        if (fmsWarn != null) {
            fmsWarnMapper.insertFmsWarn(fmsWarn);
        }

        return i;
    }
    @Transactional
    @Override
    public int insertFmsTask(String resIdsStr, FmsTask fmsTask, FmsTaskrcd fmsTaskrcd,
                             FmsTaskpick fmsTaskpick, FmsTaskpatrol fmsTaskpatrol, FmsWarn fmsWarn,
                             List<FmsTaskasset> fmsTaskassets, List<FmsTaskmachine> fmsTaskmachines) {
        int i = fmsTaskMapper.insertFmsTask(fmsTask);
        if (fmsTaskpick != null) {
//            /* 采收需要进行入库处理 */
//            String warehouseId = reqVo.getWarehouseId();
//            /* 确定货物是否有该采收农产品，没有增加 */
//            FmsCrop fmsCrop = fmsCropMapper.selectFmsCropByCropId(reqVo.getCropId());
//            WmsGoods goods = new WmsGoods();
//            goods.setGoodsType("农产品");
//            goods.setGoodsName(fmsCrop.getCropName() + fmsCrop.getCropType());
//            List<WmsGoods> wmsGoods = wmsGoodsMapper.selectWmsGoodsList(goods);
//            if (wmsGoods.size() == 0) {
//                goods.setGoodsUnit("KG");
//                goods.setGoodsId(goods.getId());
//                wmsGoodsMapper.insertWmsGoods(goods);
//            }
//            /* 确定库存是否有该采收农产品，有增加库存量，没有增加库存 */

            fmsTaskpickMapper.insertFmsTaskpick(fmsTaskpick);
        }
        if (fmsTaskpatrol != null) {
            fmsTaskpatrolMapper.insertFmsTaskpatrol(fmsTaskpatrol);
        }
        if (fmsWarn != null) {
            fmsWarnMapper.insertFmsWarn(fmsWarn);
        }

        /* 增加农事提交记录，关联农资农具*/
        if (fmsTaskrcd != null) {
            fmsTaskrcdMapper.insertFmsTaskrcd(fmsTaskrcd);
            if (fmsTaskassets != null && fmsTaskassets.size() > 0) {
                fmsTaskassets.forEach(v -> {
                    v.setTaskrcdId(fmsTaskrcd.getTaskrcdId());
                    v.setTaskassetId(v.getId());
                });
                fmsTaskassetMapper.insertFmsTaskassetBatch(fmsTaskassets);
            }
            if (fmsTaskmachines != null && fmsTaskmachines.size() > 0) {
                fmsTaskmachines.forEach(v -> {
                    v.setTaskrcdId(fmsTaskrcd.getTaskrcdId());
                    v.setTaskmachId(v.getId());
                });
                fmsTaskmachineMapper.insertFmsTaskmachineBatch(fmsTaskmachines);
            }
        }

        /* 资源处理 */
        if (!StringUtils.isEmpty(resIdsStr)) {
            String[] resIds = resIdsStr.split(",");
            for (String resId : resIds) {
                SysRes sysRes = sysResMapper.selectSysResByResId(resId);
                if (!StringUtils.isEmpty(sysRes.getResBusId())) {
                    /* 如果图片已存在业务标记，新增一条资源记录 */
                    sysRes.setResId(sysRes.getId());
                    sysRes.setResBusId(fmsTaskrcd.getTaskrcdId());
                    sysResMapper.insertSysRes(sysRes);
                } else {
                    sysRes.setResBusId(fmsTaskrcd.getTaskrcdId());
                    sysResMapper.updateSysRes(sysRes);
                }
            }
        }

        return i;
    }

    @Transactional
    @Override
    public int insertFmsTaskPick(FmsTask fmsTask, FmsTaskpick fmsTaskpick) {
        int i = fmsTaskMapper.insertFmsTask(fmsTask);
        fmsTaskpickMapper.insertFmsTaskpick(fmsTaskpick);

        return i;
    }

    @Transactional
    @Override
    public int insertFmsTaskPatrol(FmsTask fmsTask, FmsTaskpatrol fmsTaskpatrol) {
        int i = fmsTaskMapper.insertFmsTask(fmsTask);
        fmsTaskpatrolMapper.insertFmsTaskpatrol(fmsTaskpatrol);
        return i;
    }
    @Transactional
    @Override
    public int insertFmsTaskWarn(FmsTask fmsTask, FmsWarn fmsWarn) {
        int i = fmsTaskMapper.insertFmsTask(fmsTask);
        fmsWarnMapper.insertFmsWarn(fmsWarn);
        return i;
    }

    /**
     * 修改农事任务
     * 
     * @param fmsTask 农事任务
     * @return 结果
     */
    @Override
    public int updateFmsTask(FmsTask fmsTask)
    {
        return fmsTaskMapper.updateFmsTask(fmsTask);
    }

    /**
     * 批量删除农事任务
     * 
     * @param taskIds 需要删除的农事任务主键
     * @return 结果
     */
    @Override
    public int deleteFmsTaskByTaskIds(String[] taskIds)
    {
        return fmsTaskMapper.deleteFmsTaskByTaskIds(taskIds);
    }

    /**
     * 删除农事任务信息
     * 
     * @param taskId 农事任务主键
     * @return 结果
     */
    @Override
    public int deleteFmsTaskByTaskId(String taskId)
    {
        return fmsTaskMapper.deleteFmsTaskByTaskId(taskId);
    }

    @Override
    public int stop(FmsTaskStopReqVo fmsTaskStopReqVo) {
        FmsTask fmsTask = fmsTaskMapper.selectFmsTaskByTaskId(fmsTaskStopReqVo.getTaskId());
        if(ObjectUtils.isEmpty(fmsTask)){
            throw new BizException(ErrorCode.FMS_TASK_TASK_NO_EXIST);
        }
        fmsTask.setTaskCompletereason(fmsTaskStopReqVo.getTaskCompletereason());
        fmsTask.setTaskStatus(TaskStatus.STOP.getCode());
        return fmsTaskMapper.updateFmsTask(fmsTask);
    }
    @Override
    public void appendTaskMessage(Map<String, FmsMessageResVo> fmsMessageResVoMap) {
        FmsTaskListReqVo fmsTaskQueryObject = new FmsTaskListReqVo();
        fmsTaskQueryObject.setTaskAuditstatus("2");
        fmsTaskQueryObject.setDataFilter(dataAuthUtil.isValidate());
        fmsTaskQueryObject.setBlockIds(dataAuthUtil.blockList());
        fmsTaskQueryObject.setLandIds(dataAuthUtil.landList());
        if(fmsTaskQueryObject.getDataFilter()&&fmsTaskQueryObject.getBlockIds().isEmpty()){
            return;
        }
        List<FmsTaskMessageResVo> fmsTaskList = this.selectAllTaskList(fmsTaskQueryObject);
        for (FmsTaskResVo fmsTaskResVo : fmsTaskList) {
            FmsMessageResVo fmsMessageResVo;
            if (TaskType.PICK.equal(fmsTaskResVo.getTaskType())) {
                fmsMessageResVo = fmsMessageResVoMap.get(FmsMessageResVoType.PICK.getType());
            } else if (TaskType.PATROL.equal(fmsTaskResVo.getTaskType())) {
                fmsMessageResVo = fmsMessageResVoMap.get(FmsMessageResVoType.PATROL.getType());
            } else {
                fmsMessageResVo = fmsMessageResVoMap.get(FmsMessageResVoType.TASK.getType());
            }
            FmsMessageResVoItem fmsMessageResVoItem = new FmsMessageResVoItem();
            fmsMessageResVoItem.setMessageId(fmsTaskResVo.getTaskId());
            fmsMessageResVoItem.setMessageDate(fmsTaskResVo.getTaskCreateddate());
            fmsMessageResVoItem.setMessageBody(fmsTaskResVo);
            fmsMessageResVo.addData(fmsMessageResVoItem);
            fmsMessageResVoMap.put(fmsMessageResVo.getType(), fmsMessageResVo);
        }
    }

    @Override
    public int selectTaskCountOfTaskStatus(String landId) {
        return fmsTaskMapper.selectTaskCountOfTaskStatus(landId);
    }

    @Override
    public List<FmsTaskMessageResVo> selectTaskListOfTaskStatus(String landId) {
        return fmsTaskMapper.selectTaskListOfTaskStatus(landId);
    }
    
    @Override
    public Map<String,Object> selectSumTaskrcdMoney(SelectOfTssStatReqVo reqVo) {
    	return fmsTaskMapper.selectSumTaskrcdMoney(reqVo);
    }
    
    @Override
    public List<SpmPayOfTssStatResVo> selectPayByNz(SelectOfTssStatReqVo reqVo) {
    	return fmsTaskMapper.selectPayByNz(reqVo);
    }
    
    @Override
    public List<SpmPayOfTssStatResVo> selectPayByNj(SelectOfTssStatReqVo reqVo) {
    	return fmsTaskMapper.selectPayByNj(reqVo);
    }
    
    @Override
    public List<SpmPayOfTssStatResVo> selectPayByRg(SelectOfTssStatReqVo reqVo) {
    	return fmsTaskMapper.selectPayByRg(reqVo);
    }
    
    @Override
    public Map<String, Map<String, Object>> selectPayMonthByNz(SelectOfTssStatReqVo reqVo) {
    	List<Map<String, Object>> gooodsCsList = fmsTaskMapper.selectPayMonthByNz(reqVo);
		Map<String, Map<String, Object>> gooodsCsMap = null;
		if (gooodsCsList != null) {
			gooodsCsMap = gooodsCsList.stream().collect(Collectors.toMap(e -> e.get("month").toString(), e -> e));
		}
		if (gooodsCsMap == null) {
			gooodsCsMap = new HashMap<String, Map<String, Object>>();
		}
    	return gooodsCsMap;
    }
    
    @Override
    public Map<String, Map<String, Object>> selectPayMonthByRg(SelectOfTssStatReqVo reqVo) {
    	List<Map<String, Object>> gooodsCsList = fmsTaskMapper.selectPayMonthByRg(reqVo);
		Map<String, Map<String, Object>> gooodsCsMap = null;
		if (gooodsCsList != null) {
			gooodsCsMap = gooodsCsList.stream().collect(Collectors.toMap(e -> e.get("month").toString(), e -> e));
		}
		if (gooodsCsMap == null) {
			gooodsCsMap = new HashMap<String, Map<String, Object>>();
		}
    	return gooodsCsMap;
    }
    
    @Override
    public Map<String, Map<String, Object>> selectPayMonthByNj(SelectOfTssStatReqVo reqVo) {
    	List<Map<String, Object>> gooodsCsList = fmsTaskMapper.selectPayMonthByNj(reqVo);
		Map<String, Map<String, Object>> gooodsCsMap = null;
		if (gooodsCsList != null) {
			gooodsCsMap = gooodsCsList.stream().collect(Collectors.toMap(e -> e.get("month").toString(), e -> e));
		}
		if (gooodsCsMap == null) {
			gooodsCsMap = new HashMap<String, Map<String, Object>>();
		}
    	return gooodsCsMap;
    }
}
