|
- 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
- }
- if enterprise != nil {
- temp.EnterpriseList = append(temp.EnterpriseList, struct {
- Id int `json:"id"`
- Name string `json:"name"`
- }{
- Id: enterprise.Id,
- 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 SchemeGoodsDetail(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
- }
|