package cn.iocoder.yudao.module.product.controller.admin.envtype;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.admin.envtype.vo.*;
import cn.iocoder.yudao.module.product.convert.envtype.EnvTypeConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.envtype.EnvTypeDO;
import cn.iocoder.yudao.module.product.service.envtype.EnvTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
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;

@Api(tags = "管理后台 - 环境类型")
@RestController
@RequestMapping("/product/env-type")
@Validated
public class EnvTypeController {

    @Resource
    private EnvTypeService envTypeService;

    @PostMapping("/create")
    @ApiOperation("创建环境类型")
    @PreAuthorize("@ss.hasPermission('product:env-type:create')")
    public CommonResult<Long> createEnvType(@Valid @RequestBody EnvTypeCreateReqVO createReqVO) {
        return success(envTypeService.createEnvType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新环境类型")
    @PreAuthorize("@ss.hasPermission('product:env-type:update')")
    public CommonResult<Boolean> updateEnvType(@Valid @RequestBody EnvTypeUpdateReqVO updateReqVO) {
        envTypeService.updateEnvType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除环境类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:env-type:delete')")
    public CommonResult<Boolean> deleteEnvType(@RequestParam("id") Long id) {
        envTypeService.deleteEnvType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得环境类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:env-type:query')")
    public CommonResult<EnvTypeRespVO> getEnvType(@RequestParam("id") Long id) {
        EnvTypeDO envType = envTypeService.getEnvType(id);
        return success(EnvTypeConvert.INSTANCE.convert(envType));
    }

    @GetMapping("/page")
    @ApiOperation("获得环境类型分页")
    @PreAuthorize("@ss.hasPermission('product:env-type:query')")
    public CommonResult<PageResult<EnvTypeRespVO>> getEnvTypePage(@Valid EnvTypePageReqVO pageVO) {
        PageResult<EnvTypeDO> pageResult = envTypeService.getEnvTypePage(pageVO);
        return success(EnvTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/all")
    @ApiOperation("获得所有环境类型")
    @PreAuthorize("@ss.hasPermission('product:env-type:query')")
    public CommonResult<List<EnvTypeRespVO>> getAllEnvType() {
        List<EnvTypeDO> envTypeDOS = envTypeService.getAllEnvType();
        return success(EnvTypeConvert.INSTANCE.convertList(envTypeDOS));
    }

    @GetMapping("/getAvailableEnvType")
    @ApiOperation("根据商户ID获得可用环境类型")
    @ApiImplicitParam(name = "businessInfoId", value = "商户ID", required = true, example = "1", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:env-type:query')")
    public CommonResult<List<EnvTypeRespVO>> getAvailableEnvTypeByBusinessId(@RequestParam("businessInfoId") Long businessInfoId) {
        List<EnvTypeDO> envTypeDOS = envTypeService.getAvailableEnvTypeByBusinessId(businessInfoId);
        return success(EnvTypeConvert.INSTANCE.convertList(envTypeDOS));
    }

    @GetMapping("/getEnvTypeConfigGroupByLandId")
    @ApiOperation("获得环境类型配置数据（根据地块ID分组）")
    @ApiImplicitParam(name = "businessInfoId", value = "商户ID", required = true, example = "1", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:env-type:query')")
    public CommonResult<List<EnvTypeDeviceConfigRespVO>> getEnvTypeConfigGroupByLandId(@RequestParam("businessInfoId") Long businessInfoId) {
        return success(envTypeService.getEnvTypeConfigGroupByLandId(businessInfoId));
    }

}
