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

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
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.IotProductClassifySimpleRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yfclass.vo.IotYfCasCaderVo;
import cn.iocoder.yudao.module.pms.controller.admin.yfclass.vo.IotYfClassifyPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yfclass.vo.IotYfClassifyRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yfclass.vo.IotYfClassifySaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotProductClassifyDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yfclass.IotYfClassifyDO;
import cn.iocoder.yudao.module.pms.service.yfclass.IotYfClassifyService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 油服设备分类")
@RestController
@RequestMapping("/rq/iot-yf-classify")
@Validated
public class IotYfClassifyController {

    @Resource
    private IotYfClassifyService iotYfClassifyService;

    @PostMapping("/create")
    @Operation(summary = "创建油服设备分类")
    @PreAuthorize("@ss.hasPermission('rq:iot-yf-classify:create')")
    public CommonResult<Long> createIotYfClassify(@Valid @RequestBody IotYfClassifySaveReqVO createReqVO) {
        return success(iotYfClassifyService.createIotYfClassify(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新油服设备分类")
    @PreAuthorize("@ss.hasPermission('rq:iot-yf-classify:update')")
    public CommonResult<Boolean> updateIotYfClassify(@Valid @RequestBody IotYfClassifySaveReqVO updateReqVO) {
        iotYfClassifyService.updateIotYfClassify(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除油服设备分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-yf-classify:delete')")
    public CommonResult<Boolean> deleteIotYfClassify(@RequestParam("id") Long id) {
        iotYfClassifyService.deleteIotYfClassify(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得油服设备分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-yf-classify:query')")
    public CommonResult<IotYfClassifyRespVO> getIotYfClassify(@RequestParam("id") Long id) {
        IotYfClassifyDO iotYfClassify = iotYfClassifyService.getIotYfClassify(id);
        return success(BeanUtils.toBean(iotYfClassify, IotYfClassifyRespVO.class));
    }


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

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

    @GetMapping(value = {"/children-list"})
    @Operation(summary = "获取树形结构", description = "获取树形结构")
    public CommonResult<List<IotYfCasCaderVo>> getChildrenList() {
        List<IotYfCasCaderVo> casCaderList = iotYfClassifyService.getCasCaderList();
        return success(casCaderList);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出油服设备分类 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-yf-classify:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotYfClassifyExcel(@Valid IotYfClassifyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotYfClassifyDO> list = iotYfClassifyService.getIotYfClassifyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "油服设备分类.xls", "数据", IotYfClassifyRespVO.class,
                        BeanUtils.toBean(list, IotYfClassifyRespVO.class));
    }

    @GetMapping("/init")
    @PermitAll
    public void initClassify() {
        iotYfClassifyService.initClassify();
    }
}
