package dao

import (
	"git.rosy.net.cn/jx-callback/business/model"
	"sort"
	"time"
)

// GetBadCommentOrderId 根据差评商品列表获取订单号
func GetBadCommentOrderId(jxStoreId int, startTime, endTime time.Time, foodNameList []string, skuList []int) (string, error) {
	sqlParams := []interface{}{}
	sql := `
		SELECT 	count(s.vendor_order_id) count,s.vendor_order_id FROM goods_order g
		LEFT JOIN  order_sku  s ON g.vendor_order_id = s.vendor_order_id 
		WHERE g.jx_store_id = ? AND g.vendor_id = ? AND g.order_created_at >= ? AND g.order_created_at <= ? `
	sqlParams = append(sqlParams, []interface{}{
		jxStoreId,
		model.VendorIDMTWM,
		startTime,
		endTime,
	})
	if len(foodNameList) != 0 {
		sql += `AND s.sku_name IN (` + GenQuestionMarks(len(foodNameList)) + `) GROUP BY s.vendor_order_id ORDER BY count desc LIMIT 0, 10`
		sqlParams = append(sqlParams, foodNameList)
	}
	if len(skuList) != 0 {
		sql += `AND s.sku_id IN (` + GenQuestionMarks(len(skuList)) + `) GROUP BY s.vendor_order_id ORDER BY count desc  LIMIT 0, 10`
		sqlParams = append(sqlParams, skuList)
	}

	commentOrder := make([]*badCommentOrder, 0, 0)
	if err := GetRows(GetDB(), &commentOrder, sql, sqlParams); err != nil {
		return "", err
	}

	// 全等于的话就是目标订单
	vendorOrderIdList := make([]string, 0, 0)
	for _, v := range commentOrder {
		if foodNameList != nil {
			if v.Count == len(foodNameList) {
				return v.VendorOrderId, nil
			} else if v.Count-1 == len(foodNameList) || v.Count-2 == len(foodNameList) {
				vendorOrderIdList = append(vendorOrderIdList, v.VendorOrderId)
			} else if v.Count+1 == len(foodNameList) || v.Count+2 == len(foodNameList) {
				vendorOrderIdList = append(vendorOrderIdList, v.VendorOrderId)
			}
		} else if skuList != nil {
			if v.Count == len(skuList) {
				return v.VendorOrderId, nil
			} else if v.Count-1 == len(skuList) || v.Count-2 == len(skuList) {
				vendorOrderIdList = append(vendorOrderIdList, v.VendorOrderId)
			} else if v.Count+1 == len(skuList) || v.Count+2 == len(skuList) {
				vendorOrderIdList = append(vendorOrderIdList, v.VendorOrderId)
			}
		}

	}

	sql2 := `SELECT * FROM order_sku WHERE vendor_order_id = ?`
	mathProbability := make(map[string]float64, 0)
	for _, v := range vendorOrderIdList {
		skuFinancial := make([]*model.OrderSku, 0, 0)
		if err := GetRows(GetDB(), &skuFinancial, sql2, []interface{}{v}...); err != nil {
			continue
		}

		var skuMatchingCount int = 0
		if foodNameList != nil {
			for _, sf := range skuFinancial {
				for _, fnl := range foodNameList {
					if sf.SkuName == fnl {
						skuMatchingCount += 1
					}
				}
			}
		} else if skuList != nil {
			for _, sf := range skuFinancial {
				for _, fnl := range skuList {
					if sf.SkuID == fnl {
						skuMatchingCount += 1
					}
				}
			}
		}

		mathProbability[v] = float64(skuMatchingCount) / float64(len(skuFinancial))
	}

	var values []float64
	for _, v := range mathProbability {
		values = append(values, v)
	}
	sort.Slice(values, func(i, j int) bool {
		if values[i] > values[j] {
			return true
		}
		return false
	})
	for k, v := range mathProbability {
		if v == values[0] {
			return k, nil
		}
	}

	return "", nil
}

type badCommentOrder struct {
	Count         int    `json:"count"`
	VendorOrderId string `json:"vendor_order_id"`
}
