package com.ruoyi.frequency.bondamounrecord.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.exception.enums.CoreExceptionEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.frequency.bondamounrecord.entity.BondAmountRecord;
import com.ruoyi.frequency.bondamounrecord.mapper.BondAmountRecordMapper;
import com.ruoyi.frequency.bondamounrecord.service.BondAmountRecordService;
import com.ruoyi.frequency.store.mapper.StoreMapper;
import com.ruoyi.utils.BusinessUtil;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 缴纳保证金Service业务层处理
 *
 * @author liwenlong
 * @date 2023-10-31
 */
@Service
public class BondAmountRecordServiceImpl extends ServiceImpl<BondAmountRecordMapper, BondAmountRecord>  implements BondAmountRecordService
{
    @Autowired
    private BondAmountRecordMapper bondAmountRecordMapper;

    /**
     * 查询缴纳保证金
     *
     * @param id 缴纳保证金主键
     * @return 缴纳保证金
     */
    @Override
    public BondAmountRecord selectBondAmountRecordById(Long id)
    {
        return bondAmountRecordMapper.selectBondAmountRecordById(id);
    }

    /**
     * 查询缴纳保证金列表
     *
     * @param bondAmountRecord 缴纳保证金
     * @return 缴纳保证金
     */
    @Override
    public List<BondAmountRecord> selectBondAmountRecordList(BondAmountRecord bondAmountRecord)
    {
        return bondAmountRecordMapper.selectBondAmountRecordList(bondAmountRecord);
    }

    /**
     * 新增缴纳保证金
     *
     * @param bondAmountRecord 缴纳保证金
     * @return 结果
     */
    @Override
    public int insertBondAmountRecord(BondAmountRecord bondAmountRecord)
    {
        return bondAmountRecordMapper.insert(bondAmountRecord);
    }

    /**
     * 修改缴纳保证金
     *
     * @param bondAmountRecord 缴纳保证金
     * @return 结果
     */
    @Override
    public int updateBondAmountRecord(BondAmountRecord bondAmountRecord)
    {
        bondAmountRecord.setUpdateTime(DateUtils.getNowDate());
        return bondAmountRecordMapper.updateById(bondAmountRecord);
    }

    /**
     * 批量删除缴纳保证金
     *
     * @param ids 需要删除的缴纳保证金主键
     * @return 结果
     */
    @Override
    public int deleteBondAmountRecordByIds(Long[] ids,Integer delFalg)
    {
        this.lambdaUpdate().set(BondAmountRecord::getDelFlag,delFalg).in(BondAmountRecord::getId,ids).update();
        return ids.length;
    }

    @Autowired
    private StoreMapper storeMapper;

    @Override
    public void updateBond(UserVo userVo,Long objectId, Integer type, Long withDrawlRecordId, BigDecimal updatePrice, Integer status) {
        if (userVo.getUserType() != 2){
            return;
        }

        if (updatePrice.compareTo(BigDecimal.ZERO) == 0 || ObjectUtil.equal(PublicCommon.Wallet.到账取消, status)) {
            throw new CustomException(CoreExceptionEnum.ERROR_PARAM);
        }

        if (updatePrice.compareTo(BigDecimal.ZERO) == 1) {
            storeMapper.addBondAmount(userVo.getUserId(), updatePrice);
        }else {
            boolean subBalance = storeMapper.subBondAmount(userVo.getUserId(), updatePrice.negate());
            if (!subBalance) {
                throw new CustomException(CoreExceptionEnum.BALANCE_INSUFFICIENT);
            }
        }


        //添加保证金金额记录
        addBondWalletRecord(userVo, objectId, updatePrice, type, status);
    }

    @Autowired
    private BusinessUtil businessUtil;

    /**
     * 增加余额记录
     *
     * @param userVo            用户
     * @param price             金额
     */
    public void addBondWalletRecord(UserVo userVo,Long objectId, BigDecimal price, Integer type, Integer status) {
        if (price.setScale(2, BigDecimal.ROUND_HALF_UP).compareTo(new BigDecimal(0)) != 0) {
            BondAmountRecord bondAmountRecord = new BondAmountRecord();
            bondAmountRecord.setUserId(userVo.getUserId());
            bondAmountRecord.setUserType(userVo.getUserType());
            bondAmountRecord.setType(type);
            bondAmountRecord.setObjectId(objectId);
            bondAmountRecord.setAmount(price);

            //获取用户当前余额
            BigDecimal userBalance = new BigDecimal(businessUtil.getUserFieldValue(userVo, "bondAmount").toString());
            bondAmountRecord.setCurrentAmount(userBalance);
            this.save(bondAmountRecord);
        }
    }
}
