package com.ximai.mes.report.controller;

import com.ximai.common.annotation.Log;
import com.ximai.common.core.controller.BaseController;
import com.ximai.common.core.domain.AjaxResult;
import com.ximai.common.core.domain.entity.SysDictData;
import com.ximai.common.core.page.TableDataInfo;
import com.ximai.common.enums.BusinessType;
import com.ximai.common.utils.data.StringUtils;
import com.ximai.common.utils.poi.ExcelUtil;
import com.ximai.mes.report.request.FeedbackRequest;
import com.ximai.mes.report.request.ProgressReportRequest;
import com.ximai.mes.report.response.*;
import com.ximai.mes.report.service.FeedbackService;
import com.ximai.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api("生产报工报表")
@RestController
@RequestMapping("/report/pro/feedback")
public class FeedbackController extends BaseController {
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    FeedbackService feedbackService;
    /**
     * 新增生产报工记录
     */
    /**
     * 进度报表：工序名称列表（动态表头）
     */
    @ApiOperation("进度报表:工序名称列表")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "进度报表:工序名称", businessType = BusinessType.QUERY)
    @GetMapping("/progressReport/processNames")
    public AjaxResult getProgressReportProcessNames() {
        return AjaxResult.success(feedbackService.getProgressReportProcessNames());
    }

    /**
     * 进度报表：列表（筛选：排产时间、订单单号、订单序号、产品编号、产品名称、在制进度小于、需求时间小于）
     * 表格：排产时间、订单单号(工单时间降序)、产线名称、生产工单、订单序号、工单类型、产品编号、产品名称、工序1~N(动态)、计划数量、在制进度%、需求时间
     */
    @ApiOperation("进度报表:列表")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "进度报表", businessType = BusinessType.QUERY)
    @GetMapping("/progressReport/list")
    public TableDataInfo<ProgressReportResponse> getProgressReportList(ProgressReportRequest request) {
        startPage();
        ProgressReportListResult result = feedbackService.getProgressReportList(request);
        return getDataTable(result.getList(), result.getTotal());
    }

    @ApiOperation("生产报工记录:明细界面")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "生产报工记录", businessType = BusinessType.QUERY)
    @GetMapping("/getList")
    public TableDataInfo<FeedbackResponse> getList(FeedbackRequest feedbackRequest) {
        startPage();
        List<FeedbackResponse> feedbackResponseList = feedbackService.getList(feedbackRequest);
        return getDataTable(feedbackResponseList);
    }


    /**
     * 明细界面导出
     */
    @PreAuthorize("@ss.hasPermi('pro:materialReturn:export')")
    @Log(title = "生产报工记录:明细界面导出", businessType = BusinessType.EXPORT)
    @PostMapping("/getList/export")
    @ApiOperation("生产报工记录:明细界面导出")
    public void export(HttpServletResponse response, FeedbackRequest feedbackRequest) {
       // List<FeedbackResponse> feedbackResponseList = feedbackService.getList(feedbackRequest);
        List<FeedbackResponseExportByList> feedbackResponseList = feedbackService.export(feedbackRequest);
        SysDictData dictData = new SysDictData();
        dictData.setDictType("mes_workorder_type");
        List<SysDictData> sysDictDataList = dictDataService.selectDictDataList(dictData);
        Map<String,List<SysDictData>> stringListMap = sysDictDataList.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));
        feedbackResponseList.stream().forEach(feedbackResponse ->{
            feedbackResponse.setWorkorderType(stringListMap.get(feedbackResponse.getWorkorderType()) == null ? null : stringListMap.get(feedbackResponse.getWorkorderType()).get(0).getDictLabel());
        } );
        ExcelUtil<FeedbackResponseExportByList> util = new ExcelUtil<>(FeedbackResponseExportByList.class);
        util.exportExcel(response, feedbackResponseList, "生产报工报表明细","生产报工报表明细");
    }

    @ApiOperation("生产报工记录:统计方式工单")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "生产报工记录:统计方式工单", businessType = BusinessType.QUERY)
    @GetMapping("/getListByWorkOrder")
    public TableDataInfo<FeedbackResponse> getListByWorkOrder(     FeedbackRequest feedbackRequest) {
        startPage();
        List<FeedbackResponse> feedbackResponseList = feedbackService.getListByWorkOrder(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponse feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        return getDataTable(feedbackResponseList);
    }



    /**
     * 明细界面导出
     */
    @PreAuthorize("@ss.hasPermi('pro:materialReturn:export')")
    @Log(title = "生产报工记录:统计方式工单界面导出", businessType = BusinessType.EXPORT)
    @PostMapping("/getListByWorkOrder/export")
    @ApiOperation("生产报工记录:统计方式工单界面导出")
    public void getListByWorkOrderExport(HttpServletResponse response, FeedbackRequest feedbackRequest) {
        List<FeedbackResponseExportByWorkOrder> feedbackResponseList = feedbackService.getListByWorkOrderExport(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponseExportByWorkOrder feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        ExcelUtil<FeedbackResponseExportByWorkOrder> util = new ExcelUtil<>(FeedbackResponseExportByWorkOrder.class);
        util.exportExcel(response, feedbackResponseList, "生产报工报表-统计方式工单","生产报工报表-统计方式工单");
    }




    @ApiOperation("生产报工记录:统计方式车间")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "生产报工记录:统计方式车间", businessType = BusinessType.QUERY)
    @GetMapping("/getListByWorkshop")
    public TableDataInfo<FeedbackResponse> getListByWorkshop(     FeedbackRequest feedbackRequest) {
        startPage();
        List<FeedbackResponse> feedbackResponseList = feedbackService.getListByWorkshop(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponse feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        return getDataTable(feedbackResponseList);
    }

    /**
     * 统计方式车间界面导出
     */
    @PreAuthorize("@ss.hasPermi('pro:materialReturn:export')")
    @Log(title = "生产报工记录:统计方式车间界面导出", businessType = BusinessType.EXPORT)
    @PostMapping("/getListByWorkshop/export")
    @ApiOperation("生产报工记录:统计方式车间界面导出")
    public void getListByWorkshopExport(HttpServletResponse response, FeedbackRequest feedbackRequest) {
        List<FeedbackResponseExportByWorkshop> feedbackResponseList = feedbackService.getListByWorkshopExport(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponseExportByWorkshop feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        ExcelUtil<FeedbackResponseExportByWorkshop> util = new ExcelUtil<>(FeedbackResponseExportByWorkshop.class);
        util.exportExcel(response, feedbackResponseList, "生产报工报表-统计方式车间","生产报工报表-统计方式车间");
    }


    @ApiOperation("生产报工记录:统计方式工作中心")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "生产报工记录:统计方式工作中心", businessType = BusinessType.QUERY)
    @GetMapping("/getListByWorkstation")
    public TableDataInfo<FeedbackResponse> getListByWorkstation(     FeedbackRequest feedbackRequest) {
        startPage();
        List<FeedbackResponse> feedbackResponseList = feedbackService.getListByWorkstation(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponse feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        return getDataTable(feedbackResponseList);
    }


    /**
     * 统计方式车间界面导出
     */
    @PreAuthorize("@ss.hasPermi('pro:materialReturn:export')")
    @Log(title = "生产报工记录:统计方式工作中心导出", businessType = BusinessType.EXPORT)
    @PostMapping("/getListByWorkstation/export")
    @ApiOperation("生产报工记录:统计方式工作中心导出")
    public void getListByWorkstationExport(HttpServletResponse response, FeedbackRequest feedbackRequest) {
        List<FeedbackResponseExportByWorkstation> feedbackResponseList = feedbackService.getListByWorkstationExport(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponseExportByWorkstation feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        ExcelUtil<FeedbackResponseExportByWorkstation> util = new ExcelUtil<>(FeedbackResponseExportByWorkstation.class);
        util.exportExcel(response, feedbackResponseList, "生产报工报表-统计方式工作中心","生产报工报表-统计方式工作中心");
    }

    @ApiOperation("生产报工记录:统计方式工作单元")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "生产报工记录:统计方式工作单元", businessType = BusinessType.QUERY)
    @GetMapping("/getListByWorkunit")
    public TableDataInfo<FeedbackResponse> getListByWorkunit(     FeedbackRequest feedbackRequest) {
        startPage();
        List<FeedbackResponse> feedbackResponseList = feedbackService.getListByWorkunit(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponse feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        return getDataTable(feedbackResponseList);
    }


    /**
     * 统计方式车间界面导出
     */
    @PreAuthorize("@ss.hasPermi('pro:materialReturn:export')")
    @Log(title = "生产报工记录:统计统计方式工作单元导出", businessType = BusinessType.EXPORT)
    @PostMapping("/getListByWorkunit/export")
    @ApiOperation("生产报工记录:统计统计方式工作单元导出")
    public void getListByWorkunitExport(HttpServletResponse response, FeedbackRequest feedbackRequest) {
        List<FeedbackResponseExportByWorkunit> feedbackResponseList = feedbackService.getListByWorkunitExport(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponseExportByWorkunit feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        ExcelUtil<FeedbackResponseExportByWorkunit> util = new ExcelUtil<>(FeedbackResponseExportByWorkunit.class);
        util.exportExcel(response, feedbackResponseList, "生产报工报表-统计方式工作单元","生产报工报表-统计方式工作单元");
    }



    @ApiOperation("生产报工记录:统计方式工序")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "生产报工记录:统计方式工序", businessType = BusinessType.QUERY)
    @GetMapping("/getListByProcess")
    public TableDataInfo<FeedbackResponse> getListByProcess(     FeedbackRequest feedbackRequest) {
        startPage();
        List<FeedbackResponse> feedbackResponseList = feedbackService.getListByProcess(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponse feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        return getDataTable(feedbackResponseList);
    }

    /**
     * 统计方式车间界面导出
     */
    @PreAuthorize("@ss.hasPermi('pro:materialReturn:export')")
    @Log(title = "生产报工记录:统计方式工序导出", businessType = BusinessType.EXPORT)
    @PostMapping("/getListByProcess/export")
    @ApiOperation("生产报工记录:统计方式工序导出")
    public void getListByProcessExport(HttpServletResponse response, FeedbackRequest feedbackRequest) {
        List<FeedbackResponseExportByProcess> feedbackResponseList = feedbackService.getListByProcessExport(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponseExportByProcess feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        ExcelUtil<FeedbackResponseExportByProcess> util = new ExcelUtil<>(FeedbackResponseExportByProcess.class);
        util.exportExcel(response, feedbackResponseList, "生产报工报表-统计方式工序","生产报工报表-统计方式工序");
    }

    @ApiOperation("生产报工记录:统计方式用户")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "生产报工记录:统计方式用户", businessType = BusinessType.QUERY)
    @GetMapping("/getListByUser")
    public TableDataInfo<FeedbackResponse> getListByUser(     FeedbackRequest feedbackRequest) {
        startPage();
        List<FeedbackResponse> feedbackResponseList = feedbackService.getListByUser(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponse feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        return getDataTable(feedbackResponseList);
    }

    /**
     * 统计方式车间界面导出
     */
    @PreAuthorize("@ss.hasPermi('pro:materialReturn:export')")
    @Log(title = "生产报工记录:统计方式用户导出", businessType = BusinessType.EXPORT)
    @PostMapping("/getListByUser/export")
    @ApiOperation("生产报工记录:统计方式用户导出")
    public void getListByUserExport(HttpServletResponse response, FeedbackRequest feedbackRequest) {
        List<FeedbackResponseExportByUser> feedbackResponseList = feedbackService.getListByUserExport(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponseExportByUser feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        ExcelUtil<FeedbackResponseExportByUser> util = new ExcelUtil<>(FeedbackResponseExportByUser.class);
        util.exportExcel(response, feedbackResponseList, "生产报工报表-统计方式用户","生产报工报表-统计方式用户");
    }




    @ApiOperation("生产报工记录:统计方式不合格原因")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "生产报工记录:统计方式不合格原因", businessType = BusinessType.QUERY)
    @GetMapping("/getListByDefect")
    public TableDataInfo<FeedbackResponse> getListByDefect(     FeedbackRequest feedbackRequest) {
        startPage();
        List<FeedbackResponse> feedbackResponseList = feedbackService.getListByDefect(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponse feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        return getDataTable(feedbackResponseList);
    }




    /**
     * 统计方式车间界面导出
     */
    @PreAuthorize("@ss.hasPermi('pro:materialReturn:export')")
    @Log(title = "生产报工记录:统计方式不合格原因导出", businessType = BusinessType.EXPORT)
    @PostMapping("/getListByDefect/export")
    @ApiOperation("生产报工记录:统计方式不合格原因导出")
    public void getListByDefectExport(HttpServletResponse response, FeedbackRequest feedbackRequest) {
        List<FeedbackResponseExportByDefect> feedbackResponseList = feedbackService.getListByDefectExport(feedbackRequest);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(FeedbackResponseExportByDefect feedbackResponse : feedbackResponseList){
            if(feedbackResponse.getQuantityFeedback() != null
                    && feedbackResponse.getQuantityFeedback().compareTo(BigDecimal.ZERO) != 0
                    && feedbackResponse.getQuantityUnqualify()!= null ){
                feedbackResponse.setQualificationRate(df.format((feedbackResponse.getQuantityQualify().divide(feedbackResponse.getQuantityQualify().add(feedbackResponse.getQuantityUnqualify()),2, RoundingMode.HALF_UP).doubleValue())));
            }else{
                feedbackResponse.setQualificationRate("0.00%");
            }
        }
        ExcelUtil<FeedbackResponseExportByDefect> util = new ExcelUtil<>(FeedbackResponseExportByDefect.class);
        util.exportExcel(response, feedbackResponseList, "生产报工报表-统计方式不合格原因","生产报工报表-统计方式不合格原因");
    }
}
