package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/utils"
	"strconv"
	"time"
)

func GetPointsLogList(req request.GetPointsLogListReq) (error, []model.PointsLog, int64) {
	pagesize := 10
	page := 1
	if req.PageSize != 0 {
		pagesize = req.PageSize
	}
	if req.Page != 0 {
		page = req.Page
	}
	currentpage := pagesize * (page - 1)

	field := " id, change_type, change_time, change_num, now_points_num, remark, other_no, source "
	table := " points_log "
	conditions := " AND delflag=0 "
	orderby := ""
	orderby += " create_time desc"

	if req.UserId != 0 {
		conditions += " AND user_id = " + strconv.Itoa(req.UserId)
	}

	if req.Source != 0 {
		conditions += " AND source = " + strconv.Itoa(req.Source)
	}

	if req.ChangeType != 0 {
		conditions += " AND change_type = " + strconv.Itoa(req.ChangeType)
	}

	list := make([]model.PointsLog, 0)

	//@@总条数，总页数
	var totalItem int64 = 0

	sqlStr := "SELECT count(id) as totalItem FROM " + table + " where 1=1 " + conditions
	global.GVA_DB.Raw(sqlStr).Count(&totalItem) //获取总条数

	sqlStr2 := "SELECT " + field +
		" FROM " + table +
		" where 1>0 " + conditions +
		" ORDER BY " + orderby +
		" LIMIT " + strconv.Itoa(currentpage) + "," + strconv.Itoa(pagesize)

	global.GVA_DB.Raw(sqlStr2).Scan(&list)

	return nil, list, totalItem
}

func GetPointsData(userId int, changeType int) int {

	table := " points_log "
	conditions := " AND delflag=0 "

	if userId != 0 {
		conditions += " AND user_id = " + strconv.Itoa(userId)
	}

	if changeType != 0 {
		conditions += " AND change_type = " + strconv.Itoa(changeType)
	}

	//@@总条数，总页数
	var totalItem int64 = 0

	sqlStr := "SELECT sum(change_num) as totalItem FROM " + table + " where 1=1 " + conditions
	global.GVA_DB.Raw(sqlStr).Count(&totalItem) //获取总条数

	return int(totalItem)
}

func GetPointsLog(id int) (error, model.PointsLog) {
	data := new(model.PointsLog)
	table := " points_log "
	field := " id, maturity_time, vip_level "

	sqlStr := "SELECT " + field +
		" FROM " + table +
		" WHERE delflag=0 AND id =?"

	global.GVA_DB.Raw(sqlStr, id).Find(&data)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, model.PointsLog{}
	}
	return nil, *data
}

func CreatePointsLog(req model.PointsLog) error {
	err := global.GVA_DB.Table("points_log").Create(&req).Error
	return err
}

func IsPointsLog(otherNo string) int64 {
	var totalItem int64 = 0

	sqlStr := "SELECT count(id) as totalItem FROM points_log where other_no=? and source=1"
	global.GVA_DB.Raw(sqlStr, otherNo).Count(&totalItem) //获取总条数

	return totalItem
}

func GetPointsOrder(id int) (error, *model.VipConf) {
	data := new(model.VipConf)
	table := " vip_conf "
	field := " id, title, content, vip_level, vip_money, vip_day, vip_type, remark "

	sqlStr := "SELECT " + field +
		" FROM " + table +
		" WHERE delflag=0 AND id =?"

	global.GVA_DB.Raw(sqlStr, id).Find(&data)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, nil
	}
	return nil, data
}

func GetPointsOrderList(req request.GetPointsOrderListReq) (error, []model.VipConf, int64) {
	pagesize := 10
	page := 1
	currentpage := pagesize * (page - 1)

	field := " id, title, content, vip_money, vip_day, vip_type, remark "
	table := " vip_conf"
	conditions := " AND delflag=0 AND is_show=1"
	orderby := ""
	orderby += " sort asc"

	if req.IsNewUser != 1 {
		conditions += " AND vip_type != 2 "
	}

	list := make([]model.VipConf, 0)

	//@@总条数，总页数
	var totalItem int64 = 0

	sqlStr := "SELECT count(id) as totalItem FROM " + table + " where 1=1 " + conditions
	global.GVA_DB.Raw(sqlStr).Count(&totalItem) //获取总条数

	sqlStr2 := "SELECT " + field +
		" FROM " + table +
		" where 1>0 " + conditions +
		" ORDER BY " + orderby +
		" LIMIT " + strconv.Itoa(currentpage) + "," + strconv.Itoa(pagesize)

	global.GVA_DB.Raw(sqlStr2).Scan(&list)

	return nil, list, totalItem
}

func CreatePointsOrder(req model.PointsOrder) error {
	sqlStr := "INSERT INTO points_order(user_id, pay_status, pay_type, points_no, points_num, other_no, remark) VALUE(?,?,?,?,?,?,?)"

	global.GVA_DB.Exec(sqlStr, req.UserId, req.PayStatus, req.PayType, req.PointsNo, req.PointsNum, req.OtherNo, req.Remark)
	return global.GVA_DB.Error
}

func UpdatePointsOrderForOtherNo(otherNo string) error {
	payTime := time.Now().Format("2006-01-02 15:04:05")
	sqlStr := "UPDATE points_order SET pay_status=2, pay_time='" + payTime + "' WHERE other_no=?"

	global.GVA_DB.Exec(sqlStr, otherNo)
	return global.GVA_DB.Error
}

func GetPointsOrderForNo(orderNo string) (error, model.VipOrder) {
	data := new(model.VipOrder)
	table := " vip_order "
	field := " id, user_id, vip_level, vip_day, status "

	sqlStr := "SELECT " + field +
		" FROM " + table +
		" WHERE delflag=0 AND order_no =?"

	global.GVA_DB.Raw(sqlStr, orderNo).Find(&data)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, model.VipOrder{}
	}
	return nil, *data
}

func GetPointsLogNum(hospitalCode string, userId int) (int64, int64) {
	start, _ := utils.GetMonthStartEnd(time.Now())
	var totalItem int64 = 0

	sqlStr := "SELECT count(id) as totalItem FROM points_log where hospital_code=? and change_type = 2 and source=1 and change_time > ?"
	global.GVA_DB.Raw(sqlStr, hospitalCode, start).Count(&totalItem) //获取总条数

	var totalItem2 int64 = 0

	sqlStr2 := "SELECT count(id) as totalItem FROM points_log where user_id=? and change_type = 2 and source=1 and change_time > ?"
	global.GVA_DB.Raw(sqlStr2, userId, start).Count(&totalItem2) //获取总条数

	return totalItem, totalItem2
}
