|
- package svc
-
- import (
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/enum"
- "applet/app/lib/wx"
- "applet/app/md"
- "applet/app/utils"
- "errors"
- "time"
- "xorm.io/xorm"
- )
-
- func StatisticsQrcodeData() (qrcodeTotalNums, waitUseQrcodeNums, alreadyUseQrcodeNums, allowCreateQrcodeNums int64, err error) {
- qrcodeTotalNums = md.QrcodeTotalNums //二维码总量
-
- qrcodeWithBatchRecordsDb := db.QrcodeWithBatchRecordsDb{}
- qrcodeWithBatchRecordsDb.Set()
- qrcodeWithBatchRecordsForUseWait, err := qrcodeWithBatchRecordsDb.FindQrcodeWithBatchRecordsByState(enum.QrcodeWithBatchRecordsStateForWait)
- if err != nil {
- return
- }
- waitUseQrcodeNums = int64(len(qrcodeWithBatchRecordsForUseWait)) //待使用二维码数量
-
- qrcodeWithBatchRecordsForUseAlready, err := qrcodeWithBatchRecordsDb.FindQrcodeWithBatchRecordsByState(enum.QrcodeWithBatchRecordsStateForAlready)
- if err != nil {
- return
- }
- alreadyUseQrcodeNums = int64(len(qrcodeWithBatchRecordsForUseAlready)) //已使用二维码数量
-
- allowCreateQrcodeNums = qrcodeTotalNums - waitUseQrcodeNums //可生成二维码数量
- return
- }
-
- func createQrcodeIndex() string {
- date := utils.Int64ToStr(time.Now().UnixMicro())
- sceneStr := date + "_" + utils.RandString(6) //根据当前时间戳(微秒)+ 随机6位字符串 作为唯一标识符
- return sceneStr
- }
-
- func CreateQrcode(createNums int) (err error) {
- now := time.Now()
- var insertData []*model.Qrcode
- //1、调用微信 `cgi-bin/qrcode/create` 生成带参的永久二维码
- wxOfficial := wx.OfficialAccount{}
- wxOfficial.Set()
- for i := 0; i < createNums; i++ {
- sceneStr := createQrcodeIndex()
- qrcodeUrl, err1 := wxOfficial.QrcodeCreate(sceneStr)
- if err1 != nil {
- return err1
- }
- insertData = append(insertData, &model.Qrcode{
- Url: qrcodeUrl,
- State: enum.QrcodeSateAllowUse,
- Identity: sceneStr,
- CreateAt: now.Format("2006-01-02 15:00:00"),
- UpdateAt: now.Format("2006-01-02 15:00:00"),
- })
- }
- //2、批量新增二维码
- qrcodeDb := db.QrcodeDb{}
- qrcodeDb.Set()
- _, err = qrcodeDb.BatchAddQrcode(insertData)
- return
- }
-
- func OperateQrcode(batchId, totalNums int, args md.QrcodeBatchAddReq, session *xorm.Session) (err error) {
- qrcodeDb := db.QrcodeDb{}
- qrcodeDb.Set()
- //1、获取当前可用二维码
- allowUseQrcodeList, allowUseQrcodeTotal, err := qrcodeDb.FindQrcodeForAllowUse()
- if int(allowUseQrcodeTotal) < totalNums {
- err = errors.New("可用二维码不足")
- return
- }
-
- now := time.Now()
- var insertData []*model.QrcodeWithBatchRecords
- var updateQrcodeIds []int
- var k = 0
- for _, v := range args.List {
- for i := 0; i < v.Num; i++ {
- insertData = append(insertData, &model.QrcodeWithBatchRecords{
- QrcodeId: allowUseQrcodeList[k].Id,
- BatchId: batchId,
- Amount: v.Amount,
- State: enum.QrcodeWithBatchRecordsStateForWait,
- CreateAt: now.Format("2006-01-02 15:00:00"),
- UpdateAt: now.Format("2006-01-02 15:00:00"),
- })
- updateQrcodeIds = append(updateQrcodeIds, allowUseQrcodeList[k].Id)
- k++
- }
- }
-
- //2、新增“二维码-批次”记录
- qrcodeWithBatchRecordsDb := db.QrcodeWithBatchRecordsDb{}
- qrcodeWithBatchRecordsDb.Set()
- if _, err = qrcodeWithBatchRecordsDb.BatchAddQrcodeWithBatchRecordsBySession(session, insertData); err != nil {
- return
- }
-
- //3、修改"二维码状态"为不可用
- _, err = qrcodeDb.BatchUpdateQrcodeBySession(session, updateQrcodeIds, enum.QrcodeSateAllowNotUse)
- return
- }
|