| package com.xzx.gc.user.service;  | 
|   | 
| import cn.hutool.core.convert.Convert;  | 
| import cn.hutool.core.date.DateUtil;  | 
| import cn.hutool.core.util.NumberUtil;  | 
| import com.xzx.gc.common.constant.Constants;  | 
| import com.xzx.gc.common.exception.RestException;  | 
| import com.xzx.gc.common.utils.BusinessUtil;  | 
| import com.xzx.gc.entity.CityPartner;  | 
| import com.xzx.gc.entity.PartnerAccount;  | 
| import com.xzx.gc.entity.PartnerAccountLog;  | 
| import com.xzx.gc.model.admin.AccountLogModel;  | 
| import com.xzx.gc.model.admin.PartnerAccountLogModel;  | 
| import com.xzx.gc.model.admin.PartnerAccountModel;  | 
| import com.xzx.gc.user.mapper.CityPartnerMapper;  | 
| import com.xzx.gc.user.mapper.PartnerAccountMapper;  | 
| import com.xzx.gc.util.DoubleUtil;  | 
| import lombok.extern.slf4j.Slf4j;  | 
| import org.springframework.beans.factory.annotation.Autowired;  | 
| import org.springframework.stereotype.Service;  | 
| import org.springframework.transaction.annotation.Transactional;  | 
|   | 
| import java.math.BigDecimal;  | 
|   | 
| @Service  | 
| @Transactional  | 
| @Slf4j  | 
| public class PartnerAccountService {  | 
|   | 
|     @Autowired  | 
|     private PartnerAccountMapper partnerAccountMapper;  | 
|   | 
|     @Autowired  | 
|     private CityPartnerMapper cityPartnerMapper;  | 
|   | 
|   | 
|     public PartnerAccount findByPartnerId(String partnerId){  | 
|         PartnerAccount partnerAccount=new PartnerAccount();  | 
|         partnerAccount.setPartnerId(partnerId);  | 
|         partnerAccount.setDelFlag(Constants.DEL_NOT_FLAG);  | 
|         PartnerAccount partnerAccount1 = partnerAccountMapper.selectOne(partnerAccount);  | 
|         return partnerAccount1;  | 
|     }  | 
|   | 
|   | 
|   | 
|     public PartnerAccount findByAccount(String accountId){  | 
|         PartnerAccount partnerAccount=new PartnerAccount();  | 
|         partnerAccount.setAccount(accountId);  | 
|         partnerAccount.setDelFlag(0);  | 
|         return partnerAccountMapper.selectOne(partnerAccount);  | 
|     }  | 
|   | 
|     public PartnerAccount findByUserId(String userId){  | 
|         PartnerAccount partnerAccount=new PartnerAccount();  | 
|         partnerAccount.setUserId(userId);  | 
|         partnerAccount.setDelFlag(0);  | 
|         return partnerAccountMapper.selectOne(partnerAccount);  | 
|     }  | 
|   | 
|   | 
|     public void update(PartnerAccount partnerAccount){  | 
|             partnerAccountMapper.updateByPrimaryKeySelective(partnerAccount);  | 
|     }  | 
|   | 
|   | 
|     public void updateAddMoneyByPartnerId(String partnerId,BigDecimal money){  | 
|         partnerAccountMapper.updateAddMoneyByPartnerId(partnerId,money);  | 
|     }  | 
|   | 
|   | 
|     public void insertPartnerAccountLog(PartnerAccountModel accountModel, String type, BigDecimal money, String payOrderId, String packageType) {  | 
|         //写入日志  | 
|         AccountLogModel log = new AccountLogModel();  | 
|         boolean flag = true;  | 
|         if(null!=accountModel.getAddType()&&!"".equals(accountModel.getAddType())){  | 
|             log.setNewMoney(DoubleUtil.roundTwo(new BigDecimal(accountModel.getHbb()).add(money).toString()));  | 
|   | 
|         }else{  | 
|             flag = false;  | 
|             log.setNewMoney(DoubleUtil.roundTwo(new BigDecimal(accountModel.getHbb()).subtract(money).toString()));  | 
|   | 
|         }  | 
|         CityPartner partner = cityPartnerMapper.queryEntityByUserIds(accountModel.getUserId());  | 
|         PartnerAccountLogModel partnerLog = new PartnerAccountLogModel();  | 
|         partnerLog.setFlowNo(payOrderId);  | 
|         partnerLog.setCreateTime(DateUtil.now());  | 
|         partnerLog.setApproverStatus("1");  | 
|         if(type.equals("14")){  | 
|             if(!flag){  | 
|                 partnerLog.setMoney(DoubleUtil.roundTwo(money.multiply(new BigDecimal("-1")).toString()));  | 
|   | 
|             }else{  | 
|                 partnerLog.setMoney(DoubleUtil.roundTwo(money.toString()));  | 
|   | 
|             }  | 
|   | 
|         }else{  | 
|             if(!flag){  | 
|                 partnerLog.setMoney(DoubleUtil.roundTwo(money.multiply(new BigDecimal("-1")).toString()));  | 
|   | 
|             }else{  | 
|                 partnerLog.setMoney(DoubleUtil.roundTwo(money.toString()));  | 
|   | 
|             }  | 
|         }  | 
|         partnerLog.setHbb(log.getNewMoney());  | 
|         partnerLog.setDelFlag(0);  | 
|         partnerLog.setAccountId(accountModel.getAccount());  | 
|         partnerLog.setAccountMoney(DoubleUtil.roundTwo(accountModel.getHbb() + ""));  | 
|         partnerLog.setUserName(partner.getPartnerName());  | 
|         if (null != packageType) {  | 
|             partnerLog.setType(packageType);  | 
|   | 
|         } else {  | 
|             if(type.equals("9")){  | 
|                 partnerLog.setType("12");  | 
|             }else if(type.equals("8")){  | 
|                 partnerLog.setType("7");  | 
|             }else{  | 
|                 partnerLog.setType("1");  | 
|             }  | 
|         }  | 
|         partnerLog.setUserPhone(partner.getMobilePhone());  | 
|         partnerLog.setRoleName("城市合伙人");  | 
|         partnerAccountMapper.addPartnerAccountLog(partnerLog);  | 
|     }  | 
|   | 
|     /**  | 
|      * 根据money变化自动判断增长 及优先额度再是环保币的规则  | 
|      */  | 
|     public PartnerAccountLog updateAll(String accountId, BigDecimal money){  | 
|         PartnerAccountLog partnerAccountLog=new PartnerAccountLog();  | 
|         PartnerAccount byAccount = findByAccount(accountId);  | 
|         String hbb = byAccount.getHbb();  | 
|         String fixed=byAccount.getFixedLimit();  | 
|         String over=byAccount.getOverdraftLimit();  | 
|         if(money.compareTo(BigDecimal.ZERO)>0){  | 
|             money=money.abs();  | 
|             //增长  | 
|             BigDecimal limit = Convert.toBigDecimal(fixed);  | 
|             BigDecimal v = NumberUtil.add(money, Convert.toBigDecimal(over));  | 
|             //查询账户信息  | 
|             if (v.compareTo(limit) > 0) {  | 
|                 //将额度填充完   再补money  | 
|                 BigDecimal actualMoney = NumberUtil.sub(v, limit);  | 
|                 updateAddMoneyResetFixLimitByAccountId(accountId,actualMoney,limit);  | 
|                 partnerAccountLog.setAccountMoney(hbb);  | 
|                 partnerAccountLog.setMoney(actualMoney.toString());  | 
|                 partnerAccountLog.setOldLimit(over);  | 
|                 partnerAccountLog.setManageLimit(NumberUtil.sub(limit,Convert.toBigDecimal(over)).toString());  | 
|             } else {  | 
|                 //直接补额度  | 
|                 updateAddMoneyLimitByAccountId(accountId,null,money);  | 
|                 partnerAccountLog.setOldLimit(over);  | 
|                 partnerAccountLog.setManageLimit(money.toString());  | 
|             }  | 
|   | 
|         } else if(money.compareTo(BigDecimal.ZERO)<0){  | 
|             money=money.abs();  | 
|             if(NumberUtil.add(Convert.toBigDecimal(hbb,Constants.MONEY_INIT),Convert.toBigDecimal(over,Constants.MONEY_INIT)).compareTo(money)<0){  | 
|                 throw new RestException(-1,"打包站账户余额不足");  | 
|             }  | 
|             //判断要操作金额是否是在可用额度 和 环保币的和之内  | 
|             if (NumberUtil.isLessOrEqual(money, Convert.toBigDecimal(over))) {//支付金额小于等于支付额度  优先扣除支付额度  | 
|                 partnerAccountMapper.updateReduceMoneyLimitByAccountId(accountId,null,money);  | 
|                 partnerAccountLog.setOldLimit(over);  | 
|                 partnerAccountLog.setManageLimit("-"+money.toString());  | 
|             } else if (NumberUtil.isGreater(money, Convert.toBigDecimal(over))) {   //支付金额大于支付额度 也是优先扣除额度变为0  剩下的用余额扣除  | 
|                 BigDecimal actualMoney = NumberUtil.sub(money, Convert.toBigDecimal(over));  | 
|                 partnerAccountMapper.updateReduceMoneyResetLimitByAccountId(accountId,Convert.toBigDecimal(actualMoney));  | 
|                 partnerAccountLog.setAccountMoney(hbb);  | 
|                 partnerAccountLog.setMoney("-"+actualMoney);  | 
|                 partnerAccountLog.setOldLimit(over);  | 
|                 partnerAccountLog.setManageLimit("-"+over);  | 
|             }  | 
|         }  | 
|         return partnerAccountLog;  | 
|   | 
|     }  | 
|   | 
|     public void updateAddMoneyLimitByAccountId(String accountId,BigDecimal money,BigDecimal over){  | 
|         partnerAccountMapper.updateAddMoneyLimitByAccountId(accountId,money,over);  | 
|     }  | 
|   | 
|     public void updateAddMoneyResetFixLimitByAccountId(String accountId,BigDecimal money,BigDecimal over){  | 
|         partnerAccountMapper.updateAddMoneyResetFixLimitByAccountId(accountId,money,over);  | 
|     }  | 
| }  |