package vip.xiaonuo.sale.service.impl;

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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.sale.entity.SaleDetail;
import vip.xiaonuo.sale.entity.SaleSalesman;
import vip.xiaonuo.sale.mapper.SaleDetailMapper;
import vip.xiaonuo.sale.param.SaleCustomerParam;
import vip.xiaonuo.sale.param.SaleDetailPageParam;
import vip.xiaonuo.sale.param.SaleDetailParam;
import vip.xiaonuo.sale.service.ISaleDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 销售信息明细表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-13
 */
@Service
public class SaleDetailServiceImpl extends ServiceImpl<SaleDetailMapper, SaleDetail> implements ISaleDetailService {

    @Autowired
    private SaleDetailMapper detailMapper;

    @Override
    public JSONArray getList(SaleDetailParam detailParam) {
        String orgId = detailParam.getOrgId();
        String produceName = detailParam.getProduceName();
        QueryWrapper<SaleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId)
                .eq(StringUtils.isNotBlank(produceName), "produce_name", produceName);
        queryWrapper.select(" convert(IFNULL(sum(sale_money),'0'),decimal(10,2)) saleMoney,customer customer");
        queryWrapper.orderByDesc("sale_money");
        queryWrapper.groupBy("customer");
        List<SaleDetail> saleDetails = detailMapper.selectList(queryWrapper);
        JSONArray jsonArray = new JSONArray();
        Double count=0.00;
        if (saleDetails.size() != 0) {
            for (int i = 0; i < saleDetails.size(); i++) {
                if (i < 5) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("name", saleDetails.get(i).getCustomer());
                    jsonObject.put("value", saleDetails.get(i).getSaleMoney());
                    jsonArray.add(jsonObject);
                } else {
                    count = count + Double.valueOf(saleDetails.get(i).getSaleMoney());
                }
            }
        }
        DecimalFormat def = new DecimalFormat("0.00");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "others");
        jsonObject.put("value", def.format(count));
        jsonArray.add(jsonObject);
        return jsonArray;
    }

    @Override
    public Page<SaleDetail> getList2(SaleDetailPageParam detailPageParam) {
        String orgId = detailPageParam.getOrgId();
        Page<SaleDetail> page = new Page<>(detailPageParam.getPageNum(), detailPageParam.getPageSize());
        QueryWrapper<SaleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.orderByDesc("create_time");
        return detailMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Page<SaleDetail> listTop(SaleDetailPageParam customerParam) {
        Integer pageNum = customerParam.getPageNum();
        Integer pageSize = customerParam.getPageSize();
        String orgId = customerParam.getOrgId();
        Page<SaleDetail> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SaleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.select(" IFNULL(sum(sale_money),'0') saleMoney,salesman salesman");
        queryWrapper.orderByDesc("saleMoney");
        queryWrapper.groupBy("salesman");
        Page<SaleDetail> selectPage = detailMapper.selectPage(page, queryWrapper);
        List<SaleDetail> saleDetails = selectPage.getRecords();
        BigDecimal math = new BigDecimal("10000");
        if (saleDetails.size() != 0) {
            for (int i = 0; i < saleDetails.size(); i++) {
                BigDecimal bg = new BigDecimal(saleDetails.get(i).getSaleMoney());
                saleDetails.get(i).setSaleMoney(bg.divide(math, 2, RoundingMode.HALF_UP).toString());
                saleDetails.get(i).setId(i + 1);
            }
        }
        return selectPage;
    }

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

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

        //快速排序，重写compare方法，完成按指定字段比较，完成排序
        Collections.sort(list, new Comparator<SaleDetail>() {
            @Override
            public int compare(SaleDetail o1, SaleDetail 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
        for (int i = 0; i < jsonArr.size(); i++) {
            salesmanList.add(list.get(i));
        }

        return salesmanList;
    }
}
