package cn.palmte.work.service;

import cn.palmte.work.model.MonthlySettle;
import cn.palmte.work.pojo.MonthlySettleCount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.jfunc.common.db.QueryHelper;
import top.jfunc.common.db.utils.Pagination;

import java.math.BigDecimal;
import java.util.List;

@Service
public class MonthlySettleStatisticsService {
    private static final Logger logger = LoggerFactory.getLogger(MonthlySettleStatisticsService.class);

    @Autowired
    private Pagination pagination;

    public List<MonthlySettleCount> getListData(String time) {

        QueryHelper helper = new QueryHelper("sum(total_collect) as totalCollect, sum(total_purchased_paid) as totalPurchasedPaid, sum(collect_actual_amount) as collectActualAmount, " +
                "sum(uncollect_actual_amount) as uncollectActualAmount, sale_manager as manager", "monthly_settle", "ms");
        helper.addCondition("ms.month = ?", time);
        helper.addGroupProperty("ms.sale_manager");

        List<MonthlySettleCount> monthlySettleCounts = pagination.find(helper.getSql(), MonthlySettleCount.class);

        for (MonthlySettleCount monthlySettleCount : monthlySettleCounts) {
            monthlySettleCount.setTotalCollect(monthlySettleCount.getTotalCollect().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            monthlySettleCount.setTotalPurchasedPaid(monthlySettleCount.getTotalPurchasedPaid().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            monthlySettleCount.setCollectActualAmount(monthlySettleCount.getCollectActualAmount().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            monthlySettleCount.setUncollectActualAmount(monthlySettleCount.getUncollectActualAmount().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
        }

        return monthlySettleCounts;
    }
}
