package cn.iocoder.yudao.module.identity.controller.admin.businessrule;

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.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

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

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

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.identity.controller.admin.businessrule.vo.*;
import cn.iocoder.yudao.module.identity.dal.dataobject.businessrule.BusinessRuleDO;
import cn.iocoder.yudao.module.identity.convert.businessrule.BusinessRuleConvert;
import cn.iocoder.yudao.module.identity.service.businessrule.BusinessRuleService;

@Api(tags = "管理后台 - 码规则")
@RestController
@RequestMapping("/identity/business-rule")
@Validated
public class BusinessRuleController {

    @Resource
    private BusinessRuleService businessRuleService;

    @PostMapping("/create")
    @ApiOperation("创建码规则")
    @PreAuthorize("@ss.hasPermission('identity:business-rule:create')")
    public CommonResult<Long> createBusinessRule(@Valid @RequestBody BusinessRuleCreateReqVO createReqVO) {
        return success(businessRuleService.createBusinessRule(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新码规则")
    @PreAuthorize("@ss.hasPermission('identity:business-rule:update')")
    public CommonResult<Boolean> updateBusinessRule(@Valid @RequestBody BusinessRuleUpdateReqVO updateReqVO) {
        businessRuleService.updateBusinessRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除码规则")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:business-rule:delete')")
    public CommonResult<Boolean> deleteBusinessRule(@RequestParam("id") Long id) {
        businessRuleService.deleteBusinessRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得码规则")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:business-rule:query')")
    public CommonResult<BusinessRuleRespVO> getBusinessRule(@RequestParam("id") Long id) {
        BusinessRuleDO businessRule = businessRuleService.getBusinessRule(id);
        return success(BusinessRuleConvert.INSTANCE.convert(businessRule));
    }

    @GetMapping("/list")
    @ApiOperation("获得码规则列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('identity:business-rule:query')")
    public CommonResult<List<BusinessRuleRespVO>> getBusinessRuleList(@RequestParam("ids") Collection<Long> ids) {
        List<BusinessRuleDO> list = businessRuleService.getBusinessRuleList(ids);
        return success(BusinessRuleConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/listByBusinessId")
    @ApiOperation("根据所属商户编码,获得码规则列表")
    @ApiImplicitParam(name = "businessId", value = "所属商户编码", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('identity:business-rule:query')")
    public CommonResult<List<BusinessRuleRespVO>> getBusinessRuleListByBusinessId(String businessId) {
        List<BusinessRuleDO> list = businessRuleService.getBusinessRuleListByBusinessId(businessId);
        return success(BusinessRuleConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得码规则分页")
    @PreAuthorize("@ss.hasPermission('identity:business-rule:query')")
    public CommonResult<PageResult<BusinessRuleRespVO>> getBusinessRulePage(@Valid BusinessRulePageReqVO pageVO) {
        PageResult<BusinessRuleDO> pageResult = businessRuleService.getBusinessRulePage(pageVO);
        return success(BusinessRuleConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getBusinessRuleByBusinessName")
    @ApiOperation("根据商户昵称获取马规则信息")
    @ApiImplicitParam(name = "businessName", value = "商户编码", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('identity:business-rule:query')")
    public CommonResult<BusinessRuleDO> getBusinessRuleByBusinessName(@RequestParam("businessName")  String businessName) {
        BusinessRuleDO businessRuleByBusinessName = businessRuleService.getBusinessRuleByBusinessName(businessName);
        return success(businessRuleByBusinessName);
    }

}
