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.RiskSupplier;
import vip.xiaonuo.risk.mapper.RiskSupplierMapper;
import vip.xiaonuo.risk.param.RiskParam;
import vip.xiaonuo.risk.param.RiskSupplierParam;
import vip.xiaonuo.risk.service.IRiskSupplierService;
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 RiskSupplierServiceImpl extends ServiceImpl<RiskSupplierMapper, RiskSupplier> implements IRiskSupplierService {

    @Autowired
    private RiskSupplierMapper supplierMapper;

    @Override
    public List<RiskSupplier> getList(RiskSupplierParam riskSupplierParam) {
        String orgId = riskSupplierParam.getOrgId();
        Integer timeType = riskSupplierParam.getTimeType();
        String supplier = riskSupplierParam.getSupplier();
        QueryWrapper<RiskSupplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" CONVERT(IFNULL(avg(rank),'0'),decimal(10,2)) 'rank',create_time createTime");
        if (1 == timeType) {
            DateTime dateTime = DateUtil.beginOfYear(new Date());
            queryWrapper.ge("create_time", dateTime);
            if ("".equals(supplier) || null == supplier) {
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
            } else {
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')" /*+ "," + supplier*/);
            }
        } else {
            if ("".equals(supplier) || null == supplier) {
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
            } else {
                queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')" /*+ "," + supplier*/);
            }
        }
        queryWrapper.orderByAsc("create_time");
        return supplierMapper.selectList(queryWrapper);
    }
}
