package db import ( "applet/app/admin/md" "applet/app/db/model" "applet/app/utils" "applet/app/utils/logx" "errors" "fmt" "reflect" "strings" "xorm.io/xorm" ) // 递归获取所以下级 func GetCateIds(engine *xorm.Engine, cateList []string, ids []string) []string { var cateIds []string var list []string for _, v := range cateList { ids = append(ids, v) err := engine.Table("goods_category").Where("pid = ?", v).Cols("id").Find(&cateIds) if err != nil { continue } for _, v1 := range cateIds { ids = append(ids, v1) list = append(list, v1) } } if len(list) > 0 { return GetCateIds(engine, list, ids) } return ids } func GetMallGoodsList(engine *xorm.Engine, req *md.MallGoodsListReq) ([]map[string]string, []model.Sku, int64, error) { var whereCondition = "1=1" // 用作where的查询条件 if req.Title != "" { // 按照商品标题查找 whereCondition = whereCondition + " AND title LIKE '%" + req.Title + "%'" } if req.CategoryId != "0" && req.CategoryId != "" { // 类目 var cateIds = GetCateIds(engine, []string{req.CategoryId}, []string{}) whereCondition = whereCondition + " AND category_id IN(" + strings.Join(cateIds, ",") + ")" } if req.Title != "" { whereCondition += " AND title LIKE '%" + req.Title + "%'" } if req.SaleState == "1" { // 销售中 whereCondition = whereCondition + " AND sale_state = 1" } else if req.SaleState == "2" { // 已下架 whereCondition = whereCondition + " AND sale_state = 2" } orderBy := "goods_id DESC" if req.OrderBySate == "" { req.OrderBySate = "desc" } if req.OrderBySate == "desc" { switch req.OrderBy { case "create_desc": orderBy = "create_at DESC" } } if req.OrderBySate == "asc" { switch req.OrderBy { case "create_desc": orderBy = "create_at ASC" } } sql := ` SELECT * FROM goods WHERE %s ORDER BY %s limit %d, %d ` if req.PageId == "0" || req.PageId == "" { req.PageId = "1" } if req.PageSize == "0" || req.PageSize == "" || utils.StrToInt(req.PageSize) > 300 { req.PageSize = "20" } offset := (utils.StrToInt(req.PageId) - 1) * utils.StrToInt(req.PageSize) size := utils.StrToInt(req.PageSize) sql = fmt.Sprintf(sql, whereCondition, orderBy, offset, size) fmt.Printf("----- %s\n", sql) mapArr, err := QueryNativeString(engine, sql) if err != nil { return nil, nil, 0, err } // 统计数据记录数 countSql := "SELECT COUNT(*) as total FROM goods where " + whereCondition fmt.Printf("----- %s\n", countSql) totalMap, err := QueryNativeString(engine, countSql) if err != nil { return nil, nil, 0, err } var goodsIds []string for _, item := range mapArr { goodsIds = append(goodsIds, item["goods_id"]) } var skus []model.Sku err = engine.Table("sku").In("goods_id", goodsIds).Find(&skus) if err != nil { return nil, nil, 0, err } return mapArr, skus, utils.StrToInt64(totalMap[0]["total"]), err } func GetMallGoodsListLeftOnMallSku(engine *xorm.Engine, req *md.MallGoodsListReq) ([]map[string]string, []model.Sku, int64, error) { var whereCondition = "1=1" // 用作where的查询条件 if req.CategoryId != "0" && req.CategoryId != "" { // 类目 var cateIds = GetCateIds(engine, []string{req.CategoryId}, []string{}) whereCondition = whereCondition + " AND mg.category_id IN(" + strings.Join(cateIds, ",") + ")" } if req.Title != "" { whereCondition += " AND mg.title LIKE '%" + req.Title + "%'" } if req.Name != "" { whereCondition += " AND mg.title LIKE '%" + req.Name + "%'" } if req.SaleState == "1" { // 销售中 whereCondition = whereCondition + " AND mg.sale_state = 1" } else if req.SaleState == "2" { // 已下架 whereCondition = whereCondition + " AND mg.sale_state = 2" } if utils.StrToFloat64(req.StartPrice) > 0 { // 价格区间 whereCondition = whereCondition + " AND mg.price >=" + req.StartPrice } if utils.StrToFloat64(req.EndPrice) > 0 { // 价格区间 whereCondition = whereCondition + " AND mg.price <=" + req.EndPrice } if req.OrderBySate == "" { req.OrderBySate = "desc" } orderBy := "mg.create_at DESC" if req.OrderBySate == "desc" { switch req.OrderBy { case "create_desc": orderBy = "mg.create_at DESC" case "profit_desc": orderBy = "mg.profit_rate DESC" case "sale_count_desc": orderBy = "mg.sale_count DESC" case "price_desc": orderBy = "mg.price DESC" } } if req.OrderBySate == "asc" { switch req.OrderBy { case "create_desc": orderBy = "mg.create_at ASC" case "profit_desc": orderBy = "mg.profit_rate ASC" case "sale_count_desc": orderBy = "mg.sale_count ASC" case "price_desc": orderBy = "mg.price ASC" } } sql := ` SELECT mg.* FROM goods AS mg WHERE %s GROUP BY %s limit %d, %d ` if req.PageId == "0" || req.PageId == "" { req.PageId = "1" } if req.PageSize == "0" || req.PageSize == "" || utils.StrToInt(req.PageSize) > 300 { req.PageSize = "20" } offset := (utils.StrToInt(req.PageId) - 1) * utils.StrToInt(req.PageSize) size := utils.StrToInt(req.PageSize) sql = fmt.Sprintf(sql, whereCondition, orderBy, offset, size) fmt.Printf("----- %s\n", sql) mapArr, err := QueryNativeString(engine, sql) if err != nil { return nil, nil, 0, err } // 统计数据记录数 countSql := "SELECT mg.* FROM goods AS mg WHERE " + whereCondition + " GROUP BY mg.goods_id" fmt.Printf("----- %s\n", countSql) totalMap, err := QueryNativeString(engine, countSql) if err != nil { return nil, nil, 0, err } var goodsIds []string for _, item := range mapArr { goodsIds = append(goodsIds, item["goods_id"]) } var skus []model.Sku err = engine.Table("sku").In("goods_id", goodsIds).Find(&skus) if err != nil { return nil, nil, 0, err } var total int64 if totalMap == nil { total = 0 } else { total = int64(len(totalMap)) } return mapArr, skus, total, err } func GetALlMallGoodsList(engine *xorm.Engine) ([]model.Goods, error) { var goodsList []model.Goods //查詢出所有不是"倉庫中"的產品 err := engine.Table("goods").Where("sale_state != 3").Find(&goodsList) if err != nil { return goodsList, err } return goodsList, nil } // MallGoodsFindByParams 通过传入的参数查询数据(多条) func MallGoodsFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.Goods, error) { var m []model.Goods if params["key"] == nil { //查询全部数据 err := Db.Find(&m) if err != nil { return nil, logx.Error(err) } return &m, nil } else { if reflect.TypeOf(params["value"]).Kind() == reflect.Slice { //指定In查询 if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil { return nil, logx.Warn(err) } return &m, nil } else { var query = fmt.Sprintf("%s = ?", params["key"]) err := Db.Where(query, params["value"]).Find(&m) if err != nil { return nil, logx.Error(err) } return &m, nil } } } func UpdateMallGoodsByGoodsId(eg *xorm.Engine, m *model.Goods, columns ...string) error { var ( affected int64 err error ) if columns == nil || len(columns) == 0 { affected, err = eg.Where("goods_id = ?", m.GoodsId).AllCols().Update(m) } else { affected, err = eg.Where("goods_id = ?", m.GoodsId).Cols(columns...).Update(m) } if err != nil { return err } if affected == 0 { return errors.New("更新数据失败,稍后再试") } return nil } func GetMallGoodsById(Db *xorm.Engine, goodsId string) (mm *model.Goods, err error) { var m model.Goods has, err := Db.Where("goods_id=?", goodsId).Get(&m) if has == false || err != nil { return nil, err } return &m, nil } func GetGoodsMore(engine *xorm.Engine, gids []int64) map[int64]model.Goods { skuMap := make(map[int64]model.Goods) var skus []model.Goods err := engine.In("goods_id", gids).Find(&skus) if err != nil { return skuMap } for _, v := range skus { skuMap[v.GoodsId] = v } return skuMap } func GetGoodsSku(eg *xorm.Engine, gid, skuId string) map[string]string { var goods model.Goods eg.Where("goods_id=?", gid).Get(&goods) var sku model.Sku eg.Where("goods_id=? and sku_id=?", gid, skuId).Get(&sku) var data = map[string]string{ "title": goods.Title, "sku": sku.Sku, } return data }