package com.cf.imes.module.executor.controller.admin.plate;

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.executor.controller.admin.order.vo.order.OrderCadViewPlatePageReqVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrdersViewCadViewInfoRespVO;
import com.cf.imes.module.executor.controller.admin.plate.vo.*;
import com.cf.imes.module.executor.dal.dataobject.plate.PlateDO;
import com.cf.imes.module.executor.dal.dataobject.plate.PlateGoodDO;
import com.cf.imes.module.executor.service.plate.PlateService;
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 jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;
import java.util.Set;

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("/executor/plate")
@Validated
public class PlateController {

    @Resource
    private PlateService plateService;

    @PostMapping("/create")
    @Operation(summary = "创建订单板件")
    @PreAuthorize("@ss.hasPermission('executor:plate:create')")
    public CommonResult<Long> createPlate(@Valid @RequestBody PlateSaveReqVO createReqVO) {
        return success(plateService.createPlate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单板件")
    @PreAuthorize("@ss.hasPermission('executor:plate:update')")
    public CommonResult<Boolean> updatePlate(@Valid @RequestBody PlateSaveReqVO updateReqVO) {
        plateService.updatePlate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单板件")
    @Parameter(name = "plateIds", description = "板材ID", required = true)
    @Parameter(name = "planId", description = "排单ID", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('executor:plate:delete','placeorder:optimize')")
    public CommonResult<Boolean> deletePlate(@RequestParam("plateIds") @NotNull List<Long> plateIds,
                                             @RequestParam("planId") @NotNull Long planId) {
        plateService.deletePlate(plateIds,planId);
        return success(true);
    }


    @GetMapping("/page")
    @Operation(summary = "获取实际的开料大板")
    @PreAuthorize("@ss.hasAnyPermissions('unplannedOrders:query','placeorder:query')")
    public CommonResult<PageResult<PlateGoodDO>> getGoodsPlate(@Valid GoodsPlateFilterPageReqVO pageReqVO) {

        return success(plateService.getGoodsPlate(pageReqVO));
    }


    @PostMapping("/orders/noplan")
    @Operation(summary = "获取未排单的订单板件")
    @PreAuthorize("@ss.hasAnyPermissions('manage:un-order-plan:insert-plate','manage:order-plan:insert-plate')")
    public CommonResult<NoPlanPlateRespVO> getOrdersNoPlanPlate(@Valid @RequestBody NoPlanPlatePageReqVO reqVO) {
        return success(plateService.getOrdersNoPlanPlate(reqVO));
    }




    @GetMapping("/export-excel")
    @Operation(summary = "导出订单板件 Excel")
    @PreAuthorize("@ss.hasPermission('executor:plate:export')")
    @OperateLog(type = EXPORT)
    public void exportPlateExcel(@Valid PlatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlateDO> list = plateService.getPlatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单板件.xls", "数据", PlateRespVO.class,
                        BeanUtils.toBean(list, PlateRespVO.class));
    }

//    批量删除小板
    @DeleteMapping("deletePlenty")
    @Operation(summary = "批量删除小板")
    @PreAuthorize("@ss.hasPermission('executor:plate:delete')")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deletePlates(@RequestBody Set<Long> ids) {
        plateService.deletePlates(ids);
        return success(true);
    }

//    批量查询有订单号id、房间id，柜体id的板材
    @GetMapping("getPlatesByOrderId")
    @Operation(summary = "批量查询有订单号id、房间id，柜体id的板材")
    @PreAuthorize("@ss.hasPermission('executor:plate:query')")
    public  CommonResult<PageResult<PlateRespVO>> getPlatesByOrderId(@Valid PlateTermsPageReqVO pageVO) {
        return success(plateService.getPlatePageByTerms(pageVO));
    }

    @GetMapping("/getPlateModelByBodyId")
    @Operation(summary = "根据柜体id列表查询小板的造型")
    @Parameter(name = "bodyIds", description = "柜体id列表", required = true)
    @PreAuthorize("@ss.hasPermission('executor:plate:query')")
    public CommonResult<List<BodyPlateDetailVO>> getPlateModelByPlateId(@Valid @RequestParam @NotEmpty List<Long> bodyIds) {
        return success(plateService.getPlateModelByPlateId(bodyIds));
    }


    @PostMapping("/insertPlate")
    @Operation(summary = "优化生产创建板材")
    @PreAuthorize("@ss.hasAnyPermissions('executor:plate:create','placeorder:optimize:manual-optimization','producePlan:optimize:manual-optimization')")
    public CommonResult<List<InsertPlateRespVO>> createNewPlate(@Valid @RequestBody List<OptimizationSavePlateReqVO> reqVO) {
        return success(plateService.createNewPlate(reqVO));
    }

    @PostMapping("/body/view/plate/page")
    @Operation(summary = "获取柜体下的板件视图分页列表数据")
    @PreAuthorize("@ss.hasPermission('production:manager-list:detail')")
    public CommonResult<PageResult<OrderViewPlatePageRespVO>> getBodyViewPlatesPage(@Validated(OrderCadViewPageGroup.class) @RequestBody OrderCadViewPlatePageReqVO orderCadViewPlatePageReqVO) {
        return success(plateService.getBodyViewPlatesPage(orderCadViewPlatePageReqVO));
    }

    @PostMapping("/body/view/part/page")
    @Operation(summary = "获取柜体下的配件视图分页列表数据")
    @PreAuthorize("@ss.hasPermission('production:manager-list:detail')")
    public CommonResult<PageResult<OrderViewPartPageRespVO>> getBodyViewPartsPage(@Validated(OrderCadViewPageGroup.class) @RequestBody OrderCadViewPlatePageReqVO orderCadViewPlatePageReqVO) {
        return success(plateService.getBodyViewPartsPage(orderCadViewPlatePageReqVO));
    }

    @PostMapping("/body/view/cadinfo")
    @Operation(summary = "获取板件对应柜体下的所有板件+配件视图列表数据")
    @PreAuthorize("@ss.hasPermission('production:manager-list:detail')")
    public CommonResult<OrdersViewCadViewInfoRespVO> getBodyViewCadInfo(@Validated @RequestBody OrderCadViewPlatePageReqVO orderCadViewPlatePageReqVO) {
        return success(plateService.getBodyViewCadInfo(orderCadViewPlatePageReqVO));
    }

    @PostMapping("/body/view/part/cadinfo")
    @Operation(summary = "获取板件对应柜体下的所有配件视图列表数据")
    @PreAuthorize("@ss.hasPermission('production:manager-list:detail')")
    public CommonResult<OrdersViewCadViewInfoRespVO> getBodyViewPartCadInfo(@Validated @RequestBody OrderCadViewPlatePageReqVO orderCadViewPlatePageReqVO) {
        return success(plateService.getBodyViewPartCadInfo(orderCadViewPlatePageReqVO));
    }

    @GetMapping("/wechat/miniprogram/cadview/info/{plateNo:\\d{5}[A-Za-z0-9]{11}}")
    @Operation(summary = "微信小程序根据板编号获取单板视图信息")
    @Parameter(name = "plateNo", description = "板编号", required = true)
    public CommonResult<OrderPlateWechatMiniProgramCadInfoRespVO> getPlateInfoByPlateNo(@PathVariable String plateNo) {
        return success(plateService.getPlateInfoByPlateNo(plateNo));
    }

    @GetMapping("/wechat/miniprogram/cadview/order/info/{plateNo:\\d{5}[A-Za-z0-9]{11}}")
    @Operation(summary = "微信小程序根据板编号获取单板所属订单信息")
    @Parameter(name = "plateNo", description = "板编号", required = true)
    public CommonResult<OrderWechatMiniProgramCadInfoRespVO> getOrderInfoByPlateNo(@PathVariable String plateNo) {
        return success(plateService.getOrderInfoByPlateNo(plateNo));
    }

    @PostMapping("/wechat/miniprogram/cadview/order/details/{plateNo:\\d{5}[A-Za-z0-9]{11}}")
    @Operation(summary = "微信小程序根据板编号获取订单详情")
    @Parameter(name = "plateNo", description = "板编号", required = true)
    public CommonResult<OrdersViewCadViewInfoRespVO> getOrderDetailsByPlateNo(@PathVariable String plateNo, @Valid @RequestBody OrderWechatMiniProgramDetailReqVO reqVO) {
        return success(plateService.getOrderDetailsByPlateNo(plateNo, reqVO));
    }
}