package com.yunfeiyun.agmp.fms.controller;

import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsPlanReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsTaskListReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.*;
import com.yunfeiyun.agmp.fms.service.*;
import com.yunfeiyun.agmp.fms.util.DataAuthUtil;
import lombok.extern.slf4j.Slf4j;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 种植计划Controller
 * 
 * @author 杨晓辉
 * @date 2023-05-18
 */
@Slf4j
@RestController
@RequestMapping("/fms")
public class FmsStatController extends BaseController
{
    @Autowired
    private IFmsPlanService fmsPlanService;
    @Autowired
    private IFmsLandService fmsLandService;
    @Autowired
    private IFmsBlockService fmsBlockService;
    @Autowired
    private IFmsPeriodService fmsPeriodService;
    @Autowired
    private IFmsTaskService fmsTaskService;
    @Autowired
    private IFmsTaskpickService fmsTaskpickService;
    @Autowired
    private IFmsTaskassetService fmsTaskassetService;
    @Autowired
    private IFmsTaskmachineService fmsTaskmachineService;
    @Resource
    private DataAuthUtil dataAuthUtil;


    @PreAuthorize("@ss.hasPermi('fms:plan:area:stat')")
    @GetMapping("/plan/area/stat")
    public TableDataInfo planAreaStat (FmsPlanReqVo reqVo){
        reqVo.setLandIds(dataAuthUtil.landList());
        reqVo.setDataFilter(dataAuthUtil.isValidate());
        startPage();
        List<FmsPlanResVo> fmsPlanResVos = fmsPlanService.selectPlanByCropName(reqVo);
        List<FmsPlanStatGroupByCropNameResVo> list = new ArrayList<>();
        for (FmsPlanResVo fmsPlanResVo : fmsPlanResVos) {
            FmsPlanStatGroupByCropNameResVo resVo = new FmsPlanStatGroupByCropNameResVo();
            BeanUtils.copyProperties(fmsPlanResVo, resVo);
            list.add(resVo);
        }
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('fms:pick:weight:stat')")
    @GetMapping("/pick/weight/stat")
    public TableDataInfo pickWeightStat (FmsPlanReqVo reqVo){
        reqVo.setLandIds(dataAuthUtil.landList());
        reqVo.setDataFilter(dataAuthUtil.isValidate());
        startPage();
        List<FmsTaskPickStockListResVo> fmsTaskPickStockListResVos = fmsTaskpickService.selectSumPickWeightListGroupByCropName(reqVo);
        return getDataTable(fmsTaskPickStockListResVos);
    }

    @PreAuthorize("@ss.hasPermi('fms:task:hour:stat')")
    @GetMapping("/task/hour/stat")
    public TableDataInfo taskrcdHourStat (FmsTaskListReqVo reqVo){
        reqVo.setLandIds(dataAuthUtil.landList());
        reqVo.setDataFilter(dataAuthUtil.isValidate());
        startPage();

        List<FmsTaskSumTaskrcdHourResVo> fmsTaskSumTaskrcdHourResVos = fmsTaskService.selectSumTaskrcdHour(reqVo);
        for (FmsTaskSumTaskrcdHourResVo resVo : fmsTaskSumTaskrcdHourResVos) {
            if (resVo.getTaskrcdHour() == null) {
                resVo.setTaskrcdHour("0");
            }
        }
        return getDataTable(fmsTaskSumTaskrcdHourResVos);
    }

    @PreAuthorize("@ss.hasPermi('fms:task:asset:stat')")
    @GetMapping("/task/asset/stat")
    public TableDataInfo taskassetStat (FmsTaskListReqVo reqVo){
        reqVo.setLandIds(dataAuthUtil.landList());
        reqVo.setDataFilter(dataAuthUtil.isValidate());
        startPage();
        List<FmsTaskAssetUsageResVo> fmsTaskAssetUsageResVos = fmsTaskassetService.selectFmsTaskAssetStat(reqVo);
        return getDataTable(fmsTaskAssetUsageResVos);
    }

    @PreAuthorize("@ss.hasPermi('fms:task:mach:stat')")
    @GetMapping("/task/mach/stat")
    public TableDataInfo taskmachineStat (FmsTaskListReqVo reqVo){
        reqVo.setLandIds(dataAuthUtil.landList());
        reqVo.setDataFilter(dataAuthUtil.isValidate());
        startPage();
        List<FmsTaskmachineResVo> fmsTaskmachineResVos = fmsTaskmachineService.selectFmsTaskmachineStat(reqVo);
        return getDataTable(fmsTaskmachineResVos);
    }



}
