package com.cf.imes.module.executor.controller.admin.manage.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.manage.plate.vo.remain.RemainPlatePageReqVO;
import com.cf.imes.module.executor.controller.admin.manage.plate.vo.remain.RemainPlateRespVO;
import com.cf.imes.module.executor.controller.admin.manage.plate.vo.remain.RemainPlateSaveReqVO;
import com.cf.imes.module.executor.controller.admin.manage.plate.vo.remain.RemainPlateUptReqVO;
import com.cf.imes.module.executor.dal.dataobject.remainplaten.RemainPlateDO;
import com.cf.imes.module.executor.service.manage.remainplaten.RemainPlateManageService;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
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("/manage/remain-plate")
@Validated
public class RemainPlateManageController {

    @Resource
    private RemainPlateManageService remainPlateService;

    @PostMapping("/create")
    @Operation(summary = "添加余料（单个）")
    @PreAuthorize("@ss.hasAnyPermissions('manage:remain-plate:create','placeorder:optimize')")
    public CommonResult<Boolean> createRemainPlate(@Valid @RequestBody RemainPlateSaveReqVO createReqVO) {
        return success(remainPlateService.createRemainPlate(createReqVO));
    }

    @PostMapping("/createMultiple")
    @Operation(summary = "创建订单余料板表(多个)")
    @PreAuthorize("@ss.hasAnyPermissions('manage:remain-plate:create','producePlan:optimize:remain-plate-store','placeorder:optimize:remain-plate-store')")
    public CommonResult<Boolean> createRemainPlateMultiple(@Valid @RequestBody Set<RemainPlateSaveReqVO> createReqVOS) {
        return success(remainPlateService.createRemainPlateMultiple(createReqVOS));
    }

    @PutMapping("/update")
    @Operation(summary = "余料修改")
    @PreAuthorize("@ss.hasPermission('manage:remain-plate:update')")
    public CommonResult<Boolean> updateRemainPlate(@Valid @RequestBody RemainPlateSaveReqVO updateReqVO) {
        remainPlateService.updateRemainPlate(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateStatus")
    @Operation(summary = "余料板批量核销")
    @PreAuthorize("@ss.hasAnyPermissions('manage:remain-plate:update','manage:remain-plate:write-off')")
    public CommonResult<Boolean> updateRemainPlate(@RequestBody RemainPlateUptReqVO updateReqVO) {
        remainPlateService.updateRemainPlateStatus(updateReqVO.getIds(),
                updateReqVO.getStatus(),
                updateReqVO.getUseType(),
                updateReqVO.getPlanId());
        return success(true);
    }

    @PutMapping("/revertUpdate")
    @Operation(summary = "余料板批量释放")
    @PreAuthorize("@ss.hasAnyPermissions('manage:remain-plate:update','manage:remain-plate:release')")
    public CommonResult<Boolean> revertRemainPlate(@RequestBody RemainPlateUptReqVO updateReqVO) {
        remainPlateService.revertRemainPlateStatus(updateReqVO.getIds(),updateReqVO.getStatus());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除余料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('manage:remain-plate:delete')")
    public CommonResult<Boolean> deleteRemainPlate(@RequestParam("id") Set<Long> ids) {
        remainPlateService.deleteRemainPlate(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单余料板表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('manage:remain-plate:query')")
    public CommonResult<RemainPlateRespVO> getRemainPlate(@RequestParam("id") Long id) {
        RemainPlateDO remainPlate = remainPlateService.getRemainPlate(id);
        return success(BeanUtils.toBean(remainPlate, RemainPlateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单余料板表分页")
    @PreAuthorize("@ss.hasAnyPermissions('manage:remain-plate:query','placeorder:optimize','placeorder:remain-insert','production:manager-list:calculate')")
    public CommonResult<PageResult<RemainPlateRespVO>> getRemainPlatePage(@Valid RemainPlatePageReqVO pageReqVO) {
        PageResult<RemainPlateDO> pageResult = remainPlateService.getRemainPlatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RemainPlateRespVO.class));
    }

    @GetMapping("/pageAdd")
    @Operation(summary = "获得订单余料板可添加分页")
    @PreAuthorize("@ss.hasPermission('manage:remain-plate:query')")
    public CommonResult<PageResult<RemainPlateRespVO>> getRemainPlatePageToAdd(@Valid RemainPlatePageReqVO pageReqVO) {
        PageResult<RemainPlateDO> pageResult = remainPlateService.getRemainPlatePageToAdd(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RemainPlateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单余料板表Excel")
    @PreAuthorize("@ss.hasPermission('manage:remain-plate:export')")
    @OperateLog(type = EXPORT)
    public void exportRemainPlateExcel(@Valid RemainPlatePageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RemainPlateDO> list = remainPlateService.getRemainPlatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单余料板表 order_remain_plate_{N}.xls", "数据", RemainPlateRespVO.class,
                BeanUtils.toBean(list, RemainPlateRespVO.class));
    }

}