package cn.iocoder.yudao.module.identity.api.businessrule;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.identity.api.businessrule.dto.BusinessRuleRespDTO;
import cn.iocoder.yudao.module.identity.controller.admin.businessrule.vo.BusinessRuleCreateReqVO;
import cn.iocoder.yudao.module.identity.dal.dataobject.businessrule.BusinessRuleDO;
import cn.iocoder.yudao.module.identity.service.businessrule.BusinessRuleService;
import cn.iocoder.yudao.module.system.enums.ApiConstants;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

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


@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = ApiConstants.VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
public class businessRuleApiImpl implements BusinessRuleApi {

    @Resource
    private BusinessRuleService businessRuleService;

    @Override
    @Transactional
    public CommonResult<Long> createBusinessRule(@Valid BusinessRuleRespDTO businessRule) {
        // BeanUtils.copyProperties(businessRule);
        BusinessRuleCreateReqVO businessRuleCreateReqVO = new BusinessRuleCreateReqVO();
        BeanUtil.copyProperties(businessRule, businessRuleCreateReqVO);
        return CommonResult.success(businessRuleService.createBusinessRule(businessRuleCreateReqVO));
    }

    @Override
    public CommonResult<Boolean> delBusinessRule(Long codeRuleId) {
        businessRuleService.deleteBusinessRule(codeRuleId);
        return success(true);
    }
}
