• haoyanbin's avatar
    1 · 9bf9e037
    haoyanbin authored
    9bf9e037
vip.go 5.7 KB
package service

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

func GetVipUser(userId int) (error, model.VipUser) {
	data := new(model.VipUser)
	table := " vip_user "
	field := " id, maturity_time, vip_level "

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

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

func CreateVipUser(req model.VipUser) error {
	sqlStr := "INSERT INTO vip_user(user_id, maturity_time, vip_level) VALUE(?,?,?)"

	global.GVA_DB.Exec(sqlStr, req.UserId, req.MaturityTime, req.VipLevel)
	return global.GVA_DB.Error
}

func UpdateVipUser(req model.VipUser, userId int) error {
	sqlStr := "UPDATE vip_user SET maturity_time='" + utils.Time2Str(req.MaturityTime) + "', vip_level=? WHERE user_id=?"

	global.GVA_DB.Exec(sqlStr, req.VipLevel, userId)
	return global.GVA_DB.Error
}

func IsVipUser(userId int) int64 {
	var totalItem int64 = 0

	sqlStr := "SELECT count(id) as totalItem FROM vip_user where user_id=?"
	global.GVA_DB.Raw(sqlStr, userId).Count(&totalItem) //获取总条数

	return totalItem
}

func IsNewUser(userId int) int64 {
	//now := time.Now().Format("2006-01-02 15:04:05")

	var totalItem int64 = 0

	sqlStr := "SELECT count(id) as totalItem FROM vip_order where user_id='" + strconv.Itoa(userId) + "' and status = 2 "
	global.GVA_DB.Raw(sqlStr).Count(&totalItem) //获取总条数

	return totalItem
}

func GetVipConf(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 GetVipConfList(req request.GetVipConfListReq) (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 CreateVipOrder(req model.VipOrder) error {
	sqlStr := "INSERT INTO vip_order(order_no, user_id, vip_conf_id, vip_day, vip_level, vip_money, pay_money, status, is_points, points_num, remark) VALUE(?,?,?,?,?,?,?,?,?,?,?)"

	global.GVA_DB.Exec(sqlStr, req.OrderNo, req.UserId, req.VipConfId, req.VipDay, req.VipLevel, req.VipMoney, req.PayMoney, req.Status, req.IsPoints, req.PointsNum, req.Remark)
	return global.GVA_DB.Error
}

func UpdateVipOrder(req request.CallBackReq) error {
	payTime := time.Now().Format("2006-01-02 15:04:05")
	sqlStr := "UPDATE vip_order SET status=2, pay_time='" + payTime + "', out_trade_no=?, transaction_id=? WHERE order_no=?"

	global.GVA_DB.Exec(sqlStr, req.OutTradeNo, req.TransactionId, req.AttachInfo)
	return global.GVA_DB.Error
}

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

	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 GetVipOrderList(req request.GetVipOrderListReq) (error, []request.VipOrderList, int64) {
	pagesize := 10
	page := 1
	if req.PageSize != 0 {
		pagesize = req.PageSize
	}
	if req.Page != 0 {
		page = req.Page
	}
	currentpage := pagesize * (page - 1)

	list := make([]request.VipOrderList, 0)

	field := " vo.id, vo.vip_money, vo.pay_money, vo.vip_level, vo.is_points" +
		", vo.points_num, vo.create_time, vo.remark, vo.vip_day " +
		", su.mobile, su.nick_name, sua.name"

	table := " vip_order as vo " +
		" left join sys_users as su on vo.user_id=su.id " +
		" left join sys_user_authe as sua on su.id = sua.user_id and sua.delflag=0 "

	conditions := " AND vo.delflag = 0 AND vo.status = 2 "

	orderby := " create_time desc "

	if req.Mobile != "" {
		conditions += " AND su.mobile like '%" + req.Mobile + "%'"
	}

	if req.UserType != "" {
		conditions += " AND su.user_type = " + req.UserType
	}

	if req.StartCreateTime != "" {
		conditions += " AND vo.create_time >'" + req.StartCreateTime + "'"
	}

	if req.EndCreateTime != "" {
		conditions += " AND vo.create_time <='" + req.EndCreateTime + "'"
	}

	if req.VipLevel != "" {
		conditions += " AND vo.vip_level in (" + req.VipLevel + ")"
	}

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

	sqlStr := "SELECT count(vo.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)

	fmt.Println(sqlStr2)
	global.GVA_DB.Raw(sqlStr2).Scan(&list)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, nil, 0
	}
	return nil, list, totalItem
}