package org.jeecg.modules.system.app.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.system.app.entity.ReceiptPaymentDetailsInfo;
import org.jeecg.modules.system.app.entity.ShopMoney;
import org.jeecg.modules.system.app.entity.ShopMoneyHis;
import org.jeecg.modules.system.app.mapper.ShopMoneyHisMapper;
import org.jeecg.modules.system.app.mapper.ShopMoneyMapper;
import org.jeecg.modules.system.app.service.IShopMoneyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: 店铺资金总数
 * @Author: jeecg-boot
 * @Date:   2025-08-18
 * @Version: V1.0
 */
@Service
public class ShopMoneyServiceImpl extends ServiceImpl<ShopMoneyMapper, ShopMoney> implements IShopMoneyService {

    @Autowired
    ShopMoneyMapper shopMoneyMapper;

    @Autowired
    ShopMoneyHisMapper shopMoneyHisMapper;


    @Override
    public ShopMoney findByOrgCode(String orgCode) {
        return shopMoneyMapper.findByOrgCode(orgCode);
    }

    @Override
    @Transactional
    public Result<String> add(ReceiptPaymentDetailsInfo receiptPaymentDetailsInfo) {
        ShopMoney shopMoney= shopMoneyMapper.findByOrgCode(receiptPaymentDetailsInfo.getOrgCode());
        if (shopMoney==null){
            if (receiptPaymentDetailsInfo.getPayType()==1){
                return Result.error("不存在待结算金额,无法进行支出操作");
            }
            if (receiptPaymentDetailsInfo.getPayType()==0&&receiptPaymentDetailsInfo.getPurseChangeReason()==2){
                return Result.error("不存在待结算金额,无法进行提现操作");
            }
            shopMoney =  new ShopMoney();
            BeanUtils.copyProperties(receiptPaymentDetailsInfo,shopMoney);
            shopMoney.setPendSettlementMoney(receiptPaymentDetailsInfo.getChangeMoney());
            shopMoney.setReceipPaymentDetailsId(receiptPaymentDetailsInfo.getId());
        }else {
            ShopMoneyHis shopMoneyHis = new ShopMoneyHis();
            BeanUtils.copyProperties(shopMoney,shopMoneyHis);
            shopMoneyHis.setCreateTime(new Date());
            shopMoneyHis.setUpdateTime(new Date());
            int insert = shopMoneyHisMapper.insert(shopMoneyHis);
            if (insert!=1){
                throw new JeecgBootException("报错店铺资金历史错误,保存失败");
//                return Result.error("报错店铺资金错误,保存失败");
            }

            if (receiptPaymentDetailsInfo.getPurseChangeReason()==2){//分账
                shopMoney.setCanBalance(shopMoney.getCanBalance().add(receiptPaymentDetailsInfo.getChangeMoney()));
                BigDecimal bigDecimal = getBigDecimal(shopMoney.getPendSettlementMoney(), receiptPaymentDetailsInfo.getChangeMoney());
                shopMoney.setPendSettlementMoney(bigDecimal);
                shopMoney.setUpdateTime(new Date());
            }else if (receiptPaymentDetailsInfo.getPurseChangeReason()==3){//提现
                shopMoney.setReceivedAmount( shopMoney.getReceivedAmount().add(receiptPaymentDetailsInfo.getChangeMoney()));
                BigDecimal bigDecimal = getBigDecimal(shopMoney.getCanBalance(), receiptPaymentDetailsInfo.getChangeMoney());
                shopMoney.setCanBalance(bigDecimal);
                shopMoney.setUpdateTime(new Date());
            }else if (receiptPaymentDetailsInfo.getPurseChangeReason()==1){//退款
                BigDecimal bigDecimal = getBigDecimal(shopMoney.getPendSettlementMoney(), receiptPaymentDetailsInfo.getChangeMoney());
                shopMoney.setPendSettlementMoney(bigDecimal);
                shopMoney.setUpdateTime(new Date());
            } else if (receiptPaymentDetailsInfo.getPurseChangeReason()==0) {//支付
                shopMoney.setPendSettlementMoney(shopMoney.getPendSettlementMoney().add(receiptPaymentDetailsInfo.getChangeMoney()));
                shopMoney.setUpdateTime(new Date());
            }

        }
        shopMoney.setReceipPaymentDetailsId(receiptPaymentDetailsInfo.getId());
        int insertShopMoney = shopMoneyMapper.insert(shopMoney);
        if (insertShopMoney!=1){
            throw new JeecgBootException("报错店铺资金错误,保存失败");
//                return Result.error("报错店铺资金错误,保存失败");
        }
        return Result.ok("成功");
    }

    @Override
    public IPage<ShopMoney> findPage(Page<ShopMoney> page, ShopMoney shopMoney) {
        return shopMoneyMapper.findPage(page,shopMoney);
    }

    private BigDecimal getBigDecimal(BigDecimal bigDecimal1 ,BigDecimal bigDecimal2 ){
        if (bigDecimal2.compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("扣款金额必须大于0");
        }

        if (bigDecimal1.compareTo(bigDecimal2) < 0) {
            throw new IllegalArgumentException("余额不足");
        }

        return bigDecimal1.subtract(bigDecimal2);
    }
}
