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


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.*;
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.productionStatus.OrderModuleReqVO;
import com.cf.imes.module.manage.dal.dataobject.query.OrderPackageDO;
import com.cf.imes.module.manage.service.pack.PackService;
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.*;

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/pack")
@Validated
public class PackController {



    @Resource
    private PackService packService;



    @Resource
    private ProductionStatusService productionStatusService;




    @GetMapping("/page")
    @Operation(summary = "打包分页查询")
    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<PageResult<PackRespVO>> getPlatePage(@Valid PackPageReqVO pageReqVO) {

        return CommonResult.success(packService.getPackPage(pageReqVO));

    }


    @GetMapping("/getOrderPlate")
    @Operation(summary = "查看生产订单对应的板材信息")
    @Parameter(name = "orderId", description = "生产单Id", required = true)
    public CommonResult<List<PlateDetailsRespVO>> getOrderPlateList(@Valid @RequestParam("orderId") Long orderId) {

        return success(packService.getOrderPlateList(orderId));

    }


    @GetMapping("/getOrderPlateList")
    @Operation(summary = "查询多个生产单对应的板材信息")
    public CommonResult<List<PlateDetailsRespVO>> getOrdersPlateList(@Valid @RequestParam  List<Long> orderIds) {

        return success(packService.getOrdersPlateList(orderIds));

    }


    @GetMapping("/getOrderPack")
    @Operation(summary = "查看生产订单对应的包裹信息")
    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<PageResult<OrderPackageVO>> getOrderPack(@Valid PackageDetailsReqVO reqVO) {
        PageResult<OrderPackageDO> orderPack = packService.getOrderPack(reqVO);
        return success(BeanUtils.toBean(orderPack,OrderPackageVO.class));

    }



    @GetMapping("/queryPackageDetails")
    @Operation(summary = "查看包裹对应的板材信息")
    @PreAuthorize("@ss.hasPermission('manage:query:query')")
    public CommonResult<PageResult<PackageDetailsRespVO>>  queryPackageDetails(@Valid PackDetailsReqVO reqVO){

        return CommonResult.success(productionStatusService.queryPackageDetails(reqVO));

    }




    @GetMapping("/export")
    @Operation(summary = "导出缺板")
    @PreAuthorize("@ss.hasPermission('manage:pack: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, "缺板信息表.xlsx", "数据", PlateDetailsRespVO.class, list);

    }






//    @GetMapping("/pagePlate")
//    @Operation(summary = "查看板材列表分页")
//    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<PageResult<PlateDetailsRespVO>>  queryPlateDetails(@Valid OrderModuleReqVO pageReqVO){

        PageResult<PlateDetailsRespVO> packageDetailsRespVOPageResult = packService.queryPlateDetails(pageReqVO);

        return success(packageDetailsRespVOPageResult);

    }




    @GetMapping("/getParts")
    @Operation(summary = "查看配件列表分页")
    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<PageResult<OrderPartsRespVO>> getParts(@Valid OrderPartsReqVO reqVO) {

        return success(packService.getPartsList(reqVO));


    }



    @PutMapping("/updateParts")
    @Operation(summary = "更新配件")
    @PreAuthorize("@ss.hasPermission('manage:pack:updateParts')")
    public CommonResult<Boolean> updateParts(@Valid @RequestBody OrderPartsReqVO reqVO) {

        packService.updateParts(reqVO);

        return success(true);


    }






    // TODO 目前前端接口只有查询配件包裹时会调用，板材是否会调用待确认
//    @GetMapping("/getPartsPack")
//    @Operation(summary = "查看配件包裹详情")
//    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<List<OrderPartsRespVO>>  queryPackageDetails(@Valid PrintOrderPackReqVO reqVO){

        return success(packService.queryPackageDetails(reqVO));


    }





    @PostMapping("/insertPack")
    @Operation(summary = "新增包裹")
    @PreAuthorize("@ss.hasPermission('manage:pack:insertPack')")
    public CommonResult<OrderPackRespVO> insertPack(@Valid @RequestBody OrderPackReqVO packageVO) {

        OrderPackageDO orderPackageDO = packService.insertPack(packageVO);

        return success(BeanUtils.toBean(orderPackageDO, OrderPackRespVO.class));

    }




    @PutMapping("/updatePack")
    @Operation(summary = "更新包裹")
    @PreAuthorize("@ss.hasPermission('manage:pack:updatePack')")
    public CommonResult<OrderPackRespVO> updatePack(@Valid @RequestBody OrderPackReqVO packageVO) {
        OrderPackageDO orderPackageDO = packService.updatePack(packageVO);
        return success(BeanUtils.toBean(orderPackageDO, OrderPackRespVO.class));

    }




    @GetMapping("/selectPack")
    @Operation(summary = "查询包裹")
    @Parameter(name = "orderId", description = "生产单Id", required = true)
    @PreAuthorize("@ss.hasPermission('manage:pack:update')")
    public CommonResult<List<OrderPackRespVO>> selectPack(@Valid @RequestParam("orderId") Long orderId) {


        return success(packService.selectPack(orderId));

    }



    @DeleteMapping("/deletePack")
    @Operation(summary = "删除包裹及其关联的包裹信息(删除全部)")
    @PreAuthorize("@ss.hasPermission('manage:pack:delete')")
    public CommonResult<Boolean> deleteOrderPack (@Valid @RequestBody PackReqVO reqVO) {

        packService.deleteOrderPack(reqVO);
        return success(true);

    }




    @PutMapping("/unpacking")
    @Operation(summary = "拆包-更改包裹为未封包")
    @Parameter(name = "packageID", description = "包裹ID", required = true)
    @PreAuthorize("@ss.hasPermission('manage:pack:unpacking')")
    public CommonResult<Boolean> unpacking(@Valid @RequestParam("packageID") Long packageID) {

        packService.unpacking(packageID);

        return success(true);
    }








//    @GetMapping("/GetOrderPlateGroup")
//    @Operation(summary = "显示分组房间名称")
//    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<List<OrderPlateGroupRespVO>> GetOrderPlateGroup(@Valid Long orderId) {

        return success(packService.GetOrderPlateGroup(orderId));

    }





    @GetMapping("/print")
    @Operation(summary = "包裹订单打印")
    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<PrintOrderPackRespVO> printOrderPack (@Valid PrintOrderPackReqVO reqVO) {

        return success(packService.printOrderPack(reqVO));

    }





    @PostMapping("/insertParts")
    @Operation(summary = "新增配件包裹")
    @PreAuthorize("@ss.hasPermission('manage:pack:insert')")
    public CommonResult<OrderPackageDO> addNewPartsPack (@Valid @RequestBody OrderPackReqVO reqVO) {

        return success(packService.addNewPartsPack(reqVO));

    }




    @PostMapping("/insertOtherPack")
    @Operation(summary = "新增外部配件-即新增一个包裹")
    @PreAuthorize("@ss.hasPermission('manage:pack:insert')")
    public CommonResult<OrderPackageDO> addNewOtherPack(@Valid @RequestBody OtherPackReqVO reqVO) {

        return success(packService.addNewOtherPack(reqVO));

    }


    @PutMapping("/insertOther")
    @Operation(summary = "新增外部配件")
    @PreAuthorize("@ss.hasPermission('manage:pack:insert')")
    public CommonResult<OrderPartsRespVO> addNewOtherParts (@Valid @RequestBody OtherPackReqVO reqVO) {

        return success(packService.addNewOtherParts(reqVO));

    }



    @DeleteMapping("/deleteOtherParts")
    @Operation(summary = "删除配件包裹中的配件(删除某一个)")
    @PreAuthorize("@ss.hasPermission('manage:pack:delete')")
    public CommonResult<Boolean> deleteOtherParts (@Valid @RequestBody DeleteOtherPartsReqVO reqVO) {

        packService.deleteOtherParts(reqVO);
        return success(true);


    }






/*
    @GetMapping("/query")
    @Operation(summary = "板材列表下的查询")
    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<List<OrderPlateGroupRespVO>> GetOrderPlateGroup(@Valid Long orderId) {

        return success(packService.GetOrderPlateGroup(orderId));

    }
*/




}
