package dao

import (
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-print/dao"
	"time"
)

// StoreInformationStatistics 门店信息统计
func StoreInformationStatistics(db *DaoDB) (result []*model.EffectiveStores, err error) {
	sql := `
		SELECT 
			  gs.jx_store_id,
			  MAX(sm.vendor_org_code) AS vendor_org_code,
			  MAX(sm.vendor_store_id) AS vendor_store_id,
			  MAX(sm.mtwm_token) AS mtwm_token
		FROM goods_order gs
		LEFT JOIN store_map sm ON gs.jx_store_id = sm.store_id AND gs.vendor_id = sm.vendor_id
		WHERE gs.order_created_at >= ?
		  AND gs.vendor_id = ?
		GROUP BY gs.jx_store_id;
`
	parma := []interface{}{time.Now().AddDate(0, 0, -10), model.VendorIDMTWM}

	if err = GetRows(db, &result, sql, parma...); err != nil {
		return nil, err
	}

	return
}

func GetStatistics(db *DaoDB, startTime, endTime time.Time, storeId []int, offset, pageSize int) (pageInfo *model.PagedInfo, err error) {
	sql := ` SELECT SQL_CALC_FOUND_ROWS * FROM activity_station WHERE 1=1 `
	param := []interface{}{}
	if !utils.IsTimeZero(startTime) {
		sql += ` AND created_at >= ?`
		param = append(param, startTime)
	}
	if !utils.IsTimeZero(endTime) {
		sql += ` AND created_at <= ?`
		param = append(param, endTime)
	}
	if len(storeId) > 0 {
		sql += " AND store_id  (" + dao.GenQuestionMarks(len(storeId)) + ")"
		param = append(param, storeId)
	}
	sql += ` ORDER BY created_at desc LIMIT ? OFFSET ?`
	param = append(param, jxutils.FormalizePageSize(pageSize), offset)

	txDB, _ := Begin(db)
	defer Commit(db, txDB)

	var data []*model.ActivityStation
	if err := GetRowsTx(txDB, &data, sql, param...); err == nil {
		pageInfo = &model.PagedInfo{
			TotalCount: GetLastTotalRowCount2(db, txDB),
			Data:       data,
		}
	}

	return pageInfo, err
}

type StatisticsOrderObj struct {
	OrderCount int64  `json:"order_count"`
	StoreCount int64  `json:"store_count"`
	Name       string `json:"name"`
	Mobile     string `json:"mobile"`
}

// StatisticsOrderCount 统计负责人订单量
func StatisticsOrderCount(db *DaoDB, startTime, endTime time.Time, storeId []int, vendorId []int, offset, pageSize int) (pageInfo *model.PagedInfo, err error) {
	sql := `
		SELECT SQL_CALC_FOUND_ROWS
		  COUNT(a.vendor_order_id) AS order_count,
  		  COUNT(DISTINCT b.id) AS store_count,      
		  c.name AS name,
		  c.mobile AS mobile
		FROM goods_order a
		LEFT JOIN store b ON IF(a.store_id = 0, a.jx_store_id, a.store_id) = b.id
		LEFT JOIN user c ON c.mobile = b.market_man_phone
		WHERE 1 = 1
`
	param := []interface{}{}
	if !utils.IsTimeZero(startTime) {
		sql += ` AND a.order_created_at >= ?`
		param = append(param, startTime)
	}
	if !utils.IsTimeZero(endTime) {
		sql += ` AND a.order_created_at <= ?`
		param = append(param, endTime)
	}
	if len(storeId) > 0 {
		sql += " AND IF(a.store_id <> '', a.store_id, a.jx_store_id) IN (" + dao.GenQuestionMarks(len(storeId)) + ")"
		param = append(param, storeId)
	}
	if len(vendorId) != 0 {
		sql += " AND a.vendor_id IN (" + dao.GenQuestionMarks(len(vendorId)) + ")"
		param = append(param, vendorId)
	}

	sql += ` AND a.status = 110 `
	sql += ` GROUP BY c.mobile,c.name ORDER BY order_count desc LIMIT ? OFFSET ?`
	param = append(param, jxutils.FormalizePageSize(pageSize), offset)

	txDB, _ := Begin(db)
	defer Commit(db, txDB)
	var msgList []*StatisticsOrderObj
	if err = GetRowsTx(txDB, &msgList, sql, param...); err == nil {
		pageInfo = &model.PagedInfo{
			TotalCount: GetLastTotalRowCount2(db, txDB),
			Data:       msgList,
		}
	}
	return pageInfo, err

}
