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

300 lines
7.6 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 mg.sale_state = 1"
  112. } else if req.SaleState == "2" { // 已下架
  113. whereCondition = whereCondition + " AND mg.sale_state = 2"
  114. }
  115. if utils.StrToFloat64(req.StartPrice) > 0 { // 价格区间
  116. whereCondition = whereCondition + " AND mg.price >=" + req.StartPrice
  117. }
  118. if utils.StrToFloat64(req.EndPrice) > 0 { // 价格区间
  119. whereCondition = whereCondition + " AND mg.price <=" + req.EndPrice
  120. }
  121. if req.OrderBySate == "" {
  122. req.OrderBySate = "desc"
  123. }
  124. orderBy := "mg.create_at DESC"
  125. if req.OrderBySate == "desc" {
  126. switch req.OrderBy {
  127. case "create_desc":
  128. orderBy = "mg.create_at DESC"
  129. case "profit_desc":
  130. orderBy = "mg.profit_rate DESC"
  131. case "sale_count_desc":
  132. orderBy = "mg.sale_count DESC"
  133. case "price_desc":
  134. orderBy = "mg.price DESC"
  135. }
  136. }
  137. if req.OrderBySate == "asc" {
  138. switch req.OrderBy {
  139. case "create_desc":
  140. orderBy = "mg.create_at ASC"
  141. case "profit_desc":
  142. orderBy = "mg.profit_rate ASC"
  143. case "sale_count_desc":
  144. orderBy = "mg.sale_count ASC"
  145. case "price_desc":
  146. orderBy = "mg.price ASC"
  147. }
  148. }
  149. sql := `
  150. SELECT mg.* FROM goods AS mg WHERE %s GROUP BY %s limit %d, %d
  151. `
  152. if req.PageId == "0" || req.PageId == "" {
  153. req.PageId = "1"
  154. }
  155. if req.PageSize == "0" || req.PageSize == "" || utils.StrToInt(req.PageSize) > 300 {
  156. req.PageSize = "20"
  157. }
  158. offset := (utils.StrToInt(req.PageId) - 1) * utils.StrToInt(req.PageSize)
  159. size := utils.StrToInt(req.PageSize)
  160. sql = fmt.Sprintf(sql, whereCondition, orderBy, offset, size)
  161. fmt.Printf("----- %s\n", sql)
  162. mapArr, err := QueryNativeString(engine, sql)
  163. if err != nil {
  164. return nil, nil, 0, err
  165. }
  166. // 统计数据记录数
  167. countSql := "SELECT mg.* FROM goods AS mg WHERE " + whereCondition + " GROUP BY mg.goods_id"
  168. fmt.Printf("----- %s\n", countSql)
  169. totalMap, err := QueryNativeString(engine, countSql)
  170. if err != nil {
  171. return nil, nil, 0, err
  172. }
  173. var goodsIds []string
  174. for _, item := range mapArr {
  175. goodsIds = append(goodsIds, item["goods_id"])
  176. }
  177. var skus []model.Sku
  178. err = engine.Table("sku").In("goods_id", goodsIds).Find(&skus)
  179. if err != nil {
  180. return nil, nil, 0, err
  181. }
  182. var total int64
  183. if totalMap == nil {
  184. total = 0
  185. } else {
  186. total = int64(len(totalMap))
  187. }
  188. return mapArr, skus, total, err
  189. }
  190. func GetALlMallGoodsList(engine *xorm.Engine) ([]model.Goods, error) {
  191. var goodsList []model.Goods
  192. //查詢出所有不是"倉庫中"的產品
  193. err := engine.Table("goods").Where("sale_state != 3").Find(&goodsList)
  194. if err != nil {
  195. return goodsList, err
  196. }
  197. return goodsList, nil
  198. }
  199. // MallGoodsFindByParams 通过传入的参数查询数据(多条)
  200. func MallGoodsFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.Goods, error) {
  201. var m []model.Goods
  202. if params["key"] == nil {
  203. //查询全部数据
  204. err := Db.Find(&m)
  205. if err != nil {
  206. return nil, logx.Error(err)
  207. }
  208. return &m, nil
  209. } else {
  210. if reflect.TypeOf(params["value"]).Kind() == reflect.Slice {
  211. //指定In查询
  212. if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil {
  213. return nil, logx.Warn(err)
  214. }
  215. return &m, nil
  216. } else {
  217. var query = fmt.Sprintf("%s = ?", params["key"])
  218. err := Db.Where(query, params["value"]).Find(&m)
  219. if err != nil {
  220. return nil, logx.Error(err)
  221. }
  222. return &m, nil
  223. }
  224. }
  225. }
  226. func UpdateMallGoodsByGoodsId(eg *xorm.Engine, m *model.Goods, columns ...string) error {
  227. var (
  228. affected int64
  229. err error
  230. )
  231. if columns == nil || len(columns) == 0 {
  232. affected, err = eg.Where("goods_id = ?", m.GoodsId).AllCols().Update(m)
  233. } else {
  234. affected, err = eg.Where("goods_id = ?", m.GoodsId).Cols(columns...).Update(m)
  235. }
  236. if err != nil {
  237. return err
  238. }
  239. if affected == 0 {
  240. return errors.New("更新数据失败,稍后再试")
  241. }
  242. return nil
  243. }
  244. func GetMallGoodsById(Db *xorm.Engine, goodsId string) (mm *model.Goods, err error) {
  245. var m model.Goods
  246. has, err := Db.Where("goods_id=?", goodsId).Get(&m)
  247. if has == false || err != nil {
  248. return nil, err
  249. }
  250. return &m, nil
  251. }
  252. func GetGoodsMore(engine *xorm.Engine, gids []int64) map[int64]model.Goods {
  253. skuMap := make(map[int64]model.Goods)
  254. var skus []model.Goods
  255. err := engine.In("goods_id", gids).Find(&skus)
  256. if err != nil {
  257. return skuMap
  258. }
  259. for _, v := range skus {
  260. skuMap[v.GoodsId] = v
  261. }
  262. return skuMap
  263. }