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

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.iotdevicecategorytemplate.vo.DeviceCategoryTemplateUpdateStatusReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicecategorytemplate.vo.IotDeviceCategoryTemplatePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicecategorytemplate.vo.IotDeviceCategoryTemplateRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicecategorytemplate.vo.IotDeviceCategoryTemplateSaveReqVO;
import cn.iocoder.yudao.module.pms.convert.iotdevicecategorytemplate.IotDeviceCategoryTemplateConvert;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotProductClassifyDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicecategorytemplate.IotDeviceCategoryTemplateDO;
import cn.iocoder.yudao.module.pms.service.IotProductClassifyService;
import cn.iocoder.yudao.module.pms.service.iotdevicecategorytemplate.IotDeviceCategoryTemplateService;
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("/pms/iot-device-category-template")
@Validated
public class IotDeviceCategoryTemplateController {

    @Resource
    private IotDeviceCategoryTemplateService iotDeviceCategoryTemplateService;
    @Resource
    private IotProductClassifyService iotProductClassifyService;

    @PostMapping("/create")
    @Operation(summary = "创建设备分类属性公用模板")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-category-template:create')")
    public CommonResult<Long> createIotDeviceCategoryTemplate(@Valid @RequestBody IotDeviceCategoryTemplateSaveReqVO createReqVO) {
        return success(iotDeviceCategoryTemplateService.createIotDeviceCategoryTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备分类属性公用模板")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-category-template:update')")
    public CommonResult<Boolean> updateIotDeviceCategoryTemplate(@Valid @RequestBody IotDeviceCategoryTemplateSaveReqVO updateReqVO) {
        iotDeviceCategoryTemplateService.updateIotDeviceCategoryTemplate(updateReqVO);
        return success(true);
    }

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

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备分类属性公用模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-device-category-template:delete')")
    public CommonResult<Boolean> deleteIotDeviceCategoryTemplate(@RequestParam("id") Long id) {
        iotDeviceCategoryTemplateService.deleteIotDeviceCategoryTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备分类属性公用模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-category-template:query')")
    public CommonResult<IotDeviceCategoryTemplateRespVO> getIotDeviceCategoryTemplate(@RequestParam("id") Long id) {
        IotDeviceCategoryTemplateDO iotDeviceCategoryTemplate = iotDeviceCategoryTemplateService.getIotDeviceCategoryTemplate(id);
        return success(BeanUtils.toBean(iotDeviceCategoryTemplate, IotDeviceCategoryTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备分类属性公用模板分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-category-template:query')")
    public CommonResult<PageResult<IotDeviceCategoryTemplateRespVO>> getIotDeviceCategoryTemplatePage(@Valid IotDeviceCategoryTemplatePageReqVO pageReqVO) {
        PageResult<IotDeviceCategoryTemplateDO> pageResult = iotDeviceCategoryTemplateService.getIotDeviceCategoryTemplatePage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }
        // 拼接所属 设备分类 名称
        Map<Long, IotProductClassifyDO> deviceCategoryMap = iotProductClassifyService.getIotProductClassifyMap(
                convertList(pageResult.getList(), IotDeviceCategoryTemplateDO::getDeviceCategoryId));
        return success(new PageResult<>(IotDeviceCategoryTemplateConvert.INSTANCE.convertList(pageResult.getList(), deviceCategoryMap),
                pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备分类属性公用模板 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-category-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDeviceCategoryTemplateExcel(@Valid IotDeviceCategoryTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotDeviceCategoryTemplateDO> list = iotDeviceCategoryTemplateService.getIotDeviceCategoryTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备分类属性公用模板.xls", "数据", IotDeviceCategoryTemplateRespVO.class,
                        BeanUtils.toBean(list, IotDeviceCategoryTemplateRespVO.class));
    }

}