package cn.iocoder.yudao.module.mes.controller.admin.producereportplan;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.mes.controller.admin.organization.vo.ProcessTypeEnum;
import cn.iocoder.yudao.module.mes.controller.admin.plan.vo.PlanPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.producereportplan.vo.ReportPlanSummaryRespVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.plan.PlanDO;
import cn.iocoder.yudao.module.mes.service.plan.PlanService;
import cn.iocoder.yudao.module.mes.service.stockindetail.StockInDetailService;
import cn.iocoder.yudao.module.mes.service.workreportplan.WorkReportPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 报工分配计划")
@RestController
@RequestMapping("/mes/work-report-plan-view")
@Validated
public class ReportPlanViewController {

    @Resource
    private WorkReportPlanService workReportPlanService;
    @Resource
    private StockInDetailService stockInDetailService;
    @Resource
    private PlanService planService;
    @GetMapping("/getPlanProgress")
    @Operation(summary = "获得计划进度")
    @Parameter(name = "id", description = "计划id", required = true, example = "1024")
    public CommonResult<List<ReportPlanSummaryRespVO>> getPlanProgress(@RequestParam("id") Long id) {
        ReportPlanSummaryRespVO respVO = new ReportPlanSummaryRespVO();
        respVO.setPlanId(id);
        List<ReportPlanSummaryRespVO> planSummaryDOS = workReportPlanService.getPlanSummary( respVO);
        if(planSummaryDOS.size()>0){
            //按照工序枚举顺序排序
            planSummaryDOS.sort(Comparator.comparing(book -> {
                ProcessTypeEnum type = ProcessTypeEnum.fromValue(book.getOrgType());
                return type.ordinal();
            }));
        }else planSummaryDOS = new ArrayList<>();
        /****加上生产入库数量 todo
         */
        BigDecimal stockInNumber = stockInDetailService.selectSumBy(id);
        respVO.setTotalQualityNumber(stockInNumber);
        respVO.setTotalWasteNumber(BigDecimal.ZERO);
        respVO.setOrgType("ruku");
        planSummaryDOS.add(respVO);
        return success(planSummaryDOS);
    }

    @GetMapping("/getTaskProgress")
    @Operation(summary = "获得任务明细进度")
    @Parameter(name = "id", description = "任务明细id", required = true, example = "1024")
    public CommonResult<List<ReportPlanSummaryRespVO>> getTaskProgress(@RequestParam("id") Long id) {
        PlanPageReqVO reqVO = new PlanPageReqVO().setTaskId(id);
        List<PlanDO> planList = planService.getPlanBy(reqVO);
        List<Long> ids = planList.stream().map(PlanDO::getId).collect(Collectors.toList());
        ReportPlanSummaryRespVO respVO = new ReportPlanSummaryRespVO();
        respVO.setPlanIds(ids);
        List<ReportPlanSummaryRespVO> planSummaryDOS = workReportPlanService.getPlanSummary( respVO);

        if(planSummaryDOS.size()>0){
            //按照工序枚举顺序排序
            planSummaryDOS.sort(Comparator.comparing(book -> {
                ProcessTypeEnum type = ProcessTypeEnum.fromValue(book.getOrgType());
                return type.ordinal();
            }));
        }else planSummaryDOS = new ArrayList<>();
        /****加上生产入库数量 todo
         */
        BigDecimal stockInNumber = stockInDetailService.selectSumByTaskDetailId(id);
        respVO.setTotalQualityNumber(stockInNumber);
        respVO.setTotalWasteNumber(BigDecimal.ZERO);
        respVO.setOrgType("ruku");
        planSummaryDOS.add(respVO);
        return success(planSummaryDOS);
    }
}