You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
9.9 KiB

package user
import (
"fmt"
mysql2 "git.oa00.com/go/mysql"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/gin-gonic/gin"
"github.com/golangkit/formatime"
"github.com/shopspring/decimal"
"gorm.io/gorm"
"path/filepath"
"recook/internal/libs/bean"
"recook/internal/static_path"
"recook/internal/v2/controller/manage/activebit"
"recook/internal/v2/lib/back"
"recook/internal/v2/lib/common"
user2 "recook/internal/v2/logic/manage/user"
"recook/internal/v2/model/recook/coin"
user3 "recook/internal/v2/model/recook/user"
"recook/tools"
"time"
)
type Manage struct {
}
func (m *Manage) List(c *gin.Context) {
args := bean.Page{}
err := tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
total, list := user2.ManageLogic.List(args)
back.Suc(c, "操作成功", bean.ResultLists{
List: list,
Total: total,
})
}
type argsDeny struct {
ManageId uint `json:"manageId" form:"manageId"`
}
// @Style 禁用账号
func (m *Manage) Deny(c *gin.Context) {
args := argsDeny{}
err := tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
if args.ManageId == 0 {
back.Fail(c, "参数错误")
return
}
if err := user2.ManageLogic.Deny(args.ManageId); err != nil {
back.Fail(c, err.Error())
return
}
back.Suc(c, "操作成功", nil)
}
type argsAllow struct {
ManageId uint `json:"manageId" form:"manageId"`
}
// @Style 禁用账号
func (m *Manage) Allow(c *gin.Context) {
args := argsAllow{}
err := tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
if args.ManageId == 0 {
back.Fail(c, "参数错误")
return
}
if err := user2.ManageLogic.Allow(args.ManageId); err != nil {
back.Fail(c, err.Error())
return
}
back.Suc(c, "操作成功", nil)
}
type argsAdd struct {
Name string `json:"name" form:"name"`
Mobile string `json:"mobile" form:"mobile"`
Password string `json:"password" form:"password"`
RoleId uint `json:"roleId" form:"roleId"`
SourceRoleId uint `json:"source_role_id" form:"source_role_id"`
}
// @Style 添加账号
func (m *Manage) Add(c *gin.Context) {
args := argsAdd{}
err := tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
if args.Name == "" || args.Mobile == "" || args.Password == "" || args.RoleId == 0 {
back.Fail(c, "参数错误")
return
}
user, _ := common.GetManageUser(c)
if err = user2.ManageLogic.Add(args.Name, args.Mobile, args.Password, args.RoleId, user.Id, user.Name, args.SourceRoleId); err != nil {
back.Fail(c, err.Error())
return
}
back.Suc(c, "操作成功", nil)
}
type argsEdit struct {
ManageId uint `json:"manageId" form:"manageId"`
argsAdd
}
// @Style 添加账号
func (m *Manage) Edit(c *gin.Context) {
args := argsEdit{}
err := tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
fmt.Println(args.SourceRoleId)
if args.Name == "" || args.Mobile == "" || args.RoleId == 0 || args.ManageId == 0 {
back.Fail(c, "参数错误")
return
}
user, _ := common.GetManageUser(c)
if err = user2.ManageLogic.Edit(args.ManageId, args.Name, args.Mobile, args.Password, args.RoleId, user.Id, user.Name, args.SourceRoleId); err != nil {
back.Fail(c, err.Error())
return
}
back.Suc(c, "操作成功", nil)
}
type mrResp struct {
FileUrl string `json:"file_url"`
}
type recharge struct {
Mobile string `json:"mobile"` //手机号
Balance decimal.Decimal `json:"balance"` //金额
}
type coins struct {
Mobile string `json:"mobile"` //手机号
Coin decimal.Decimal `json:"coin"` //瑞币
}
type rechargeResp struct {
Name string `json:"name"`
FileUrl string `json:"file_url"`
}
type detailMR struct {
Phone string `json:"phone" xlsx:"会员手机号"` //手机号
Balance decimal.Decimal `json:"balance" xlsx:"充值金额"` //充值金额
Status string `json:"status" xlsx:"充值状态"` //成功或失败
}
//会员充值余额接口
func (m *Manage) MemberRecharge(c *gin.Context) {
var p mrResp
if err := tools.Params(&p, c); err != nil {
back.Fail(c, err.Error())
return
}
//读取excel
//打开文件, 文件地址
xlsx, err := excelize.OpenFile(filepath.Join(static_path.Dir.Root, p.FileUrl))
if err != nil {
back.Fail(c, err.Error())
return
}
//获取文件所有行
rows, err1 := xlsx.GetRows(xlsx.GetSheetName(xlsx.GetActiveSheetIndex()))
if err1 != nil {
back.Fail(c, err1.Error())
return
}
result := make([]recharge, 0)
for _, row := range rows[1:] {
one := recharge{
Mobile: row[0],
Balance: decimal.RequireFromString(fmt.Sprintf("%s", row[1])),
}
result = append(result, one)
}
//获取可充值的所有用户及金额
fmt.Println(result)
//记录成功和失败的数据:
var list []detailMR
err = mysql2.Db.Transaction(func(tx *gorm.DB) error {
for _, v := range result {
//遍历内容
var user user3.RecookUserInfoModel
tx.Table(user.TableName()).Where("mobile=?", v.Mobile).First(&user)
//没有找到该用户,则跳过
if user.Id == 0 {
var one = detailMR{
Phone: v.Mobile,
Balance: v.Balance,
Status: "充值失败,未找到该用户",
}
//新增失败记录
list = append(list, one)
continue
} else {
//用户存在
// 给 wall 表充入balance 充钱
var wall user3.RecookUserWalletModel
err = tx.Table(wall.TableName()).Where("user_id=?", user.Id).First(&wall).Error
if err != nil {
return err
}
newBalance := wall.Balance.Add(v.Balance)
err = tx.Table(wall.TableName()).Where("id=?", wall.Id).Update("balance", newBalance).Error
if err != nil {
return err
}
//新增充值记录
info, _ := common.GetManageUser(c)
var wallBalance = user3.RecookUserWalletBalanceListModel{
UserId: user.Id,
IncomeType: 6,
Amount: v.Balance,
Title: "平台给会员充值",
Comment: fmt.Sprintf("平台充值,操作者Id:%v", info.Id),
CreatedAt: formatime.NewSecondNow(),
}
err = tx.Table(wallBalance.TableName()).Create(&wallBalance).Error
if err != nil {
return err
}
var one = detailMR{
Phone: v.Mobile,
Balance: v.Balance,
Status: "充值成功",
}
//新增成功记录
list = append(list, one)
}
}
return nil
})
if err != nil {
back.Fail(c, err.Error())
return
}
fmt.Println(list)
// 创建一个工作表
f := activebit.WriteXlsx("sheet1", list)
// 根据指定路径保存文件
name := time.Now().Format("20060102-150405") + tools.GenerateGoodsHashSign() + ".xlsx"
err = f.SaveAs(filepath.Join(static_path.Dir.Root, static_path.Dir.Temp, name))
if err != nil {
back.Fail(c, err.Error())
return
}
var fileRest = rechargeResp{
Name: name,
FileUrl: filepath.Join(static_path.Dir.Static, static_path.Dir.Temp, name),
}
back.Suc(c, "", fileRest)
}
type detailCoin struct {
Phone string `json:"phone" xlsx:"会员手机号"` //手机号
Coin decimal.Decimal `json:"coin" xlsx:"充值瑞币"` //充值金额
Status string `json:"status" xlsx:"充值状态"` //成功或失败
}
//会员充值瑞币接口
func (m *Manage) MemberRechargeCoin(c *gin.Context) {
var p mrResp
if err := tools.Params(&p, c); err != nil {
back.Fail(c, err.Error())
return
}
//读取excel
//打开文件, 文件地址
xlsx, err := excelize.OpenFile(filepath.Join(static_path.Dir.Root, p.FileUrl))
if err != nil {
back.Fail(c, err.Error())
return
}
//获取文件所有行
rows, err1 := xlsx.GetRows(xlsx.GetSheetName(xlsx.GetActiveSheetIndex()))
if err1 != nil {
back.Fail(c, err1.Error())
return
}
result := make([]coins, 0)
for _, row := range rows[1:] {
one := coins{
Mobile: row[0],
Coin: decimal.RequireFromString(fmt.Sprintf("%s", row[1])),
}
result = append(result, one)
}
//获取可充值的所有用户及瑞币
fmt.Println(result)
//记录成功和失败的数据:
var list []detailCoin
err = mysql2.Db.Transaction(func(tx *gorm.DB) error {
for _, v := range result {
//遍历内容
var user user3.RecookUserInfoModel
tx.Table(user.TableName()).Where("mobile=?", v.Mobile).First(&user)
//没有找到该用户,则跳过
if user.Id == 0 {
var one = detailCoin{
Phone: v.Mobile,
Coin: v.Coin,
Status: "充值失败,未找到该用户",
}
//新增失败记录
list = append(list, one)
continue
} else {
//用户存在
// 给 wall 表充入balance 充钱
var wall user3.RecookUserWalletModel
err = tx.Table(wall.TableName()).Where("user_id=?", user.Id).First(&wall).Error
if err != nil {
return err
}
newCoin := wall.Coin.Add(v.Coin)
err = tx.Table(wall.TableName()).Where("id=?", wall.Id).Update("coin", newCoin).Error
if err != nil {
return err
}
//新增充值记录
info, _ := common.GetManageUser(c)
var wallBalance = coin.RecookCoinHistoryModel{
UserId: user.Id,
CoinType: 10,
CoinNum: v.Coin,
Remark: fmt.Sprintf("平台充值,操作者Id:%v", info.Id),
CreatedAt: formatime.NewSecondNow(),
}
err = tx.Table(wallBalance.TableName()).Create(&wallBalance).Error
if err != nil {
return err
}
var one = detailCoin{
Phone: v.Mobile,
Coin: v.Coin,
Status: "充值成功",
}
//新增成功记录
list = append(list, one)
}
}
return nil
})
if err != nil {
back.Fail(c, err.Error())
return
}
fmt.Println(list)
// 创建一个工作表
f := activebit.WriteXlsx("sheet1", list)
// 根据指定路径保存文件
name := time.Now().Format("20060102-150405") + tools.GenerateGoodsHashSign() + ".xlsx"
err = f.SaveAs(filepath.Join(static_path.Dir.Root, static_path.Dir.Temp, name))
if err != nil {
back.Fail(c, err.Error())
return
}
var fileRest = rechargeResp{
Name: name,
FileUrl: filepath.Join(static_path.Dir.Static, static_path.Dir.Temp, name),
}
back.Suc(c, "", fileRest)
}