package vip.xiaonuo.risk.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.purchase.entity.PurchaseOrder;
import vip.xiaonuo.purchase.mapper.PurchaseOrderMapper;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 风控供应商交货 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-09
 */
@Service
public class RiskSupplierServiceImpl extends ServiceImpl<RiskSupplierMapper, RiskSupplier> implements IRiskSupplierService {

    @Autowired
    private RiskSupplierMapper supplierMapper;
    @Autowired
    private PurchaseOrderMapper orderMapper;

    @Override
    public List<RiskSupplier> getList(RiskSupplierParam riskSupplierParam) {
        String orgId = riskSupplierParam.getOrgId();
        Integer timeType = riskSupplierParam.getTimeType();
        String supplier = riskSupplierParam.getSupplier();
        QueryWrapper<PurchaseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" IFNULL(sum(purchase_money),'0') 'purchaseMoney',create_time createTime");
        queryWrapper.eq(StringUtils.isNotBlank(supplier), "supplier", supplier);
        if (1 == timeType) {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        } else {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
        }
        queryWrapper.orderByAsc("create_time");
        List<PurchaseOrder> orders = orderMapper.selectList(queryWrapper);
        List<RiskSupplier> list = new ArrayList<>();
        if (orders.size() != 0) {
            for (PurchaseOrder order : orders) {
                RiskSupplier supplier1 = new RiskSupplier();
                if ("0".equals(order.getPurchaseMoney())) {
                    supplier1.setRank("0");
                } else {
                    supplier1.setRank("100");
                }
                supplier1.setCreateTime(order.getCreateTime());
                list.add(supplier1);
            }
        }
        return list;
    }

    @Override
    public JSONArray listSupplier(Map<String, String> paramsMap) {
        String orgId = paramsMap.get("orgId");
        QueryWrapper<PurchaseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.groupBy("supplier");
        List<PurchaseOrder> orders = orderMapper.selectList(queryWrapper);
        JSONArray jsonArray = new JSONArray();
        if (orders.size() != 0) {
            for (PurchaseOrder order : orders) {
                if (order.getSupplier()!=null) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("supplier", order.getSupplier());
                    jsonObject.put("orgId", order.getOrgId());
                    jsonArray.add(jsonObject);
                }
            }
        }
        return jsonArray;
    }

}
