package vip.xiaonuo.money.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.money.entity.MoneyForecast;
import vip.xiaonuo.money.mapper.MoneyForecastMapper;
import vip.xiaonuo.money.param.MoneyInOutParam;
import vip.xiaonuo.money.service.IMoneyForecastService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 财务费用预测 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Service
public class MoneyForecastServiceImpl extends ServiceImpl<MoneyForecastMapper, MoneyForecast> implements IMoneyForecastService {

    @Autowired
    private MoneyForecastMapper forecastMapper;

    @Override
    public List<MoneyForecast> listForecast(MoneyInOutParam outParam) {
        QueryWrapper<MoneyForecast> queryWrapper = new QueryWrapper<>();
        if ("".equals(outParam.getOrgId()) || null == outParam.getOrgId()) {
            queryWrapper.select(" IFNULL(sum(sale_number),'0') saleNumber,IFNULL(sum(sale_profit),'0') saleProfit,IFNULL(sum(sale_cost),'0') saleCost," +
                    "create_time createTime,type type");
        } else {
            queryWrapper.eq( "org_id", outParam.getOrgId());
        }
        DateTime dateTime = DateUtil.beginOfMonth(new Date());
        queryWrapper.ge("create_time", dateTime);
        List<MoneyForecast> list = forecastMapper.selectList(queryWrapper);
        return list;
    }
}
