package com.ximai.mes.report.controller;

import com.ximai.common.annotation.Log;
import com.ximai.common.core.controller.BaseController;
import com.ximai.common.core.page.TableDataInfo;
import com.ximai.common.enums.BusinessType;
import com.ximai.common.utils.poi.ExcelUtil;
import com.ximai.mes.constant.WorkorderStatusEnum;
import com.ximai.mes.constant.WorkorderTypeEnum;
import com.ximai.mes.report.request.FeedbackRequest;
import com.ximai.mes.report.request.WorkOrderProgressRequest;
import com.ximai.mes.report.response.FeedbackResponse;
import com.ximai.mes.report.response.WorkOrderProgressFeedbackListResponse;
import com.ximai.mes.report.response.WorkOrderProgressListResponse;
import com.ximai.mes.report.response.WorkOrderProgressProcessListResponse;
import com.ximai.mes.report.service.WorkOrderProgressService;
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.util.List;

@Api("生产进度")
@RestController
@RequestMapping("/report/pro/workOrderProgress")
public class WorkOrderProgressController  extends BaseController {

    @Autowired
    WorkOrderProgressService workOrderProgressService;
    @Autowired
    private ISysDictDataService dictDataService;
    @ApiOperation("生产进度:明细界面")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "生产进度:明细界面", businessType = BusinessType.QUERY)
    @GetMapping("/getList")
    public TableDataInfo<WorkOrderProgressListResponse> getList( WorkOrderProgressRequest workOrderProgressRequest) {
        startPage();
        List<WorkOrderProgressListResponse> workOrderProgressListResponses = workOrderProgressService.getList(workOrderProgressRequest);
        return getDataTable(workOrderProgressListResponses);
    }

    @PreAuthorize("@ss.hasPermi('pro:materialReturn:export')")
    @Log(title = "生产进度:明细界面导出", businessType = BusinessType.EXPORT)
    @ApiOperation("生产进度:明细界面导出")
    @PostMapping("/getList/export")
    public void export(HttpServletResponse response, WorkOrderProgressRequest workOrderProgressRequest) {
        List<WorkOrderProgressListResponse> workOrderProgressListResponses = workOrderProgressService.getList(workOrderProgressRequest);
        workOrderProgressListResponses.forEach(workOrderProgressListResponse -> {
            WorkorderStatusEnum statusEnum = WorkorderStatusEnum.get(workOrderProgressListResponse.getStatus());
            if (statusEnum != null) {
                workOrderProgressListResponse.setStatus(statusEnum.getEnumName());
            }
            workOrderProgressListResponse.setWorkorderType(dictDataService.getDictLabel("mes_workorder_type", workOrderProgressListResponse.getWorkorderType()));
        });
        ExcelUtil<WorkOrderProgressListResponse> util = new ExcelUtil<>(WorkOrderProgressListResponse.class);
        util.exportExcel(response, workOrderProgressListResponses, "生产进度-明细界面导出");
    }

    @ApiOperation("生产进度:工序明细")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "生产进度:工序明细", businessType = BusinessType.QUERY)
    @GetMapping("/getProcessList")
    public TableDataInfo<WorkOrderProgressProcessListResponse> getProcessList(
            @RequestParam("workorderCode") String workorderCode,
            @RequestParam(value = "taskBatch", required = false) String taskBatch) {
        List<WorkOrderProgressProcessListResponse> workOrderProgressListResponses =
                workOrderProgressService.getProcessList(workorderCode, taskBatch);
        return getDataTable(workOrderProgressListResponses);
    }


    @ApiOperation("生产进度:报工明细")
    @PreAuthorize("@ss.hasPermi('mes:pro:feedback:get')")
    @Log(title = "生产进度:报工明细", businessType = BusinessType.QUERY)
    @GetMapping("/getFeedbackList")
    public TableDataInfo<WorkOrderProgressFeedbackListResponse> getFeedbackList(Long taskWorkunitId, Long taskId,@RequestParam("workorderCode") String workorderCode) {
        if(taskWorkunitId != null && taskWorkunitId == 0)
            taskWorkunitId = null;
        List<WorkOrderProgressFeedbackListResponse> workOrderProgressFeedbackListResponses = workOrderProgressService.getFeedbackList(workorderCode,taskId,taskWorkunitId);
        return getDataTable(workOrderProgressFeedbackListResponses);
    }
}
