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

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.OrderRespVO;
import com.cf.imes.module.executor.controller.admin.plan.bo.OrderRoomBodyList;
import com.cf.imes.module.executor.controller.admin.plan.vo.*;
import com.cf.imes.module.executor.controller.admin.plate.vo.OrderViewPlatePageRespVO;
import com.cf.imes.module.executor.service.plan.PlanService;
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 jakarta.validation.constraints.Size;
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.*;

@Tag(name = "管理后台 - 排单")
@RestController
@RequestMapping("/executor/plan")
@Validated
public class PlanController {

    @Resource
    private PlanService planService;

    @PostMapping("/create")
    @Operation(summary = "创建排单")
//    @PreAuthorize("@ss.hasPermission('manage:un-order-olan:create')")
    public CommonResult<Boolean> createPlan(@RequestBody PlanSaveReqVO createReqVO) {
        planService.createPlan(createReqVO);
        return success(Boolean.TRUE);
    }

    @PutMapping("/update")
    @Operation(summary = "更新排单")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:update','manage:order-plan:insert-plate','manage:order-plan:remove-plate')")
    public CommonResult<Boolean> updatePlan(@RequestBody PlanSaveReqVO updateReqVO) {
        planService.updatePlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除排单")
    @Parameter(name = "ids", description = "排单ids", required = true)
    @PreAuthorize("@ss.hasPermission('placeorder:delete')")
    public CommonResult<Boolean> deletePlan(@RequestParam("ids") @NotEmpty @Size(max = 20,message = "最多可同时删除20个排单") List<Long> ids) {
        planService.deletePlan(ids);
        return success(Boolean.TRUE);
    }


    @GetMapping("/get")
    @Operation(summary = "获得排单")
    @Parameter(name = "id", description = "排单id", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:query','producePlan:query')")
    public CommonResult<PlanRespVO> getPlan(@RequestParam("id") @NotNull(message = "排单数据异常，请重新操作") Long id) {
        PlanRespVO plan = planService.getPlan(id);
        return success(BeanUtils.toBean(plan, PlanRespVO.class));
    }

    @GetMapping("getPlateByPlanId")
    @Operation(summary = "根据排单id获取小板分页")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:query','producePlan:query')")
    public CommonResult<PageResult<OrderViewPlatePageRespVO>> getPlateByPlanId(@Valid GetPlateByPlanIdVO vo) {
        return success(planService.getPlateByPlanId(vo));
    }

    @GetMapping("getOrderByPlan")
    @Operation(summary = "根据排单获取订单分页")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:query','placeorder:priority','producePlan:query')")
    public CommonResult<PageResult<OrderRespVO>> getOrderByPlanId(@Valid PlanOrderPageReqVO pageReqVO) {
        return success(planService.getOrderByPlanId(pageReqVO));
    }

    @GetMapping("/page")
    @Operation(summary = "获得排单分页")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:query','placeorder:priority','producePlan:query')")
    public CommonResult<PageResult<PlanRespVO>> getPlanPage(@Valid PlanPageReqVO pageReqVO) {
        PageResult<PlanRespVO> pageResult = planService.getPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlanRespVO.class));
    }

    @GetMapping("/sort/page")
    @Operation(summary = "获得排单优先级列表")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:query','placeorder:priority','producePlan:query')")
    public CommonResult<List<PlanRespVO>> getPlanSortPage() {
        return success(planService.getPlanSortPage());
    }

    @PutMapping("/setSort")
    @Operation(summary = "设置排单优先级")
    @PreAuthorize("@ss.hasPermission('placeorder:priority')")
    public CommonResult<Boolean> setSort(@Valid  @NotEmpty(message = "参数不能空") @RequestBody List<SortVO> sortVos) {

        planService.setSort(sortVos);

        return success(Boolean.TRUE);
    }


    @PostMapping("getNotPlanOrderListPage")
    @Operation(summary = "获取未排单的订单分页列表")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<PageResult<OrderRespVOCopy>> getOrderPage(@Valid @RequestBody OrderPageReqVOCopy pageReqVO) {
        return success(planService.getOrderPage(pageReqVO));
    }


    @PostMapping("getNotPlanGoodsListPage")
    @Operation(summary = "获取未排单的大板板材分页列表")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<PageResult<OrderGoodsResp>> getOrderGoodsPage(@Valid @RequestBody OrderPageReqVOCopy pageReqVO) {
        return success(planService.getOrderGoodsPage(pageReqVO));
    }




    @GetMapping("/export-excel")
    @Operation(summary = "导出排单 Excel")
    @PreAuthorize("@ss.hasPermission('executor:plan:export')")
    @OperateLog(type = EXPORT)
    public void exportPlanExcel(@Valid PlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlanRespVO> list = planService.getPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "排单.xls", "数据", PlanRespVO.class,
                        BeanUtils.toBean(list, PlanRespVO.class));
    }



    @GetMapping("getPlateList")
    @Operation(summary = "根据排单ID获取板材列表（未分页）")
    @Parameter(name = "planIds", description = "排单id", required = true)
    @PreAuthorize("@ss.hasPermission('placeorder:query')")
    public CommonResult<List<PlateResList>> getPlateList(@Valid @RequestParam("planIds") @Size(max = 20,message = "最多可同时导出20个排单") List<Long> planIds) {
        return success(planService.getPlateList(planIds));
    }


    @GetMapping("getOrderRoomBodyList")
    @Operation(summary = "获取未排单的订单的柜体和房间名称")
    @Parameter(name = "orderIds", description = "订单ID", required = true)
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<List<OrderRoomBodyList>> getOrderRoomBody(@Valid @NotEmpty(message = "订单ID不能为空") @RequestParam List<Long> orderIds) {

        return success(planService.getOrderRoomBody(orderIds));
    }



    @PostMapping("getPlanGoodsList")
    @Operation(summary = "未排单混单获取分类的排单信息")
    @OperateLog(enable = false)
    public CommonResult<List<PlanActualGoodsResp>> getPlanGoodsList(@Valid @RequestBody SavePlanPlateList savePlanPlateList) {

        return success(planService.getPlanGoodsList(savePlanPlateList));

    }


    @GetMapping("getPlanPlate")
    @Operation(summary = "根据排单ID获取打印标签需要的小板信息")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:print-label','producePlan:print-label')")
    public CommonResult<List<PlateResList>> getPlanPlateList(@Valid @RequestParam("planId") Long planId) {

        return success(planService.getPlanPlateList(planId));

    }


    @PostMapping("/lock")
    @Operation(summary = "获取排单锁")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:optimize','production:manager-list:calculate','manage:order-plan:process-scheme','producePlan:order-plan:process-scheme','producePlan:optimize')")
    public CommonResult<Boolean> getPlanLock(@RequestBody OrderPlanLockReqVO orderPlanLockReqVO) {
        planService.getPlanLock(orderPlanLockReqVO);
        return success(true);
    }

    @PostMapping("/release/lock")
    @Operation(summary = "释放排单锁")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:optimize','production:manager-list:calculate','manage:order-plan:process-scheme','producePlan:order-plan:process-scheme','producePlan:optimize')")
    public CommonResult<Boolean> releasePlanLock(@RequestBody OrderPlanLockReqVO orderPlanLockReqVO) {
        planService.releasePlanLock(orderPlanLockReqVO);
        return success(true);
    }
}