product_service.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. mercator "git.xiaoniaokuaiyan.com/qiaozhi/xn-mercator"
  6. "math/rand"
  7. "strings"
  8. "time"
  9. "xiaoniaokuaiyan.com/xiaoniao/dal"
  10. "xiaoniaokuaiyan.com/xiaoniao/entity"
  11. "xiaoniaokuaiyan.com/xiaoniao/util"
  12. )
  13. type ProductService struct {
  14. dal.IProduct
  15. }
  16. func (psrv *ProductService) ProductGet(ids []int) (interface{}, error) {
  17. return psrv.IProduct.Get(ids)
  18. }
  19. func (psrv *ProductService) ProductGetCoordinates(pid []int, coordinates string, cityId int) (interface{}, error) {
  20. //如果不传坐标,则表示返回产品的经纬度
  21. db := util.GetSqlDB()
  22. strSql := "select maps from v_product_city where product_id = ? and city_id = ? limit 1;"
  23. if len(coordinates) == 0 {
  24. if len(pid) > 1 {
  25. return nil, fmt.Errorf("1::产品大于1个")
  26. }
  27. var result string
  28. err := db.Get(&result, strSql, pid[0], cityId)
  29. strSql = "select center from t_city where id = ?"
  30. var center string
  31. err = db.Get(&center, strSql, cityId)
  32. return map[string]interface{}{"center": center, "maps": result}, err
  33. } else {
  34. list := map[int]int{}
  35. for i := range pid {
  36. var saleMode string
  37. db.Get(&saleMode, "select sale_mode from t_product where id = ?", pid[i])
  38. if saleMode != "100" {
  39. continue
  40. }
  41. //如果非邮寄产品 保存到list
  42. list[pid[i]] = 0
  43. var maps string
  44. err := db.Get(&maps, strSql, pid[i], cityId)
  45. if err != nil {
  46. list[pid[i]] = 1
  47. //return nil, fmt.Errorf("2::get maps error pid:%d,cityid:%d", pid[i], cityId)
  48. continue
  49. }
  50. result := mercator.IsPointInRingByStr(coordinates, maps)
  51. if !result {
  52. list[pid[i]] = 2
  53. //err1 = fmt.Errorf("3::超区")
  54. //return false, fmt.Errorf("3::超区")
  55. }
  56. }
  57. return list, nil
  58. }
  59. }
  60. func (psrv *ProductService) ProductGetByCateId(cateIds []int, pager dal.Pager, cityId int, sp dal.SortParam) (interface{}, error) {
  61. return psrv.IProduct.GetByCateId(cateIds, pager, cityId, sp)
  62. }
  63. func (psrv *ProductService) GetProductCate(posType int) (interface{}, error) {
  64. return psrv.IProduct.GetCate(posType)
  65. }
  66. func (psrv *ProductService) GetRecommendProduct(pager dal.Pager, cityId int, sp dal.SortParam) (interface{}, error) {
  67. return psrv.IProduct.GetRecommend(pager, cityId, sp)
  68. }
  69. func (psrv *ProductService) GetTags(level int, cityId int) (interface{}, error) {
  70. return psrv.IProduct.GetTags(level, cityId)
  71. }
  72. func (psrv *ProductService) GetByTags(tags []string, pageIndex, pageSize uint, cityId int) (interface{}, error) {
  73. return psrv.IProduct.GetByTags(tags, pageIndex, pageSize, cityId)
  74. }
  75. // 保存消费者基因类产品协议签署信息
  76. func (psrv *ProductService) SaveConsumerGeneSign(signInfo *entity.ConsumerGeneSign) (interface{}, error) {
  77. db := util.GetWriteSqlDB()
  78. strSql, kvm := util.GenerateInsertSqlFromStruct("t_consumer_gene_sign", signInfo)
  79. sqlResult, err := db.NamedExec(strSql, kvm)
  80. if err != nil {
  81. return nil, err
  82. }
  83. if ra, _ := sqlResult.RowsAffected(); ra <= 0 {
  84. return nil, errors.New("failed to save info")
  85. }
  86. return signInfo, nil
  87. }
  88. func (psrv *ProductService) GetProductAddingCate(cid int) (interface{}, error) {
  89. if cid == 0 {
  90. return psrv.IProduct.GetProductAddingCate()
  91. }
  92. return psrv.IProduct.GetProductByAddingCate(cid)
  93. }
  94. func (psrv *ProductService) AddByDpids(dpids []int) (interface{}, error) {
  95. if len(dpids) <= 0 {
  96. return nil, errors.New("dpids is empty")
  97. }
  98. return psrv.IProduct.AddProductByDpIds(dpids)
  99. }
  100. func (prsv *ProductService) GetTop(n int, cityId int) (interface{}, error) {
  101. return prsv.IProduct.GetTop(n, cityId)
  102. }
  103. // 猜你喜欢 20230105
  104. // 之前选2个 现在选6个20230505
  105. func (prsv *ProductService) GetByGuess(mobile string) (interface{}, error) {
  106. db := util.GetSqlDB()
  107. sql := "select distinct op.product_id from t_order_product op left join t_product p on op.product_id = p.id left join t_order o on op.order_id = o.id where o.mobile = ? and o.retype in ('100','110') and p.is_putaway = 0 and p.is_delete = 0 limit 200 "
  108. ids := []string{}
  109. err := db.Select(&ids, sql, mobile)
  110. //if err != nil {
  111. // return nil, err
  112. //}
  113. //池子id
  114. poolPids := []string{}
  115. randids := map[string]struct{}{}
  116. //if len(ids) >= 2 {
  117. // rand.Seed(time.Now().UnixNano())
  118. // for i := 0; i < 2; i++ {
  119. // randids = append(randids, ids[rand.Intn(len(ids))])
  120. // }
  121. //} else {
  122. // //随机获取两个产品两个产品
  123. // sql = "select id from t_product where is_delete = 0 and is_putaway =0 order by rand() limit 2"
  124. // err = db.Select(&randids, sql)
  125. // //如果len(ids)==1 切randids 都不曾经下单产品 更换其中一个产品
  126. // if len(randids) == 2 && len(ids) == 1 && randids[0] != ids[0] && randids[1] != ids[0] {
  127. // randids[0] = ids[0]
  128. // }
  129. //}
  130. //20230505逻辑,从20里面选6个
  131. if len(ids) < 20 {
  132. sql = "select pid from t_guess_product g left join t_product p on g.pid = p.id where g.pid not in (?) and p.is_putaway = 0 and p.is_delete = 0 limit 20"
  133. db.Select(&poolPids, sql, strings.Join(ids, ","))
  134. end := len(poolPids)
  135. if end > 20-len(ids) {
  136. end = 20 - len(ids)
  137. }
  138. ids = append(ids, poolPids[0:end]...)
  139. }
  140. strids := "0"
  141. if len(ids) < 0 {
  142. return nil, fmt.Errorf("暂无推荐")
  143. }
  144. //最大推荐数 与池子总数取最小
  145. max := 6
  146. if max > len(ids) {
  147. max = len(ids)
  148. }
  149. for {
  150. if len(randids) == max {
  151. break
  152. }
  153. id := ids[rand.Intn(len(ids))]
  154. if _, ok := randids[id]; !ok {
  155. randids[id] = struct{}{}
  156. strids = fmt.Sprintf("%s,%s", strids, id)
  157. }
  158. }
  159. sql = "select t2.*,group_concat(city_id) as cityIds from t_product t2 left join v_product_city t3 on t2.id = t3.product_id where t2.id in (" + strids + ") group by t2.id "
  160. plist := []*entity.ProductDB{}
  161. err = db.Select(&plist, sql)
  162. if err != nil {
  163. return nil, err
  164. }
  165. for _, pitem := range plist {
  166. dps := []entity.DetectProduct{}
  167. db.Select(&dps, "select t1.* from t_detect_product t1 left join t_product_detect_product t2 on t1.id = t2.detect_product_id where t2.product_id = ?", pitem.Id)
  168. pitem.Items = dps
  169. }
  170. return plist, nil
  171. }
  172. // region
  173. func (*ProductService) ZFB_ProductGetByCateId(cateIds []int, pager dal.Pager, cityId int, sp dal.SortParam) (interface{}, error) {
  174. if pager.PageIndex < 1 {
  175. pager.PageIndex = 1
  176. }
  177. var skip = (pager.PageIndex - 1) * pager.PageSize
  178. var (
  179. sortDir = "desc"
  180. sortBy = "is_recommend"
  181. )
  182. if sp.SortDir == 1 {
  183. sortDir = "asc"
  184. }
  185. switch sp.SortBy {
  186. case "SALE_NUM":
  187. sortBy = "sale_num"
  188. case "NEW":
  189. sortBy = "t2.created_at"
  190. case "SUM_UP":
  191. sortBy = "sort_no"
  192. case "PRICE":
  193. sortBy = "price"
  194. }
  195. var strCity = "and city_id =" + fmt.Sprintf("%d", cityId)
  196. if cityId == 0 {
  197. strCity = ""
  198. }
  199. db := util.GetSqlDB()
  200. if len(cateIds) > 0 && cateIds[0] == -3 {
  201. var childCids = []int{}
  202. db.Select(&childCids, "SELECT id from t_product_category WHERE parent_id in(?);", util.IntJoin(cateIds[1:], ","))
  203. cateIds = childCids
  204. }
  205. //strSql := "select t2.*,group_concat(city_id) as cityIds from t_product t2 left join t_product_category_product t1 on t1.product_id = t2.id join t_product_city t3 on t2.id = t3.product_id where t1.cat_id in(?) " + strCity + " and t2.is_putaway = 0 and t2.is_delete = 0 group by t2.id order by " + sortBy + " " + sortDir + ", t2.id desc limit ?,?;"
  206. strSql := "select t2.*,group_concat(city_id) as cityIds from t_product t2 left join t_product_category_product t1 on t1.product_id = t2.id join v_product_city t3 on t2.id = t3.product_id where t1.cat_id in(?) and t2.is_zfb = 1 " + strCity + " and t2.is_putaway = 0 and t2.is_delete = 0 group by t2.id order by " + sortBy + " " + sortDir + ", t2.id desc limit ?,?;"
  207. var firstParam interface{} = util.IntJoin(cateIds, ",")
  208. if len(cateIds) > 0 {
  209. if cateIds[0] == -1 { //促销产品
  210. //strSql = "select t1.*, group_concat(city_id) as cityIds from t_product t1 left join t_product_city t2 on t1.id = t2.product_id where t1.id > ? and price <> market_price and market_price > 0 " + strCity + " and t1.is_putaway = 0 and t1.is_delete = 0 group by t1.id order by " + sortBy + " " + sortDir + ", t1.id desc limit ?,?;"
  211. strSql = "select t1.*, group_concat(city_id) as cityIds from t_product t1 left join v_product_city t2 on t1.id = t2.product_id where t1.id > ? and zfb_price <> market_price and market_price > 0 and t1.is_zfb = 1 " + strCity + " and t1.is_putaway = 0 and t1.is_delete = 0 group by t1.id order by " + sortBy + " " + sortDir + ", t1.id desc limit ?,?;"
  212. firstParam = 0
  213. } else if cateIds[0] == -2 {
  214. //strSql = "select t1.*, group_concat(city_id) as cityIds from t_product t1 left join t_product_city t2 on t1.id = t2.product_id where t1.id > ? " + strCity + " and t1.is_putaway = 0 and t1.is_delete = 0 group by t1.id order by " + sortBy + " " + sortDir + ",t1.id desc limit ?,?"
  215. strSql = "select t1.*, group_concat(city_id) as cityIds from t_product t1 left join v_product_city t2 on t1.id = t2.product_id where t1.id > ? and t1.is_zfb = 1 " + strCity + " and t1.is_putaway = 0 and t1.is_delete = 0 group by t1.id order by " + sortBy + " " + sortDir + ",t1.id desc limit ?,?"
  216. firstParam = 0
  217. }
  218. }
  219. plist := []*entity.ProductDB{}
  220. err := db.Select(&plist, strSql, firstParam, skip, pager.PageSize)
  221. if err != nil {
  222. return nil, err
  223. }
  224. entity.ZFBWash(plist)
  225. for _, pitem := range plist {
  226. dps := []entity.DetectProduct{}
  227. db.Select(&dps, "select t1.* from t_detect_product t1 left join t_product_detect_product t2 on t1.id = t2.detect_product_id where t2.product_id = ?", pitem.Id)
  228. pitem.Items = dps
  229. }
  230. return plist, nil
  231. }
  232. func (*ProductService) ZFB_GetByTags(tags []string, pageIndex, pageSize uint, cityId int) (interface{}, error) {
  233. if pageIndex < 1 {
  234. pageIndex = 1
  235. }
  236. if pageSize < 5 {
  237. pageSize = 5
  238. }
  239. var skip = pageSize * (pageIndex - 1)
  240. var strTag = ""
  241. for _, tag := range tags {
  242. strTag += "'" + tag + "',"
  243. }
  244. strTag = strTag[0 : len(strTag)-1]
  245. strSql := "select distinct t3.* from t_tag t1 inner join t_product_tag t2 on t1.id = t2.tag_id inner join t_product t3 on t2.product_id = t3.id left join v_product_city t4 on t3.id = t4.product_id where t4.city_id = ? and t3.is_zfb = 1 and t3.is_putaway = 0 and is_delete = 0 and t1.tag_name in (" + strTag + ") order by sort_no desc limit ?,?;"
  246. db := util.GetSqlDB()
  247. plist := []*entity.ProductDB{}
  248. err := db.Select(&plist, strSql, cityId, skip, pageSize)
  249. if err != nil {
  250. return nil, err
  251. }
  252. entity.ZFBWash(plist)
  253. for _, pitem := range plist {
  254. dps := []entity.DetectProduct{}
  255. db.Select(&dps, "select t1.* from t_detect_product t1 left join t_product_detect_product t2 on t1.id = t2.detect_product_id where t2.product_id = ?", pitem.Id)
  256. pitem.Items = dps
  257. }
  258. return plist, nil
  259. }
  260. func (*ProductService) ZFB_GetRecommendProduct(pager dal.Pager, cityId int, sp dal.SortParam) (interface{}, error) {
  261. if pager.PageIndex < 1 {
  262. pager.PageIndex = 1
  263. }
  264. if pager.PageSize < 5 {
  265. pager.PageSize = 5
  266. }
  267. var skip = (pager.PageIndex - 1) * pager.PageSize
  268. var (
  269. sortDir = "desc"
  270. sortBy = "sort_no"
  271. )
  272. if sp.SortDir == 1 {
  273. sortDir = "asc"
  274. }
  275. switch sp.SortBy {
  276. case "SALE_NUM":
  277. sortBy = "sale_num"
  278. case "NEW":
  279. sortBy = "t1.created_at"
  280. case "PRICE":
  281. sortBy = "price"
  282. }
  283. strSql := "select * from t_product t1 left join v_product_city t2 on t1.id = t2.product_id where city_id = ? and is_recommend = 1 and is_putaway = 0 and is_delete = 0 and t1.is_zfb = 1 order by " + sortBy + " " + sortDir + ", t1.id desc limit ?,?;"
  284. db := util.GetSqlDB()
  285. plist := []*entity.ProductDB{}
  286. err := db.Select(&plist, strSql, cityId, skip, pager.PageSize)
  287. if err != nil {
  288. return nil, err
  289. }
  290. entity.ZFBWash(plist)
  291. for _, pitem := range plist {
  292. dps := []entity.DetectProduct{}
  293. db.Select(&dps, "select t1.* from t_detect_product t1 left join t_product_detect_product t2 on t1.id = t2.detect_product_id where t2.product_id = ?", pitem.Id)
  294. pitem.Items = dps
  295. }
  296. return plist, nil
  297. }
  298. func (psrv *ProductService) ZFB_ProductGet(ids []int) (interface{}, error) {
  299. p, err := psrv.IProduct.Get(ids)
  300. if err == nil {
  301. entity.ZFBWash(p)
  302. }
  303. return p, err
  304. }
  305. func (prsv *ProductService) ZFB_GetTop(n int, cityId int) (interface{}, error) {
  306. var (
  307. now = time.Now()
  308. startDate = now.Add(-time.Hour * 24 * 90).Format("2006-01-02")
  309. endDate = now.Add(time.Hour * 24).Format("2006-01-02")
  310. )
  311. strSql := `select * from t_product t1 right join(
  312. select tp.* from (
  313. SELECT product_id, count(*) as num from t_order_product where order_id in(
  314. SELECT id from t_order where source='sp_zfb' and actual_payment >0 and (created_at BETWEEN ? and ?) and status not in(1,7,8,9,14) and retype in ('100','110') and is_delete='N'
  315. ) GROUP BY product_id
  316. )tp left JOIN v_product_city tc on tp.product_id = tc.product_id where tc.city_id = ?
  317. ) t2 on t1.id = t2.product_id where is_delete = 0 and is_putaway =0 and t1.is_zfb=1 order by num desc LIMIT ?;`
  318. db := util.GetSqlDB()
  319. plist := []*entity.ProductDB{}
  320. err := db.Select(&plist, strSql, startDate, endDate, cityId, n)
  321. entity.ZFBWash(plist)
  322. return plist, err
  323. }
  324. //endregion