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

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.iotmodeltemplate.vo.ModelTemplateUpdateStatusReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmodeltemplate.vo.IotModelTemplatePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmodeltemplate.vo.IotModelTemplateRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmodeltemplate.vo.IotModelTemplateSaveReqVO;
import cn.iocoder.yudao.module.pms.convert.iotdevicetemplate.IotDeviceTemplateConvert;
import cn.iocoder.yudao.module.pms.convert.iotmodeltemplate.IotModelTemplateConvert;
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.dal.dataobject.iotmodeltemplate.IotModelTemplateDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotopeationfill.IotOpeationFillDO;
import cn.iocoder.yudao.module.pms.service.IotProductClassifyService;
import cn.iocoder.yudao.module.pms.service.iotdevicetemplate.IotDeviceTemplateService;
import cn.iocoder.yudao.module.pms.service.iotmodeltemplate.IotModelTemplateService;
import cn.iocoder.yudao.module.pms.service.iotopeationfill.IotOpeationFillService;
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.*;
import java.util.stream.Collectors;

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-model-template")
@Validated
public class IotModelTemplateController {

    @Resource
    private IotModelTemplateService iotDeviceTemplateService;
    @Resource
    private IotProductClassifyService iotProductClassifyService;
    @Resource
    private IotOpeationFillService iotOpeationFillService;


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

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

    @PutMapping("/update-status")
    @Operation(summary = "修改 设备属性模板 状态")
    @PreAuthorize("@ss.hasPermission('rq:iot-model-template:update')")
    public CommonResult<Boolean> updateDeviceTemplateStatus(@Valid @RequestBody ModelTemplateUpdateStatusReqVO 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-model-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-model-template:query')")
    public CommonResult<IotModelTemplateRespVO> getIotDeviceTemplate(@RequestParam("id") Long id) {
        IotModelTemplateDO iotDeviceTemplate = iotDeviceTemplateService.getIotDeviceTemplate(id);
        return success(BeanUtils.toBean(iotDeviceTemplate, IotModelTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS 功能优化 设备模板分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-model-template:query')")
    public CommonResult<PageResult<IotModelTemplateRespVO>> getIotDeviceTemplatePage(@Valid IotModelTemplatePageReqVO pageReqVO) {

        Set<Long> idList = new HashSet<>();
        if(Objects.nonNull(pageReqVO.getDeviceCategoryId())){
            idList=iotProductClassifyService.getChildIotProductClassifyList(pageReqVO.getDeviceCategoryId());
            idList.add(pageReqVO.getDeviceCategoryId());
        }

        PageResult<IotModelTemplateDO> pageResult = iotDeviceTemplateService.getIotDeviceTemplatePage(pageReqVO,idList);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }
        // 拼接所属 设备分类
        Map<Long, IotProductClassifyDO> deviceCategoryMap = iotProductClassifyService.getIotProductClassifyMap(
                convertList(pageResult.getList(), IotModelTemplateDO::getDeviceCategoryId));
        return success(new PageResult<>(IotModelTemplateConvert.INSTANCE.convertList(pageResult.getList(), deviceCategoryMap),
                pageResult.getTotal()));
    }


    @GetMapping("/isRelated")
    @Operation(summary = "获得PMS 功能优化 设备模板分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-model-template:query')")
    public CommonResult<List<IotModelTemplateRespVO>> getIsRlated(@RequestParam("deviceId") Long deviceId) {
        IotModelTemplatePageReqVO reqVO = new IotModelTemplatePageReqVO();
        reqVO.setDeviceCategoryId(deviceId);
        List<IotModelTemplateDO> related = iotDeviceTemplateService.isRelated(reqVO);
        return success(BeanUtils.toBean(related, IotModelTemplateRespVO.class));
    }


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

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

}