selection.go 10 KB

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