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

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.constraints.*;
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.moduleitem.vo.*;
import com.cf.imes.module.executor.dal.dataobject.moduleitem.ModuleItemDO;
import com.cf.imes.module.executor.service.moduleitem.ModuleItemService;

@Tag(name = "管理后台 - 生产单模块明细")
@RestController
@RequestMapping("/executor/module-item")
@Validated
public class ModuleItemController {

    @Resource
    private ModuleItemService moduleItemService;

    @PostMapping("/create")
    @Operation(summary = "创建生产单模块明细")
    @PreAuthorize("@ss.hasPermission('executor:module-item:create')")
    public CommonResult<Long> createModuleItem(@Valid @RequestBody ModuleItemSaveReqVO createReqVO) {
        return success(moduleItemService.createModuleItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产单模块明细")
    @PreAuthorize("@ss.hasPermission('executor:module-item:update')")
    public CommonResult<Boolean> updateModuleItem(@Valid @RequestBody ModuleItemSaveReqVO updateReqVO) {
        moduleItemService.updateModuleItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产单模块明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('executor:module-item:delete')")
    public CommonResult<Boolean> deleteModuleItem(@RequestParam("id") Long id) {
        moduleItemService.deleteModuleItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产单模块明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('executor:module-item:query')")
    public CommonResult<ModuleItemRespVO> getModuleItem(@RequestParam("id") Long id) {
        ModuleItemDO moduleItem = moduleItemService.getModuleItem(id);
        return success(BeanUtils.toBean(moduleItem, ModuleItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产单模块明细分页")
    @PreAuthorize("@ss.hasPermission('executor:module-item:query')")
    public CommonResult<PageResult<ModuleItemRespVO>> getModuleItemPage(@Valid ModuleItemPageReqVO pageReqVO) {
        PageResult<ModuleItemDO> pageResult = moduleItemService.getModuleItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ModuleItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产单模块明细 Excel")
    @PreAuthorize("@ss.hasPermission('executor:module-item:export')")
    @OperateLog(type = EXPORT)
    public void exportModuleItemExcel(@Valid ModuleItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ModuleItemDO> list = moduleItemService.getModuleItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产单模块明细.xls", "数据", ModuleItemRespVO.class,
                        BeanUtils.toBean(list, ModuleItemRespVO.class));
    }

}