package com.yunfeiyun.agmp.fms.controller;

import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.constant.ResConstants;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.core.domain.model.LoginUser;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
import com.yunfeiyun.agmp.common.enums.*;
import com.yunfeiyun.agmp.common.framework.manager.RedisCacheManager;
import com.yunfeiyun.agmp.common.framework.manager.ResManager;
import com.yunfeiyun.agmp.common.utils.SecurityUtils;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import com.yunfeiyun.agmp.common.utils.poi.ExcelUtil;
import com.yunfeiyun.agmp.common.utils.serialno.NoGenerator;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.common.web.system.service.ISysDictDataService;
import com.yunfeiyun.agmp.common.web.system.service.ISysResService;
import com.yunfeiyun.agmp.fms.domain.*;
import com.yunfeiyun.agmp.fms.domain.reqvo.*;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsTaskMessageResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsTaskResVo;
import com.yunfeiyun.agmp.fms.util.DataAuthUtil;
import com.yunfeiyun.agmp.fms.service.IFmsBlockService;
import com.yunfeiyun.agmp.fms.service.IFmsTaskService;
import com.yunfeiyun.agmp.fms.service.IFmsWarnService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 农事任务Controller
 *
 * @author 杨晓辉
 * @date 2023-05-19
 */
@RestController
@RequestMapping("/fms/task")
public class FmsTaskController extends BaseController
{
    @Autowired
    private IFmsTaskService fmsTaskService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ISysResService sysResService;
    @Autowired
    private IFmsWarnService  fmsWarnService;
    @Autowired
    private RedisCacheManager redisCacheManager;
    @Autowired
    private ResManager resManager;
    @Autowired
    private NoGenerator noGenerator;
    @Resource
    private DataAuthUtil dataAuthUtil;
    @Autowired
    private IFmsBlockService fmsBlockService;

    /**
     * 查询农事任务列表
     */
    @PreAuthorize("@ss.hasPermi('fms:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(FmsTaskListReqVo reqVo)
    {
        reqVo.setDataFilter(dataAuthUtil.isValidate());
        reqVo.setBlockIds(dataAuthUtil.blockList());
        if(reqVo.getDataFilter()&&reqVo.getBlockIds().isEmpty()){
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<FmsTaskResVo> list = fmsTaskService.selectTaskList(reqVo);
        return getDataTable(list);
    }

    /**
     * 查询农事任务列表
     */
    @PreAuthorize("@ss.hasPermi('fms:task:type:all:list')")
    @GetMapping("/type/all/list")
    public TableDataInfo typeAllList(FmsTaskListReqVo reqVo)
    {
        reqVo.setDataFilter(dataAuthUtil.isValidate());
        reqVo.setBlockIds(dataAuthUtil.blockList());
        if(reqVo.getDataFilter()&&reqVo.getBlockIds().isEmpty()){
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<FmsTaskResVo> list = fmsTaskService.selectTaskListAll(reqVo);
        return getDataTable(list);
    }

    /**
     * 导出农事任务列表
     */
    @PreAuthorize("@ss.hasPermi('fms:task:export')")
    @Log(title = "农事任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FmsTask fmsTask)
    {
        List<FmsTask> list = fmsTaskService.selectFmsTaskList(fmsTask);
        ExcelUtil<FmsTask> util = new ExcelUtil<FmsTask>(FmsTask.class);
        util.exportExcel(response, list, "农事任务数据");
    }

    /**
     * 获取农事任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('fms:task:info')")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam("taskId") String taskId)
    {
        return success(fmsTaskService.selectFmsTaskByTaskId(taskId));
    }

    /**
     * 获取农事任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('fms:task:show:info')")
    @GetMapping(value = "/show/info")
    public AjaxResult getInfoByTaskId(@RequestParam("taskId") String taskId)
    {
        return success(fmsTaskService.getInfoByTaskId(taskId));
    }

    /**
     * 下发农事任务
     */
    @PreAuthorize("@ss.hasPermi('fms:task:add')")
    @Log(title = "下发农事任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Valid @RequestBody AddFmsTaskReqVo reqVo)
    {
        AjaxResult ajaxResult = checkAddFmsTaskReqVo(reqVo);
        if (!ErrorCode.SUCCESS.getCode().equals(ajaxResult.get(AjaxResult.CODE_TAG))) {
            return ajaxResult;
        }

        LoginUser loginUser = getLoginUser();

        FmsTask fmsTask = new FmsTask();
        BeanUtils.copyProperties(reqVo, fmsTask);

        fmsTask.setTaskId(fmsTask.getId());
        fmsTask.setTaskCreator(loginUser.getUserId());
        fmsTask.setTaskAuditor(loginUser.getUserId());
        fmsTask.setTaskCreateddate(LocalDateTime.now().format(DateTimeFormatter.ofPattern(CommonEnums.PATTERN_DATETIME.getCode())));
        fmsTask.setTaskNo(noGenerator.getTaskNo());
        fmsTask.setTaskStatus(TaskStatus.EXECUTING.getCode());//执行中
        fmsTask.setTaskCreatetype(CommonEnums.TASK_CREATE_TYPE_MOVE.getCode());
        fmsTask.setTaskAudittype(CommonEnums.TASK_AUDIT_TYPE_DOWN.getCode());

        if (CommonEnums.TASKREQTYPE_NORMAL.getCode().equals(reqVo.getTaskReqType())) {
            /*农事任务*/

            return toAjax(fmsTaskService.insertFmsTask(fmsTask));
        } else if (CommonEnums.TASKREQTYPE_PICK.getCode().equals(reqVo.getTaskReqType())) {
            /*农事任务采收*/
            fmsTask.setTaskType(TaskType.PICK.getCode());
            FmsTaskpick fmsTaskpick = new FmsTaskpick();
            fmsTaskpick.setTaskId(fmsTask.getTaskId());
            fmsTaskpick.setTaskpickId(fmsTaskpick.getId());
            fmsTaskpick.setPickNo(noGenerator.getPickNo());
            //采收面积取采收地块的面积
            FmsBlock block = fmsBlockService.selectFmsBlockByBlockId(reqVo.getBlockId());
            fmsTaskpick.setPickArea(block.getBlockArea());

            fmsTask.setTaskType(TaskType.PICK.getCode());
            return toAjax(fmsTaskService.insertFmsTaskPick(fmsTask, fmsTaskpick));
        } else if (CommonEnums.TASKREQTYPE_PATROL.getCode().equals(reqVo.getTaskReqType())) {
            /*农事任务巡田*/
            fmsTask.setTaskType(TaskType.PATROL.getCode());
            FmsTaskpatrol fmsTaskpatrol = new FmsTaskpatrol();
            fmsTaskpatrol.setTaskId(fmsTask.getTaskId());
            fmsTaskpatrol.setTaskpatrolId(fmsTaskpatrol.getId());
            fmsTaskpatrol.setTaskpatrolAddress(reqVo.getTaskpatrolAddress());
            fmsTaskpatrol.setTaskpatrolWay(reqVo.getTaskrcdPatrolway());
            return toAjax(fmsTaskService.insertFmsTaskPatrol(fmsTask, fmsTaskpatrol));

        } else if (CommonEnums.TASKREQTYPE_WARN.getCode().equals(reqVo.getTaskReqType())) {
            /*农事任务农事预警*/
            String warnId = reqVo.getWarnId();
            return toAjax(this.fmsWarnService.updateFmsWarnByTask(fmsTask, warnId));
        }

        return error(ErrorCode.INVALID_PARAMETER.getCode());
    }

    /**
     * 新增农事记录
     */
    @PreAuthorize("@ss.hasPermi('fms:task:record:add')")
    @Log(title = "新增农事记录", businessType = BusinessType.INSERT)
    @PostMapping("/record/add")
    public AjaxResult addRecord(@Valid @RequestBody AddFmsTaskRecordReqVo reqVo)
    {
        AjaxResult ajaxResult = checkAddFmsTaskRecordReqVo(reqVo);
        if (!ErrorCode.SUCCESS.getCode().equals(ajaxResult.get(AjaxResult.CODE_TAG))) {
            return ajaxResult;
        }

        LoginUser loginUser = getLoginUser();
        /*农事记录*/
        FmsTask fmsTask = new FmsTask();
        BeanUtils.copyProperties(reqVo, fmsTask);
        fmsTask.setTaskId(fmsTask.getId());
        fmsTask.setTaskCreator(loginUser.getUserId());
        fmsTask.setTaskCreateddate(LocalDateTime.now().format(DateTimeFormatter.ofPattern(CommonEnums.PATTERN_DATETIME.getCode())));
        fmsTask.setTaskNo(noGenerator.getTaskNo());
        fmsTask.setTaskStatus(TaskStatus.COMPLETED.getCode());//已完成
        fmsTask.setTaskCreatetype(CommonEnums.TASK_CREATE_TYPE_MOVE.getCode());
        fmsTask.setTaskAudittype(CommonEnums.TASK_AUDIT_TYPE_LOG.getCode());
        fmsTask.setTaskCompletedate(fmsTask.getTaskPlanenddate());
        fmsTask.setTaskAuditor(loginUser.getUserId());

        FmsTaskrcd fmsTaskrcd = new FmsTaskrcd();
        BeanUtils.copyProperties(reqVo, fmsTaskrcd);
        fmsTaskrcd.setTaskrcdId(fmsTaskrcd.getId());
        fmsTaskrcd.setTaskId(fmsTask.getTaskId());
        fmsTaskrcd.setTaskrcdAuditstatus(TaskrcdAuditStatus.PASS.getCode());
        fmsTaskrcd.setTaskrcdAuditdate(fmsTask.getTaskCreateddate());
        fmsTaskrcd.setTaskrcdAuditor(fmsTask.getTaskAuditor());
        fmsTaskrcd.setTaskrcdSubmitdate(fmsTask.getTaskCreateddate());
        fmsTaskrcd.setTaskrcdCompletedate(fmsTask.getTaskCompletedate());
        fmsTaskrcd.setTaskrcdAuditcontent(CommonEnums.TASKRCD_AUDIT_CONTENT.getMessage());


        if (CommonEnums.TASKREQTYPE_NORMAL.getCode().equals(reqVo.getTaskReqType())) {
            /*农事任务*/
            return toAjax(fmsTaskService.insertFmsTask(reqVo.getResIds(), fmsTask, fmsTaskrcd, null, null,
                    null, reqVo.getFmsTaskassets(), reqVo.getFmsTaskmachines()));
        } else if (CommonEnums.TASKREQTYPE_PICK.getCode().equals(reqVo.getTaskReqType())) {
            /*农事任务采收*/
            FmsTaskpick fmsTaskpick = new FmsTaskpick();
            fmsTaskpick.setTaskId(fmsTask.getTaskId());
            fmsTaskpick.setTaskpickId(fmsTaskpick.getId());
            fmsTaskpick.setPickWeight(reqVo.getTaskrcdWeight());
            fmsTaskpick.setPickNo(noGenerator.getPickNo());
            fmsTaskpick.setPickDate(fmsTask.getTaskPlanenddate());
            //采收面积取采收地块的面积
            FmsBlock block = fmsBlockService.selectFmsBlockByBlockId(reqVo.getBlockId());
            fmsTaskpick.setPickArea(block.getBlockArea());

            fmsTask.setTaskType(TaskType.PICK.getCode());
            return toAjax(fmsTaskService.insertFmsTask(reqVo.getResIds(), fmsTask, fmsTaskrcd, fmsTaskpick, null,
                    null, reqVo.getFmsTaskassets(), reqVo.getFmsTaskmachines()));
        } else if (CommonEnums.TASKREQTYPE_PATROL.getCode().equals(reqVo.getTaskReqType())) {
            /*农事任务巡田*/
            FmsTaskpatrol fmsTaskpatrol = new FmsTaskpatrol();
            fmsTaskpatrol.setTaskId(fmsTask.getTaskId());
            fmsTaskpatrol.setTaskpatrolId(fmsTaskpatrol.getId());
            fmsTaskpatrol.setTaskpatrolAddress(reqVo.getTaskpatrolAddress());
            fmsTaskpatrol.setTaskpatrolWay(reqVo.getTaskrcdPatrolway());
            fmsTaskpatrol.setTaskpatrolHour(reqVo.getTaskrcdHour());
            return toAjax(fmsTaskService.insertFmsTask(reqVo.getResIds(), fmsTask, fmsTaskrcd, null, fmsTaskpatrol,
                    null, reqVo.getFmsTaskassets(), reqVo.getFmsTaskmachines()));
        }
        return error(ErrorCode.INVALID_PARAMETER.getCode());
    }

    /**
     * 上报农事任务
     */
    @PreAuthorize("@ss.hasPermi('fms:task:app:add')")
    @Log(title = "上报农事任务", businessType = BusinessType.INSERT)
    @PostMapping("/app/add")
    public AjaxResult addApp(@Valid @RequestBody AddAppFmsTaskReqVo reqVo)
    {
        AjaxResult ajaxResult = checkAddAppFmsTaskReqVo(reqVo);
        if (!ErrorCode.SUCCESS.getCode().equals(ajaxResult.get(AjaxResult.CODE_TAG))) {
            return ajaxResult;
        }

        LoginUser loginUser = getLoginUser();

        if (StringUtils.isEmpty(reqVo.getTaskCompletedate())) {
            reqVo.setTaskCompletedate(reqVo.getTaskPlanenddate());
        }

        /*农事记录*/
        FmsTask fmsTask = new FmsTask();
        BeanUtils.copyProperties(reqVo, fmsTask);
        fmsTask.setTaskId(fmsTask.getId());
        fmsTask.setTaskCreator(loginUser.getUserId());
        fmsTask.setTaskCreateddate(LocalDateTime.now().format(DateTimeFormatter.ofPattern(CommonEnums.PATTERN_DATETIME.getCode())));
        fmsTask.setTaskNo(noGenerator.getTaskNo());
        fmsTask.setTaskStatus(TaskStatus.UNAUDITED.getCode());//待审核
        fmsTask.setTaskCreatetype(CommonEnums.TASK_CREATE_TYPE_MOVE.getCode());
        fmsTask.setTaskAudittype(CommonEnums.TASK_AUDIT_TYPE_UP.getCode());
        fmsTask.setTaskManager(loginUser.getUserId());
        fmsTask.setTaskManagername(reqVo.getTaskManagername() );
        fmsTask.setTaskAuditor(reqVo.getTaskManager());

        FmsTaskrcd fmsTaskrcd = new FmsTaskrcd();
        BeanUtils.copyProperties(reqVo, fmsTaskrcd);
        fmsTaskrcd.setTaskrcdId(fmsTaskrcd.getId());
        fmsTaskrcd.setTaskId(fmsTask.getTaskId());
        fmsTaskrcd.setTaskrcdAuditstatus(TaskrcdAuditStatus.UNAUDITED.getCode());
        fmsTaskrcd.setTaskrcdSubmitdate(LocalDateTime.now().format(DateTimeFormatter.ofPattern(CommonEnums.PATTERN_DATETIME.getCode())));
        fmsTaskrcd.setTaskrcdSubmitcontent(reqVo.getTaskContent());
        fmsTaskrcd.setTaskrcdCompletedate(reqVo.getTaskCompletedate());
        if (CommonEnums.TASKREQTYPE_NORMAL.getCode().equals(reqVo.getTaskReqType())) {
            /*农事任务*/
            return toAjax(fmsTaskService.insertFmsTask(reqVo.getResIds(), fmsTask, fmsTaskrcd, null, null,
                    null, reqVo.getFmsTaskassets(), reqVo.getFmsTaskmachines()));
        } else if (CommonEnums.TASKREQTYPE_PICK.getCode().equals(reqVo.getTaskReqType())) {
            /*农事任务采收*/
            FmsTaskpick fmsTaskpick = new FmsTaskpick();
            fmsTaskpick.setTaskId(fmsTask.getTaskId());
            fmsTaskpick.setTaskpickId(fmsTaskpick.getId());
            fmsTaskpick.setPickWeight(reqVo.getTaskrcdWeight());
            fmsTaskpick.setPickNo(noGenerator.getPickNo());
            fmsTaskpick.setPickDate(fmsTask.getTaskPlanenddate());
            //采收面积取采收地块的面积
            FmsBlock block = fmsBlockService.selectFmsBlockByBlockId(reqVo.getBlockId());
            fmsTaskpick.setPickArea(block.getBlockArea());

            fmsTask.setTaskType(TaskType.PICK.getCode());
            return toAjax(fmsTaskService.insertFmsTask(reqVo.getResIds(), fmsTask, fmsTaskrcd, fmsTaskpick, null,
                    null, reqVo.getFmsTaskassets(), reqVo.getFmsTaskmachines()));
        } else if (CommonEnums.TASKREQTYPE_PATROL.getCode().equals(reqVo.getTaskReqType())) {
            /*农事任务巡田*/
            FmsTaskpatrol fmsTaskpatrol = new FmsTaskpatrol();
            fmsTaskpatrol.setTaskId(fmsTask.getTaskId());
            fmsTaskpatrol.setTaskpatrolId(fmsTaskpatrol.getId());
            fmsTaskpatrol.setTaskpatrolAddress(reqVo.getTaskpatrolAddress());
            fmsTaskpatrol.setTaskpatrolWay(reqVo.getTaskrcdPatrolway());
            fmsTaskpatrol.setTaskpatrolHour(reqVo.getTaskrcdHour());
            return toAjax(fmsTaskService.insertFmsTask(reqVo.getResIds(), fmsTask, fmsTaskrcd, null, fmsTaskpatrol,
                    null, reqVo.getFmsTaskassets(), reqVo.getFmsTaskmachines()));
        }
        return error(ErrorCode.INVALID_PARAMETER.getCode());
    }

    /**
     * 修改农事任务
     */
    @PreAuthorize("@ss.hasPermi('fms:task:edit')")
    @Log(title = "农事任务", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody FmsTask fmsTask)
    {
        LoginUser loginUser = getLoginUser();
//        fmsTask.setTaskModifier(loginUser.getUserId());
        fmsTask.setTaskModifieddate(LocalDateTime.now().format(DateTimeFormatter.ofPattern(CommonEnums.PATTERN_DATETIME.getCode())));
        return toAjax(fmsTaskService.updateFmsTask(fmsTask));
    }

    /**
     * 删除农事任务
     */
    @PreAuthorize("@ss.hasPermi('fms:task:delete')")
    @Log(title = "农事任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult remove(@RequestParam("taskIds") String[] taskIds)
    {
        return toAjax(fmsTaskService.deleteFmsTaskByTaskIds(taskIds));
    }

    /**
     * 终止农事任务
     */
    @PreAuthorize("@ss.hasPermi('fms:task:stop')")
    @Log(title = "终止农事任务", businessType = BusinessType.DELETE)
    @PostMapping("/stop")
    public AjaxResult stop(@Valid @RequestBody FmsTaskStopReqVo fmsTaskStopReqVo)
    {
        return toAjax(fmsTaskService.stop(fmsTaskStopReqVo));
    }

    /**
     * 作物图片上传
     */
    @PreAuthorize("@ss.hasPermi('fms:task:preview:upload')")
    @Log(title = "作物图片上传", businessType = BusinessType.UPDATE)
    @PostMapping("/preview/upload")
    public AjaxResult uploadCropPreview(MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            SysRes sysRes  = resManager.uploadRes( ResConstants.BizType.TASKRCD, ResConstants.mediaType.IMG,"",file,getLoginUser());
            return AjaxResult.success(sysRes);
        }
        return error(ErrorCode.UPLOAD_FAIL.getCode());
    }

    public AjaxResult checkAddFmsTaskReqVo(AddFmsTaskReqVo reqVo) {
        if (CommonEnums.TASKREQTYPE_NORMAL.getCode().equals(reqVo.getTaskReqType())) {
            if (StringUtils.isEmpty(reqVo.getTaskType()) || StringUtils.isEmpty(reqVo.getBlockId()) ||
                    StringUtils.isEmpty(reqVo.getPlanId()) || StringUtils.isEmpty(reqVo.getCropId())) {
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
            }
        } else if (CommonEnums.TASKREQTYPE_PICK.getCode().equals(reqVo.getTaskReqType())) {
            if (StringUtils.isEmpty(reqVo.getBlockId()) || StringUtils.isEmpty(reqVo.getPlanId()) ||
                    StringUtils.isEmpty(reqVo.getCropId())) {
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
            }
        } else if (CommonEnums.TASKREQTYPE_PATROL.getCode().equals(reqVo.getTaskReqType())) {
            if (StringUtils.isEmpty(reqVo.getTaskpatrolAddress())
//                    || StringUtils.isEmpty(reqVo.getTaskContent())
            ) {
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
            }
        } else if (CommonEnums.TASKREQTYPE_WARN.getCode().equals(reqVo.getTaskReqType())) {
            if (StringUtils.isEmpty(reqVo.getTaskType()) || StringUtils.isEmpty(reqVo.getBlockId()) ||
                    StringUtils.isEmpty(reqVo.getPlanId()) || StringUtils.isEmpty(reqVo.getCropId())) {
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
            }
        }

        return AjaxResult.success();
    }

    public AjaxResult checkAddFmsTaskRecordReqVo(AddFmsTaskRecordReqVo reqVo) {
        if (CommonEnums.TASKREQTYPE_NORMAL.getCode().equals(reqVo.getTaskReqType())) {
            if (StringUtils.isEmpty(reqVo.getTaskType()) || StringUtils.isEmpty(reqVo.getBlockId()) ||
                    StringUtils.isEmpty(reqVo.getPlanId()) || StringUtils.isEmpty(reqVo.getCropId())) {
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
            }
        } else if (CommonEnums.TASKREQTYPE_PICK.getCode().equals(reqVo.getTaskReqType())) {
            if (StringUtils.isEmpty(reqVo.getBlockId()) || StringUtils.isEmpty(reqVo.getPlanId()) ||
                    StringUtils.isEmpty(reqVo.getCropId()) || reqVo.getTaskrcdWeight() == null) {
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
            }
        } else if (CommonEnums.TASKREQTYPE_PATROL.getCode().equals(reqVo.getTaskReqType())) {
            if (StringUtils.isEmpty(reqVo.getTaskpatrolAddress()) || StringUtils.isEmpty(reqVo.getTaskrcdPatrolway())) {
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
            }
        } else if (CommonEnums.TASKREQTYPE_WARN.getCode().equals(reqVo.getTaskReqType())) {
            if (StringUtils.isEmpty(reqVo.getTaskType()) || StringUtils.isEmpty(reqVo.getBlockId()) ||
                    StringUtils.isEmpty(reqVo.getPlanId()) || StringUtils.isEmpty(reqVo.getCropId())) {
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
            }
        }

        return AjaxResult.success();
    }

    public AjaxResult checkAddAppFmsTaskReqVo(AddAppFmsTaskReqVo reqVo) {
        if (CommonEnums.TASKREQTYPE_NORMAL.getCode().equals(reqVo.getTaskReqType())) {
            if (StringUtils.isEmpty(reqVo.getTaskType()) || StringUtils.isEmpty(reqVo.getBlockId()) ||
                    StringUtils.isEmpty(reqVo.getPlanId()) || StringUtils.isEmpty(reqVo.getCropId()) ) {
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
            }
        } else if (CommonEnums.TASKREQTYPE_PICK.getCode().equals(reqVo.getTaskReqType())) {
            if (StringUtils.isEmpty(reqVo.getBlockId()) || StringUtils.isEmpty(reqVo.getPlanId()) ||
                    StringUtils.isEmpty(reqVo.getCropId()) || reqVo.getTaskrcdWeight() == null ) {
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
            }
        } else if (CommonEnums.TASKREQTYPE_PATROL.getCode().equals(reqVo.getTaskReqType())) {
            if (StringUtils.isEmpty(reqVo.getTaskpatrolAddress()) || StringUtils.isEmpty(reqVo.getTaskrcdPatrolway()) ) {
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
            }
        } else if (CommonEnums.TASKREQTYPE_WARN.getCode().equals(reqVo.getTaskReqType())) {
            if (StringUtils.isEmpty(reqVo.getTaskType()) || StringUtils.isEmpty(reqVo.getBlockId()) ||
                    StringUtils.isEmpty(reqVo.getPlanId()) || StringUtils.isEmpty(reqVo.getCropId())) {
                return AjaxResult.error(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMessage());
            }
        }

        return AjaxResult.success();
    }

    /**
     * App端首页查询代办事项总条数
     * @param landId 基地标识
     * @return
     */
    @PreAuthorize("@ss.hasPermi('fms:task:todo:count')")
    @GetMapping("/todo/count")
    public AjaxResult taskToDoCount(@RequestParam(value = "landId") String landId){
        int count = fmsTaskService.selectTaskCountOfTaskStatus(landId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("count", count);
        return success(resultMap);
    }

    /**
     * App端首页查询代办事项列表
     * @param landId 基地标识
     * @return
     */
    @PreAuthorize("@ss.hasPermi('fms:task:todo:count:list')")
    @GetMapping("/todo/count/list")
    public TableDataInfo taskToDoList(@RequestParam(value = "landId") String landId){
        startPage();
        List<FmsTaskMessageResVo> fmsTaskMessageResVos = fmsTaskService.selectTaskListOfTaskStatus(landId);
        return getDataTable(fmsTaskMessageResVos);
    }

    @PreAuthorize("@ss.hasPermi('fms:task:app:stat')")
    @GetMapping(value = "/app/stat")
    public AjaxResult selectTaskStat() {
        FmsTaskListReqVo reqVo = new FmsTaskListReqVo();
        reqVo.setTaskManager(SecurityUtils.getUserId());

        reqVo.setStartDate(LocalDateTime.of(LocalDate.now(), LocalTime.MIN).format(DateTimeFormatter.ofPattern(CommonEnums.PATTERN_DATETIME.getCode())));
        int count1 = fmsTaskService.selectCountFmsTask(reqVo);

        reqVo.setStartDate(LocalDateTime.of(LocalDate.now(), LocalTime.MIN).with(DayOfWeek.MONDAY).format(DateTimeFormatter.ofPattern(CommonEnums.PATTERN_DATETIME.getCode())));
        int count2 = fmsTaskService.selectCountFmsTask(reqVo);

        reqVo.setStartDate(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN).format(DateTimeFormatter.ofPattern(CommonEnums.PATTERN_DATETIME.getCode())));
        int count3 = fmsTaskService.selectCountFmsTask(reqVo);

        Map<String, Object> statMap = new HashMap<>();
        statMap.put("count1", count1);
        statMap.put("count2", count2);
        statMap.put("count3", count3);

        return AjaxResult.success(statMap);
    }




}
