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

import com.cf.imes.module.executor.dal.dataobject.ordermodel.OrderModelDO;
import io.swagger.v3.oas.annotations.media.Schema;
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.plate.vo.*;
import com.cf.imes.module.executor.dal.dataobject.plate.PlateDO;
import com.cf.imes.module.executor.service.plate.PlateService;

@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 = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('executor:plate:delete')")
    public CommonResult<Boolean> deletePlate(@RequestParam("id") Long id) {
        plateService.deletePlate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产单板件")
    @PreAuthorize("@ss.hasPermission('executor:plate:query')")
    public CommonResult<List<PlateRespVO>> getPlate(@Valid PlateDetailReqVO reqVO) {

        List<PlateRespVO> plate = plateService.getPlate(reqVO);

        return success(plate);
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产单板件分页")
    @PreAuthorize("@ss.hasPermission('executor:plate:query')")
    public CommonResult<PageResult<PlateRespVO>> getPlatePage(@Valid PlatePageReqVO pageReqVO) {
        PageResult<PlateDO> pageResult = plateService.getPlatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlateRespVO.class));
    }

    @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));
    }


}