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 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;

    @Override
    public List<RiskSale> getList(RiskSaleParam riskSaleParam) {
        String orgId = riskSaleParam.getOrgId();
        Integer timeType = riskSaleParam.getTimeType();
        QueryWrapper<RiskSale> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" IFNULL(sum(sale_money),'0') saleMoney,IFNULL(sum(back_money),'0') backMoney,create_time createTime");
        if (1 == timeType) {
            DateTime dateTime = DateUtil.beginOfYear(new Date());
            queryWrapper.ge("create_time", dateTime);
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        } else {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
        }
        queryWrapper.orderByAsc("create_time");
        return saleMapper.selectList(queryWrapper);
    }
}
