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.IotProductClassifyListReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotProductClassifyRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotProductClassifySaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotProductClassifySimpleRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotProductClassifyDO;
import cn.iocoder.yudao.module.pms.service.IotProductClassifyService;
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.validation.Valid;
import java.util.List;

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

@Tag(name = "管理后台 - 产品分类")
@RestController
@RequestMapping("/rq/iot-product-classify")
@Validated
public class IotProductClassifyController {

    @Resource
    private IotProductClassifyService iotProductClassifyService;

    @PostMapping("create")
    @Operation(summary = "创建产品分类")
    @PreAuthorize("@ss.hasPermission('iot:product-classify:create')")
    public CommonResult<Long> createIotProductClassify(@Valid @RequestBody IotProductClassifySaveReqVO createReqVO) {
        Long deptId = iotProductClassifyService.createIotProductClassify(createReqVO);
        return success(deptId);
    }

    @PutMapping("update")
    @Operation(summary = "更新产品分类")
    @PreAuthorize("@ss.hasPermission('iot:product-classify:update')")
    public CommonResult<Boolean> updateIotProductClassify(@Valid @RequestBody IotProductClassifySaveReqVO updateReqVO) {
        iotProductClassifyService.updateIotProductClassify(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/list")
    @Operation(summary = "获取产品分类列表")
    @PreAuthorize("@ss.hasPermission('iot:product-classify:query')")
    public CommonResult<List<IotProductClassifyRespVO>> getIotProductClassifyList(IotProductClassifyListReqVO reqVO) {
        List<IotProductClassifyDO> list = iotProductClassifyService.getIotProductClassifyList(reqVO);
        return success(BeanUtils.toBean(list, IotProductClassifyRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取产品分类精简信息列表", description = "只包含被开启的产品分类，主要用于前端的下拉选项")
    public CommonResult<List<IotProductClassifySimpleRespVO>> getSimpleIotProductClassifyList() {
        List<IotProductClassifyDO> list = iotProductClassifyService.getIotProductClassifyList(
                new IotProductClassifyListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
        return success(BeanUtils.toBean(list, IotProductClassifySimpleRespVO.class));
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品分类信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:product-classify:query')")
    public CommonResult<IotProductClassifyRespVO> getIotProductClassify(@RequestParam("id") Long id) {
        IotProductClassifyDO dept = iotProductClassifyService.getIotProductClassify(id);
        return success(BeanUtils.toBean(dept, IotProductClassifyRespVO.class));
    }

}
