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

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.StatisticsInfo;
import org.jeecg.modules.system.app.entity.StatisticsInfoHis;
import org.jeecg.modules.system.app.mapper.StatisticsInfoHisMapper;
import org.jeecg.modules.system.app.mapper.StatisticsInfoMapper;
import org.jeecg.modules.system.app.service.IStatisticsInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Description: 首页统计表
 * @Author: jeecg-boot
 * @Date:   2025-08-18
 * @Version: V1.0
 */
@Service
public class StatisticsInfoServiceImpl extends ServiceImpl<StatisticsInfoMapper, StatisticsInfo> implements IStatisticsInfoService {

    @Autowired
    StatisticsInfoMapper statisticsInfoMapper;

    @Autowired
    StatisticsInfoHisMapper statisticsInfoHisMapper;

    @Override
    public Result<String> add(ReceiptPaymentDetailsInfo receiptPaymentDetailsInfo,BigDecimal money) {
        int dateAsInt = Integer.parseInt(
                LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
        );
        StatisticsInfo statisticsInfo= statisticsInfoMapper.findByOrgCode(receiptPaymentDetailsInfo.getOrgCode(),dateAsInt);
        if (statisticsInfo==null){
            statisticsInfo =  new StatisticsInfo();
            BeanUtils.copyProperties(receiptPaymentDetailsInfo,statisticsInfo);
            statisticsInfo.setDateDaily(dateAsInt);
        }else {
            StatisticsInfoHis statisticsInfoHis = new StatisticsInfoHis();
            BeanUtils.copyProperties(statisticsInfo,statisticsInfoHis);
            statisticsInfoHis.setCreateTime(new Date());
            statisticsInfoHis.setUpdateTime(new Date());
            int insert = statisticsInfoHisMapper.insert(statisticsInfoHis);
            if (insert!=1){
                throw new JeecgBootException("报错首页统计历史错误,保存失败");
            }

        }
        if (receiptPaymentDetailsInfo.getPayType()==0){
            statisticsInfo.setConsumeNumber(statisticsInfo.getConsumeNumber()+1);
            statisticsInfo.setSaleMoney(statisticsInfo.getSaleMoney().add(money));
            statisticsInfo.setExpectIncomeMoney(statisticsInfo.getExpectIncomeMoney().add(receiptPaymentDetailsInfo.getChangeMoney()));
            statisticsInfo.setEffectiveOrderNum(statisticsInfo.getEffectiveOrderNum()+1);
        }else if (receiptPaymentDetailsInfo.getPayType()==1){
            statisticsInfo.setEffectiveOrderNum(statisticsInfo.getEffectiveOrderNum()-1);
            statisticsInfo.setSaleMoney(statisticsInfo.getSaleMoney().subtract(money));
            statisticsInfo.setExpectIncomeMoney(statisticsInfo.getExpectIncomeMoney().subtract(receiptPaymentDetailsInfo.getChangeMoney()));
        }
        statisticsInfo.setUpdateTime(new Date());
        statisticsInfo.setReceipPaymentDetailsId(receiptPaymentDetailsInfo.getId());
        return Result.ok("成功");
    }

    @Override
    public StatisticsInfo findByCode(String orgCode) {
        return statisticsInfoMapper.findByCode(orgCode);
    }
}
