Преглед на файлове

add 方案管理

master
DengBiao преди 8 месеца
родител
ревизия
286bc421f5
променени са 16 файла, в които са добавени 1110 реда и са изтрити 24 реда
  1. +19
    -0
      app/admin/enum/enum_scheme.go
  2. +12
    -11
      app/admin/hdl/hdl_enterprise.go
  3. +274
    -0
      app/admin/hdl/hdl_scheme.go
  4. +11
    -9
      app/admin/md/md_enterprise.go
  5. +44
    -0
      app/admin/md/md_scheme.go
  6. +90
    -0
      app/admin/svc/goods/svc_goods_list.go
  7. +334
    -0
      app/admin/svc/svc_scheme.go
  8. +68
    -0
      app/db/db_scheme.go
  9. +89
    -0
      app/db/db_scheme_with_enterprise.go
  10. +52
    -0
      app/db/db_scheme_with_goods.go
  11. +72
    -0
      app/db/db_scheme_with_sku.go
  12. +10
    -0
      app/db/model/scheme.go
  13. +7
    -0
      app/db/model/scheme_with_enterprise.go
  14. +7
    -0
      app/db/model/scheme_with_goods.go
  15. +9
    -0
      app/db/model/scheme_with_sku.go
  16. +12
    -4
      app/router/admin_router.go

+ 19
- 0
app/admin/enum/enum_scheme.go Целия файл

@@ -0,0 +1,19 @@
package enum

type SchemeState int32

const (
SchemeStateForNormal = 1
SchemeStateForFreeze = 2
)

func (gt SchemeState) String() string {
switch gt {
case SchemeStateForNormal:
return "正常"
case SchemeStateForFreeze:
return "冻结"
default:
return "未知"
}
}

+ 12
- 11
app/admin/hdl/hdl_enterprise.go Целия файл

@@ -37,8 +37,7 @@ func EnterpriseList(c *gin.Context) {
engine.And("company.lead_name like ?", "%"+req.LeadName+"%")
}
var list []db.EnterpriseWithCompany
count, err := engine.Join("LEFT", "grade", "enterprise.id = grade.enterprise_id").
Join("LEFT", "class", "class.grade_id = grade.id").
count, err := engine.Join("LEFT", "company", "enterprise.company_id = company.id").
FindAndCount(&list)
if err != nil {
e.OutErr(c, e.ERR_DB_ORM, err.Error())
@@ -48,18 +47,20 @@ func EnterpriseList(c *gin.Context) {
var result []md.EnterpriseListResp
for _, v := range list {
result = append(result, md.EnterpriseListResp{
Id: v.Enterprise.Id,
State: v.Enterprise.State,
Name: v.Enterprise.Name,
Memo: v.Enterprise.Memo,
LeadName: v.Company.LeadName,
LeadPhone: v.Company.LeadPhone,
CreateAt: v.Enterprise.CreateAt,
UpdateAt: v.Enterprise.UpdateAt,
Id: v.Enterprise.Id,
State: v.Enterprise.State,
Name: v.Enterprise.Name,
Memo: v.Enterprise.Memo,
CompanyName: v.Company.Name,
CompanyId: v.Company.Id,
LeadName: v.Company.LeadName,
LeadPhone: v.Company.LeadPhone,
CreateAt: v.Enterprise.CreateAt,
UpdateAt: v.Enterprise.UpdateAt,
})
}
e.OutSuc(c, map[string]interface{}{
"list": list,
"list": result,
"total": count,
"state": []map[string]interface{}{
{


+ 274
- 0
app/admin/hdl/hdl_scheme.go Целия файл

@@ -0,0 +1,274 @@
package hdl

import (
"applet/app/admin/enum"
"applet/app/admin/lib/validate"
"applet/app/admin/md"
"applet/app/admin/svc"
svc2 "applet/app/admin/svc/category"
"applet/app/admin/svc/goods"
"applet/app/db"
"applet/app/db/model"
"applet/app/e"
"applet/app/utils"
"github.com/gin-gonic/gin"
"time"
)

func SchemeList(c *gin.Context) {
schemeDb := db.SchemeDb{}
schemeDb.Set()
schemeList, err := schemeDb.FindScheme()
if err != nil {
e.OutErr(c, e.ERR_DB_ORM, err.Error())
return
}

schemeEnterpriseDb := db.SchemeEnterpriseDb{}
schemeEnterpriseDb.Set()
enterpriseDb := db.EnterpriseDb{}
enterpriseDb.Set()
var result []*md.SchemeListResp
for _, v := range *schemeList {
var temp md.SchemeListResp
temp.Data = v
schemeWithEnterprises, err1 := schemeEnterpriseDb.FindSchemeEnterprise(v.Id)
if err1 != nil {
e.OutErr(c, e.ERR_DB_ORM, err1.Error())
return
}
for _, schemeWithEnterprise := range *schemeWithEnterprises {
enterprise, err2 := enterpriseDb.GetEnterprise(schemeWithEnterprise.EnterpriseId)
if err2 != nil {
e.OutErr(c, e.ERR_DB_ORM, err2.Error())
return
}
temp.EnterpriseList = append(temp.EnterpriseList, struct {
Name string `json:"name"`
}{
Name: enterprise.Name,
})
}
result = append(result, &temp)
}
e.OutSuc(c, map[string]interface{}{
"list": result,
"state": []map[string]interface{}{
{
"name": enum.SchemeState(enum.SchemeStateForNormal).String(),
"value": enum.SchemeStateForNormal,
},
{
"name": enum.SchemeState(enum.SchemeStateForFreeze).String(),
"value": enum.SchemeStateForFreeze,
},
},
}, nil)
return
}

func AddScheme(c *gin.Context) {
var req md.AddSchemeReq
err := c.ShouldBindJSON(&req)
if err != nil {
err = validate.HandleValidateErr(err)
err1 := err.(e.E)
e.OutErr(c, err1.Code, err1.Error())
return
}
schemeDb := db.SchemeDb{}
schemeDb.Set()
now := time.Now()
_, err = schemeDb.SchemeInsert(&model.Scheme{
Name: req.Name,
State: enum.SchemeStateForNormal,
Memo: req.Memo,
CreateAt: now.Format("2006-01-02 15:04:05"),
UpdateAt: now.Format("2006-01-02 15:04:05"),
})
if err != nil {
e.OutErr(c, e.ERR_DB_ORM, err.Error())
return
}

e.OutSuc(c, "success", nil)
return
}

func UpdateScheme(c *gin.Context) {
var req md.UpdateSchemeReq
err := c.ShouldBindJSON(&req)
if err != nil {
err = validate.HandleValidateErr(err)
err1 := err.(e.E)
e.OutErr(c, err1.Code, err1.Error())
return
}
schemeDb := db.SchemeDb{}
schemeDb.Set()
scheme, err := schemeDb.GetScheme(req.Id)
if err != nil {
e.OutErr(c, e.ERR_DB_ORM, err.Error())
return
}
if scheme == nil {
e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
return
}
scheme.Name = req.Name
scheme.Memo = req.Memo
_, err = schemeDb.UpdateScheme(scheme, "name", "memo")
if err != nil {
e.OutErr(c, e.ERR_DB_ORM, err.Error())
return
}
e.OutSuc(c, "success", nil)
return
}

func SchemeBindEnterprise(c *gin.Context) {
var req md.SchemeBindEnterpriseReq
err := c.ShouldBindJSON(&req)
if err != nil {
err = validate.HandleValidateErr(err)
err1 := err.(e.E)
e.OutErr(c, err1.Code, err1.Error())
return
}
err = svc.SchemeBindEnterprise(req)
if err != nil {
e.OutErr(c, e.ERR, err.Error())
return
}

e.OutSuc(c, "success", nil)
return
}

func UpdateSchemeState(c *gin.Context) {
var req md.UpdateSchemeStateReq
err := c.ShouldBindJSON(&req)
if err != nil {
err = validate.HandleValidateErr(err)
err1 := err.(e.E)
e.OutErr(c, err1.Code, err1.Error())
return
}
schemeDb := db.SchemeDb{}
schemeDb.Set()
scheme, err := schemeDb.GetScheme(req.Id)
if err != nil {
e.OutErr(c, e.ERR_DB_ORM, err.Error())
return
}
if scheme == nil {
e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
return
}
scheme.State = req.State
_, err = schemeDb.UpdateScheme(scheme, "state")
if err != nil {
e.OutErr(c, e.ERR_DB_ORM, err.Error())
return
}
e.OutSuc(c, "success", nil)
return
}

func DeleteScheme(c *gin.Context) {
id := c.Param("id")
schemeDb := db.SchemeDb{}
schemeDb.Set()
scheme, err := schemeDb.GetScheme(utils.StrToInt(id))
if err != nil {
e.OutErr(c, e.ERR_DB_ORM, err.Error())
return
}
if scheme == nil {
e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
return
}

err = svc.DeleteScheme(utils.StrToInt(id))
if err != nil {
e.OutErr(c, e.ERR, err.Error())
return
}

e.OutSuc(c, "success", nil)
return
}

func GoodsDetail(c *gin.Context) {
goodsId := c.Query("goods_id")
if goodsId == "" {
goodsId = "0"
}
schemeId := c.Query("scheme_id")
if schemeId == "" {
schemeId = "0"
}
resp, err := svc.MallGoodsDetailByScheme(c, utils.StrToInt64(goodsId), utils.StrToInt(schemeId))
if err != nil {
e.OutErr(c, 400, err.Error())
return
}
e.OutSuc(c, resp, nil)
return
}

func SaveSchemeGoods(c *gin.Context) {
var req md.SaveSchemeGoodsReq
err := c.ShouldBindJSON(&req)
if err != nil {
err = validate.HandleValidateErr(err)
err1 := err.(e.E)
e.OutErr(c, err1.Code, err1.Error())
return
}

err = svc.SaveSchemeGoods(req)
if err != nil {
e.OutErr(c, e.ERR, err.Error())
return
}

e.OutSuc(c, "success", nil)
return
}

func GoodsList(c *gin.Context) {
var args md.MallGoodsListReq
args.Title = c.Query("title")
args.CategoryId = c.Query("category_id")
args.SaleState = c.Query("sale_state")
args.PageId = c.Query("page")
args.PageSize = c.Query("limit")
args.SkuCode = c.Query("sku_code")
tree, err := svc2.GetAllCategory(c)
data, total, err := goods.GetMallGoodsList(c, &args)
if err != nil {
e.OutErr(c, e.ERR_NO_DATA, err)
return
}
var returnData = map[string]interface{}{
"total": total,
"cateList": tree,
// "dataList": data,
//-1审核拒绝 0审核中 1销售中 2已售罄 3放入仓库 4下架
"stateList": []map[string]interface{}{
{"name": "全部", "value": 99},
{"name": "销售中", "value": 1},
{"name": "已下架", "value": 2},
},
}

if data == nil {
returnData["dataList"] = []string{}
} else {
returnData["dataList"] = data
}

e.OutSuc(c, returnData, nil)
return
}

+ 11
- 9
app/admin/md/md_enterprise.go Целия файл

@@ -3,21 +3,23 @@ package md
type EnterpriseListReq struct {
Limit int `json:"limit"`
Page int `json:"page" `
Name string `json:"name" binding:"required" label:"名称"`
Name string `json:"name" label:"名称"`
Memo string `json:"memo" label:"备注"`
LeadName string `json:"lead_name" label:"负责人姓名"`
LeadPhone string `json:"lead_phone" label:"负责人手机号"`
}

type EnterpriseListResp struct {
Id int `json:"id" binding:"required" label:"公司id"`
State int32 `json:"state" label:"状态"`
Name string `json:"name" binding:"required" label:"名称"`
Memo string `json:"memo" label:"备注"`
LeadName string `json:"lead_name" label:"负责人姓名"`
LeadPhone string `json:"lead_phone" label:"负责人手机号"`
CreateAt string `json:"create_at" label:"创建时间"`
UpdateAt string `json:"update_at" label:"更新时间"`
Id int `json:"id" binding:"required" label:"公司id"`
State int32 `json:"state" label:"状态"`
Name string `json:"name" binding:"required" label:"名称"`
Memo string `json:"memo" label:"备注"`
CompanyName string `json:"company_name" label:"公司名称"`
CompanyId int `json:"company_id" label:"公司Id"`
LeadName string `json:"lead_name" label:"负责人姓名"`
LeadPhone string `json:"lead_phone" label:"负责人手机号"`
CreateAt string `json:"create_at" label:"创建时间"`
UpdateAt string `json:"update_at" label:"更新时间"`
}

type EnterpriseUpdateStateReq struct {


+ 44
- 0
app/admin/md/md_scheme.go Целия файл

@@ -0,0 +1,44 @@
package md

import (
"applet/app/db/model"
)

type SchemeListResp struct {
Data model.Scheme `json:"data"`
EnterpriseList []struct {
Name string `json:"name"`
} `json:"enterprise_list"`
}

type UpdateSchemeStateReq struct {
Id int `json:"id" binding:"required" label:"id"`
State int `json:"state" binding:"required" label:"状态"`
}

type AddSchemeReq struct {
Name string `json:"name" binding:"required" label:"名称"`
Memo string `json:"memo" binding:"required" label:"备注"`
}

type UpdateSchemeReq struct {
Id int `json:"id" binding:"required" label:"id"`
Name string `json:"name" binding:"required" label:"名称"`
Memo string `json:"memo" binding:"required" label:"备注"`
}

type SchemeBindEnterpriseReq struct {
Id int `json:"id" binding:"required" label:"id"`
EnterpriseIds []int `json:"enterprise_ids" label:"单位id"`
}

type SaveSchemeGoodsReq struct {
SchemeId int `json:"scheme_id" binding:"required" label:"id"`
GoodsId int64 `json:"goods_id"` // 商品id
SkuList []SaveSchemeSku `json:"sku_list"`
}

type SaveSchemeSku struct {
SkuId int64 `json:"sku_id"`
Price string `json:"price"` // 商品id
}

+ 90
- 0
app/admin/svc/goods/svc_goods_list.go Целия файл

@@ -71,6 +71,96 @@ func GetMallGoodsList(c *gin.Context, req *md.MallGoodsListReq) (interface{}, in
return goodsListResp, total, err
}

// 获取列表数据(给方案用)
//func GetMallGoodsListForScheme(c *gin.Context, req *md.MallGoodsListReq, schemeId int) (interface{}, int64, error) {
// schemeDb := db.SchemeDb{}
// schemeDb.Set()
// schemeInfo, err := schemeDb.GetScheme(schemeId)
// if err != nil {
// return nil, 0, err
// }
// if schemeInfo == nil {
// return nil, 0, errors.New("未查询到相应方案记录")
// }
//
// var goodsListResp []md.MallGoodListResp
// // 获取分库链接
// engine := db.Db
// // 查询goods数据表获取数据
// mallGoodsList, skus, total, err := db.GetMallGoodsListLeftOnMallSku(engine, req)
// if err != nil {
// return nil, 0, err
// }
// var tmpMap interface{}
// scheme, host, subDomain, moreSubDomain := svc.ImageBucketNew(c)
// if err != nil {
// return nil, 0, err
// }
//
// //获取方案中sku的价格
// var skuIds []string
// for _, v := range skus {
// skuIds = append(skuIds, utils.Int64ToStr(v.SkuId))
// }
//
// var schemeWithSkusMap = map[int]*model.SchemeWithSku{}
// if len(skuIds) > 0 {
// var schemeWithSkus []*model.SchemeWithSku
// err = engine.Where("1 = 1").And(fmt.Sprintf("sku_id In (%s)", strings.Join(skuIds, ","))).Find(&schemeWithSkus)
// if err != nil {
// return nil, 0, err
// }
// for _, v := range schemeWithSkus {
// schemeWithSkusMap[v.SkuId] = v
// }
// }
//
// for _, item := range mallGoodsList {
// var goods md.MallGoodListResp
// goods.GoodsId = item["goods_id"]
// goods.SaleState = utils.StrToInt(item["sale_state"])
// goods.Sales = utils.StrToInt(item["sales"])
// goods.CreateAt = item["create_at"]
//
// // 商品主图
// if item["image_list"] != "" {
// utils.Unserialize([]byte(item["image_list"]), &tmpMap)
// var imageListUrlTmp []string
// for _, v := range tmpMap.([]interface{}) {
// imageListUrlTmp = append(imageListUrlTmp, svc.ImageFormatWithBucketNew(scheme, host, subDomain, moreSubDomain, v.(string)))
// }
// if imageListUrlTmp != nil {
// goods.Image = imageListUrlTmp[0]
// }
// }
//
// goods.UpdateAt = item["update_at"]
// goods.Title = item["title"]
//
// // 状态
// switch utils.StrToInt64(item["sale_state"]) {
// case 1.00:
// goods.SaleStateText = "銷售中"
// break
// case 2.00:
// goods.SaleStateText = "下架"
// break
// }
//
// // sku
// for _, skuItem := range skus {
// if skuItem.GoodsId == utils.StrToInt64(item["goods_id"]) {
// goods.SkuList = append(goods.SkuList, skuItem)
// }
// }
// // 获取价格、库存、重量范围、利润空间
// goods.Price = item["price"]
// goodsListResp = append(goodsListResp, goods)
// }
//
// return goodsListResp, total, err
//}

func CheckSkuCode(c *gin.Context, skuCode, goodsId string) (bool, error) {
engine := db.Db
isHas, _, err := db.GetMallSkuBySkuCode(engine, skuCode, goodsId)


+ 334
- 0
app/admin/svc/svc_scheme.go Целия файл

@@ -0,0 +1,334 @@
package svc

import (
"applet/app/admin/md"
"applet/app/db"
"applet/app/db/model"
"applet/app/e"
"applet/app/svc"
"applet/app/utils"
"applet/app/utils/logx"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"reflect"
"strings"
)

func SchemeBindEnterprise(req md.SchemeBindEnterpriseReq) (err error) {
session := db.Db.NewSession()
defer session.Close()
session.Begin()
//1、查询 `scheme`
schemeDb := db.SchemeDb{}
schemeDb.Set()
scheme, err := schemeDb.GetScheme(req.Id)
if err != nil {
return
}
if scheme == nil {
return errors.New("未查询到相应记录")
}

//1、删除 `scheme_with_enterprise`
schemeEnterpriseDb := db.SchemeEnterpriseDb{}
schemeEnterpriseDb.Set()
_, err = schemeEnterpriseDb.SchemeEnterpriseDeleteBySession(session, req.Id)
if err != nil {
_ = session.Rollback()
return
}

//2、新增 `scheme_with_enterprise``
var mm []*model.SchemeWithEnterprise
for _, v := range req.EnterpriseIds {
mm = append(mm, &model.SchemeWithEnterprise{
SchemeId: scheme.Id,
EnterpriseId: v,
})
}
_, err = schemeEnterpriseDb.BatchAddSchemeEnterpriseBySession(session, mm)
if err != nil {
_ = session.Rollback()
return
}

return session.Commit()
}

func DeleteScheme(id int) (err error) {
session := db.Db.NewSession()
defer session.Close()
session.Begin()

//1、删除 `scheme`
schemeDb := db.SchemeDb{}
schemeDb.Set()
_, err = schemeDb.SchemeDeleteBySession(session, id)
if err != nil {
_ = session.Rollback()
return
}

//2、删除 `scheme_with_enterprise`
schemeEnterpriseDb := db.SchemeEnterpriseDb{}
schemeEnterpriseDb.Set()
_, err = schemeEnterpriseDb.SchemeEnterpriseDeleteBySession(session, id)
if err != nil {
_ = session.Rollback()
return
}

return session.Commit()
}

func MallGoodsDetailByScheme(c *gin.Context, id int64, schemeId int) (map[string]interface{}, error) {
schemeDb := db.SchemeDb{}
schemeDb.Set()
schemeInfo, err := schemeDb.GetScheme(schemeId)
if err != nil {
return nil, err
}
if schemeInfo == nil {
return nil, errors.New("未查询到相应方案记录")
}

resp := make(map[string]interface{})
var skuList []*model.Sku
var goodsModel *model.Goods

// 新增商品的时候默认数据
if id == 0 {
resp["base_goods"] = nil
resp["sku"] = nil
return resp, nil
}

goodsModelInterface, has, err := db.GetComm(db.Db, &model.Goods{GoodsId: int64(id)})
if !has {
return nil, errors.New("当前商品不存在")
}
if err != nil {
_ = logx.Error(err)
return nil, e.NewErr(e.ERR_DB_ORM, fmt.Sprintf("查询商品失败:%#v", err))
}
goodsModel = goodsModelInterface.(*model.Goods)

err = db.Db.Where("goods_id=?", id).Find(&skuList)
if err != nil || len(skuList) == 0 {
_ = logx.Error(err)
return nil, e.NewErr(e.ERR_DB_ORM, fmt.Sprintf("查询商品规格失败:%#v\n", err))
}

var goodsMap map[string]interface{}
utils.Unserialize([]byte(utils.SerializeStr(goodsModel)), &goodsMap)
goodsMap["goods_id"] = utils.AnyToString(goodsModel.GoodsId)
// 分类名称
if goodsModel != nil && goodsModel.CategoryId != 0 {
categoryInterface, has, err := db.GetComm(db.Db, &model.GoodsCategory{Id: goodsModel.CategoryId})
if err != nil {
return nil, errors.New("查询商品类目失败")
}
categoryModel := categoryInterface.(*model.GoodsCategory)
if !has {
goodsMap["category_name"] = ""
} else {
goodsMap["category_name"] = categoryModel.Name
}
}

// **处理各复合结构**
var tmpMap interface{}

scheme, host, subDomain, moreSubDomain := svc.ImageBucketNew(c)

// 商品主图
if goodsMap["image_list"] != nil && goodsMap["image_list"] != "null" {
utils.Unserialize([]byte(goodsMap["image_list"].(string)), &tmpMap)
goodsMap["image_list"] = tmpMap
var imageListUrlTmp []string
for _, v := range goodsMap["image_list"].([]interface{}) {
imageListUrlTmp = append(imageListUrlTmp, svc.ImageFormatWithBucketNew(scheme, host, subDomain, moreSubDomain, v.(string)))
}
goodsMap["image_list_url"] = imageListUrlTmp
} else {
goodsMap["image_list"] = []struct{}{}
goodsMap["image_list_url"] = []struct{}{}
}

// 详情图
if goodsMap["detail"] != nil && goodsMap["detail"] != "null" {
var tmpDetailImageList []interface{}
utils.Unserialize([]byte(goodsMap["detail"].(string)), &tmpDetailImageList)
goodsMap["detail"] = tmpDetailImageList
var detailImageUrlTmp []string
for _, v := range goodsMap["detail"].([]interface{}) {
detailImageUrlTmp = append(detailImageUrlTmp, svc.ImageFormatWithBucketNew(scheme, host, subDomain, moreSubDomain, v.(string)))
}
goodsMap["detail_url"] = detailImageUrlTmp
}

if goodsMap["detail"] == nil || reflect.ValueOf(goodsMap["detail"]).IsNil() {
goodsMap["detail"] = []interface{}{}
goodsMap["detail_url"] = []interface{}{}
}

// 规格属性
speJson, ok := goodsMap["spe"].(string)
if ok {
if goodsMap["spe"] != nil && speJson != "" && speJson != "null" {
utils.Unserialize([]byte(speJson), &tmpMap)
goodsMap["spe"] = tmpMap
} else {
utils.Unserialize([]byte("[{\"name\": \"默认\",\"values\": [\"默认\"]}]"), &tmpMap)
goodsMap["spe"] = tmpMap
}
} else {
utils.Unserialize([]byte("[{\"name\": \"默认\",\"values\": [\"默认\"]}]"), &tmpMap)
goodsMap["spe"] = tmpMap
}
if goodsMap["spe_images"] != nil && goodsMap["spe_images"] != "null" && goodsMap["spe_images"] != "[]" && goodsMap["spe_images"] != "" {
var speImageListUrlTmp []string
utils.Unserialize([]byte(goodsMap["spe_images"].(string)), &tmpMap)
goodsMap["spe_images"] = tmpMap
for _, v := range goodsMap["spe_images"].([]interface{}) {
speImageListUrlTmp = append(speImageListUrlTmp, svc.ImageFormatWithBucketNew(scheme, host, subDomain, moreSubDomain, v.(string)))
}
goodsMap["spe_images_url"] = speImageListUrlTmp
} else {
goodsMap["spe_images"] = []struct{}{}
goodsMap["spe_images_url"] = []struct{}{}
}

// sku处理
var skuIds []string
for _, v := range skuList {
skuIds = append(skuIds, utils.Int64ToStr(v.SkuId))
}

var schemeWithSkusMap = map[int64]string{}
if len(skuIds) > 0 {
var schemeWithSkus []*model.SchemeWithSku
err = db.Db.Where("scheme_id = ?", schemeId).And(fmt.Sprintf("sku_id In (%s)", strings.Join(skuIds, ","))).Find(&schemeWithSkus)
if err != nil {
return nil, err
}
for _, v := range schemeWithSkus {
schemeWithSkusMap[v.SkuId] = v.Price
}
}

skuListMap := make([]map[string]interface{}, 0, len(skuList))
for _, item := range skuList {
tmp := make(map[string]interface{})
tmp["goods_id"] = item.GoodsId
tmp["sku_id"] = item.SkuId
tmp["sku_code"] = item.SkuCode
tmp["price"] = item.Price
tmp["indexes"] = item.Indexes
utils.Unserialize([]byte(item.Sku), &tmpMap)
tmp["sku"] = tmpMap
tmp["scheme_price"] = schemeWithSkusMap[item.SkuId]
if item.Indexes == "" || item.Indexes == " " {
tmp["indexes"] = "0"
utils.Unserialize([]byte("[{\"name\": \"默认\",\"values\": [\"默认\"]}]"), &tmpMap)
tmp["sku"] = tmpMap
}
skuListMap = append(skuListMap, tmp)
}

// 清理不用的字段
if _, ok := goodsMap["create_time"]; ok {
delete(goodsMap, "create_at")
}
if _, ok := goodsMap["update_time"]; ok {
delete(goodsMap, "update_at")
}
if _, ok := goodsMap["delete_at"]; ok {
delete(goodsMap, "delete_at")
}

goodsMap["sale_state_zh"] = ""
if _, ok := goodsMap["sale_state"]; ok {
switch goodsMap["sale_state"] {
case 1.00:
goodsMap["sale_state_zh"] = "销售中"
break
case 2.00:
goodsMap["sale_state_zh"] = "下架"
break
}
}

goodsMap["create_at"] = goodsModel.CreateAt
goodsMap["update_at"] = goodsModel.UpdateAt

resp["base_goods"] = goodsMap
resp["sku"] = skuListMap

return resp, nil

}

func SaveSchemeGoods(req md.SaveSchemeGoodsReq) (err error) {
//1、查询方案、商品记录是否存在
schemeDb := db.SchemeDb{}
schemeDb.Set()
schemeInfo, err := schemeDb.GetScheme(req.SchemeId)
if err != nil {
return err
}
if schemeInfo == nil {
return errors.New("未查询到相应方案记录")
}
goods := new(model.Goods)
has, err := schemeDb.Db.Where("goods_id =?", req.GoodsId).Get(goods)
if err != nil {
return
}
if !has {
return errors.New("未查询到相应商品记录")
}

//2、处理 scheme_with_goods 数据
schemeWithGoodsDb := db.SchemeWithGoodsDb{}
schemeWithGoodsDb.Set()
schemeWithGoods, err := schemeWithGoodsDb.GetSchemeWithGoods(req.SchemeId, req.GoodsId)
if err != nil {
return err
}
if schemeWithGoods == nil {
_, err = schemeWithGoodsDb.SchemeWithGoodsInsert(&model.SchemeWithGoods{
SchemeId: req.SchemeId,
GoodsId: req.GoodsId,
})
if err != nil {
return err
}
}

//3、处理 scheme_with_sku 数据
schemeWithSkuDb := db.SchemeWithSkuDb{}
schemeWithSkuDb.Set()
//3.1 删除之前所有记录
_, err = schemeWithSkuDb.SchemeDeleteByGoods(req.SchemeId, req.GoodsId)
if err != nil {
return err
}

//3.2 新增记录
var mm []*model.SchemeWithSku
for _, v := range req.SkuList {
mm = append(mm, &model.SchemeWithSku{
SchemeId: req.SchemeId,
GoodsId: req.GoodsId,
SkuId: v.SkuId,
Price: v.Price,
})
}
_, err = schemeWithSkuDb.BatchAddSchemeWithSku(mm)
if err != nil {
return err
}
return nil
}

+ 68
- 0
app/db/db_scheme.go Целия файл

@@ -0,0 +1,68 @@
package db

import (
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

type SchemeDb struct {
Db *xorm.Engine `json:"db"`
}

func (schemeDb *SchemeDb) Set() { // set方法
schemeDb.Db = Db
}

func (schemeDb *SchemeDb) FindScheme() (*[]model.Scheme, error) {
var m []model.Scheme
if err := schemeDb.Db.Desc("id").Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (schemeDb *SchemeDb) GetScheme(id int) (m *model.Scheme, err error) {
m = new(model.Scheme)
has, err := schemeDb.Db.Where("id =?", id).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (schemeDb *SchemeDb) SchemeDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("id", id).Delete(model.Scheme{})
} else {
return session.Where("id = ?", id).Delete(model.Scheme{})
}
}

func (schemeDb *SchemeDb) BatchAddSchemeBySession(session *xorm.Session, mm []*model.Scheme) (int64, error) {
affected, err := session.Insert(mm)
if err != nil {
return 0, err
}
return affected, nil
}

func (schemeDb *SchemeDb) SchemeInsert(m *model.Scheme) (int, error) {
_, err := schemeDb.Db.InsertOne(m)
if err != nil {
return 0, err
}
return m.Id, nil
}

func (schemeDb *SchemeDb) UpdateScheme(m *model.Scheme, columns ...string) (int64, error) {
affected, err := schemeDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
if err != nil {
return 0, err
}
return affected, nil
}

+ 89
- 0
app/db/db_scheme_with_enterprise.go Целия файл

@@ -0,0 +1,89 @@
package db

import (
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

type SchemeEnterpriseDb struct {
Db *xorm.Engine `json:"db"`
}

func (adminRoleDb *SchemeEnterpriseDb) Set() { // set方法
adminRoleDb.Db = Db
}

func (adminRoleDb *SchemeEnterpriseDb) FindSchemeEnterprise(schemeId int) (*[]model.SchemeWithEnterprise, error) {
var m []model.SchemeWithEnterprise
if err := adminRoleDb.Db.Where("scheme_id =?", schemeId).Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (adminRoleDb *SchemeEnterpriseDb) FindSchemeEnterpriseByEnterpriseId(id int) (*[]model.SchemeWithEnterprise, error) {
var m []model.SchemeWithEnterprise
if err := adminRoleDb.Db.Where("role_id =?", id).Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (adminRoleDb *SchemeEnterpriseDb) GetSchemeEnterpriseByEnterpriseId(enterpriseId int) (m *model.SchemeWithEnterprise, err error) {
m = new(model.SchemeWithEnterprise)
has, err := adminRoleDb.Db.Where("enterprise_id =?", enterpriseId).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (adminRoleDb *SchemeEnterpriseDb) GetSchemeEnterprise(id int) (m *model.SchemeWithEnterprise, err error) {
m = new(model.SchemeWithEnterprise)
has, err := adminRoleDb.Db.Where("id =?", id).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (adminRoleDb *SchemeEnterpriseDb) SchemeEnterpriseDeleteForEnterpriseBySession(session *xorm.Session, enterpriseId interface{}) (int64, error) {
if reflect.TypeOf(enterpriseId).Kind() == reflect.Slice {
return session.In("enterprise_id", enterpriseId).Delete(model.SchemeWithEnterprise{})
} else {
return session.Where("enterprise_id = ?", enterpriseId).Delete(model.SchemeWithEnterprise{})
}
}

func (adminRoleDb *SchemeEnterpriseDb) SchemeEnterpriseDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("scheme_id", id).Delete(model.SchemeWithEnterprise{})
} else {
return session.Where("scheme_id = ?", id).Delete(model.SchemeWithEnterprise{})
}
}

func (adminRoleDb *SchemeEnterpriseDb) BatchAddSchemeEnterpriseBySession(session *xorm.Session, mm []*model.SchemeWithEnterprise) (int64, error) {
affected, err := session.Insert(mm)
if err != nil {
return 0, err
}
return affected, nil
}

type SchemeEnterprise struct {
model.Enterprise `xorm:"extends"`
model.SchemeWithEnterprise `xorm:"extends"`
}

func (SchemeEnterprise) TableName() string {
return "scheme_with_enterprise"
}

+ 52
- 0
app/db/db_scheme_with_goods.go Целия файл

@@ -0,0 +1,52 @@
package db

import (
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

type SchemeWithGoodsDb struct {
Db *xorm.Engine `json:"db"`
}

func (schemeWithGoodsDb *SchemeWithGoodsDb) Set() { // set方法
schemeWithGoodsDb.Db = Db
}

func (schemeWithGoodsDb *SchemeWithGoodsDb) FindSchemeWithGoods(schemeId int) (*[]model.SchemeWithGoods, error) {
var m []model.SchemeWithGoods
if err := schemeWithGoodsDb.Db.Where("scheme_id =?", schemeId).Desc("id").Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (schemeWithGoodsDb *SchemeWithGoodsDb) GetSchemeWithGoods(schemeId int, goodsId int64) (m *model.SchemeWithGoods, err error) {
m = new(model.SchemeWithGoods)
has, err := schemeWithGoodsDb.Db.Where("scheme_id =?", schemeId).And("goods_id =?", goodsId).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (schemeWithGoodsDb *SchemeWithGoodsDb) SchemeDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("id", id).Delete(model.SchemeWithGoods{})
} else {
return session.Where("id = ?", id).Delete(model.SchemeWithGoods{})
}
}

func (schemeWithGoodsDb *SchemeWithGoodsDb) SchemeWithGoodsInsert(m *model.SchemeWithGoods) (int, error) {
_, err := schemeWithGoodsDb.Db.InsertOne(m)
if err != nil {
return 0, err
}
return m.Id, nil
}

+ 72
- 0
app/db/db_scheme_with_sku.go Целия файл

@@ -0,0 +1,72 @@
package db

import (
"applet/app/db/model"
"applet/app/utils/logx"
"reflect"
"xorm.io/xorm"
)

type SchemeWithSkuDb struct {
Db *xorm.Engine `json:"db"`
}

func (schemeWithSkuDb *SchemeWithSkuDb) Set() { // set方法
schemeWithSkuDb.Db = Db
}

func (schemeWithSkuDb *SchemeWithSkuDb) FindSchemeWithScheme(schemeId int) (*[]model.SchemeWithSku, error) {
var m []model.SchemeWithSku
if err := schemeWithSkuDb.Db.Where("scheme_id =?", schemeId).Desc("id").Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (schemeWithSkuDb *SchemeWithSkuDb) FindSchemeWithGoods(schemeId int, goodsId int64) (*[]model.SchemeWithSku, error) {
var m []model.SchemeWithSku
if err := schemeWithSkuDb.Db.Where("goods_id =?", goodsId).And("scheme_id =?", schemeId).Desc("id").Find(&m); err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

func (schemeWithSkuDb *SchemeWithSkuDb) GetSchemeWithSku(schemeId int, skuId int64) (m *model.SchemeWithSku, err error) {
m = new(model.SchemeWithSku)
has, err := schemeWithSkuDb.Db.Where("scheme_id =?", schemeId).And("sku_id =?", skuId).Get(m)
if err != nil {
return nil, logx.Error(err)
}
if has == false {
return nil, nil
}
return m, nil
}

func (schemeWithSkuDb *SchemeWithSkuDb) SchemeDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return session.In("id", id).Delete(model.SchemeWithSku{})
} else {
return session.Where("id = ?", id).Delete(model.SchemeWithSku{})
}
}

func (schemeWithSkuDb *SchemeWithSkuDb) SchemeDeleteByGoods(schemeId int, goodsId int64) (int64, error) {
return schemeWithSkuDb.Db.Where("scheme_id = ?", schemeId).And("goods_id =?", goodsId).Delete(model.SchemeWithSku{})
}

func (schemeWithSkuDb *SchemeWithSkuDb) SchemeWithSkuInsert(m *model.SchemeWithSku) (int, error) {
_, err := schemeWithSkuDb.Db.InsertOne(m)
if err != nil {
return 0, err
}
return m.Id, nil
}

func (schemeWithSkuDb *SchemeWithSkuDb) BatchAddSchemeWithSku(mm []*model.SchemeWithSku) (int64, error) {
affected, err := schemeWithSkuDb.Db.Insert(mm)
if err != nil {
return 0, err
}
return affected, nil
}

+ 10
- 0
app/db/model/scheme.go Целия файл

@@ -0,0 +1,10 @@
package model

type Scheme struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
Name string `json:"name" xorm:"not null default '' comment('名称') VARCHAR(255)"`
State int `json:"state" xorm:"not null default 1 comment('状态(1:正常 2:冻结)') TINYINT(1)"`
Memo string `json:"memo" xorm:"not null default '' comment('备注信息') VARCHAR(244)"`
CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
}

+ 7
- 0
app/db/model/scheme_with_enterprise.go Целия файл

@@ -0,0 +1,7 @@
package model

type SchemeWithEnterprise struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
SchemeId int `json:"scheme_id" xorm:"not null default 0 comment('方案id') INT(11)"`
EnterpriseId int `json:"enterprise_id" xorm:"not null default 0 comment('单位id') INT(11)"`
}

+ 7
- 0
app/db/model/scheme_with_goods.go Целия файл

@@ -0,0 +1,7 @@
package model

type SchemeWithGoods struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
SchemeId int `json:"scheme_id" xorm:"not null default 0 comment('方案id') INT(11)"`
GoodsId int64 `json:"goods_id" xorm:"not null default 0 comment('商品ID') INT(11)"`
}

+ 9
- 0
app/db/model/scheme_with_sku.go Целия файл

@@ -0,0 +1,9 @@
package model

type SchemeWithSku struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
SchemeId int `json:"scheme_id" xorm:"not null default 0 comment('方案id') INT(11)"`
GoodsId int64 `json:"goods_id" xorm:"not null default 0 comment('商品ID') INT(11)"`
SkuId int64 `json:"sku_id" xorm:"not null default 0 comment('skuId') INT(11)"`
Price string `json:"price" xorm:"not null default 0.00 comment('价格') DECIMAL(12,2)"`
}

+ 12
- 4
app/router/admin_router.go Целия файл

@@ -51,8 +51,16 @@ func rHomePage(r *gin.RouterGroup) {

}

func rBanner(r *gin.RouterGroup) {

func rScheme(r *gin.RouterGroup) {
r.GET("/list", hdl.SchemeList)
r.POST("/add", hdl.AddScheme)
r.POST("/update", hdl.UpdateScheme)
r.POST("/bindEnterprise", hdl.SchemeBindEnterprise)
r.POST("/updateSchemeState", hdl.UpdateSchemeState)
r.DELETE("/delete/:id", hdl.DeleteScheme)
r.GET("/goodsList", goodsHdl.GetMallGoodsList)
r.GET("/goodsDetail", hdl.GoodsDetail)
r.POST("/saveSchemeGoods", hdl.SaveSchemeGoods)
}

func rComm(r *gin.RouterGroup) {
@@ -73,7 +81,7 @@ func rUser(r *gin.RouterGroup) {
}

func rEnterprise(r *gin.RouterGroup) {
r.GET("/list", hdl.EnterpriseList)
r.POST("/list", hdl.EnterpriseList)
r.POST("/add", hdl.EnterpriseAdd)
r.POST("/update", hdl.EnterpriseUpdate)
r.POST("/updateState", hdl.EnterpriseUpdateState)
@@ -130,7 +138,7 @@ func AdminRoute(r *gin.RouterGroup) {

rHomePage(r.Group("/homePage")) //首页
rCompany(r.Group("/company")) //公司管理
rBanner(r.Group("/banner")) //轮播图管理
rScheme(r.Group("/scheme")) //方案管理
rEnterprise(r.Group("/enterprise")) //校企
rCategory(r.Group("/category")) //商品类目
rGoods(r.Group("/goods")) //商品


Зареждане…
Отказ
Запис