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

import cn.hutool.core.collection.CollUtil;
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.iotdevicetemplate.vo.DeviceTemplateUpdateStatusReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicetemplate.vo.IotDeviceTemplatePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicetemplate.vo.IotDeviceTemplateRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicetemplate.vo.IotDeviceTemplateSaveReqVO;
import cn.iocoder.yudao.module.pms.convert.iotdevicetemplate.IotDeviceTemplateConvert;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotProductClassifyDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicetemplate.IotDeviceTemplateDO;
import cn.iocoder.yudao.module.pms.service.IotProductClassifyService;
import cn.iocoder.yudao.module.pms.service.iotdevicetemplate.IotDeviceTemplateService;
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 = "管理后台 - PMS 功能优化 设备模板")
@RestController
@RequestMapping("/rq/iot-device-template")
@Validated
public class IotDeviceTemplateController {

    @Resource
    private IotDeviceTemplateService iotDeviceTemplateService;
    @Resource
    private IotProductClassifyService iotProductClassifyService;

    @PostMapping("/create")
    @Operation(summary = "创建PMS 功能优化 设备模板")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-template:create')")
    public CommonResult<Long> createIotDeviceTemplate(@Valid @RequestBody IotDeviceTemplateSaveReqVO createReqVO) {
        return success(iotDeviceTemplateService.createIotDeviceTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS 功能优化 设备模板")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-template:update')")
    public CommonResult<Boolean> updateIotDeviceTemplate(@Valid @RequestBody IotDeviceTemplateSaveReqVO updateReqVO) {
        iotDeviceTemplateService.updateIotDeviceTemplate(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改 设备属性模板 状态")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-template:update')")
    public CommonResult<Boolean> updateDeviceTemplateStatus(@Valid @RequestBody DeviceTemplateUpdateStatusReqVO reqVO) {
        iotDeviceTemplateService.updateDeviceTemplateStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS 功能优化 设备模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-device-template:delete')")
    public CommonResult<Boolean> deleteIotDeviceTemplate(@RequestParam("id") Long id) {
        iotDeviceTemplateService.deleteIotDeviceTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS 功能优化 设备模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-template:query')")
    public CommonResult<IotDeviceTemplateRespVO> getIotDeviceTemplate(@RequestParam("id") Long id) {
        IotDeviceTemplateDO iotDeviceTemplate = iotDeviceTemplateService.getIotDeviceTemplate(id);
        return success(BeanUtils.toBean(iotDeviceTemplate, IotDeviceTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS 功能优化 设备模板分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-template:query')")
    public CommonResult<PageResult<IotDeviceTemplateRespVO>> getIotDeviceTemplatePage(@Valid IotDeviceTemplatePageReqVO pageReqVO) {
        PageResult<IotDeviceTemplateDO> pageResult = iotDeviceTemplateService.getIotDeviceTemplatePage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }
        // 拼接所属 设备分类
        Map<Long, IotProductClassifyDO> deviceCategoryMap = iotProductClassifyService.getIotProductClassifyMap(
                convertList(pageResult.getList(), IotDeviceTemplateDO::getDeviceCategoryId));
        return success(new PageResult<>(IotDeviceTemplateConvert.INSTANCE.convertList(pageResult.getList(), deviceCategoryMap),
                pageResult.getTotal()));
    }

    @GetMapping("/list-by-device-category-id")
    @Operation(summary = "根据设备分类id获取模板属性列表")
    @Parameter(name = "deviceCategoryId", description = "设备分类id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-template:query')")
    public CommonResult<IotDeviceTemplateRespVO> getAttrsByDeviceCategoryId(@RequestParam("deviceCategoryId") Long deviceCategoryId) {
        IotDeviceTemplateDO deviceTemplate = iotDeviceTemplateService.getTemplateByDeviceCategoryId(deviceCategoryId);
        return success(BeanUtils.toBean(deviceTemplate, IotDeviceTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS 功能优化 设备模板 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-device-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDeviceTemplateExcel(@Valid IotDeviceTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotDeviceTemplateDO> list = iotDeviceTemplateService.getIotDeviceTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS 功能优化 设备模板.xls", "数据", IotDeviceTemplateRespVO.class,
                        BeanUtils.toBean(list, IotDeviceTemplateRespVO.class));
    }

}