package service

import (
	"gadmin/config"
	"gadmin/internal/admin/consts"
	"gadmin/internal/admin/forms"
	"gadmin/internal/gorm/query"
	"gadmin/utility/serializer"
	"github.com/gin-gonic/gin"
	"time"
)

var ChatLog = new(sChatLog)

type sChatLog struct{}

type ChatRecord struct {
	RecordList []record `json:"recordlist,omitempty"`
	Number     int      `json:"number,omitempty"`
	MsgId      int      `json:"msgid,omitempty"`
}

type record struct {
	OpenId   string `json:"openid,omitempty"`
	OperCode int    `json:"opercode,omitempty"`
	Text     string `json:"text,omitempty"`
	Time     int64  `json:"time,omitempty"`
	Worker   string `json:"worker,omitempty"`
}

func (c *sChatLog) ChatLogPlayerList(ctx *gin.Context, req forms.ChatLogPlayerListReq) (resp serializer.Response) {
	var (
		offset int64 = 0
		count  int64 = 0
		result forms.ListRes
	)
	//page
	req.Page, req.PerPage, offset = forms.CalculatePage(req.Page, req.PerPage)
	result.Page = req.Page
	result.PerPage = req.PerPage

	subquery := config.DB.Table("customer_service_chat_log").
		Select("open_id, max(time) as max_time").
		Where("type = 1").
		Group("open_id")

	if len(req.Time) != 0 {
		subquery = subquery.Where("time between ? and ?", time.Unix(req.Time[0], 0), time.Unix(req.Time[1], 0))
	}
	if req.Name != "" {
		subquery = subquery.Where("player_name = ?", req.Name)
	}
	if req.AccId != 0 {
		subquery = subquery.Where("acc_id = ?", req.AccId)
	}
	if req.Text != "" {
		subquery = subquery.Where("text like ?", "%"+req.Text+"%")
	}
	if req.OpenId != "" {
		subquery = subquery.Where("open_id = ?", req.OpenId)
	}

	sql := config.DB.Table("customer_service_chat_log").
		Select("*").
		Joins("JOIN (?) sub ON customer_service_chat_log.open_id = sub.open_id AND customer_service_chat_log.time = sub.max_time", subquery).
		Where("customer_service_chat_log.type = 1").
		Order("customer_service_chat_log.time desc")

	err := sql.Count(&count).Error
	if err != nil {
		return serializer.Err(consts.CodeParamErr, "查询出错 count", err)
	}
	if count == 0 {
		result.PageCount = (count + req.PerPage - 1) / req.PerPage
		return serializer.Suc(result)
	}

	type playerList struct {
		PlayerName string    `json:"playerName"`
		AccId      int64     `json:"accId"`
		OpenId     string    `json:"openId"`
		Time       time.Time `json:"time"`
	}
	list := make([]playerList, 0)
	err = sql.Limit(int(req.PerPage)).Offset(int(offset)).Scan(&list).Error
	if err != nil {
		return serializer.Err(consts.CodeParamErr, "查询出错 find", err)
	}
	result.PageCount = (count + req.PerPage - 1) / req.PerPage
	result.List = list
	return serializer.Suc(result)
}

func (c *sChatLog) ChatLogInfo(ctx *gin.Context, req forms.ChatLogInfoReq) (resp serializer.Response) {
	var (
		chatLogQuery       = query.Use(config.DB).CustomerServiceChatLog
		chatLogModel       = chatLogQuery.WithContext(ctx)
		offset       int64 = 0
		result       forms.ListRes
	)
	//page
	req.Page, req.PerPage, offset = forms.CalculatePage(req.Page, req.PerPage)
	result.Page = req.Page
	result.PerPage = req.PerPage

	if len(req.Time) != 0 {
		chatLogModel = chatLogModel.Where(chatLogQuery.Time.Between(time.Unix(req.Time[0], 0), time.Unix(req.Time[1], 0)))
	}

	count, err := chatLogModel.Where(chatLogQuery.OpenID.Eq(req.OpenId)).Order(chatLogQuery.Time).Count()
	if err != nil {
		return serializer.Err(consts.CodeParamErr, "查询出错 ChatLogInfo count", err)
	}
	if count == 0 {
		result.PageCount = (0 + req.PerPage - 1) / req.PerPage
		return serializer.Suc(result)
	}
	chatLogList, err := chatLogModel.Where(chatLogQuery.OpenID.Eq(req.OpenId)).Limit(int(req.PerPage)).Offset(int(offset)).Order(chatLogQuery.Time).Find()
	if err != nil {
		return serializer.Err(consts.CodeParamErr, "查询出错 ChatLogInfo find", err)
	}
	result.PageCount = (count + req.PerPage - 1) / req.PerPage
	result.List = chatLogList
	return serializer.Suc(result)
}
