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

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.information.vo.IotInformationDbPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.information.vo.IotInformationDbRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.information.vo.IotInformationDbSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.vo.IotMaintainRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotProductClassifyDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.information.IotInformationDbDO;
import cn.iocoder.yudao.module.pms.service.IotProductClassifyService;
import cn.iocoder.yudao.module.pms.service.information.IotInformationDbService;
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 static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 故障知识库")
@RestController
@RequestMapping("/rq/iot-information-db")
@Validated
public class IotInformationDbController {

    @Resource
    private IotInformationDbService iotInformationDbService;
    @Resource
    private IotProductClassifyService iotProductClassifyService;

    @PostMapping("/create")
    @Operation(summary = "创建故障知识库")
    @PreAuthorize("@ss.hasPermission('rq:iot-information-db:create')")
    public CommonResult<Long> createIotInformationDb(@Valid @RequestBody IotInformationDbSaveReqVO createReqVO) {
        return success(iotInformationDbService.createIotInformationDb(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新故障知识库")
    @PreAuthorize("@ss.hasPermission('rq:iot-information-db:update')")
    public CommonResult<Boolean> updateIotInformationDb(@Valid @RequestBody IotInformationDbSaveReqVO updateReqVO) {
        iotInformationDbService.updateIotInformationDb(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除故障知识库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-information-db:delete')")
    public CommonResult<Boolean> deleteIotInformationDb(@RequestParam("id") Long id) {
        iotInformationDbService.deleteIotInformationDb(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得故障知识库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-information-db:query')")
    public CommonResult<IotInformationDbRespVO> getIotInformationDb(@RequestParam("id") Long id) {
        IotInformationDbDO iotInformationDb = iotInformationDbService.getIotInformationDb(id);
        return success(BeanUtils.toBean(iotInformationDb, IotInformationDbRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得故障知识库分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-information-db:query')")
    public CommonResult<PageResult<IotInformationDbRespVO>> getIotInformationDbPage(@Valid IotInformationDbPageReqVO pageReqVO) {
        PageResult<IotInformationDbDO> pageResult = iotInformationDbService.getIotInformationDbPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotInformationDbRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出故障知识库 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-information-db:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotInformationDbExcel(@Valid IotInformationDbPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotInformationDbDO> list = iotInformationDbService.getIotInformationDbPage(pageReqVO).getList();
        List<IotInformationDbRespVO> bean = BeanUtils.toBean(list, IotInformationDbRespVO.class);
        bean.forEach(e ->{
            IotProductClassifyDO iotProductClassify = iotProductClassifyService.getIotProductClassify(e.getDeviceType());
            if (iotProductClassify != null) {
                e.setDeviceTypeName(iotProductClassify.getName());
            }
        });
        // 导出 Excel
        ExcelUtils.write(response, "故障知识库.xls", "数据", IotInformationDbRespVO.class,
                        BeanUtils.toBean(bean, IotInformationDbRespVO.class));
    }

}
