selection.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package db
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/issue9/conv"
  8. "github.com/sirupsen/logrus"
  9. "github.com/tidwall/gjson"
  10. "gorm.io/gorm"
  11. "reflect"
  12. "strings"
  13. "youngee_b_api/model/common_model"
  14. "youngee_b_api/model/gorm_model"
  15. "youngee_b_api/model/http_model"
  16. "youngee_b_api/pack"
  17. "youngee_b_api/util"
  18. )
  19. func CreateSelection(ctx context.Context, selectionInfo gorm_model.YounggeeSelectionInfo) error {
  20. db := GetWriteDB(ctx)
  21. err := db.Create(&selectionInfo).Error
  22. if err != nil {
  23. return err
  24. }
  25. return nil
  26. }
  27. func UpdateSelection(ctx context.Context, selectionInfo gorm_model.YounggeeSelectionInfo) error {
  28. db := GetWriteDB(ctx)
  29. whereCondition := gorm_model.YounggeeSelectionInfo{SelectionID: selectionInfo.SelectionID}
  30. err := db.Model(&gorm_model.YounggeeSelectionInfo{}).Where(whereCondition).Updates(selectionInfo).Error
  31. if err != nil {
  32. return err
  33. }
  34. return nil
  35. }
  36. func DeleteSelection(ctx context.Context, SelectionId string) error {
  37. db := GetReadDB(ctx)
  38. err := db.Where("selection_id = ?", SelectionId).Delete(&gorm_model.YounggeeSelectionInfo{}).Error
  39. if err != nil {
  40. return err
  41. }
  42. return nil
  43. }
  44. func GetSelectionById(ctx context.Context, selectionId string) (*gorm_model.YounggeeSelectionInfo, error) {
  45. db := GetWriteDB(ctx)
  46. selectionInfo := gorm_model.YounggeeSelectionInfo{}
  47. whereCondition := gorm_model.YounggeeSelectionInfo{SelectionID: selectionId}
  48. result := db.Where(&whereCondition).First(&selectionInfo)
  49. if result.Error != nil {
  50. if errors.Is(result.Error, gorm.ErrRecordNotFound) {
  51. return nil, nil
  52. } else {
  53. return nil, result.Error
  54. }
  55. }
  56. return &selectionInfo, nil
  57. }
  58. func GetSelectionByEnterpiseIdAndProductId(ctx context.Context, enterpriseId string, productId int) (*gorm_model.YounggeeSelectionInfo, error) {
  59. db := GetWriteDB(ctx)
  60. selectionInfo := gorm_model.YounggeeSelectionInfo{}
  61. whereCondition := gorm_model.YounggeeSelectionInfo{EnterpriseID: enterpriseId, ProductID: productId}
  62. result := db.Where(&whereCondition).First(&selectionInfo)
  63. if result.Error != nil {
  64. if errors.Is(result.Error, gorm.ErrRecordNotFound) {
  65. return nil, nil
  66. } else {
  67. return nil, result.Error
  68. }
  69. }
  70. return &selectionInfo, nil
  71. }
  72. func GetSelectionList(ctx context.Context, enterpriseID string, pageSize, pageNum int64, conditions *common_model.SelectionConditions) ([]*gorm_model.YounggeeSelectionInfo, int64, error) {
  73. db := GetReadDB(ctx)
  74. db = db.Debug().Model(gorm_model.YounggeeSelectionInfo{}).Where("enterprise_id = ?", enterpriseID)
  75. conditionType := reflect.TypeOf(conditions).Elem()
  76. conditionValue := reflect.ValueOf(conditions).Elem()
  77. fmt.Printf("状态内容:%v %v", conditionType, conditionValue)
  78. selectionStatus := ""
  79. searchValue := ""
  80. for i := 0; i < conditionType.NumField(); i++ {
  81. field := conditionType.Field(i)
  82. tag := field.Tag.Get("condition")
  83. fmt.Printf("Tag:%v %v", tag, field.Name)
  84. value := conditionValue.FieldByName(field.Name)
  85. if tag == "selection_status" {
  86. selectionStatus = fmt.Sprintf("%v", conv.MustInt(value.Interface()))
  87. db = db.Where("selection_status = ?", selectionStatus)
  88. } else if tag == "search_value" {
  89. searchValue = fmt.Sprintf("%v", value.Interface())
  90. } else if tag == "submit_at" && value.Interface() != "" {
  91. db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
  92. } else if tag == "task_ddl" && value.Interface() != "" {
  93. db = db.Where(fmt.Sprintf("task_ddl like '%s%%'", value.Interface()))
  94. } else if !util.IsBlank(value) && tag != "task_ddl" && tag != "submit_at" && tag != "search_value" {
  95. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  96. }
  97. }
  98. // 查询总数
  99. var total int64
  100. var selectionInfos []*gorm_model.YounggeeSelectionInfo
  101. if err := db.Count(&total).Error; err != nil {
  102. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  103. return nil, 0, err
  104. }
  105. // 查询该页数据
  106. limit := pageSize
  107. offset := pageSize * pageNum // assert pageNum start with 0\
  108. fmt.Printf("选品状态:%v", selectionStatus)
  109. if selectionStatus == "1" {
  110. err := db.Order("updated_at desc").Limit(int(limit)).Offset(int(offset)).Find(&selectionInfos).Error
  111. if err != nil {
  112. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  113. return nil, 0, err
  114. }
  115. } else {
  116. err := db.Order("task_ddl desc").Limit(int(limit)).Offset(int(offset)).Find(&selectionInfos).Error
  117. if err != nil {
  118. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  119. return nil, 0, err
  120. }
  121. }
  122. var newSelectionInfos []*gorm_model.YounggeeSelectionInfo
  123. for _, v := range selectionInfos {
  124. fmt.Printf("查询选品列表 %+v\n", v)
  125. //v.ProductSnap = conv.MustString(gjson.Get(v.ProductSnap, "ProductName"), "") + " " + conv.MustString(gjson.Get(v.ProductSnap, "ProductPrice"), "")
  126. kuaiShouProductInfo := map[string]interface{}{
  127. "ProductName": conv.MustString(gjson.Get(v.ProductSnap, "ProductName")),
  128. "ProductPrice": conv.MustString(gjson.Get(v.ProductSnap, "ProductPrice")),
  129. }
  130. //kuaiShouProductInfo["ProductName"] = conv.MustString(gjson.Get(v.ProductSnap, "ProductName"))
  131. //kuaiShouProductInfo["ProductPrice"] = conv.MustString(gjson.Get(v.ProductSnap, "ProductPrice"))
  132. jsonData, mapErr := json.Marshal(kuaiShouProductInfo)
  133. if mapErr != nil {
  134. return nil, 0, mapErr
  135. }
  136. v.ProductSnap = string(jsonData)
  137. if searchValue == "" {
  138. newSelectionInfos = append(newSelectionInfos, v)
  139. } else if strings.Contains(v.SelectionID, searchValue) {
  140. newSelectionInfos = append(newSelectionInfos, v)
  141. } else if strings.Contains(v.SelectionName, searchValue) {
  142. newSelectionInfos = append(newSelectionInfos, v)
  143. } else {
  144. total--
  145. }
  146. }
  147. return newSelectionInfos, total, nil
  148. }
  149. func GetSelectionBriefInfo(ctx context.Context, selectionId string) ([]*gorm_model.YounggeeSecBrief, error) {
  150. db := GetReadDB(ctx)
  151. var selectionBriefInfos []*gorm_model.YounggeeSecBrief
  152. err := db.Model(gorm_model.YounggeeSecBrief{}).Where("selection_id = ?", selectionId).Find(&selectionBriefInfos).Error
  153. if err != nil {
  154. logrus.WithContext(ctx).Errorf("[GetSelectionBriefInfo] error query mysql, err:%+v", err)
  155. return nil, err
  156. }
  157. return selectionBriefInfos, nil
  158. }
  159. func GetSelectionExampleInfo(ctx context.Context, selectionId string) ([]*gorm_model.YounggeeSecExample, error) {
  160. db := GetReadDB(ctx)
  161. var selectionExampleInfos []*gorm_model.YounggeeSecExample
  162. err := db.Model(gorm_model.YounggeeSecExample{}).Where("selection_id = ?", selectionId).Find(&selectionExampleInfos).Error
  163. if err != nil {
  164. logrus.WithContext(ctx).Errorf("[GetSelectionExampleInfo] error query, err:%+v", err)
  165. return nil, err
  166. }
  167. return selectionExampleInfos, nil
  168. }
  169. func PaySelection(ctx context.Context, enterpriseId string, payMoney float64, selectionId string) error {
  170. db := GetWriteDB(ctx)
  171. err := db.Transaction(func(tx *gorm.DB) error {
  172. // 1. 冻结账户余额
  173. whereCondition := gorm_model.Enterprise{
  174. EnterpriseID: enterpriseId,
  175. }
  176. updateData := map[string]interface{}{
  177. "frozen_balance": gorm.Expr("frozen_balance + ?", payMoney),
  178. "available_balance": gorm.Expr("available_balance - ?", payMoney)}
  179. if err := tx.Model(gorm_model.Enterprise{}).Where(whereCondition).Updates(updateData).Error; err != nil {
  180. return err
  181. }
  182. // 2. 更新选品项目状态
  183. whereCondition1 := gorm_model.YounggeeSelectionInfo{SelectionID: selectionId, SelectionStatus: 4}
  184. updateData1 := gorm_model.YounggeeSelectionInfo{SelectionStatus: 6}
  185. if err := tx.Model(gorm_model.YounggeeSelectionInfo{}).Where(whereCondition1).Updates(updateData1).Error; err != nil {
  186. return err
  187. }
  188. // 返回 nil 提交事务
  189. return nil
  190. })
  191. if err != nil {
  192. return err
  193. }
  194. return nil
  195. }
  196. func CreateSecBrief(ctx context.Context, briefInfo gorm_model.YounggeeSecBrief) error {
  197. db := GetWriteDB(ctx)
  198. err := db.Create(&briefInfo).Error
  199. if err != nil {
  200. return err
  201. }
  202. return nil
  203. }
  204. func DeleteSecBriefBySelectionId(ctx context.Context, selectionId string) error {
  205. db := GetWriteDB(ctx)
  206. deleteCondition := gorm_model.YounggeeSecBrief{
  207. SelectionID: selectionId,
  208. }
  209. err := db.Where(deleteCondition).Delete(gorm_model.YounggeeSecBrief{}).Error
  210. if err != nil {
  211. return err
  212. }
  213. return nil
  214. }
  215. func CreateSecExample(ctx context.Context, ExampleInfo gorm_model.YounggeeSecExample) error {
  216. db := GetWriteDB(ctx)
  217. err := db.Create(&ExampleInfo).Error
  218. if err != nil {
  219. return err
  220. }
  221. return nil
  222. }
  223. func DeleteSecExampleBySelectionId(ctx context.Context, selectionId string) error {
  224. db := GetWriteDB(ctx)
  225. deleteCondition := gorm_model.YounggeeSecExample{
  226. SelectionID: selectionId,
  227. }
  228. err := db.Where(deleteCondition).Delete(gorm_model.YounggeeSecExample{}).Error
  229. if err != nil {
  230. return err
  231. }
  232. return nil
  233. }
  234. // 更新选品结算金额
  235. func UpdateSelectionSettleMoney(ctx context.Context, selectionID string, payMoney float64) (bool, error) {
  236. db := GetReadDB(ctx)
  237. err := db.Model(gorm_model.YounggeeSelectionInfo{}).Where("selection_id", selectionID).
  238. Updates(map[string]interface{}{"settlement_amount": gorm.Expr("settlement_amount + ?", payMoney)}).Error
  239. if err != nil {
  240. return false, err
  241. }
  242. return true, nil
  243. }
  244. func GetSelectionInfoList(ctx context.Context, pageNum, pageSize int64, conditions http_model.SelectionSquareCondition) ([]*http_model.SelectionBriefInfo, int64, error) {
  245. db := GetReadDB(ctx)
  246. db = db.Debug().Model(gorm_model.YounggeeSelectionInfo{}).Where("selection_status>2")
  247. fmt.Println("conditions: ", conditions)
  248. conditionType := reflect.TypeOf(&conditions).Elem()
  249. conditionValue := reflect.ValueOf(&conditions).Elem()
  250. for i := 0; i < conditionType.NumField(); i++ {
  251. field := conditionType.Field(i)
  252. tag := field.Tag.Get("condition")
  253. value := conditionValue.FieldByName(field.Name)
  254. if tag == "product_type" {
  255. continue
  256. } else {
  257. if value.Interface().(int16) != 0 {
  258. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  259. }
  260. }
  261. }
  262. // 查询总数
  263. var total int64
  264. var selectionInfos []*gorm_model.YounggeeSelectionInfo
  265. if err := db.Count(&total).Error; err != nil {
  266. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  267. return nil, 0, err
  268. }
  269. limit := pageSize
  270. offset := pageSize * pageNum // assert pageNum start with 0\
  271. tempList := []*gorm_model.YounggeeSelectionInfo{}
  272. if conditions.ProductType == 0 {
  273. // 查询该页数据
  274. err := db.Order("task_ddl desc").Limit(int(limit)).Offset(int(offset)).Find(&selectionInfos).Error
  275. if err != nil {
  276. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  277. return nil, 0, err
  278. }
  279. tempList = selectionInfos
  280. } else {
  281. err := db.Order("task_ddl desc").Find(&selectionInfos).Error
  282. if err != nil {
  283. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  284. return nil, 0, err
  285. }
  286. total = 0
  287. var num int64 = 0
  288. //fmt.Println("offset: ", offset)
  289. for _, v := range selectionInfos {
  290. var p gorm_model.YounggeeProduct
  291. _ = json.Unmarshal([]byte(v.ProductSnap), &p)
  292. if p.ProductType == int64(conditions.ProductType) {
  293. total++
  294. //fmt.Println("total++", total)
  295. if total > offset && num < pageSize {
  296. //fmt.Println("num++", num)
  297. num++
  298. tempList = append(tempList, v)
  299. }
  300. }
  301. }
  302. }
  303. newSelectionInfos := pack.GormSelectionListToSelectionBriefInfoList(tempList)
  304. return newSelectionInfos, total, nil
  305. }
  306. // UpdateSelectionNum 更新带货任务已发货人数
  307. func UpdateSelectionNum(ctx context.Context, selectionID string) error {
  308. db := GetWriteDB(ctx)
  309. err := db.Model(gorm_model.YounggeeSelectionInfo{}).Where("selection_id = ?", selectionID).Updates(
  310. map[string]interface{}{"delivery_num": gorm.Expr("delivery_num + ?", 1)}).Error
  311. if err != nil {
  312. return err
  313. }
  314. err = db.Model(gorm_model.YounggeeSelectionInfo{}).Where("selection_id = ?", selectionID).Updates(
  315. map[string]interface{}{"after_delivery_num": gorm.Expr("after_delivery_num - ?", 1)}).Error
  316. if err != nil {
  317. return err
  318. }
  319. return nil
  320. }