面包店
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.
 
 
 
 
 

278 lines
7.0 KiB

  1. package db
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db/model"
  5. "applet/app/utils"
  6. "applet/app/utils/logx"
  7. "errors"
  8. "fmt"
  9. "reflect"
  10. "strings"
  11. "xorm.io/xorm"
  12. )
  13. // 递归获取所以下级
  14. func GetCateIds(engine *xorm.Engine, cateList []string, ids []string) []string {
  15. var cateIds []string
  16. var list []string
  17. for _, v := range cateList {
  18. ids = append(ids, v)
  19. err := engine.Table("goods_category").Where("pid = ?", v).Cols("id").Find(&cateIds)
  20. if err != nil {
  21. continue
  22. }
  23. for _, v1 := range cateIds {
  24. ids = append(ids, v1)
  25. list = append(list, v1)
  26. }
  27. }
  28. if len(list) > 0 {
  29. return GetCateIds(engine, list, ids)
  30. }
  31. return ids
  32. }
  33. func GetMallGoodsList(engine *xorm.Engine, req *md.MallGoodsListReq) ([]map[string]string, []model.Sku, int64, error) {
  34. var whereCondition = "1=1" // 用作where的查询条件
  35. if req.Title != "" { // 按照商品标题查找
  36. whereCondition = whereCondition + " AND title LIKE '%" + req.Title + "%'"
  37. }
  38. if req.CategoryId != "0" && req.CategoryId != "" { // 类目
  39. var cateIds = GetCateIds(engine, []string{req.CategoryId}, []string{})
  40. whereCondition = whereCondition + " AND category_id IN(" + strings.Join(cateIds, ",") + ")"
  41. }
  42. if req.Title != "" {
  43. whereCondition += " AND title LIKE '%" + req.Title + "%'"
  44. }
  45. if req.SaleState == "1" { // 销售中
  46. whereCondition = whereCondition + " AND sale_state = 1"
  47. } else if req.SaleState == "2" { // 已下架
  48. whereCondition = whereCondition + " AND sale_state = 2"
  49. }
  50. orderBy := "goods_id DESC"
  51. if req.OrderBySate == "" {
  52. req.OrderBySate = "desc"
  53. }
  54. if req.OrderBySate == "desc" {
  55. switch req.OrderBy {
  56. case "create_desc":
  57. orderBy = "create_at DESC"
  58. }
  59. }
  60. if req.OrderBySate == "asc" {
  61. switch req.OrderBy {
  62. case "create_desc":
  63. orderBy = "create_at ASC"
  64. }
  65. }
  66. sql := `
  67. SELECT * FROM goods WHERE %s ORDER BY %s limit %d, %d
  68. `
  69. if req.PageId == "0" || req.PageId == "" {
  70. req.PageId = "1"
  71. }
  72. if req.PageSize == "0" || req.PageSize == "" || utils.StrToInt(req.PageSize) > 300 {
  73. req.PageSize = "20"
  74. }
  75. offset := (utils.StrToInt(req.PageId) - 1) * utils.StrToInt(req.PageSize)
  76. size := utils.StrToInt(req.PageSize)
  77. sql = fmt.Sprintf(sql, whereCondition, orderBy, offset, size)
  78. fmt.Printf("----- %s\n", sql)
  79. mapArr, err := QueryNativeString(engine, sql)
  80. if err != nil {
  81. return nil, nil, 0, err
  82. }
  83. // 统计数据记录数
  84. countSql := "SELECT COUNT(*) as total FROM goods where " + whereCondition
  85. fmt.Printf("----- %s\n", countSql)
  86. totalMap, err := QueryNativeString(engine, countSql)
  87. if err != nil {
  88. return nil, nil, 0, err
  89. }
  90. var goodsIds []string
  91. for _, item := range mapArr {
  92. goodsIds = append(goodsIds, item["goods_id"])
  93. }
  94. var skus []model.Sku
  95. err = engine.Table("sku").In("goods_id", goodsIds).Find(&skus)
  96. if err != nil {
  97. return nil, nil, 0, err
  98. }
  99. return mapArr, skus, utils.StrToInt64(totalMap[0]["total"]), err
  100. }
  101. func GetMallGoodsListLeftOnMallSku(engine *xorm.Engine, req *md.MallGoodsListReq) ([]map[string]string, []model.Sku, int64, error) {
  102. var whereCondition = "1=1" // 用作where的查询条件
  103. if req.CategoryId != "0" && req.CategoryId != "" { // 类目
  104. var cateIds = GetCateIds(engine, []string{req.CategoryId}, []string{})
  105. whereCondition = whereCondition + " AND mg.category_id IN(" + strings.Join(cateIds, ",") + ")"
  106. }
  107. if req.Title != "" {
  108. whereCondition += " AND mg.title LIKE '%" + req.Title + "%'"
  109. }
  110. if req.SaleState == "1" { // 销售中
  111. whereCondition = whereCondition + " AND sale_state = 1"
  112. } else if req.SaleState == "2" { // 已下架
  113. whereCondition = whereCondition + " AND sale_state = 2"
  114. }
  115. if req.OrderBySate == "" {
  116. req.OrderBySate = "desc"
  117. }
  118. orderBy := "mg.create_at DESC"
  119. if req.OrderBySate == "desc" {
  120. switch req.OrderBy {
  121. case "create_desc":
  122. orderBy = "mg.create_at DESC"
  123. case "profit_desc":
  124. orderBy = "mg.profit_rate DESC"
  125. case "sale_count_desc":
  126. orderBy = "mg.sale_count DESC"
  127. }
  128. }
  129. if req.OrderBySate == "asc" {
  130. switch req.OrderBy {
  131. case "create_desc":
  132. orderBy = "mg.create_at ASC"
  133. case "profit_desc":
  134. orderBy = "mg.profit_rate ASC"
  135. case "sale_count_desc":
  136. orderBy = "mg.sale_count ASC"
  137. }
  138. }
  139. sql := `
  140. SELECT mg.* FROM goods AS mg WHERE %s GROUP BY %s limit %d, %d
  141. `
  142. if req.PageId == "0" || req.PageId == "" {
  143. req.PageId = "1"
  144. }
  145. if req.PageSize == "0" || req.PageSize == "" || utils.StrToInt(req.PageSize) > 300 {
  146. req.PageSize = "20"
  147. }
  148. offset := (utils.StrToInt(req.PageId) - 1) * utils.StrToInt(req.PageSize)
  149. size := utils.StrToInt(req.PageSize)
  150. sql = fmt.Sprintf(sql, whereCondition, orderBy, offset, size)
  151. fmt.Printf("----- %s\n", sql)
  152. mapArr, err := QueryNativeString(engine, sql)
  153. if err != nil {
  154. return nil, nil, 0, err
  155. }
  156. // 统计数据记录数
  157. countSql := "SELECT mg.* FROM goods AS mg WHERE " + whereCondition + " GROUP BY mg.goods_id"
  158. fmt.Printf("----- %s\n", countSql)
  159. totalMap, err := QueryNativeString(engine, countSql)
  160. if err != nil {
  161. return nil, nil, 0, err
  162. }
  163. var goodsIds []string
  164. for _, item := range mapArr {
  165. goodsIds = append(goodsIds, item["goods_id"])
  166. }
  167. var skus []model.Sku
  168. err = engine.Table("sku").In("goods_id", goodsIds).Find(&skus)
  169. if err != nil {
  170. return nil, nil, 0, err
  171. }
  172. var total int64
  173. if totalMap == nil {
  174. total = 0
  175. } else {
  176. total = int64(len(totalMap))
  177. }
  178. return mapArr, skus, total, err
  179. }
  180. func GetALlMallGoodsList(engine *xorm.Engine) ([]model.Goods, error) {
  181. var goodsList []model.Goods
  182. //查詢出所有不是"倉庫中"的產品
  183. err := engine.Table("goods").Where("sale_state != 3").Find(&goodsList)
  184. if err != nil {
  185. return goodsList, err
  186. }
  187. return goodsList, nil
  188. }
  189. // MallGoodsFindByParams 通过传入的参数查询数据(多条)
  190. func MallGoodsFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.Goods, error) {
  191. var m []model.Goods
  192. if params["key"] == nil {
  193. //查询全部数据
  194. err := Db.Find(&m)
  195. if err != nil {
  196. return nil, logx.Error(err)
  197. }
  198. return &m, nil
  199. } else {
  200. if reflect.TypeOf(params["value"]).Kind() == reflect.Slice {
  201. //指定In查询
  202. if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil {
  203. return nil, logx.Warn(err)
  204. }
  205. return &m, nil
  206. } else {
  207. var query = fmt.Sprintf("%s = ?", params["key"])
  208. err := Db.Where(query, params["value"]).Find(&m)
  209. if err != nil {
  210. return nil, logx.Error(err)
  211. }
  212. return &m, nil
  213. }
  214. }
  215. }
  216. func UpdateMallGoodsByGoodsId(eg *xorm.Engine, m *model.Goods, columns ...string) error {
  217. var (
  218. affected int64
  219. err error
  220. )
  221. if columns == nil || len(columns) == 0 {
  222. affected, err = eg.Where("goods_id = ?", m.GoodsId).AllCols().Update(m)
  223. } else {
  224. affected, err = eg.Where("goods_id = ?", m.GoodsId).Cols(columns...).Update(m)
  225. }
  226. if err != nil {
  227. return err
  228. }
  229. if affected == 0 {
  230. return errors.New("更新数据失败,稍后再试")
  231. }
  232. return nil
  233. }
  234. func GetMallGoodsById(Db *xorm.Engine, goodsId string) (mm *model.Goods, err error) {
  235. var m model.Goods
  236. has, err := Db.Where("goods_id=?", goodsId).Get(&m)
  237. if has == false || err != nil {
  238. return nil, err
  239. }
  240. return &m, nil
  241. }