package vip.xiaonuo.money.service.impl;

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.MoneyCost;
import vip.xiaonuo.money.entity.MoneyProducePrice;
import vip.xiaonuo.money.mapper.MoneyCostMapper;
import vip.xiaonuo.money.param.MoneyInOutParam;
import vip.xiaonuo.money.service.IMoneyCostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 财务费用分析 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Service
public class MoneyCostServiceImpl extends ServiceImpl<MoneyCostMapper, MoneyCost> implements IMoneyCostService {

    @Autowired
    private MoneyCostMapper costMapper;

    @Override
    public List<MoneyCost> listCost(MoneyInOutParam outParam) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.add(Calendar.MONTH, -11);
        String currentDateString = sdf.format(calendar.getTime());
        QueryWrapper<MoneyCost> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("create_time", currentDateString);
        queryWrapper.eq(StringUtils.isNotBlank(outParam.getOrgId()), "org_id", outParam.getOrgId());
        queryWrapper.select(" IFNULL(sum(manage),'0') manage,IFNULL(sum(sale),'0') sale,IFNULL(sum(finance),'0') finance," +
                "create_time createTime");
        queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        queryWrapper.orderByAsc("create_time");
        return costMapper.selectList(queryWrapper);

    }




}
