package cn.iocoder.yudao.module.server.controller.admin.workorderInfos;

import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileUploadReqVO;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import cn.iocoder.yudao.module.server.dal.dataobject.workorderInfos.WorkorderInfosDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
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 java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.module.server.controller.admin.workorderInfos.vo.*;
import cn.iocoder.yudao.module.server.service.workorderInfos.WorkorderInfosService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通辽工单控制器
 */
@Tag(name = "管理后台 - 工单")
@RestController
@RequestMapping("/server/workorder-infos")
@Validated
public class WorkorderInfosController {

    @Resource
    private WorkorderInfosService workorderInfosService;

    @Resource
    private FileService fileService;

    @PostMapping("/create")
    @Operation(summary = "创建工单")
    @PreAuthorize("@ss.hasPermission('server:workorder-infos:create')")
    public CommonResult<Long> createWorkorderInfos(@Valid @RequestBody WorkorderInfosSaveReqVO createReqVO) {
        return success(workorderInfosService.createWorkorderInfos(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新工单")
    @PreAuthorize("@ss.hasPermission('server:workorder-infos:update')")
    public CommonResult<Boolean> updateWorkorderInfos(@Valid @RequestBody WorkorderInfosSaveReqVO updateReqVO) {
        workorderInfosService.updateWorkorderInfos(updateReqVO);
        return success(true);
    }

    @PostMapping("/close")
    @Operation(summary = "关闭工单")
    @PreAuthorize("@ss.hasPermission('server:workorder-infos:update')")
    public CommonResult<Boolean> closeWorkorderInfos(@RequestParam("id") Long id) {
        workorderInfosService.closeWorkorderInfos(id);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除工单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:workorder-infos:delete')")
    public CommonResult<Boolean> deleteWorkorderInfos(@RequestParam("id") Long id) {
        workorderInfosService.deleteWorkorderInfos(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:workorder-infos:query')")
    public CommonResult<WorkorderInfosRespVO> getWorkorderInfos(@RequestParam("id") Long id) {
        WorkorderInfosRespVO workorderInfosRespVO = workorderInfosService.getWorkorderInfos(id);
        return success(workorderInfosRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单分页")
    @PreAuthorize("@ss.hasPermission('server:workorder-infos:query')")
    public CommonResult<PageResult<WorkorderInfosRespVO>> getWorkorderInfosPage(@Valid WorkorderInfosPageReqVO pageReqVO) {
        PageResult<WorkorderInfosRespVO> pageResult = workorderInfosService.getWorkorderInfosPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/belonging-user")
    @Operation(summary = "获得所属用户")
    public CommonResult<List<DeptDO>> getBelongingUser(@Valid DeptDO deptDO) {
        List<DeptDO> deptDOList = workorderInfosService.getBelongingUser(deptDO);
        return success(BeanUtils.toBean(deptDOList, DeptDO.class));
    }

    @GetMapping("/workorder-belonging-user")
    @Operation(summary = "工单获得所属用户")
    public CommonResult<List<DeptDO>> getWorkorderBelongingUser() {
        List<DeptDO> deptDOList = workorderInfosService.getWorkorderBelongingUser();
        return success(BeanUtils.toBean(deptDOList, DeptDO.class));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出工单 Excel")
//    @PreAuthorize("@ss.hasPermission('server:workorder-infos:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportWorkorderInfosExcel(@Valid WorkorderInfosPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<WorkorderInfosDO> list = workorderInfosService.getWorkorderInfosPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "工单.xls", "数据", WorkorderInfosRespVO.class,
//                        BeanUtils.toBean(list, WorkorderInfosRespVO.class));
//    }

    @PostMapping("/upload")
    @Operation(summary = "上传文件", description = "模式一：后端上传文件")
    public CommonResult<Integer> uploadFile(FileUploadReqVO uploadReqVO) throws Exception {
        MultipartFile file = uploadReqVO.getFile();
        byte[] content = IoUtil.readBytes(file.getInputStream());
        return success(workorderInfosService.createFile(content, file.getOriginalFilename(),
                uploadReqVO.getDirectory(), file.getContentType(),"workorder"));
    }

    @PostMapping("/check")
    @Operation(summary = "审核工单")
    public CommonResult<Boolean> checkWorkorderInfos(@Valid @RequestBody WorkorderInfosSaveReqVO updateReqVO) {
        workorderInfosService.checkWorkorderInfos(updateReqVO);
        return success(true);
    }

    /**
     * 首页统计接口
     */
    @GetMapping("/card")
    @Operation(summary = "首页卡片")
    public CommonResult<HashMap<String,Long>> getCardList() {
        HashMap<String,Long> CardList = workorderInfosService.getCardList();
        return success(CardList);
    }

    @GetMapping("/dept-workorder")
    @Operation(summary = "首页部门工单统计")
    public CommonResult<List<DeptWorkorderVO>> getDeptWorkorder() {
        List<DeptWorkorderVO> CardList = workorderInfosService.getDeptWorkorder();
        return success(CardList);
    }

    @GetMapping("/pending-workorder")
    @Operation(summary = "首页待处理工单列表")
    public CommonResult<PageResult<WorkorderInfosRespVO>> getPendingWorkorder(@Valid WorkorderInfosPageReqVO pageReqVO) {
        PageResult<WorkorderInfosRespVO> CardList = workorderInfosService.getPendingWorkorder(pageReqVO);
        return success(CardList);
    }

}