product.go 23 KB

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