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

import cn.iocoder.yudao.framework.common.enums.CodeEnum;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.basic.api.businessinfo.BusinessInfoApi;
import cn.iocoder.yudao.module.basic.api.redis.dto.BusinessInfoDTO;
import cn.iocoder.yudao.module.identity.api.businessrule.BusinessRuleApi;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.identity.controller.admin.businessrule.vo.*;
import cn.iocoder.yudao.module.identity.dal.dataobject.businessrule.BusinessRuleDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.identity.convert.businessrule.BusinessRuleConvert;
import cn.iocoder.yudao.module.identity.dal.mysql.businessrule.BusinessRuleMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.basic.enums.ErrorCodeConstants.BUSINESS_INFO_NOT_EXISTS;
import static cn.iocoder.yudao.module.identity.enums.ErrorCodeConstants.*;

/**
 * 码规则 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BusinessRuleServiceImpl implements BusinessRuleService {

    @Resource
    private BusinessRuleMapper businessRuleMapper;

    @Resource
    private BusinessInfoApi businessInfoApi;


    /**
     * 创建码规则
     *
     * @param createReqVO 创建信息
     * @return 编号
     */
    @Override
    public Long createBusinessRule(BusinessRuleCreateReqVO createReqVO) {
        // 设置默认值
        String codeRuleName = "默认码规则"; //码规则名称
        //码规则
        String codeRuleFormat = "^https://trace.minipro.yuanhaotest.com/1304120058656711/(.*)";
        //码类型  0：普通类型
        Integer CodeType = 0;
        // 插入 将请求VO转换成DO
        BusinessRuleDO businessRule = new BusinessRuleDO();
        try {
            businessRule = BusinessRuleConvert.INSTANCE.convert(createReqVO);

            // 如果请求中的码规则名称为空，则使用默认值
            businessRule.setCodeRuleName(StringUtils.defaultIfBlank(businessRule.getCodeRuleName(), codeRuleName));
            // 如果请求中的码规则为空，则使用默认值
            businessRule.setCodeRuleFormat(StringUtils.defaultIfBlank(businessRule.getCodeRuleFormat(), codeRuleFormat));
            // 如果请求中的码类型为空，则使用默认值
            businessRule.setCodeType(businessRule.getCodeType() == 0 ? businessRule.getCodeType() : CodeType);
            businessRule.setCodeRuleId(CodeEnum.BUSINESS_RULE_CODE.getCode());

            // 插入到数据库中
            businessRuleMapper.insert(businessRule);
        }catch (Exception e){
            throw exception(BUSINESS_RULE_CREAT_FAIL);
        }
        // 返回ID
        return businessRule.getId();

    }

    /**
     * 更新码规则
     *
     * @param updateReqVO 更新信息
     */
    @Override
    public void updateBusinessRule(BusinessRuleUpdateReqVO updateReqVO) {
        // 校验存在
        validateBusinessRuleExists(updateReqVO.getId());
        // 更新
        BusinessRuleDO updateObj = BusinessRuleConvert.INSTANCE.convert(updateReqVO);
        businessRuleMapper.updateById(updateObj);
    }

    /**
     * 删除码规则
     *
     * @param id 编号
     */
    @Override
    public void deleteBusinessRule(Long id) {
        // 校验存在
        validateBusinessRuleExists(id);
        // 删除
        businessRuleMapper.deleteById(id);
    }

    private void validateBusinessRuleExists(Long id) {
        if (businessRuleMapper.selectById(id) == null) {
            throw exception(BUSINESS_RULE_NOT_EXISTS);
        }
    }

    /**
     * 获得码规则
     *
     * @param id 编号
     * @return 码规则
     */
    @Override
    public BusinessRuleDO getBusinessRule(Long id) {
        return businessRuleMapper.selectById(id);
    }

    /**
     * 获得码规则列表
     *
     * @param ids 编号
     * @return 码规则列表
     */
    @Override
    public List<BusinessRuleDO> getBusinessRuleList(Collection<Long> ids) {
        return businessRuleMapper.selectBatchIds(ids);
    }

    /**
     * 获得码规则列表
     *
     * @param businessId 商户编编码
     * @return 码规则列表
     */
    @Override
    public List<BusinessRuleDO> getBusinessRuleListByBusinessId(String businessId) {
        return businessRuleMapper.selectList(BusinessRuleDO::getBusinessId,businessId);
    }

    /**
     * 获得码规则分页
     *
     * @param pageReqVO 分页查询
     * @return 码规则分页
     */
    @Override
    public PageResult<BusinessRuleDO> getBusinessRulePage(BusinessRulePageReqVO pageReqVO) {
        return businessRuleMapper.selectPage(pageReqVO);
    }

    /**
     * 根据商户ID获取马规则信息
     *
     * @param businessName
     * @return
     */
    @Override
    public BusinessRuleDO getBusinessRuleByBusinessName(String businessName) {
        QueryWrapper<BusinessRuleDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_name", businessName);
        return businessRuleMapper.selectOne(queryWrapper);
    }

}
