package com.cf.imes.module.manage.controller.admin.query;


import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.excel.core.util.ExcelUtils;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import com.cf.imes.module.manage.controller.admin.pack.vo.OrderPartsReqVO;
import com.cf.imes.module.manage.controller.admin.pack.vo.PlateDetailsRespVO;
import com.cf.imes.module.manage.controller.admin.process.vo.ProcessRespVO;
import com.cf.imes.module.manage.controller.admin.query.vo.pack.OrderPackagePageReqVO;
import com.cf.imes.module.manage.controller.admin.query.vo.pack.OrderPackageVO;
import com.cf.imes.module.manage.controller.admin.query.vo.pack.PackageDetailsRespVO;
import com.cf.imes.module.manage.controller.admin.query.vo.pack.PrintOrderRespVO;
import com.cf.imes.module.manage.controller.admin.query.vo.productionStatus.*;
import com.cf.imes.module.manage.controller.admin.query.vo.salary.PriceRatePageReqVO;
import com.cf.imes.module.manage.controller.admin.query.vo.salary.PriceRateReqVO;
import com.cf.imes.module.manage.dal.dataobject.process.OrderDO;
import com.cf.imes.module.manage.service.query.ProductionStatusService;
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.security.access.prepost.PreAuthorize;
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 javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.List;

import static com.cf.imes.framework.common.pojo.CommonResult.success;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 生产查询")
@RestController
@RequestMapping("/manage/query")
@Validated
public class ProductionStatusController {


    @Resource
    private ProductionStatusService productionStatusService;



//    @GetMapping("/page")
//    @Operation(summary = "生产状态查询分页")
//    @PreAuthorize("@ss.hasPermission('manage:query:query')")
    public CommonResult<PageResult<ProcessRespVO>> pageProductionStatus(@Valid OrderPlatePageReqVO pageReqVO){
        PageResult<OrderDO> pageResult = productionStatusService.getProductionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProcessRespVO.class));
    }




    @GetMapping("/select")
    @Operation(summary = "查看生产单下的工序组工序")
    @Parameter(name = "orderNo", description = "生产单Id", required = true)
    @PreAuthorize("@ss.hasPermission('manage:query:select')")
    public CommonResult<List<ProductionGroupVO>> selectProduction(@Valid @RequestParam("orderNo") Long orderNo){

        List<ProductionGroupVO> orderProcessById = productionStatusService.selectProduction(orderNo);

        return CommonResult.success(orderProcessById);

    }





//    @GetMapping("/selectPlateName")
//    @Operation(summary = "查看板材信息的组合名称")
//    @PreAuthorize("@ss.hasPermission('manage:query:query')")
    public CommonResult<List<OrderModuleVO>> selectPlateName(@Valid OrderModuleReqVO reqVO){

        return success(productionStatusService.selectPlateName(reqVO));

    }





//    @GetMapping("/pagePlate")
//    @Operation(summary = "查看板材信息分页")
//    @PreAuthorize("@ss.hasPermission('manage:query:query')")
    public CommonResult<PageResult<OrderPlateRespVO>> queryPlate(@Valid PlatePageVO pageReqVO){
        return success(productionStatusService.getOrderPlatePage(pageReqVO));
    }





    @GetMapping("/pageSalary")
    @Operation(summary = "计件工资分页查询")
    @PreAuthorize("@ss.hasPermission('manage:query:pageSalary')")
    public CommonResult<PageResult<PriceRateReqVO>> StepSalary(@Valid PriceRatePageReqVO pageReqVO){

        PageResult<PriceRateReqVO> salaryPage = productionStatusService.getSalaryPage(pageReqVO);

        return CommonResult.success(salaryPage);

    }





//    @GetMapping("/pagePackage")
//    @Operation(summary = "打包查询分页")
//    @PreAuthorize("@ss.hasPermission('manage:query:query')")
    public CommonResult<PageResult<ProcessRespVO>>  queryPackage(@Valid OrderPlatePageReqVO pageReqVO){

        PageResult<OrderDO> pageResult = productionStatusService.queryPackage(pageReqVO);

        return success(BeanUtils.toBean(pageResult, ProcessRespVO.class));

    }





//    @GetMapping("/queryPackageList")
//    @Operation(summary = "生产单打包完成包裹查看")
//    @Parameter(name = "orderNo", description = "生产单Id", required = true)
//    @PreAuthorize("@ss.hasPermission('manage:query:queryPackageList')")
    public CommonResult<List<OrderPackageVO>> queryPackageList(@Valid @RequestParam("orderNo") Long orderNo){

        List<OrderPackageVO> orderProcessById = productionStatusService.getPackageList(orderNo);

        return CommonResult.success(orderProcessById);

    }





//    @GetMapping("/queryPackageDetails")
//    @Operation(summary = "查看包裹详情分页")
//    @Parameter(name = "packId", description = "包裹Id", required = true)
//    @PreAuthorize("@ss.hasPermission('manage:query:query')")
    public CommonResult<List<PackageDetailsRespVO>>  queryPackageDetails(@Valid @RequestParam("packId") Long packId){

        return null;
//        return CommonResult.success(productionStatusService.queryPackageDetails(packId));

    }





    @GetMapping("/printOrder")
    @Operation(summary = "打印订单")
    @Parameter(name = "orderNo", description = "生产单Id", required = true)
    @PreAuthorize("@ss.hasPermission('manage:query:export')")
    public CommonResult<PrintOrderRespVO>  printOrder(@Valid @RequestParam("orderNo") Long orderNo){

        return CommonResult.success(productionStatusService.printOrder(orderNo));

    }





    @GetMapping("/export")
    @Operation(summary = "导出缺板")
    @PreAuthorize("@ss.hasPermission('manage:query:export')")
    @OperateLog(type = EXPORT)
    public void exportPlateExcel(@Valid OrderPartsReqVO pageReqVO,
                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlateDetailsRespVO> list = productionStatusService.getMissingBoard(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "缺板信息表.xls", "数据", PlateDetailsRespVO.class, list);

    }




}
