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

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