package com.sqx.modules.reconciliation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.DateUtils;
import com.sqx.modules.reconciliation.model.PlatformBill;
import com.sqx.modules.reconciliation.mapper.PlatformBillMapper;
import com.sqx.modules.reconciliation.service.PlatformBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 对账管理表 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2025-02-27
 */
@Slf4j
@Service
public class PlatformBillServiceImpl extends ServiceImpl<PlatformBillMapper, PlatformBill> implements PlatformBillService {


    /**
     * @return
     */
    @Override
    public int insertPlatformBill() {
        //当天开始时间
        String endTime= DateUtils.format(new Date(),"yyyy-MM-dd")+" 00:00:00";
        //对账账期
        String dayId= DateUtils.addDateDaysString(new Date(),-1,"yyyy-MM-dd");
        //前一天开始时间
        String startTime=dayId+" 00:00:00";
        //当前时间
        String now=DateUtils.format(new Date());
        //先删除防止重复生成
        baseMapper.delete(new QueryWrapper<PlatformBill>().eq("day_id",dayId));
        //导入商家和骑手对账
        int count1 =baseMapper.insertPlatformBill(dayId,startTime,endTime,now);
        log.info("导入商家和骑手对账:{}条",count1);
        //导入账期开始金额
        int count2=baseMapper.updateStartMoney(dayId);
        log.info("导入账期开始金额:{}条",count2);
        //导入平台对账
        int count3 =baseMapper.insertTotalPlatformBill(dayId);
        log.info("导入平台对账:{}条",count3);
        if(count1<1||count2<1||count3<1){
            return 0;
        }
        return count3;
    }
}
