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

import com.cf.imes.framework.es.core.valid.CreateGroup;
import com.cf.imes.framework.es.core.valid.UpdateGroup;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderRespVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import javax.validation.constraints.NotEmpty;
import java.util.*;
import java.io.IOException;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import static com.cf.imes.framework.common.pojo.CommonResult.success;
import com.cf.imes.framework.excel.core.util.ExcelUtils;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.*;
import com.cf.imes.module.executor.controller.admin.plan.vo.*;
import com.cf.imes.module.executor.service.plan.PlanService;

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

    @Resource
    private PlanService planService;

    @PostMapping("/create")
    @Operation(summary = "创建排单")
    @PreAuthorize("@ss.hasPermission('executor:plan:create')")
    public CommonResult<Long> createPlan(@Validated(CreateGroup.class) @RequestBody PlanSaveReqVO createReqVO) {
        return success(planService.createPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新排单")
    @PreAuthorize("@ss.hasPermission('executor:plan:update')")
    public CommonResult<Boolean> updatePlan(@Validated(UpdateGroup.class) @RequestBody PlanSaveReqVO updateReqVO) {
        planService.updatePlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除排单")
    @Parameter(name = "id", description = "排单id", required = true)
    @PreAuthorize("@ss.hasPermission('executor:plan:delete')")
    public CommonResult<Boolean> deletePlan(@RequestParam("id") Long id) {
        return success(planService.deletePlan(id));
    }

    @DeleteMapping("cancellation")
    @Operation(summary = "作废")
    @Parameter(name = "id", description = "排单id", required = true)
    @PreAuthorize("@ss.hasPermission('executor:plan:delete')")
    public CommonResult<Boolean> cancellation(@RequestParam("id") Long id) {
        return success(planService.cancellation(id));
    }


    @GetMapping("/get")
    @Operation(summary = "获得排单")
    @Parameter(name = "id", description = "排单id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('executor:plan:query')")
    public CommonResult<PlanRespVO> getPlan(@RequestParam("id") Long id) {
        PlanRespVO plan = planService.getPlan(id);
        return success(BeanUtils.toBean(plan, PlanRespVO.class));
    }

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

    @GetMapping("getOrderByPlan")
    @Operation(summary = "根据排单获取生产单分页")
    @PreAuthorize("@ss.hasPermission('executor:plan:query')")
    public CommonResult<PageResult<OrderRespVO>> getOrderByPlanId(@Valid PlanOrderPageReqVO pageReqVO) {
        return success(planService.getOrderByPlanId(pageReqVO));
    }

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

    @PostMapping("/setSort")
    @Operation(summary = "设置排单优先级")
    @PreAuthorize("@ss.hasPermission('executor:plan:query')")
    public CommonResult<Boolean> setSort(@Valid  @NotEmpty(message = "参数不能空") @RequestBody List<SortVO> list) {
        return success(planService.setSort(list));
    }

    @GetMapping("getNotPlanOrderListPage")
    @Operation(summary = "获取未排单的板材生产单板材分页列表")
    @PreAuthorize("@ss.hasPermission('executor:plan:query')")
    public CommonResult<PageResult<OrderRespVOCopy>> getOrderPage(@Valid OrderPageReqVOCopy pageReqVO) {
        return success(planService.getOrderPage(pageReqVO));
    }

    @GetMapping("getNotPlanPlateListPage")
    @Operation(summary = "获取未排单的板材分页列表")
    @PreAuthorize("@ss.hasPermission('executor:plan:query')")
    public CommonResult<List<PlatePage>> getNotPlanPlateListPage(@Valid PlateReqPageVO pageVO) {
        return success(planService.getNotPlanPlateListPage(pageVO));
    }

    @GetMapping("getNotPlanGoodsListPageByOrderId")
    @Operation(summary = "根据生产单id获取未排单的商品分页列表")
    @PreAuthorize("@ss.hasPermission('executor:plan:query')")
    public CommonResult<PageResult<PlanGoodsVO>> getNotPlanGoodsListPageByOrderId(@Valid NotPlanGoodsListPageVO pageVO) {
        return success(planService.getNotPlanGoodsListPageByOrderId(pageVO));
    }


    @PostMapping("addPlate")
    @Operation(summary = "添加板材")
    @PreAuthorize("@ss.hasPermission('executor:plan:update')")
    public CommonResult<Boolean> addPlate(@Valid @RequestBody AddPlateReq req) {
        return success(planService.addPlate(req));
    }


    @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("/export-plate-excel")
    @Operation(summary = "导出板材 Excel")
    @PreAuthorize("@ss.hasPermission('executor:plan:export')")
    @OperateLog(type = EXPORT)
    public void exportPlateExcel(@Valid GetPlateByPlanIdVO vo,
                                HttpServletResponse response) throws IOException {
        PageResult<PlateResList> plateByPlanId = planService.getPlateByPlanId(vo);
        // 导出 Excel
        ExcelUtils.write(response, "排单板材.xls", "数据", PlateResList.class,
                BeanUtils.toBean(plateByPlanId.getList(), PlateResList.class));
    }


}