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

import com.cf.imes.module.executor.controller.admin.plate.vo.PlateTermsPageReqVO;
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 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.orderParts.vo.*;
import com.cf.imes.module.executor.dal.dataobject.orderParts.OrderPartsDO;
import com.cf.imes.module.executor.service.orderParts.OrderPartsService;

@Tag(name = "管理后台 - 生产单配件表")
@RestController
@RequestMapping("/executor/order-parts")
@Validated
public class OrderPartsController {

    @Resource
    private OrderPartsService orderPartsService;

    @PostMapping("/create")
    @Operation(summary = "创建生产单配件")
    @PreAuthorize("@ss.hasPermission('executor:order-parts:create')")
    public CommonResult<Long> createOrderParts(@Valid @RequestBody OrderPartsSaveReqVO createReqVO) {
        return success(orderPartsService.createOrderParts(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产单配件")
    @PreAuthorize("@ss.hasPermission('executor:order-parts:update')")
    public CommonResult<Boolean> updateOrderParts(@Valid @RequestBody OrderPartsSaveReqVO updateReqVO) {
        orderPartsService.updateOrderParts(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产单配件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('executor:order-parts:delete')")
    public CommonResult<Boolean> deleteOrderParts(@RequestParam("id") Long id) {
        orderPartsService.deleteOrderParts(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产单配件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('executor:order-parts:query')")
    public CommonResult<OrderPartsRespVO> getOrderParts(@RequestParam("id") Long id) {
        OrderPartsDO orderParts = orderPartsService.getOrderParts(id);
        return success(BeanUtils.toBean(orderParts, OrderPartsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产单配件分页")
    @PreAuthorize("@ss.hasPermission('executor:order-parts:query')")
    public CommonResult<PageResult<OrderPartsRespVO>> getOrderPartsPage(@Valid OrderPartsPageReqVO pageReqVO) {
        PageResult<OrderPartsDO> pageResult = orderPartsService.getOrderPartsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderPartsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产单配件 Excel")
    @PreAuthorize("@ss.hasPermission('executor:order-parts:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderPartsExcel(@Valid OrderPartsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderPartsDO> list = orderPartsService.getOrderPartsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产单配件表 order_parts_{N}.xls", "数据", OrderPartsRespVO.class,
                        BeanUtils.toBean(list, OrderPartsRespVO.class));
    }

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

}