123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- package service
- import (
- "errors"
- "fmt"
- mercator "git.xiaoniaokuaiyan.com/qiaozhi/xn-mercator"
- "math/rand"
- "strings"
- "time"
- "xiaoniaokuaiyan.com/xiaoniao/dal"
- "xiaoniaokuaiyan.com/xiaoniao/entity"
- "xiaoniaokuaiyan.com/xiaoniao/util"
- )
- type ProductService struct {
- dal.IProduct
- }
- func (psrv *ProductService) ProductGet(ids []int) (interface{}, error) {
- return psrv.IProduct.Get(ids)
- }
- func (psrv *ProductService) ProductGetCoordinates(pid []int, coordinates string, cityId int) (interface{}, error) {
- //如果不传坐标,则表示返回产品的经纬度
- db := util.GetSqlDB()
- strSql := "select maps from v_product_city where product_id = ? and city_id = ? limit 1;"
- if len(coordinates) == 0 {
- if len(pid) > 1 {
- return nil, fmt.Errorf("1::产品大于1个")
- }
- var result string
- err := db.Get(&result, strSql, pid[0], cityId)
- strSql = "select center from t_city where id = ?"
- var center string
- err = db.Get(¢er, strSql, cityId)
- return map[string]interface{}{"center": center, "maps": result}, err
- } else {
- list := map[int]int{}
- for i := range pid {
- var saleMode string
- db.Get(&saleMode, "select sale_mode from t_product where id = ?", pid[i])
- if saleMode != "100" {
- continue
- }
- //如果非邮寄产品 保存到list
- list[pid[i]] = 0
- var maps string
- err := db.Get(&maps, strSql, pid[i], cityId)
- if err != nil {
- list[pid[i]] = 1
- //return nil, fmt.Errorf("2::get maps error pid:%d,cityid:%d", pid[i], cityId)
- continue
- }
- result := mercator.IsPointInRingByStr(coordinates, maps)
- if !result {
- list[pid[i]] = 2
- //err1 = fmt.Errorf("3::超区")
- //return false, fmt.Errorf("3::超区")
- }
- }
- return list, nil
- }
- }
- func (psrv *ProductService) ProductGetByCateId(cateIds []int, pager dal.Pager, cityId int, sp dal.SortParam) (interface{}, error) {
- return psrv.IProduct.GetByCateId(cateIds, pager, cityId, sp)
- }
- func (psrv *ProductService) GetProductCate(posType int) (interface{}, error) {
- return psrv.IProduct.GetCate(posType)
- }
- func (psrv *ProductService) GetRecommendProduct(pager dal.Pager, cityId int, sp dal.SortParam) (interface{}, error) {
- return psrv.IProduct.GetRecommend(pager, cityId, sp)
- }
- func (psrv *ProductService) GetTags(level int, cityId int) (interface{}, error) {
- return psrv.IProduct.GetTags(level, cityId)
- }
- func (psrv *ProductService) GetByTags(tags []string, pageIndex, pageSize uint, cityId int) (interface{}, error) {
- return psrv.IProduct.GetByTags(tags, pageIndex, pageSize, cityId)
- }
- // 保存消费者基因类产品协议签署信息
- func (psrv *ProductService) SaveConsumerGeneSign(signInfo *entity.ConsumerGeneSign) (interface{}, error) {
- db := util.GetWriteSqlDB()
- strSql, kvm := util.GenerateInsertSqlFromStruct("t_consumer_gene_sign", signInfo)
- sqlResult, err := db.NamedExec(strSql, kvm)
- if err != nil {
- return nil, err
- }
- if ra, _ := sqlResult.RowsAffected(); ra <= 0 {
- return nil, errors.New("failed to save info")
- }
- return signInfo, nil
- }
- func (psrv *ProductService) GetProductAddingCate(cid int) (interface{}, error) {
- if cid == 0 {
- return psrv.IProduct.GetProductAddingCate()
- }
- return psrv.IProduct.GetProductByAddingCate(cid)
- }
- func (psrv *ProductService) AddByDpids(dpids []int) (interface{}, error) {
- if len(dpids) <= 0 {
- return nil, errors.New("dpids is empty")
- }
- return psrv.IProduct.AddProductByDpIds(dpids)
- }
- func (prsv *ProductService) GetTop(n int, cityId int) (interface{}, error) {
- return prsv.IProduct.GetTop(n, cityId)
- }
- // 猜你喜欢 20230105
- // 之前选2个 现在选6个20230505
- func (prsv *ProductService) GetByGuess(mobile string) (interface{}, error) {
- db := util.GetSqlDB()
- 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 "
- ids := []string{}
- err := db.Select(&ids, sql, mobile)
- //if err != nil {
- // return nil, err
- //}
- //池子id
- poolPids := []string{}
- randids := map[string]struct{}{}
- //if len(ids) >= 2 {
- // rand.Seed(time.Now().UnixNano())
- // for i := 0; i < 2; i++ {
- // randids = append(randids, ids[rand.Intn(len(ids))])
- // }
- //} else {
- // //随机获取两个产品两个产品
- // sql = "select id from t_product where is_delete = 0 and is_putaway =0 order by rand() limit 2"
- // err = db.Select(&randids, sql)
- // //如果len(ids)==1 切randids 都不曾经下单产品 更换其中一个产品
- // if len(randids) == 2 && len(ids) == 1 && randids[0] != ids[0] && randids[1] != ids[0] {
- // randids[0] = ids[0]
- // }
- //}
- //20230505逻辑,从20里面选6个
- if len(ids) < 20 {
- 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"
- db.Select(&poolPids, sql, strings.Join(ids, ","))
- end := len(poolPids)
- if end > 20-len(ids) {
- end = 20 - len(ids)
- }
- ids = append(ids, poolPids[0:end]...)
- }
- strids := "0"
- if len(ids) < 0 {
- return nil, fmt.Errorf("暂无推荐")
- }
- //最大推荐数 与池子总数取最小
- max := 6
- if max > len(ids) {
- max = len(ids)
- }
- for {
- if len(randids) == max {
- break
- }
- id := ids[rand.Intn(len(ids))]
- if _, ok := randids[id]; !ok {
- randids[id] = struct{}{}
- strids = fmt.Sprintf("%s,%s", strids, id)
- }
- }
- 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 "
- plist := []*entity.ProductDB{}
- err = db.Select(&plist, sql)
- if err != nil {
- return nil, err
- }
- for _, pitem := range plist {
- dps := []entity.DetectProduct{}
- 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)
- pitem.Items = dps
- }
- return plist, nil
- }
- // region
- func (*ProductService) ZFB_ProductGetByCateId(cateIds []int, pager dal.Pager, cityId int, sp dal.SortParam) (interface{}, error) {
- if pager.PageIndex < 1 {
- pager.PageIndex = 1
- }
- var skip = (pager.PageIndex - 1) * pager.PageSize
- var (
- sortDir = "desc"
- sortBy = "is_recommend"
- )
- if sp.SortDir == 1 {
- sortDir = "asc"
- }
- switch sp.SortBy {
- case "SALE_NUM":
- sortBy = "sale_num"
- case "NEW":
- sortBy = "t2.created_at"
- case "SUM_UP":
- sortBy = "sort_no"
- case "PRICE":
- sortBy = "price"
- }
- var strCity = "and city_id =" + fmt.Sprintf("%d", cityId)
- if cityId == 0 {
- strCity = ""
- }
- db := util.GetSqlDB()
- if len(cateIds) > 0 && cateIds[0] == -3 {
- var childCids = []int{}
- db.Select(&childCids, "SELECT id from t_product_category WHERE parent_id in(?);", util.IntJoin(cateIds[1:], ","))
- cateIds = childCids
- }
- //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 ?,?;"
- 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 ?,?;"
- var firstParam interface{} = util.IntJoin(cateIds, ",")
- if len(cateIds) > 0 {
- if cateIds[0] == -1 { //促销产品
- //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 ?,?;"
- 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 ?,?;"
- firstParam = 0
- } else if cateIds[0] == -2 {
- //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 ?,?"
- 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 ?,?"
- firstParam = 0
- }
- }
- plist := []*entity.ProductDB{}
- err := db.Select(&plist, strSql, firstParam, skip, pager.PageSize)
- if err != nil {
- return nil, err
- }
- entity.ZFBWash(plist)
- for _, pitem := range plist {
- dps := []entity.DetectProduct{}
- 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)
- pitem.Items = dps
- }
- return plist, nil
- }
- func (*ProductService) ZFB_GetByTags(tags []string, pageIndex, pageSize uint, cityId int) (interface{}, error) {
- if pageIndex < 1 {
- pageIndex = 1
- }
- if pageSize < 5 {
- pageSize = 5
- }
- var skip = pageSize * (pageIndex - 1)
- var strTag = ""
- for _, tag := range tags {
- strTag += "'" + tag + "',"
- }
- strTag = strTag[0 : len(strTag)-1]
- 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 ?,?;"
- db := util.GetSqlDB()
- plist := []*entity.ProductDB{}
- err := db.Select(&plist, strSql, cityId, skip, pageSize)
- if err != nil {
- return nil, err
- }
- entity.ZFBWash(plist)
- for _, pitem := range plist {
- dps := []entity.DetectProduct{}
- 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)
- pitem.Items = dps
- }
- return plist, nil
- }
- func (*ProductService) ZFB_GetRecommendProduct(pager dal.Pager, cityId int, sp dal.SortParam) (interface{}, error) {
- if pager.PageIndex < 1 {
- pager.PageIndex = 1
- }
- if pager.PageSize < 5 {
- pager.PageSize = 5
- }
- var skip = (pager.PageIndex - 1) * pager.PageSize
- var (
- sortDir = "desc"
- sortBy = "sort_no"
- )
- if sp.SortDir == 1 {
- sortDir = "asc"
- }
- switch sp.SortBy {
- case "SALE_NUM":
- sortBy = "sale_num"
- case "NEW":
- sortBy = "t1.created_at"
- case "PRICE":
- sortBy = "price"
- }
- 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 ?,?;"
- db := util.GetSqlDB()
- plist := []*entity.ProductDB{}
- err := db.Select(&plist, strSql, cityId, skip, pager.PageSize)
- if err != nil {
- return nil, err
- }
- entity.ZFBWash(plist)
- for _, pitem := range plist {
- dps := []entity.DetectProduct{}
- 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)
- pitem.Items = dps
- }
- return plist, nil
- }
- func (psrv *ProductService) ZFB_ProductGet(ids []int) (interface{}, error) {
- p, err := psrv.IProduct.Get(ids)
- if err == nil {
- entity.ZFBWash(p)
- }
- return p, err
- }
- func (prsv *ProductService) ZFB_GetTop(n int, cityId int) (interface{}, error) {
- var (
- now = time.Now()
- startDate = now.Add(-time.Hour * 24 * 90).Format("2006-01-02")
- endDate = now.Add(time.Hour * 24).Format("2006-01-02")
- )
- strSql := `select * from t_product t1 right join(
- select tp.* from (
- SELECT product_id, count(*) as num from t_order_product where order_id in(
- 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'
- ) GROUP BY product_id
- )tp left JOIN v_product_city tc on tp.product_id = tc.product_id where tc.city_id = ?
- ) 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 ?;`
- db := util.GetSqlDB()
- plist := []*entity.ProductDB{}
- err := db.Select(&plist, strSql, startDate, endDate, cityId, n)
- entity.ZFBWash(plist)
- return plist, err
- }
- //endregion
|