package com.hfkj.service.agent.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.BsAgentApiAccountMapper;
import com.hfkj.entity.BsAgentApiAccount;
import com.hfkj.entity.BsAgentApiAccountExample;
import com.hfkj.service.agent.BsAgentApiAccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @className: BsAgentApiAccountServiceImpl
 * @author: HuRui
 * @date: 2024/8/28
 **/
@Service("agentApiAccountService")
public class BsAgentApiAccountServiceImpl implements BsAgentApiAccountService {
    @Resource
    private BsAgentApiAccountMapper agentApiAccountMapper;

    @Override
    public void create(Long agentId) {
        BsAgentApiAccount data = new BsAgentApiAccount();
        data.setAgentId(agentId);
        data.setAmount(new BigDecimal("0"));
        data.setAmountConsume(new BigDecimal("0"));
        data.setStatus(1);
        data.setCreateTime(new Date());
        data.setUpdateTime(new Date());
        agentApiAccountMapper.insert(data);
    }

    @Override
    public void update(BsAgentApiAccount data) {
        data.setUpdateTime(new Date());
        agentApiAccountMapper.updateByPrimaryKey(data);
    }

    @Override
    public BsAgentApiAccount getAccount(Long agentId) {
        BsAgentApiAccountExample example = new BsAgentApiAccountExample();
        example.createCriteria().andAgentIdEqualTo(agentId).andStatusNotEqualTo(0);
        List<BsAgentApiAccount> list = agentApiAccountMapper.selectByExample(example);
        if (list.isEmpty()) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到账户");
        }
        return list.get(0);

    }
}
