product.go 22 KB

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