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

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 javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

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.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.server.controller.admin.workorderType.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.workorderType.WorkorderTypeDO;
import cn.iocoder.yudao.module.server.service.workorderType.WorkorderTypeService;

@Tag(name = "管理后台 - 工单类型")
@RestController
@RequestMapping("/server/workorder-type")
@Validated
public class WorkorderTypeController {

    @Resource
    private WorkorderTypeService workorderTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建工单类型")
    @PreAuthorize("@ss.hasPermission('server:workorder-type:create')")
    public CommonResult<Long> createWorkorderType(@Valid @RequestBody WorkorderTypeSaveReqVO createReqVO) {
        return success(workorderTypeService.createWorkorderType(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新工单类型")
    @PreAuthorize("@ss.hasPermission('server:workorder-type:update')")
    public CommonResult<Boolean> updateWorkorderType(@Valid @RequestBody WorkorderTypeSaveReqVO updateReqVO) {
        workorderTypeService.updateWorkorderType(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得工单类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:workorder-type:query')")
    public CommonResult<WorkorderTypeRespVO> getWorkorderType(@RequestParam("id") Long id) {
        WorkorderTypeDO workorderType = workorderTypeService.getWorkorderType(id);
        return success(BeanUtils.toBean(workorderType, WorkorderTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得工单类型列表")
    @PreAuthorize("@ss.hasPermission('server:workorder-type:query')")
    public CommonResult<List<WorkorderTypeRespVO>> getWorkorderTypeList(@Valid WorkorderTypeListReqVO listReqVO) {
        List<WorkorderTypeDO> list = workorderTypeService.getWorkorderTypeList(listReqVO);
        return success(BeanUtils.toBean(list, WorkorderTypeRespVO.class));
    }
    @Operation(summary = "工单查询运维商")
    @GetMapping("/suplierlist-workorder")
    public CommonResult<List<DeptDO>> getSuplierListWorkorder(@RequestParam("suplier") String suplier) {
        List<DeptDO> list = workorderTypeService.getSuplierListWorkorder(suplier);
        return success(BeanUtils.toBean(list, DeptDO.class));
    }

    @Operation(summary = "查询运维商")
    @GetMapping("/suplierlist")
    public CommonResult<List<DeptDO>> getSuplierList(@RequestParam("suplier") String suplier) {
        List<DeptDO> list = workorderTypeService.getSuplierList(suplier);
        return success(BeanUtils.toBean(list, DeptDO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出工单类型 Excel")
    @PreAuthorize("@ss.hasPermission('server:workorder-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkorderTypeExcel(@Valid WorkorderTypeListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<WorkorderTypeDO> list = workorderTypeService.getWorkorderTypeList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "工单类型.xls", "数据", WorkorderTypeRespVO.class,
                        BeanUtils.toBean(list, WorkorderTypeRespVO.class));
    }

}