package vip.xiaonuo.risk.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.risk.entity.RiskSale;
import vip.xiaonuo.risk.mapper.RiskSaleMapper;
import vip.xiaonuo.risk.param.RiskSaleParam;
import vip.xiaonuo.risk.service.IRiskSaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.sale.entity.SaleDetail;
import vip.xiaonuo.sale.mapper.SaleDetailMapper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 风控销售回款 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-09
 */
@Service
public class RiskSaleServiceImpl extends ServiceImpl<RiskSaleMapper, RiskSale> implements IRiskSaleService {

    @Autowired
    private RiskSaleMapper saleMapper;
    @Autowired
    private SaleDetailMapper saleDetailMapper;

    @Override
    public List<RiskSale> getList(RiskSaleParam riskSaleParam) {
        String orgId = riskSaleParam.getOrgId();
        Integer timeType = riskSaleParam.getTimeType();
        Integer number = riskSaleParam.getNumber();
        QueryWrapper<SaleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" IFNULL(sum(sale_money),'0') saleMoney,create_time createTime");
        if (1 == timeType) {
            Calendar calendar = Calendar.getInstance();
            if (null == number) {
                calendar.add(Calendar.MONTH, -13);
            } else {
                calendar.add(Calendar.MONTH, -number);
            }
            queryWrapper.between("create_time", calendar.getTime(), new Date());
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        } else {
            Calendar calendar = Calendar.getInstance();
            if (null == number) {
                calendar.add(Calendar.YEAR, -3);
            } else {
                calendar.add(Calendar.YEAR, -number);
            }
            queryWrapper.between("create_time", calendar.getTime(), new Date());
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
        }
        queryWrapper.orderByAsc("create_time");
        List<SaleDetail> saleDetails = saleDetailMapper.selectList(queryWrapper);
        List<RiskSale> list = new ArrayList<>();
        BigDecimal math = new BigDecimal("10000");
        if (saleDetails.size() != 0) {
            for (SaleDetail saleDetail : saleDetails) {
                RiskSale riskSale = new RiskSale();
                BigDecimal bg = new BigDecimal(saleDetail.getSaleMoney());
                riskSale.setSaleMoney(bg.divide(math, 2, RoundingMode.HALF_UP).toString());
                riskSale.setBackMoney(bg.divide(math, 2, RoundingMode.HALF_UP).toString());
                riskSale.setCreateTime(saleDetail.getCreateTime());
                list.add(riskSale);
            }
        }
        return list;
    }
}
