package cn.iocoder.yudao.module.pms.controller.admin.iotprojecttasktemplate;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttasktemplate.vo.IotProjectTaskTemplatePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttasktemplate.vo.IotProjectTaskTemplateRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttasktemplate.vo.IotProjectTaskTemplateSaveReqVO;
import cn.iocoder.yudao.module.pms.convert.iotprojecttasktemplate.IotProjectTaskTemplateConvert;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttasktemplate.IotProjectTaskTemplateDO;
import cn.iocoder.yudao.module.pms.service.iotprojecttasktemplate.IotProjectTaskTemplateService;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 项目任务属性模板")
@RestController
@RequestMapping("/rq/iot-project-task-template")
@Validated
public class IotProjectTaskTemplateController {

    @Resource
    private IotProjectTaskTemplateService iotProjectTaskTemplateService;
    @Resource
    private DeptService deptService;

    @PostMapping("/create")
    @Operation(summary = "创建项目任务属性模板")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-template:create')")
    public CommonResult<Long> createIotProjectTaskTemplate(@Valid @RequestBody IotProjectTaskTemplateSaveReqVO createReqVO) {
        return success(iotProjectTaskTemplateService.createIotProjectTaskTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目任务属性模板")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-template:update')")
    public CommonResult<Boolean> updateIotProjectTaskTemplate(@Valid @RequestBody IotProjectTaskTemplateSaveReqVO updateReqVO) {
        iotProjectTaskTemplateService.updateIotProjectTaskTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目任务属性模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-template:delete')")
    public CommonResult<Boolean> deleteIotProjectTaskTemplate(@RequestParam("id") Long id) {
        iotProjectTaskTemplateService.deleteIotProjectTaskTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目任务属性模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-template:query')")
    public CommonResult<IotProjectTaskTemplateRespVO> getIotProjectTaskTemplate(@RequestParam("id") Long id) {
        IotProjectTaskTemplateDO iotProjectTaskTemplate = iotProjectTaskTemplateService.getIotProjectTaskTemplate(id);
        return success(BeanUtils.toBean(iotProjectTaskTemplate, IotProjectTaskTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目任务属性模板分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-template:query')")
    public CommonResult<PageResult<IotProjectTaskTemplateRespVO>> getIotProjectTaskTemplatePage(@Valid IotProjectTaskTemplatePageReqVO pageReqVO) {
        PageResult<IotProjectTaskTemplateDO> pageResult = iotProjectTaskTemplateService.getIotProjectTaskTemplatePage(pageReqVO);
        Map<Long, DeptDO> deptMap = deptService.getDeptMap(
                convertList(pageResult.getList(), IotProjectTaskTemplateDO::getDeptId));
        List<IotProjectTaskTemplateRespVO> taskTemplateRespVOS = IotProjectTaskTemplateConvert.INSTANCE.convertList(pageResult.getList(), deptMap);
        return success(new PageResult<>(taskTemplateRespVOS, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目任务属性模板 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotProjectTaskTemplateExcel(@Valid IotProjectTaskTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotProjectTaskTemplateDO> list = iotProjectTaskTemplateService.getIotProjectTaskTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目任务属性模板.xls", "数据", IotProjectTaskTemplateRespVO.class,
                        BeanUtils.toBean(list, IotProjectTaskTemplateRespVO.class));
    }

}