package com.cf.imes.module.system.service.funds.recharge;

import cn.hutool.core.util.IdUtil;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.Assert.AssertUtils;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.system.controller.admin.funds.recharge.vo.RechargePageReqVO;
import com.cf.imes.module.system.controller.admin.funds.recharge.vo.RechargeSaveReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.recharge.GiftMoneyDetailsDO;
import com.cf.imes.module.system.dal.mysql.funds.recharge.GiftMoneyDetailsMapper;
import com.cf.imes.module.system.framework.snowflake.config.MybatisIdProperties;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;

import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.RECHARGE_ACTIVE_NAME_IS_EXIST;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.RECHARGE_ACTIVE_NO_EXIST;

/**
 * 充值活动详情表 gift_money_details Service 实现类
 *
 * @author 晨丰科技
 */
@Service
@Validated
public class RechargeActiveServiceImpl implements RechargeActiveService{

    @Resource
    private MybatisIdProperties mybatisIdProperties;

    @Resource
    private GiftMoneyDetailsMapper giftMoneyDetailsMapper;

    @Override
    public Long create(RechargeSaveReqVO createReqVO) {

        long id = IdUtil.getSnowflake(mybatisIdProperties.getWorkerId(), mybatisIdProperties.getDatacenterId()).nextId();

        GiftMoneyDetailsDO giftMoneyDetailsDO = giftMoneyDetailsMapper.selectOne(new LambdaQueryWrapperX<GiftMoneyDetailsDO>()
                .eq(GiftMoneyDetailsDO::getDeleted, false)
                .eq(GiftMoneyDetailsDO::getActivityName,createReqVO.getActivityName()));

        AssertUtils.empty(giftMoneyDetailsDO,RECHARGE_ACTIVE_NAME_IS_EXIST);

        GiftMoneyDetailsDO giftMoneyDetailsDOn = BeanUtils.toBean(createReqVO, GiftMoneyDetailsDO.class).setId(id);
        giftMoneyDetailsMapper.insert(giftMoneyDetailsDOn);
        return id;
    }

    @Override
    public void update(RechargeSaveReqVO updateReqVO) {
        validateRechargeActiveExists(updateReqVO.getId());

        GiftMoneyDetailsDO giftMoneyDetailsDO = giftMoneyDetailsMapper.selectOne(new LambdaQueryWrapperX<GiftMoneyDetailsDO>()
                .eq(GiftMoneyDetailsDO::getDeleted, false)
                .eq(GiftMoneyDetailsDO::getActivityName,updateReqVO.getActivityName())
                .ne(GiftMoneyDetailsDO::getId,updateReqVO.getId()));

        AssertUtils.empty(giftMoneyDetailsDO,RECHARGE_ACTIVE_NAME_IS_EXIST);

        GiftMoneyDetailsDO giftMoneyDetailsDOn = BeanUtils.toBean(updateReqVO, GiftMoneyDetailsDO.class);
        giftMoneyDetailsMapper.updateById(giftMoneyDetailsDOn);

    }


    @Override
    public void delete(Long id) {
        validateRechargeActiveExists(id);
        giftMoneyDetailsMapper.deleteById(id);
    }


    @Override
    public GiftMoneyDetailsDO get(Long id) {
        return validateRechargeActiveExists(id);
    }

    @Override
    public GiftMoneyDetailsDO getDefaultNull(Long id) {
        return giftMoneyDetailsMapper.selectById(id);
    }

    @Override
    public PageResult<GiftMoneyDetailsDO> getPage(RechargePageReqVO pageReqVO) {
        return giftMoneyDetailsMapper.selectPage(pageReqVO);
    }



    // 校验是否存在
    private GiftMoneyDetailsDO validateRechargeActiveExists(Long id) {
        if (id == null) {
            throw exception(RECHARGE_ACTIVE_NO_EXIST);
        }
        GiftMoneyDetailsDO giftMoneyDetailsDO = giftMoneyDetailsMapper.selectOne(new LambdaQueryWrapperX<GiftMoneyDetailsDO>()
                .eq(GiftMoneyDetailsDO::getId, id)
                .eq(GiftMoneyDetailsDO::getDeleted, false));
        if (giftMoneyDetailsDO == null) {
            throw exception(RECHARGE_ACTIVE_NO_EXIST);
        }

        return giftMoneyDetailsDO;
    }


}
