package cms

import (
	"time"

	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/dao"
)

type MessageStatusExt struct {
	model.MessageStatus

	Title string `json:"title"`
}

func ReadStoreMessage(ctx *jxcontext.Context, msgID, msgStatusID int) (redirectURL string, err error) {
	msgStatus := &model.MessageStatus{}
	msgStatus.ID = msgStatusID
	db := dao.GetDB()
	if err = dao.GetEntity(db, msgStatus); err != nil {
		return redirectURL, err
	}
	msgStatus.ReadCount++
	dao.WrapUpdateULEntity(msgStatus, ctx.GetUserName())
	_, err = dao.UpdateEntity(db, msgStatus)
	return redirectURL, err
}

func GetStoreMessages(ctx *jxcontext.Context, msgIDs, storeIDs, types []int, fromTime, toTime time.Time, keyword string, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) {
	sql := `
	SELECT SQL_CALC_FOUND_ROWS t1.*
	FROM message t1
	WHERE 1 = 1
	`
	sqlParams := []interface{}{}
	if len(storeIDs) > 0 {
		sql += " AND (SELECT COUNT(*) FROM message_status t2 WHERE t2.message_id = t1.id AND t2.store_id IN (" + dao.GenQuestionMarks(len(storeIDs)) + ") ) > 0"
		sqlParams = append(sqlParams, storeIDs)
	}
	if len(msgIDs) > 0 {
		sql += " AND t1.id IN (" + dao.GenQuestionMarks(len(msgIDs)) + ")"
		sqlParams = append(sqlParams, msgIDs)
	}
	if len(types) > 0 {
		sql += " AND t1.type IN (" + dao.GenQuestionMarks(len(types)) + ")"
		sqlParams = append(sqlParams, types)
	}
	if fromTime != utils.DefaultTimeValue {
		sql += " AND t1.created_at >= ?"
		sqlParams = append(sqlParams, fromTime)
	}
	if toTime != utils.DefaultTimeValue {
		sql += " AND t1.created_at <= ?"
		sqlParams = append(sqlParams, toTime)
	}
	if keyword != "" {
		keywordLike := "%" + keyword + "%"
		sql += " AND (t1.title LIKE ? OR t1.content LIKE ?)"
		sqlParams = append(sqlParams, keywordLike, keywordLike)
	}
	sql += " ORDER BY t1.id DESC LIMIT ? OFFSET ?"
	pageSize = jxutils.FormalizePageSize(pageSize)
	sqlParams = append(sqlParams, pageSize, offset)
	db := dao.GetDB()
	txDB, _ := dao.Begin(db)
	defer dao.Commit(db, txDB)
	var msgList []*model.Message
	if err = dao.GetRowsTx(txDB, &msgList, sql, sqlParams...); err == nil {
		pagedInfo = &model.PagedInfo{
			TotalCount: dao.GetLastTotalRowCount2(db, txDB),
			Data:       msgList,
		}
	}
	return pagedInfo, err
}

func GetStoreMessageStatuses(ctx *jxcontext.Context, msgIDs, storeIDs, confirmStatuss []int, fromReadCount, toReadCount int, fromTime, toTime time.Time, keyword string, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) {
	sql := `
	SELECT SQL_CALC_FOUND_ROWS
	t1.*,
	t2.title
	FROM message_status t1
	JOIN message t2 ON t2.id = t1.message_id
	WHERE 1 = 1
	`
	sqlParams := []interface{}{}
	if len(storeIDs) > 0 {
		sql += " AND t1.store_id IN (" + dao.GenQuestionMarks(len(storeIDs)) + ")"
		sqlParams = append(sqlParams, storeIDs)
	}
	if len(confirmStatuss) > 0 {
		sql += " AND t1.confirm_status IN (" + dao.GenQuestionMarks(len(confirmStatuss)) + ")"
		sqlParams = append(sqlParams, confirmStatuss)
	}
	if len(msgIDs) > 0 {
		sql += " AND t1.message_id IN (" + dao.GenQuestionMarks(len(msgIDs)) + ")"
		sqlParams = append(sqlParams, msgIDs)
	}
	if fromTime != utils.DefaultTimeValue {
		sql += " AND t1.created_at >= ?"
		sqlParams = append(sqlParams, fromTime)
	}
	if toTime != utils.DefaultTimeValue {
		sql += " AND t1.created_at <= ?"
		sqlParams = append(sqlParams, toTime)
	}
	if fromReadCount >= 0 {
		sql += " AND t1.read_count >= ?"
		sqlParams = append(sqlParams, fromReadCount)
	}
	if toReadCount >= 0 {
		sql += " AND t1.read_count <= ?"
		sqlParams = append(sqlParams, toReadCount)
	}
	if keyword != "" {
		keywordLike := "%" + keyword + "%"
		sql += " AND (t1.last_operator LIKE ?)"
		sqlParams = append(sqlParams, keywordLike)
	}
	sql += " LIMIT ? OFFSET ?"
	sqlParams = append(sqlParams, jxutils.FormalizePageSize(pageSize), offset)
	db := dao.GetDB()
	txDB, _ := dao.Begin(db)
	defer dao.Commit(db, txDB)
	var msgStatusList []*MessageStatusExt
	if err = dao.GetRowsTx(txDB, &msgStatusList, sql, sqlParams...); err == nil {
		pagedInfo = &model.PagedInfo{
			TotalCount: dao.GetLastTotalRowCount2(db, txDB),
			Data:       msgStatusList,
		}
	}
	return pagedInfo, err
}
