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

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.servlet.http.HttpServletResponse;


import com.cf.imes.module.system.controller.admin.process.vo.group.ProcessGroupRespVO;
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 com.cf.imes.module.system.controller.admin.process.vo.process.*;
import com.cf.imes.module.system.dal.dataobject.process.ProcessDO;
import com.cf.imes.module.system.service.process.ProcessService;

@Tag(name = "管理后台 - 工序信息")
@RestController
@RequestMapping("/system/process")
@Validated
public class ProcessController {

    @Resource
    private ProcessService processService;

    @PostMapping("/create")
    @Operation(summary = "新建工序信息")
    @PreAuthorize("@ss.hasPermission('system:process:create')")
    public CommonResult<Long> createProcess(@Valid @RequestBody ProcessUserSaveReqVO createReqVO) {
        return success(processService.createProcess(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工序信息")
    @PreAuthorize("@ss.hasPermission('system:process:update')")
    public CommonResult<Boolean> updateProcess(@Valid @RequestBody ProcessUserSaveReqVO updateReqVO) {
        processService.updateProcess(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得工序信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:process:query')")
    public CommonResult<ProcessUserRespVO> getProcess(@RequestParam("id") Long id) {
        ProcessUserRespVO processUser = processService.getProcess(id);
        return success(BeanUtils.toBean(processUser, ProcessUserRespVO.class));
    }

    @GetMapping("/allGet")
    @Operation(summary = "获得工序全部信息")
    @PreAuthorize("@ss.hasPermission('system:process:query')")
    public CommonResult<PageResult<ProcessDO>> getProcessAll(@Valid ProcessPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<ProcessDO> pageResult = processService.getProcessPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProcessDO.class));
    }

    @GetMapping("/getAll")
    @Operation(summary = "获得所有工序信息")
    @PreAuthorize("@ss.hasPermission('system:process:query')")
    public CommonResult<List<ProcessDO>> getAllProcess() {
        List<ProcessDO> processUser = processService.getAllProcess();
        return success(BeanUtils.toBean(processUser, ProcessDO.class));
    }


    @GetMapping("/page")
    @Operation(summary = "获得工序信息分页")
    @PreAuthorize("@ss.hasPermission('system:process:query')")
    public CommonResult<PageResult<ProcessRespVO>> getProcessPage(@Valid ProcessPageReqVO pageReqVO) {
        PageResult<ProcessRespVO> pageResult = processService.getProcessPageAll(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProcessRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工序信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:process:export')")
    @OperateLog(type = EXPORT)
    public void exportProcessExcel(@Valid ProcessPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProcessDO> list = processService.getProcessPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工序信息表.xls", "数据", ProcessRespVO.class,
                BeanUtils.toBean(list, ProcessRespVO.class));
    }



    @GetMapping("/select")
    @Operation(summary = "查看当前登录用户的组织的工序信息")
    @Parameter(name = "userId", description = "当前登录的用户ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('manage:process:query')")
    public CommonResult<List<ProcessDO>> getUserPage(@Valid @RequestParam("userId") Long userId) {

        return success(processService.getUserProcess(userId));


    }






}