product.go 22 KB

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