product.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mini_crm_ht/models"
  6. "eta/eta_mini_crm_ht/models/request"
  7. "eta/eta_mini_crm_ht/models/response"
  8. "eta/eta_mini_crm_ht/services"
  9. "eta/eta_mini_crm_ht/utils"
  10. "fmt"
  11. "github.com/go-sql-driver/mysql"
  12. "github.com/rdlucklib/rdluck_tools/paging"
  13. "github.com/shopspring/decimal"
  14. "math/rand"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "time"
  19. )
  20. var (
  21. CNProductMap = map[models.MerchantProductType]string{
  22. models.ProductPackage: "套餐",
  23. models.ProductVideo: "视频",
  24. models.ProductAudio: "音频",
  25. models.ProductReport: "报告",
  26. }
  27. CNSaleStatusMap = map[models.SaleStatus]string{
  28. models.OnSale: "已上架",
  29. models.OffSale: "未上架",
  30. }
  31. )
  32. type ProductController struct {
  33. BaseAuthController
  34. }
  35. // UnSetProductList
  36. // @Title 未设置的产品列表
  37. // @Description 未设置的产品列表
  38. // @Param PageSize query int true "每页数据条数"
  39. // @Param CurrentIndex query int true "当前页页码,从1开始"
  40. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  41. // @Param KeyWord query string true "报告标题/创建人"
  42. // @Param SortType query string true "排序方式"
  43. // @Success 200 {object} models.ReportAuthorResp
  44. // @router /unSetProductList [get]
  45. func (this *ProductController) UnSetProductList() {
  46. br := new(models.BaseResponse).Init()
  47. defer func() {
  48. this.Data["json"] = br
  49. this.ServeJSON()
  50. }()
  51. pageSize, _ := this.GetInt("PageSize")
  52. currentIndex, _ := this.GetInt("CurrentIndex")
  53. sortType := this.GetString("SortType")
  54. ProductType := this.GetString("ProductType")
  55. permissionIds := this.GetString("PermissionIds")
  56. KeyWord := this.GetString("KeyWord")
  57. var condition string
  58. var pars []interface{}
  59. if pageSize <= 0 {
  60. pageSize = utils.PageSize20
  61. }
  62. if currentIndex <= 0 {
  63. currentIndex = 1
  64. }
  65. if ProductType == "" {
  66. br.Msg = "产品类型不能为空"
  67. br.ErrMsg = "获取未设置产品列表失败,Err:产品类型为空"
  68. return
  69. }
  70. if KeyWord != "" {
  71. switch ProductType {
  72. case "report":
  73. condition += " AND title like '%?%'"
  74. case "media":
  75. condition += " AND media_name like '%?%'"
  76. }
  77. pars = append(pars, KeyWord)
  78. }
  79. sortCondition := " ORDER BY published_time "
  80. if sortType == "" {
  81. sortType = "DESC"
  82. }
  83. sortCondition = sortCondition + sortType
  84. var permissionIdsList []int
  85. if permissionIds != "" {
  86. permissionStr := strings.Split(permissionIds, ",")
  87. for _, permissionItem := range permissionStr {
  88. permissionId, _ := strconv.Atoi(permissionItem)
  89. permissionIdsList = append(permissionIdsList, permissionId)
  90. }
  91. }
  92. total, ids, err := services.GetUnsetProductCountByCondition(ProductType, permissionIdsList, condition, pars)
  93. if err != nil {
  94. br.Msg = "获取未设置产品列表失败"
  95. br.ErrMsg = "获取未设置产品列表失败,Err:" + err.Error()
  96. return
  97. }
  98. var list []*services.ProductView
  99. if len(ids) > 0 {
  100. startSize := utils.StartIndex(currentIndex, pageSize)
  101. list, err = services.GetUnsetProductByCondition(ProductType, ids, sortCondition, startSize, pageSize)
  102. if err != nil {
  103. br.Msg = "获取未设置产品列表失败"
  104. br.ErrMsg = "获取未设置产品列表失败,Err:" + err.Error()
  105. return
  106. }
  107. }
  108. var wg sync.WaitGroup
  109. wg.Add(len(list))
  110. for _, product := range list {
  111. go func(product *services.ProductView) {
  112. defer wg.Done()
  113. switch product.ProductType {
  114. case "report":
  115. product.RiskLevel, _, _ = services.GetRiskLevel("report", product.SourceId)
  116. product.ProductType = "报告"
  117. case "video":
  118. product.RiskLevel, _, _ = services.GetRiskLevel("media", product.SourceId)
  119. product.ProductType = "视频"
  120. case "audio":
  121. product.RiskLevel, _, _ = services.GetRiskLevel("media", product.SourceId)
  122. product.ProductType = "音频"
  123. }
  124. }(product)
  125. }
  126. wg.Wait()
  127. page := paging.GetPaging(currentIndex, pageSize, total)
  128. resp := new(response.ProductListResp)
  129. resp.List = list
  130. resp.Paging = page
  131. br.Ret = 200
  132. br.Success = true
  133. br.Data = resp
  134. br.Msg = "获取成功"
  135. }
  136. // AddProduct @Title 新增单品
  137. // @Description 新增单品
  138. // @Param File query file true "文件"
  139. // @Success 200 {object} models.ReportAuthorResp
  140. // @router /addProduct [post]
  141. func (this *ProductController) AddProduct() {
  142. br := new(models.BaseResponse).Init()
  143. defer func() {
  144. this.Data["json"] = br
  145. this.ServeJSON()
  146. }()
  147. var req request.ProductReq
  148. var permissionName string
  149. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  150. if err != nil {
  151. br.Msg = "参数解析异常!"
  152. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  153. return
  154. }
  155. var product models.MerchantProduct
  156. if req.Type == "package" {
  157. if req.ValidDays <= 0 {
  158. br.Msg = "套餐有效期非法"
  159. br.ErrMsg = "套餐有效期非法,天数不能是负数"
  160. return
  161. }
  162. if req.ProductName == "" {
  163. br.Msg = "套餐名称不能为空"
  164. br.ErrMsg = "套餐名称不能为空"
  165. return
  166. }
  167. product.Title = req.ProductName
  168. product.ValidDays = req.ValidDays
  169. product.Description = req.Description
  170. if req.CoverSrc == "" {
  171. var imageList []models.ImageSource
  172. imageList, err = models.GetImageByPermissionId(req.SourceId)
  173. if err != nil {
  174. utils.FileLog.Error("套餐封面获取失败", err.Error())
  175. //br.Msg = "默认套餐封面获取失败,请上传封面"
  176. //br.ErrMsg = "默认套餐封面获取失败,请上传封面,Err:" + err.Error()
  177. //return
  178. } else {
  179. var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
  180. index := rnd.Intn(len(imageList))
  181. product.CoverSrc = imageList[index].SrcUrl
  182. }
  183. } else {
  184. product.CoverSrc = req.CoverSrc
  185. }
  186. }
  187. switch req.Type {
  188. case "report":
  189. product.RiskLevel, product.Title, err = services.GetRiskLevel("report", req.SourceId)
  190. case "audio":
  191. product.RiskLevel, product.Title, err = services.GetRiskLevel("audio", req.SourceId)
  192. case "video":
  193. product.RiskLevel, product.Title, err = services.GetRiskLevel("video", req.SourceId)
  194. case "package":
  195. product.RiskLevel, permissionName, err = services.GetRiskLevel("package", req.SourceId)
  196. default:
  197. br.Msg = "产品类型错误"
  198. br.ErrMsg = "获取产品列表失败,Err:产品类型错误"
  199. return
  200. }
  201. if err != nil {
  202. utils.FileLog.Error("新增单品失败", err.Error())
  203. br.Msg = "新增产品错误"
  204. if strings.Contains(err.Error(), "<QuerySeter> no row found") {
  205. br.Msg = "新增产品错误,产品信息不存在"
  206. } else {
  207. br.Msg = "新增产品错误" + err.Error()
  208. }
  209. return
  210. }
  211. if product.RiskLevel == "" {
  212. br.Msg = "新增产品错误"
  213. br.ErrMsg = "未获取到风险等级"
  214. return
  215. }
  216. if !checkProductRiskLevel(product.RiskLevel) {
  217. br.Msg = "产品风险等级不合法"
  218. br.ErrMsg = "产品风险等级不合法:" + product.RiskLevel
  219. return
  220. }
  221. if product.Title == "" {
  222. br.Msg = "产品名称不能为空"
  223. br.ErrMsg = "产品名称不能为空"
  224. return
  225. }
  226. var price decimal.Decimal
  227. price, err = decimal.NewFromString(req.Price)
  228. if err != nil {
  229. br.Msg = "产品价格格式不正确"
  230. br.ErrMsg = "产品价格格式不正确,err:" + err.Error() + "price:" + product.Price
  231. return
  232. }
  233. if price.Cmp(decimal.New(0, 0)) <= 0 {
  234. br.Msg = "产品价格不能小于0"
  235. br.ErrMsg = "产品价格不能小于0"
  236. return
  237. }
  238. product.SaleStatus = models.OnSale
  239. product.CreatedTime = time.Now()
  240. product.Price = req.Price
  241. product.SourceId = req.SourceId
  242. product.Type = models.MerchantProductType(req.Type)
  243. product.Creator = this.SysUser.SysRealName
  244. if product.Type == "" {
  245. br.Msg = "新增产品错误"
  246. br.ErrMsg = "产品类型为空"
  247. return
  248. }
  249. err = product.AddProduct()
  250. if err != nil {
  251. var mysqlErr *mysql.MySQLError
  252. if errors.As(err, &mysqlErr) {
  253. if mysqlErr.Number == 1062 {
  254. if product.Type == models.ProductPackage {
  255. var dbProduct models.MerchantProduct
  256. dbProduct, err = models.GetProductByProductType(product.SourceId, models.ProductPackage)
  257. if err != nil {
  258. utils.FileLog.Error("获取套餐产品信息失败,err:" + err.Error())
  259. br.Msg = "[" + permissionName + "]已设置付费套餐,请重新选择"
  260. br.ErrMsg = "[" + permissionName + "]已设置付费套餐,请重新选择"
  261. } else {
  262. br.Msg = "[" + permissionName + "]已设置付费套餐[" + dbProduct.Title + "],请重新选择"
  263. br.ErrMsg = "[" + permissionName + "]已设置付费套餐[" + dbProduct.Title + "],请重新选择"
  264. }
  265. } else {
  266. br.Msg = "该产品已设置付费,请刷新后重试"
  267. br.ErrMsg = "该产品已设置付费,请刷新后重试"
  268. }
  269. } else {
  270. utils.FileLog.Error("新增产品失败,err:" + err.Error())
  271. br.Msg = "新增产品失败"
  272. br.ErrMsg = "新增产品失败,err:" + err.Error()
  273. }
  274. } else {
  275. utils.FileLog.Error("新增产品失败,err:" + err.Error())
  276. br.Msg = "新增产品失败"
  277. br.ErrMsg = "新增产品失败,err:" + err.Error()
  278. }
  279. return
  280. }
  281. br.Ret = 200
  282. br.Success = true
  283. br.Msg = "新增产品成功"
  284. return
  285. }
  286. // UpdateSaleStatus @Title 上下架产品
  287. // @Description 上下架产品
  288. // @Param File query file true "文件"
  289. // @Success 200 {object} models.ReportAuthorResp
  290. // @router /updateSaleStatus [post]
  291. func (this *ProductController) UpdateSaleStatus() {
  292. br := new(models.BaseResponse).Init()
  293. defer func() {
  294. this.Data["json"] = br
  295. this.ServeJSON()
  296. }()
  297. defer func() {
  298. this.Data["json"] = br
  299. this.ServeJSON()
  300. }()
  301. var req request.ProductSaleStatusReq
  302. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  303. if err != nil {
  304. br.Msg = "参数解析异常!"
  305. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  306. return
  307. }
  308. if req.ProductId <= 0 {
  309. br.Msg = "产品编号非法!"
  310. br.ErrMsg = "产品编号非法,不能小于0"
  311. return
  312. }
  313. if req.SaleStatus != "onSale" && req.SaleStatus != "offSale" {
  314. br.Msg = "产品销售状态非法!"
  315. br.ErrMsg = "产品销售状态非法,未知的上下架类型:" + req.SaleStatus
  316. return
  317. }
  318. var saleStatus models.SaleStatus
  319. var messageStatus string
  320. switch req.SaleStatus {
  321. case "onSale":
  322. saleStatus = models.OnSale
  323. messageStatus = "上架"
  324. case "offSale":
  325. saleStatus = models.OffSale
  326. messageStatus = "下架"
  327. }
  328. product := models.MerchantProduct{
  329. Id: req.ProductId,
  330. SaleStatus: saleStatus,
  331. UpdatedTime: time.Now(),
  332. }
  333. err = product.UpdateProductSaleStatus()
  334. if err != nil {
  335. br.Msg = messageStatus + "失败"
  336. br.ErrMsg = messageStatus + "失败,err:" + err.Error()
  337. return
  338. }
  339. br.Msg = messageStatus + "成功"
  340. br.Ret = 200
  341. br.Success = true
  342. }
  343. // DeleteProduct @Title 删除产品
  344. // @Description 删除产品
  345. // @Param File query file true "文件"
  346. // @Success 200 {object} models.ReportAuthorResp
  347. // @router /deleteProduct [post]
  348. func (this *ProductController) DeleteProduct() {
  349. br := new(models.BaseResponse).Init()
  350. defer func() {
  351. this.Data["json"] = br
  352. this.ServeJSON()
  353. }()
  354. defer func() {
  355. this.Data["json"] = br
  356. this.ServeJSON()
  357. }()
  358. var req request.ProductSaleStatusReq
  359. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  360. if err != nil {
  361. br.Msg = "参数解析异常!"
  362. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  363. return
  364. }
  365. if req.ProductId <= 0 {
  366. br.Msg = "产品编号非法!"
  367. br.ErrMsg = "产品编号非法,不能小于0"
  368. return
  369. }
  370. product := models.MerchantProduct{
  371. Id: req.ProductId,
  372. Deleted: true,
  373. UpdatedTime: time.Now(),
  374. }
  375. err = product.Delete()
  376. if err != nil {
  377. br.Msg = "删除产品失败"
  378. br.ErrMsg = "删除产品失败,err:" + err.Error()
  379. return
  380. }
  381. br.Msg = "删除产品成功"
  382. br.Ret = 200
  383. br.Success = true
  384. }
  385. // ProductList
  386. // @Title 产品列表
  387. // @Description pdf研报列表
  388. // @Param PageSize query int true "每页数据条数"
  389. // @Param CurrentIndex query int true "当前页页码,从1开始"
  390. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  391. // @Param KeyWord query string true "报告标题/创建人"
  392. // @Param SortType query string true "排序方式"
  393. // @Success 200 {object} models.ReportAuthorResp
  394. // @router /productList [get]
  395. func (this *ProductController) ProductList() {
  396. br := new(models.BaseResponse).Init()
  397. defer func() {
  398. this.Data["json"] = br
  399. this.ServeJSON()
  400. }()
  401. pageSize, _ := this.GetInt("PageSize")
  402. currentIndex, _ := this.GetInt("CurrentIndex")
  403. sortType := this.GetString("SortType")
  404. KeyWord := this.GetString("KeyWord")
  405. CreatedTime := this.GetString("CreatedTime")
  406. UpdatedTime := this.GetString("UpdatedTime")
  407. IsSingle, _ := this.GetBool("IsSingle", true)
  408. ProductType := this.GetString("ProductType")
  409. SaleStatus := this.GetString("SaleStatus")
  410. var condition string
  411. if pageSize <= 0 {
  412. pageSize = utils.PageSize20
  413. }
  414. if currentIndex <= 0 {
  415. currentIndex = 1
  416. }
  417. if KeyWord != "" {
  418. condition += " AND title like '%" + KeyWord + "%'"
  419. }
  420. sortCondition := " ORDER BY created_time "
  421. if sortType == "" {
  422. sortType = "DESC"
  423. }
  424. if CreatedTime != "" {
  425. condition += " AND Date(created_time) = '" + CreatedTime + "'"
  426. }
  427. if UpdatedTime != "" {
  428. condition += " AND Date(updated_time) = '" + UpdatedTime + "'"
  429. }
  430. if SaleStatus != "" {
  431. switch SaleStatus {
  432. case "onSale":
  433. condition += " AND sale_status='on_sale'"
  434. case "offSale":
  435. condition += " AND sale_status='off_sale'"
  436. default:
  437. br.Msg = "无效的销售状态"
  438. br.ErrMsg = "无效的销售状态:" + SaleStatus
  439. return
  440. }
  441. }
  442. if IsSingle {
  443. if ProductType != "" {
  444. switch ProductType {
  445. case "report":
  446. condition += " AND type='" + string(models.ProductReport) + "'"
  447. case "audio":
  448. condition += " AND type='" + string(models.ProductAudio) + "'"
  449. case "video":
  450. condition += " AND type='" + string(models.ProductVideo) + "'"
  451. default:
  452. br.Msg = "无效的产品类型"
  453. br.ErrMsg = "无效的产品类型:" + ProductType
  454. return
  455. }
  456. }
  457. } else {
  458. condition += " AND type = '" + string(models.ProductPackage) + "'"
  459. }
  460. sortCondition = sortCondition + sortType
  461. total, err := models.GetProductCountByCondition(condition)
  462. if err != nil {
  463. br.Msg = "获取产品列表失败"
  464. br.ErrMsg = "获取产品列表失败,Err:" + err.Error()
  465. return
  466. }
  467. startSize := utils.StartIndex(currentIndex, pageSize)
  468. List, err := models.GetProductByCondition(condition, sortCondition, startSize, pageSize)
  469. if err != nil {
  470. br.Msg = "获取产品列表失败"
  471. br.ErrMsg = "获取产品列表失败,Err:" + err.Error()
  472. return
  473. }
  474. var ListView []*services.ProductView
  475. for _, product := range List {
  476. view := &services.ProductView{
  477. ProductName: product.Title,
  478. ProductType: CNProductMap[product.Type],
  479. PublishedTime: product.CreatedTime.Format(time.DateTime),
  480. RiskLevel: product.RiskLevel,
  481. Price: fmt.Sprintf("¥%s", product.Price),
  482. SaleStatus: CNSaleStatusMap[product.SaleStatus],
  483. }
  484. if !product.UpdatedTime.IsZero() {
  485. view.UpdatedTime = product.UpdatedTime.Format(time.DateTime)
  486. }
  487. ListView = append(ListView, view)
  488. }
  489. page := paging.GetPaging(currentIndex, pageSize, total)
  490. resp := new(response.ProductListResp)
  491. resp.List = ListView
  492. resp.Paging = page
  493. br.Ret = 200
  494. br.Success = true
  495. br.Data = resp
  496. br.Msg = "获取成功"
  497. }
  498. // ProductRisk
  499. // @Title 产品列表
  500. // @Description pdf研报列表
  501. // @Param PageSize query int true "每页数据条数"
  502. // @Param CurrentIndex query int true "当前页页码,从1开始"
  503. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  504. // @Param KeyWord query string true "报告标题/创建人"
  505. // @Param SortType query string true "排序方式"
  506. // @Success 200 {object} models.ReportAuthorResp
  507. // @router /productRisk [get]
  508. func (this *ProductController) ProductRisk() {
  509. br := new(models.BaseResponse).Init()
  510. defer func() {
  511. this.Data["json"] = br
  512. this.ServeJSON()
  513. }()
  514. SourceId, _ := this.GetInt("SourceId", 0)
  515. ProductType := this.GetString("ProductType")
  516. if SourceId <= 0 {
  517. br.Msg = "无效的产品ID"
  518. br.ErrMsg = "无效的产品ID:" + strconv.Itoa(SourceId)
  519. return
  520. }
  521. riskLevel, _, err := services.GetRiskLevel(ProductType, SourceId)
  522. if err != nil {
  523. utils.FileLog.Error("查询产品风险等级失败", err.Error)
  524. return
  525. }
  526. resp := new(response.ProductRiskResp)
  527. resp.RiskLevel = riskLevel
  528. resp.SourceId = SourceId
  529. resp.ProductType = ProductType
  530. br.Ret = 200
  531. br.Success = true
  532. br.Data = resp
  533. br.Msg = "获取成功"
  534. }
  535. // EditProduct @Title 编辑产品
  536. // @Description 编辑产品
  537. // @Param File query file true "文件"
  538. // @Success 200 {object} models.ReportAuthorResp
  539. // @router /editProduct [post]
  540. func (this *ProductController) EditProduct() {
  541. br := new(models.BaseResponse).Init()
  542. defer func() {
  543. this.Data["json"] = br
  544. this.ServeJSON()
  545. }()
  546. var req request.ProductReq
  547. var permissionName string
  548. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  549. if err != nil {
  550. br.Msg = "参数解析异常!"
  551. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  552. return
  553. }
  554. var product models.MerchantProduct
  555. if req.Type == "package" {
  556. if req.ValidDays <= 0 {
  557. br.Msg = "套餐有效期非法"
  558. br.ErrMsg = "套餐有效期非法,天数不能是负数"
  559. return
  560. }
  561. if req.ProductName == "" {
  562. br.Msg = "套餐名称不能为空"
  563. br.ErrMsg = "套餐名称不能为空"
  564. return
  565. }
  566. product.Title = req.ProductName
  567. product.ValidDays = req.ValidDays
  568. product.Description = req.Description
  569. if req.CoverSrc == "" {
  570. var imageList []models.ImageSource
  571. imageList, err = models.GetImageByPermissionId(req.SourceId)
  572. if err != nil {
  573. utils.FileLog.Error("套餐封面获取失败", err.Error())
  574. //br.Msg = "默认套餐封面获取失败,请上传封面"
  575. //br.ErrMsg = "默认套餐封面获取失败,请上传封面,Err:" + err.Error()
  576. //return
  577. } else {
  578. var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
  579. index := rnd.Intn(len(imageList))
  580. product.CoverSrc = imageList[index].SrcUrl
  581. }
  582. } else {
  583. product.CoverSrc = req.CoverSrc
  584. }
  585. }
  586. switch req.Type {
  587. case "report":
  588. product.RiskLevel, product.Title, err = services.GetRiskLevel("report", req.SourceId)
  589. case "audio":
  590. product.RiskLevel, product.Title, err = services.GetRiskLevel("audio", req.SourceId)
  591. case "video":
  592. product.RiskLevel, product.Title, err = services.GetRiskLevel("video", req.SourceId)
  593. case "package":
  594. product.RiskLevel, permissionName, err = services.GetRiskLevel("package", req.SourceId)
  595. default:
  596. br.Msg = "产品类型错误"
  597. br.ErrMsg = "获取产品列表失败,Err:产品类型错误"
  598. return
  599. }
  600. if err != nil {
  601. utils.FileLog.Error("新增单品失败", err.Error())
  602. br.Msg = "新增产品错误"
  603. if strings.Contains(err.Error(), "<QuerySeter> no row found") {
  604. br.Msg = "新增产品错误,产品信息不存在"
  605. } else {
  606. br.Msg = "新增产品错误" + err.Error()
  607. }
  608. return
  609. }
  610. if product.RiskLevel == "" {
  611. br.Msg = "新增产品错误"
  612. br.ErrMsg = "未获取到风险等级"
  613. return
  614. }
  615. if !checkProductRiskLevel(product.RiskLevel) {
  616. br.Msg = "产品风险等级不合法"
  617. br.ErrMsg = "产品风险等级不合法:" + product.RiskLevel
  618. return
  619. }
  620. if product.Title == "" {
  621. br.Msg = "产品名称不能为空"
  622. br.ErrMsg = "产品名称不能为空"
  623. return
  624. }
  625. var price decimal.Decimal
  626. price, err = decimal.NewFromString(req.Price)
  627. if err != nil {
  628. br.Msg = "产品价格格式不正确"
  629. br.ErrMsg = "产品价格格式不正确,err:" + err.Error() + "price:" + product.Price
  630. return
  631. }
  632. if price.Cmp(decimal.New(0, 0)) <= 0 {
  633. br.Msg = "产品价格不能小于0"
  634. br.ErrMsg = "产品价格不能小于0"
  635. return
  636. }
  637. product.SaleStatus = models.OnSale
  638. product.CreatedTime = time.Now()
  639. product.Price = req.Price
  640. product.SourceId = req.SourceId
  641. product.Type = models.MerchantProductType(req.Type)
  642. product.Creator = this.SysUser.SysRealName
  643. if product.Type == "" {
  644. br.Msg = "新增产品错误"
  645. br.ErrMsg = "产品类型为空"
  646. return
  647. }
  648. err = product.EditProduct()
  649. if err != nil {
  650. var mysqlErr *mysql.MySQLError
  651. if errors.As(err, &mysqlErr) {
  652. if mysqlErr.Number == 1062 {
  653. if product.Type == models.ProductPackage {
  654. var dbProduct models.MerchantProduct
  655. dbProduct, err = models.GetProductByProductType(product.SourceId, models.ProductPackage)
  656. if err != nil {
  657. utils.FileLog.Error("获取套餐产品信息失败,err:" + err.Error())
  658. br.Msg = "[" + permissionName + "]已设置付费套餐,请重新选择"
  659. br.ErrMsg = "[" + permissionName + "]已设置付费套餐,请重新选择"
  660. } else {
  661. br.Msg = "[" + permissionName + "]已设置付费套餐[" + dbProduct.Title + "],请重新选择"
  662. br.ErrMsg = "[" + permissionName + "]已设置付费套餐[" + dbProduct.Title + "],请重新选择"
  663. }
  664. } else {
  665. br.Msg = "该产品已设置付费,请刷新后重试"
  666. br.ErrMsg = "该产品已设置付费,请刷新后重试"
  667. }
  668. } else {
  669. utils.FileLog.Error("新增产品失败,err:" + err.Error())
  670. br.Msg = "新增产品失败"
  671. br.ErrMsg = "新增产品失败,err:" + err.Error()
  672. }
  673. } else {
  674. utils.FileLog.Error("新增产品失败,err:" + err.Error())
  675. br.Msg = "新增产品失败"
  676. br.ErrMsg = "新增产品失败,err:" + err.Error()
  677. }
  678. return
  679. }
  680. br.Ret = 200
  681. br.Success = true
  682. br.Msg = "新增产品成功"
  683. return
  684. }