package cn.com.ctop.kuaishou.modules.report.service.impl;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.entity.RuleDataAccountKuaishou;
import cn.com.ctop.common.module.mapper.RuleDataAccountKuaishouMapper;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.service.IRuleDataAccountKuaishouService;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaiShouGetAccountFundService;
import cn.com.ctop.kuaishou.modules.report.service.IRuleKuaiShouAccountService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RuleKuaiShouAccountServiceImpl extends ServiceImpl<RuleDataAccountKuaishouMapper, RuleDataAccountKuaishou> implements IRuleKuaiShouAccountService {

    @Autowired
    IKuaiShouGetAccountFundService kuaiShouGetAccountFundService;
    @Autowired
    ICtopOauthTokenService oauthTokenService;
    @Autowired
    IRuleDataAccountKuaishouService ruleDataAccountKuaishouService;
    @Autowired
    IUserAllocationService userAllocationService;

    @Override
    public void cleanRuleDataAccount(Long accountId) {
        CtopOauthToken oauthToken=oauthTokenService.getTokenByAccountId(accountId);
        //查询账户实时数据
        JSONObject dataAccount=kuaiShouGetAccountFundService.getAccountReportBy(oauthToken,accountId);
        if(!dataAccount.isEmpty()){
            JSONObject fundData=kuaiShouGetAccountFundService.getAccountFundBy(oauthToken,accountId);
            RuleDataAccountKuaishou ruleDataAccount=new RuleDataAccountKuaishou();
            ruleDataAccount.setId(accountId);
            ruleDataAccount.setAccountName(userAllocationService.getByAccountId(accountId).getAuthName());
            ruleDataAccount.setAccountId(accountId);
            ruleDataAccount.setCost(dataAccount.getBigDecimal("charge"));
            ruleDataAccount.setValidBalance(fundData.getBigDecimal("balance"));

            ruleDataAccountKuaishouService.saveOrUpdate(ruleDataAccount);
        }
    }
}
