selection.go 11 KB

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