package com.heyu.api.data.service.impl.api;
/**
 * <p>
 * 服务类
 * </p>
 *
 * @author quyixiao
 * @since 2025-04-16
 */

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heyu.api.data.dao.api.ApiRechargeMoneyDao;
import com.heyu.api.data.entity.api.ApiRechargeMoneyEntity;
import com.heyu.api.data.service.api.ApiRechargeMoneyService;
import com.heyu.api.data.utils.DingDingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApiRechargeMoneyServiceImpl extends ServiceImpl<ApiRechargeMoneyDao, ApiRechargeMoneyEntity> implements ApiRechargeMoneyService {


    @Autowired
    private ApiRechargeMoneyDao apiRechargeMoneyDao;


    @Override
    public ApiRechargeMoneyEntity selectApiRechargeMoneyById(Long id) {
        return apiRechargeMoneyDao.selectApiRechargeMoneyById(id);
    }


    @Override
    public Long insertApiRechargeMoney(ApiRechargeMoneyEntity apiRechargeMoney) {
        return apiRechargeMoneyDao.insertApiRechargeMoney(apiRechargeMoney);
    }


    @Override
    public Long insertOrUpdateApiRechargeMoney(ApiRechargeMoneyEntity apiRechargeMoney) {
        return apiRechargeMoneyDao.insertOrUpdateApiRechargeMoney(apiRechargeMoney);
    }


    @Override
    public int updateApiRechargeMoneyById(ApiRechargeMoneyEntity apiRechargeMoney) {
        return apiRechargeMoneyDao.updateApiRechargeMoneyById(apiRechargeMoney);
    }


    @Override
    public int updateCoverApiRechargeMoneyById(ApiRechargeMoneyEntity apiRechargeMoney) {
        return apiRechargeMoneyDao.updateCoverApiRechargeMoneyById(apiRechargeMoney);
    }

    @Override
    public int deleteApiRechargeMoneyById(Long id) {
        return apiRechargeMoneyDao.deleteApiRechargeMoneyById(id);
    }

    @Override
    public void addUseCountAndSendWarnMsg(String scene, int count) {
        ApiRechargeMoneyEntity apiRechargeMoney = apiRechargeMoneyDao.selectApiRechargeMoneyByScene(scene);
        if (apiRechargeMoney !=null && apiRechargeMoney.getCurrentCount() <= apiRechargeMoney.getWarnCount()) {
            StringBuilder sb = new StringBuilder();
            sb.append(apiRechargeMoney.getSceneName()).append("\n")
                    .append("当前预警域值：").append(apiRechargeMoney.getWarnCount()).append("\n")
                    .append("当前剩余次数：").append(apiRechargeMoney.getCurrentCount()).append("\n")
                    .append("api 对应的url:").append(apiRechargeMoney.getApiUrl()).append("\n")
                    .append("充值页面url：").append(apiRechargeMoney.getRechargePageUrl()).append("\n")
            ;

            DingDingUtils.sendText(sb.toString());

        }
        apiRechargeMoneyDao.updateApiRechargeMoneyByScene(count, scene);
    }


}
