package vip.xiaonuo.sale.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
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.sale.entity.SaleSalesman;
import vip.xiaonuo.sale.mapper.SaleSalesmanMapper;
import vip.xiaonuo.sale.param.SaleSalesmanParam;
import vip.xiaonuo.sale.service.ISaleSalesmanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * <p>
 * 销售业务员发货 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-13
 */
@Service
public class SaleSalesmanServiceImpl extends ServiceImpl<SaleSalesmanMapper, SaleSalesman> implements ISaleSalesmanService {

    @Autowired
    private SaleSalesmanMapper salesmanMapper;

    @Override
    public List<SaleSalesman> getList(SaleSalesmanParam salesmanParam) {
        String orgId = salesmanParam.getOrgId();
//        String time = salesmanParam.getTime();
        QueryWrapper<SaleSalesman> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.le("create_time", DateUtil.beginOfYear(new Date()));
        queryWrapper.select(" salesman_name salesmanName,IFNULL(sum(salesman_money),'0') salesmanMoney");
        queryWrapper.groupBy("salesman_name");
        List<SaleSalesman> saleSalesmens = salesmanMapper.selectList(queryWrapper);
        BigDecimal math = new BigDecimal("10000");
        for (SaleSalesman saleSalesman : saleSalesmens) {
            BigDecimal bg = new BigDecimal(saleSalesman.getSalesmanMoney());
            saleSalesman.setSalesmanMoney(bg.divide(math, 2, RoundingMode.HALF_UP).toString());
            if (saleSalesman.getSalesmanName() == null) {
                saleSalesman.setSalesmanName("无名");
            }
        }
        List<SaleSalesman> list = jsonArraySort(saleSalesmens, false);
        return list;
    }

    /*数组排序*/
    public static List<SaleSalesman> jsonArraySort(List<SaleSalesman> jsonArr, boolean is_desc) {
        //存放排序结果json数组
        List<SaleSalesman> salesmanList = new ArrayList<>();
        //用于排序的list
        List<SaleSalesman> list = new ArrayList<>();
        //将参数json数组每一项取出，放入list

        for (int i = 0; i <jsonArr.size() ; i++) {
            list.add(jsonArr.get(i));
        }

        //快速排序，重写compare方法，完成按指定字段比较，完成排序
        Collections.sort(list, new Comparator<SaleSalesman>() {
            @Override
            public int compare(SaleSalesman o1, SaleSalesman o2) {
                //如果用String接会导致一位数和两位数混合比对的时候不能准确比出来，要用int类型接
                //String valA = new String();
                //String valB = new String();
                Float valA = 0.00f;
                Float valB = 0.00f;
                try {
                    valA = Float.parseFloat(o1.getSalesmanMoney());
                    valB = Float.parseFloat(o2.getSalesmanMoney());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //是升序还是降序
                if (is_desc) {
                    return -valA.compareTo(valB);
                } else {
                    return -valB.compareTo(valA);
                }

            }
        });
        //将排序后结果放入结果jsonArray
        for (int i = 0; i < jsonArr.size(); i++) {
            salesmanList.add(list.get(i));
        }

        return salesmanList;
    }
}
