package com.cf.imes.module.system.controller.admin.process;

import com.cf.imes.framework.common.exception.ErrorCode;
import com.cf.imes.module.system.controller.admin.process.vo.group.ProcessGroupPageReqVO;
import com.cf.imes.module.system.controller.admin.process.vo.group.ProcessGroupRespVO;
import com.cf.imes.module.system.controller.admin.process.vo.group.ProcessGroupSaveReqVO;
import com.cf.imes.module.system.controller.admin.process.vo.group.ProcessListSaveReqVO;
import com.cf.imes.module.system.controller.admin.process.vo.process.ProcessRespVO;
import com.cf.imes.module.system.dal.dataobject.process.ProcessDO;
import com.cf.imes.module.system.dal.dataobject.process.ProcessGroupDO;
import com.cf.imes.module.system.service.process.ProcessGroupService;
import com.cf.imes.module.system.service.process.ProcessService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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 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 javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 工序组")
@RestController
@RequestMapping("/system/process-group")
@Validated
public class ProcessGroupController {

    @Resource
    private ProcessGroupService processGroupService;


    @PostMapping("/create")
    @Operation(summary = "创建工序组")
    @PreAuthorize("@ss.hasPermission('system:process-group:create')")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Long> createProcessGroup(@Valid @RequestBody ProcessGroupSaveReqVO createReqVOLists) {
        return success(processGroupService.createProcessGroup(createReqVOLists));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工序组")
    @PreAuthorize("@ss.hasPermission('system:process-group:update')")
    public CommonResult<Boolean> updateProcessGroup(@Valid @RequestBody ProcessGroupSaveReqVO updateReqVO) {
        processGroupService.updateProcessGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工序组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:process-group:delete')")
    public CommonResult<Boolean> deleteProcessGroup(@RequestParam("id") Long id) {
        processGroupService.deleteProcessGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工序组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:process-group:query')")
    public CommonResult<ProcessListSaveReqVO> getProcessGroup(@RequestParam("id") Long id) {
        ProcessListSaveReqVO processGroup = processGroupService.getProcessGroup(id);
        return success(BeanUtils.toBean(processGroup, ProcessListSaveReqVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工序组分页")
    @PreAuthorize("@ss.hasPermission('system:process-group:query')")
    public CommonResult<PageResult<ProcessGroupRespVO>> getProcessGroupPage(@Valid ProcessGroupPageReqVO pageReqVO) {
        PageResult<ProcessGroupDO> pageResult = processGroupService.getProcessGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProcessGroupRespVO.class));
    }

    @GetMapping({"/list-all-simple","/simple-list"})
    @Operation(summary = "获得工序组全部")
    @Parameter(name = "name", description = "工序组名称", required = false, example = "xxx")
    @PreAuthorize("@ss.hasPermission('system:process-group:query')")
    public CommonResult<List<ProcessGroupRespVO>> getProcessGroupAll(@RequestParam(required = false, value = "name") String name) {
        return success(processGroupService.getProcessGroupList(name));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工序组表")
    @PreAuthorize("@ss.hasPermission('system:process-group:export')")
    @OperateLog(type = EXPORT)
    public void exportProcessGroupExcel(@Valid ProcessGroupPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProcessGroupDO> list = processGroupService.getProcessGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工序组表 process_group.xls", "数据", ProcessGroupRespVO.class,
                BeanUtils.toBean(list, ProcessGroupRespVO.class));
    }

}