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

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

/**
 * <p>
 * 财务产品成本收入 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Service
public class MoneyProducePriceServiceImpl extends ServiceImpl<MoneyProducePriceMapper, MoneyProducePrice> implements IMoneyProducePriceService {

    @Autowired
    private MoneyProducePriceMapper producePriceMapper;
    @Override
    public List<MoneyProducePrice> listProduceProfit(MoneyInOutParam outParam) {
        QueryWrapper<MoneyProducePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(outParam.getOrgId()), "org_id", outParam.getOrgId());
        queryWrapper.select(" produce_name produceName,profit profit,create_time createTime");
        queryWrapper.groupBy("produce_name,DATE_FORMAT(create_time,'%Y-%m')");
        queryWrapper.orderByAsc("create_time");
        return producePriceMapper.selectList(queryWrapper);
    }

    @Override
    public List<MoneyProduceVo> listProduce(MoneyInOutParam outParam) {
        DateTime dateTime = DateUtil.beginOfYear(new Date());
        QueryWrapper<MoneyProducePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(outParam.getOrgId()), "org_id", outParam.getOrgId());
        queryWrapper.ge("create_time",dateTime).groupBy("produce_name,DATE_FORMAT(create_time,'%Y-%m')");
        queryWrapper.orderByAsc("create_time");
        List<MoneyProducePrice> prices = producePriceMapper.selectList(queryWrapper);
        List<MoneyProduceVo> list = new ArrayList<>();
        if (prices.size() != 0) {
            for (MoneyProducePrice price : prices) {
                MoneyProduceVo vo = new MoneyProduceVo();
                vo.setCost(price.getCost());
                vo.setIncome(price.getIncome());
                vo.setTime(price.getCreateTime());
                list.add(vo);
            }
        }
        return list;
    }

}
