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 io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.sale.entity.SaleCustomer;
import vip.xiaonuo.sale.entity.SaleSalesman;
import vip.xiaonuo.sale.entity.vo.SaleCustomerVo;
import vip.xiaonuo.sale.mapper.SaleCustomerMapper;
import vip.xiaonuo.sale.param.SaleCustomerParam;
import vip.xiaonuo.sale.service.ISaleCustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 销售客户贡献 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-13
 */
@Service
public class SaleCustomerServiceImpl extends ServiceImpl<SaleCustomerMapper, SaleCustomer> implements ISaleCustomerService {

    @Autowired
    private SaleCustomerMapper customerMapper;

    @Override
    public List<SaleCustomer> getList(SaleCustomerParam customerParam) {
        String orgId = customerParam.getOrgId();
        QueryWrapper<SaleCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" customer customer,IFNULL(sum(sale_money),'0') saleMoney,create_time createTime");
        queryWrapper.groupBy("customer,DATE_FORMAT(create_time,'%Y-%m')");
        List<SaleCustomer> saleCustomers = customerMapper.selectList(queryWrapper);
        BigDecimal math = new BigDecimal("10000");
        for (SaleCustomer saleCustomer : saleCustomers) {
            BigDecimal bg = new BigDecimal(saleCustomer.getSaleMoney());
            bg = bg.divide(math, 2, RoundingMode.HALF_UP);
            saleCustomer.setSaleMoney(bg.toString());
        }
        return saleCustomers;
    }

    @Override
    public List<SaleCustomer> getListTop(SaleCustomerParam customerParam) {
        String orgId = customerParam.getOrgId();
        QueryWrapper<SaleCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" customer customer,IFNULL(sum(sale_money),'0') saleMoney");
        queryWrapper.groupBy("customer");
        List<SaleCustomer> saleCustomers = customerMapper.selectList(queryWrapper);
        BigDecimal math = new BigDecimal("10000");
        for (SaleCustomer saleCustomer : saleCustomers) {
            BigDecimal bg = new BigDecimal(saleCustomer.getSaleMoney());
            bg = bg.divide(math, 2, RoundingMode.HALF_UP);
            saleCustomer.setSaleMoney(bg.toString());
        }
        List<SaleCustomer> customers = jsonArraySort(saleCustomers, true);
        return customers;
    }

    @Override
    public List<SaleCustomerVo> getDetail(SaleCustomerParam customerParam) {
        String orgId = customerParam.getOrgId();
        QueryWrapper<SaleCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(date);
        queryWrapper.between("create_time", Integer.parseInt(year) - 1 + "-01-01 00:00:00", Integer.parseInt(year) - 1 + "-12-31 23:59:59");
        queryWrapper.select(" IFNULL(sum(sale_money),'0') saleMoney,customer");
        queryWrapper.groupBy("customer");
        List<SaleCustomer> saleCustomers = customerMapper.selectList(queryWrapper);
        List<SaleCustomerVo> customerVos = new ArrayList<>();
        BigDecimal math = new BigDecimal("10000");
        if (saleCustomers.size() != 0) {
            List<SaleCustomer> customers = jsonArraySort(saleCustomers, true);
            for (SaleCustomer customer : customers) {
                SaleCustomerVo vo = new SaleCustomerVo();
                QueryWrapper<SaleCustomer> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("customer", customer.getCustomer()).ge("create_time", DateUtil.beginOfYear(new Date()));
                queryWrapper1.select(" IFNULL(sum(sale_money),'0') saleMoney");
                SaleCustomer saleCustomer = customerMapper.selectOne(queryWrapper1);
                vo.setCustomer(customer.getCustomer());
                BigDecimal bg = new BigDecimal(saleCustomer.getSaleMoney());
                bg = bg.divide(math, 2, RoundingMode.HALF_UP);
                BigDecimal bg1 = new BigDecimal(customer.getSaleMoney());
                bg1 = bg1.divide(math, 2, RoundingMode.HALF_UP);
                vo.setLastSaleMoney(bg1.toString());
                vo.setSaleMoney(bg.toString());
                customerVos.add(vo);
            }
        }

        return customerVos;
    }


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

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

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

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