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

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.vo.*;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotInfoClassifyDO;
import cn.iocoder.yudao.module.pms.service.IotInfoClassifyService;
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.beans.factory.annotation.Autowired;
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.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 资料分类")
@RestController
@RequestMapping("/rq/iot-info-classify")
@Validated
public class IotInfoClassifyController {
    @Autowired
    private IotInfoClassifyService iotInfoClassifyService;

    @PostMapping("create")
    @Operation(summary = "创建资料分类")
    @PreAuthorize("@ss.hasPermission('iot:info-classify:create')")
    public CommonResult<Long> createIotInfoClassify(@Valid @RequestBody IotInfoClassifySaveReqVO createReqVO) {
        Long deptId = iotInfoClassifyService.createIotInfoClassify(createReqVO);
        return success(deptId);
    }

    @PutMapping("update")
    @Operation(summary = "更新资料分类")
    @PreAuthorize("@ss.hasPermission('iot:info-classify:update')")
    public CommonResult<Boolean> updateIotInfoClassify(@Valid @RequestBody IotInfoClassifySaveReqVO updateReqVO) {
        iotInfoClassifyService.updateIotInfoClassify(updateReqVO,"update");
        return success(true);
    }

    @PutMapping("/update/remove")
    @Operation(summary = "更新资料分类")
    @PreAuthorize("@ss.hasPermission('rq:iot-classify:update')")
    public CommonResult<Boolean> removeIotInfo(@Valid @RequestBody IotInfoClassifySaveReqVO updateReqVO) {
        IotInfoClassifyDO classify = iotInfoClassifyService.getIotInfoClassify(updateReqVO.getId());
        classify.setParentId(updateReqVO.getParentId());
        BeanUtils.copyProperties(classify, updateReqVO);
        iotInfoClassifyService.updateIotInfoClassify(updateReqVO, "remove");
        return success(true);
    }

    @DeleteMapping("delete")
    @Operation(summary = "删除资料分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:info-classify:delete')")
    public CommonResult<Boolean> deleteIotInfoClassify(@RequestParam("id") Long id) {
        iotInfoClassifyService.deleteIotInfoClassify(id);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获取资料分类列表")
    @PreAuthorize("@ss.hasPermission('iot:info-classify:query')")
    public CommonResult<List<IotInfoClassifyRespVO>> getIotInfoClassifyList(IotInfoClassifyListReqVO reqVO) {
        List<IotInfoClassifyDO> list = iotInfoClassifyService.getIotInfoClassifyList(reqVO);
        return success(BeanUtils.toBean(list, IotInfoClassifyRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取资料分类精简信息列表", description = "只包含被开启的资料分类，主要用于前端的下拉选项")
    public CommonResult<List<IotInfoClassifySimpleRespVO>> getSimpleIotInfoClassifyList(Long deviceId) {
        List<IotInfoClassifyDO> list = iotInfoClassifyService.getIotInfoClassifyList(
                new IotInfoClassifyListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()).setDeviceId(deviceId));
        return success(BeanUtils.toBean(list, IotInfoClassifySimpleRespVO.class));
    }

    @GetMapping("/get")
    @Operation(summary = "获得资料分类信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:info-classify:query')")
    public CommonResult<IotInfoClassifyRespVO> getIotInfoClassify(@RequestParam("id") Long id) {
        IotInfoClassifyDO dept = iotInfoClassifyService.getIotInfoClassify(id);
        return success(BeanUtils.toBean(dept, IotInfoClassifyRespVO.class));
    }

}
