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


import com.alibaba.fastjson.JSONObject;
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.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.system.app.dto.receiptPaymentDetails.ReceiptPaymentDetailsInfoVo;
import org.jeecg.modules.system.app.entity.ReceiptPaymentDetailsInfo;
import org.jeecg.modules.system.app.mapper.ReceiptPaymentDetailsInfoMapper;
import org.jeecg.modules.system.app.service.IReceiptPaymentDetailsInfoService;
import org.jeecg.modules.system.app.service.IShopMoneyService;
import org.jeecg.modules.system.app.service.IStatisticsInfoService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: 店铺资金明细
 * @Author: jeecg-boot
 * @Date:   2025-08-18
 * @Version: V1.0
 */
@Service
public class ReceiptPaymentDetailsInfoServiceImpl extends ServiceImpl<ReceiptPaymentDetailsInfoMapper, ReceiptPaymentDetailsInfo> implements IReceiptPaymentDetailsInfoService {

    @Autowired
    ReceiptPaymentDetailsInfoMapper receiptPaymentDetailsInfoMapper;
    SysDepartMapper sysDepartMapper;

    @Autowired
    IShopMoneyService iShopMoneyService;

    @Autowired
    IStatisticsInfoService iStatisticsInfoService;

    @Override
    public Result<String> add(@Valid ReceiptPaymentDetailsInfoVo receiptPaymentDetailsInfoVo) {
        ReceiptPaymentDetailsInfo receiptPaymentDetailsInfo = new ReceiptPaymentDetailsInfo();
        BeanUtils.copyProperties(receiptPaymentDetailsInfoVo,receiptPaymentDetailsInfo);
        if (StringUtils.isEmpty(receiptPaymentDetailsInfo.getOrgCode())){
            return Result.error("添加资金明细失败,机构编码为空");
        }
        if (ObjectUtils.isEmpty(receiptPaymentDetailsInfo.getPurseChangeReason())){
            return Result.error("添加资金明细失败,资金变化原因为空");
        }
        if (receiptPaymentDetailsInfo.getPurseChangeReason()==0||receiptPaymentDetailsInfo.getPurseChangeReason()==1){//用户支付和退款
            receiptPaymentDetailsInfo.setMoneyType(0);//待结算
        } else if (receiptPaymentDetailsInfo.getPurseChangeReason()==2) {//分账
            receiptPaymentDetailsInfo.setMoneyType(1);//可用余额
        } else if (receiptPaymentDetailsInfo.getPurseChangeReason()==3) {//银行卡导致
            receiptPaymentDetailsInfo.setMoneyType(2);//已到账金额
        }
        SysDepart sysDepart = sysDepartMapper.findByOrgCode(receiptPaymentDetailsInfo.getOrgCode());
        receiptPaymentDetailsInfo.setDeptId(sysDepart.getId());
        ReceiptPaymentDetailsInfo receiptPaymentDetailsInfoOld =  receiptPaymentDetailsInfoMapper.findByOrgCode(receiptPaymentDetailsInfo.getOrgCode());
        BigDecimal bigDecimal =null;
        if (receiptPaymentDetailsInfoOld==null||receiptPaymentDetailsInfoOld.getAfterMoney()==null){
            bigDecimal =BigDecimal.ZERO;
        }else {
            bigDecimal= receiptPaymentDetailsInfoOld.getAfterMoney();
        }
        receiptPaymentDetailsInfo.setBeforeMoney(bigDecimal);
        receiptPaymentDetailsInfo.setAfterMoney(bigDecimal.add(receiptPaymentDetailsInfo.getChangeMoney()));
        receiptPaymentDetailsInfo.setChangeMoney(receiptPaymentDetailsInfo.getChangeMoney());
        receiptPaymentDetailsInfo.setCreateTime(new Date());
        receiptPaymentDetailsInfo.setUpdateTime(new Date());
        int insert = receiptPaymentDetailsInfoMapper.insert(receiptPaymentDetailsInfo);

        if (insert!=1){
            return Result.error("添加资金明细失败");
        }
        if (ObjectUtils.isEmpty(receiptPaymentDetailsInfoVo.getDeptType())){
            return Result.error("添加资金明细成功,保存总额失败,部门类型不能为空");
        }
        //需要nm_statistics_info这个表里面插数据 只属于支出和退款进行
        if (receiptPaymentDetailsInfoVo.getPurseChangeReason()==0||receiptPaymentDetailsInfoVo.getPurseChangeReason()==1){
            Result<String> addResult = null;
            try {
                addResult = iStatisticsInfoService.add(receiptPaymentDetailsInfo,receiptPaymentDetailsInfoVo.getMoney());
            } catch (Exception e) {
                log.error("保存首页数据："+JSONObject.toJSONString(e));
                log.error("保存首页数据："+e.getMessage());
            }
            if (addResult !=null&&!addResult.isSuccess()){
                return addResult;
            }
        }
        if (receiptPaymentDetailsInfoVo.getDeptType()==2){
            Result<String> result= null;
            try {
                result = iShopMoneyService.add(receiptPaymentDetailsInfo);
            } catch (Exception e) {
                log.error("保存店铺数据："+JSONObject.toJSONString(e));
                log.error("保存店铺数据："+e.getMessage());
            }
            if (result !=null&&!result.isSuccess()){
                return result;
            }
        }


        return Result.ok("成功");
    }

    @Override
    public IPage<ReceiptPaymentDetailsInfo> findPage(Page<ReceiptPaymentDetailsInfo> page, ReceiptPaymentDetailsInfo receiptPaymentDetailsInfo) {
        return receiptPaymentDetailsInfoMapper.findPage(page,receiptPaymentDetailsInfo);
    }
}
