product.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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].Id
  184. }
  185. } else {
  186. product.CoverUrl = req.CoverSrc
  187. }
  188. }
  189. switch req.Type {
  190. case "report":
  191. _, product.Title, err = services.GetRiskLevel("report", req.SourceId)
  192. product.IsPermanent = true
  193. case "audio":
  194. _, product.Title, err = services.GetRiskLevel("audio", req.SourceId)
  195. product.IsPermanent = true
  196. case "video":
  197. _, product.Title, err = services.GetRiskLevel("video", req.SourceId)
  198. product.IsPermanent = true
  199. case "package":
  200. _, 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.Title == "" {
  217. br.Msg = "产品名称不能为空"
  218. br.ErrMsg = "产品名称不能为空"
  219. return
  220. }
  221. var price decimal.Decimal
  222. price, err = decimal.NewFromString(req.Price)
  223. if err != nil {
  224. br.Msg = "产品价格格式不正确"
  225. br.ErrMsg = "产品价格格式不正确,err:" + err.Error() + "price:" + product.Price
  226. return
  227. }
  228. if price.Cmp(decimal.New(0, 0)) <= 0 {
  229. br.Msg = "产品价格不能小于0"
  230. br.ErrMsg = "产品价格不能小于0"
  231. return
  232. }
  233. product.SaleStatus = models.OnSale
  234. product.CreatedTime = time.Now()
  235. product.Price = req.Price
  236. product.SourceId = req.SourceId
  237. product.Type = models.MerchantProductType(req.Type)
  238. product.Creator = this.SysUser.SysRealName
  239. if product.Type == "" {
  240. br.Msg = "新增产品错误"
  241. br.ErrMsg = "产品类型为空"
  242. return
  243. }
  244. err = product.AddProduct()
  245. if err != nil {
  246. var mysqlErr *mysql.MySQLError
  247. if errors.As(err, &mysqlErr) {
  248. if mysqlErr.Number == 1062 {
  249. if product.Type == models.ProductPackage {
  250. var dbProduct models.MerchantProduct
  251. dbProduct, err = models.GetProductByProductType(product.SourceId, models.ProductPackage)
  252. if err != nil {
  253. utils.FileLog.Error("获取套餐产品信息失败,err:" + err.Error())
  254. br.Msg = "[" + permissionName + "]已设置付费套餐,请重新选择"
  255. br.ErrMsg = "[" + permissionName + "]已设置付费套餐,请重新选择"
  256. } else {
  257. br.Msg = "[" + permissionName + "]已设置付费套餐[" + dbProduct.Title + "],请重新选择"
  258. br.ErrMsg = "[" + permissionName + "]已设置付费套餐[" + dbProduct.Title + "],请重新选择"
  259. }
  260. } else {
  261. br.Msg = "该产品已设置付费,请刷新后重试"
  262. br.ErrMsg = "该产品已设置付费,请刷新后重试"
  263. }
  264. } else {
  265. utils.FileLog.Error("新增产品失败,err:" + err.Error())
  266. br.Msg = "新增产品失败"
  267. br.ErrMsg = "新增产品失败,err:" + err.Error()
  268. }
  269. } else {
  270. utils.FileLog.Error("新增产品失败,err:" + err.Error())
  271. br.Msg = "新增产品失败"
  272. br.ErrMsg = "新增产品失败,err:" + err.Error()
  273. }
  274. return
  275. }
  276. br.Ret = 200
  277. br.Success = true
  278. br.Msg = "新增产品成功"
  279. return
  280. }
  281. // UpdateSaleStatus @Title 上下架产品
  282. // @Description 上下架产品
  283. // @Param File query file true "文件"
  284. // @Success 200 {object} models.ReportAuthorResp
  285. // @router /updateSaleStatus [post]
  286. func (this *ProductController) UpdateSaleStatus() {
  287. br := new(models.BaseResponse).Init()
  288. defer func() {
  289. this.Data["json"] = br
  290. this.ServeJSON()
  291. }()
  292. defer func() {
  293. this.Data["json"] = br
  294. this.ServeJSON()
  295. }()
  296. var req request.ProductSaleStatusReq
  297. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  298. if err != nil {
  299. br.Msg = "参数解析异常!"
  300. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  301. return
  302. }
  303. if req.ProductId <= 0 {
  304. br.Msg = "产品编号非法!"
  305. br.ErrMsg = "产品编号非法,不能小于0"
  306. return
  307. }
  308. if req.SaleStatus != "onSale" && req.SaleStatus != "offSale" {
  309. br.Msg = "产品销售状态非法!"
  310. br.ErrMsg = "产品销售状态非法,未知的上下架类型:" + req.SaleStatus
  311. return
  312. }
  313. var saleStatus models.SaleStatus
  314. var messageStatus string
  315. switch req.SaleStatus {
  316. case "onSale":
  317. saleStatus = models.OnSale
  318. messageStatus = "上架"
  319. case "offSale":
  320. saleStatus = models.OffSale
  321. messageStatus = "下架"
  322. }
  323. product := models.MerchantProduct{
  324. Id: req.ProductId,
  325. SaleStatus: saleStatus,
  326. UpdatedTime: time.Now(),
  327. }
  328. err = product.UpdateProductSaleStatus()
  329. if err != nil {
  330. br.Msg = messageStatus + "失败"
  331. br.ErrMsg = messageStatus + "失败,err:" + err.Error()
  332. return
  333. }
  334. br.Msg = messageStatus + "成功"
  335. br.Ret = 200
  336. br.Success = true
  337. }
  338. // DeleteProduct @Title 删除产品
  339. // @Description 删除产品
  340. // @Param File query file true "文件"
  341. // @Success 200 {object} models.ReportAuthorResp
  342. // @router /deleteProduct [post]
  343. func (this *ProductController) DeleteProduct() {
  344. br := new(models.BaseResponse).Init()
  345. defer func() {
  346. this.Data["json"] = br
  347. this.ServeJSON()
  348. }()
  349. defer func() {
  350. this.Data["json"] = br
  351. this.ServeJSON()
  352. }()
  353. var req request.ProductSaleStatusReq
  354. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  355. if err != nil {
  356. br.Msg = "参数解析异常!"
  357. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  358. return
  359. }
  360. if req.ProductId <= 0 {
  361. br.Msg = "产品编号非法!"
  362. br.ErrMsg = "产品编号非法,不能小于0"
  363. return
  364. }
  365. product := models.MerchantProduct{
  366. Id: req.ProductId,
  367. Deleted: true,
  368. UpdatedTime: time.Now(),
  369. }
  370. err = product.Delete()
  371. if err != nil {
  372. br.Msg = "删除产品失败"
  373. br.ErrMsg = "删除产品失败,err:" + err.Error()
  374. return
  375. }
  376. br.Msg = "删除产品成功"
  377. br.Ret = 200
  378. br.Success = true
  379. }
  380. // ProductList
  381. // @Title 产品列表
  382. // @Description pdf研报列表
  383. // @Param PageSize query int true "每页数据条数"
  384. // @Param CurrentIndex query int true "当前页页码,从1开始"
  385. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  386. // @Param KeyWord query string true "报告标题/创建人"
  387. // @Param SortType query string true "排序方式"
  388. // @Success 200 {object} models.ReportAuthorResp
  389. // @router /productList [get]
  390. func (this *ProductController) ProductList() {
  391. br := new(models.BaseResponse).Init()
  392. defer func() {
  393. this.Data["json"] = br
  394. this.ServeJSON()
  395. }()
  396. pageSize, _ := this.GetInt("PageSize")
  397. currentIndex, _ := this.GetInt("CurrentIndex")
  398. sortType := this.GetString("SortType")
  399. KeyWord := this.GetString("KeyWord")
  400. CreatedTime := this.GetString("CreatedTime")
  401. UpdatedTime := this.GetString("UpdatedTime")
  402. IsSingle, _ := this.GetBool("IsSingle", true)
  403. ProductType := this.GetString("ProductType")
  404. SaleStatus := this.GetString("SaleStatus")
  405. var condition string
  406. if pageSize <= 0 {
  407. pageSize = utils.PageSize20
  408. }
  409. if currentIndex <= 0 {
  410. currentIndex = 1
  411. }
  412. if KeyWord != "" {
  413. condition += " AND title like '%" + KeyWord + "%'"
  414. }
  415. sortCondition := " ORDER BY created_time "
  416. if sortType == "" {
  417. sortType = "DESC"
  418. }
  419. if CreatedTime != "" {
  420. condition += " AND Date(created_time) = '" + CreatedTime + "'"
  421. }
  422. if UpdatedTime != "" {
  423. condition += " AND Date(updated_time) = '" + UpdatedTime + "'"
  424. }
  425. if SaleStatus != "" {
  426. switch SaleStatus {
  427. case "onSale":
  428. condition += " AND sale_status='on_sale'"
  429. case "offSale":
  430. condition += " AND sale_status='off_sale'"
  431. default:
  432. br.Msg = "无效的销售状态"
  433. br.ErrMsg = "无效的销售状态:" + SaleStatus
  434. return
  435. }
  436. }
  437. if IsSingle {
  438. if ProductType != "" {
  439. switch ProductType {
  440. case "report":
  441. condition += " AND type='" + string(models.ProductReport) + "'"
  442. case "audio":
  443. condition += " AND type='" + string(models.ProductAudio) + "'"
  444. case "video":
  445. condition += " AND type='" + string(models.ProductVideo) + "'"
  446. default:
  447. br.Msg = "无效的产品类型"
  448. br.ErrMsg = "无效的产品类型:" + ProductType
  449. return
  450. }
  451. } else {
  452. condition += " AND type != '" + string(models.ProductPackage) + "'"
  453. }
  454. } else {
  455. condition += " AND type = '" + string(models.ProductPackage) + "'"
  456. }
  457. sortCondition = sortCondition + sortType
  458. total, err := models.GetProductCountByCondition(condition)
  459. if err != nil {
  460. br.Msg = "获取产品列表失败"
  461. br.ErrMsg = "获取产品列表失败,Err:" + err.Error()
  462. return
  463. }
  464. startSize := utils.StartIndex(currentIndex, pageSize)
  465. List, err := models.GetProductByCondition(condition, sortCondition, startSize, pageSize)
  466. if err != nil {
  467. br.Msg = "获取产品列表失败"
  468. br.ErrMsg = "获取产品列表失败,Err:" + err.Error()
  469. return
  470. }
  471. var ListView []*services.ProductView
  472. for _, product := range List {
  473. view := &services.ProductView{
  474. Id: product.Id,
  475. ProductName: product.Title,
  476. ProductType: CNProductMap[product.Type],
  477. PublishedTime: product.CreatedTime.Format(time.DateTime),
  478. Price: fmt.Sprintf("¥%s", product.Price),
  479. SaleStatus: CNSaleStatusMap[product.SaleStatus],
  480. CoverSrc: product.CoverUrl,
  481. ValidDays: product.ValidDays,
  482. Creator: product.Creator,
  483. IsPermanent: product.IsPermanent,
  484. Description: product.Description,
  485. SourceId: product.SourceId,
  486. }
  487. if product.CoverUrl == "" && product.CoverSrc > 0 {
  488. image, imageErr := models.GetImageById(product.CoverSrc)
  489. if err != nil {
  490. utils.FileLog.Warn("获取图片资源失败,err:%s,imageId:%d", imageErr, product.CoverSrc)
  491. } else {
  492. view.CoverSrc = image.SrcUrl
  493. }
  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].Id
  591. }
  592. } else {
  593. product.CoverUrl = req.CoverSrc
  594. }
  595. }
  596. switch req.Type {
  597. case "report":
  598. _, product.Title, err = services.GetRiskLevel("report", req.SourceId)
  599. case "audio":
  600. _, product.Title, err = services.GetRiskLevel("audio", req.SourceId)
  601. case "video":
  602. _, product.Title, err = services.GetRiskLevel("video", req.SourceId)
  603. case "package":
  604. _, _, 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.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. utils.FileLog.Error("编辑产品失败,err:" + err.Error())
  651. br.Msg = "编辑产品失败"
  652. br.ErrMsg = "编辑产品失败,err:" + err.Error()
  653. return
  654. }
  655. br.Ret = 200
  656. br.Success = true
  657. br.Msg = "编辑产品成功"
  658. return
  659. }
  660. // UploadFile @Title 上传图片
  661. // @Description 上传图片
  662. // @Param File query file true "文件"
  663. // @Success 200 {object} models.ReportAuthorResp
  664. // @router /uploadFile [post]
  665. func (this *ProductController) UploadFile() {
  666. br := new(models.BaseResponse).Init()
  667. defer func() {
  668. this.Data["json"] = br
  669. this.ServeJSON()
  670. }()
  671. f, h, err := this.GetFile("File")
  672. if err != nil {
  673. br.Msg = "获取资源信息失败"
  674. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  675. return
  676. }
  677. defer f.Close()
  678. size, err := strconv.Atoi(utils.UPLOAD_IMG_SIZE)
  679. if err != nil {
  680. size = 100
  681. }
  682. if h.Size > 1024*1024*int64(size) {
  683. br.Msg = fmt.Sprintf("图片大小不能超过%dK", size)
  684. br.ErrMsg = "图片上传失败,Err:" + err.Error()
  685. return
  686. }
  687. ext := path.Ext(h.Filename)
  688. dateDir := time.Now().Format("20060102")
  689. uploadDir := utils.STATIC_DIR + "ht/product" + dateDir
  690. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  691. if err != nil {
  692. br.Msg = "存储目录创建失败"
  693. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  694. return
  695. }
  696. randStr := utils.GetRandStringNoSpecialChar(28)
  697. fileName := randStr + ext
  698. fpath := uploadDir + "/" + fileName
  699. err = this.SaveToFile("File", fpath)
  700. if err != nil {
  701. br.Msg = "图片上传失败"
  702. br.ErrMsg = "图片上传失败,Err:" + err.Error()
  703. return
  704. }
  705. audioUploadDir := utils.RESOURCE_DIR + "img/"
  706. savePdfToOssPath := audioUploadDir + time.Now().Format("200601/20060102/")
  707. audioName := utils.GetRandStringNoSpecialChar(28)
  708. savePdfToOssPath += audioName + ext
  709. defer func() {
  710. err = os.Remove(fpath)
  711. fmt.Sprintf("删除文件失败:%v", err)
  712. }()
  713. ossClient := services.NewOssClient()
  714. if ossClient == nil {
  715. br.Msg = "图片上传失败"
  716. br.ErrMsg = "初始化OSS服务失败"
  717. return
  718. }
  719. mp3Url, err := ossClient.UploadFile("", fpath, savePdfToOssPath)
  720. if err != nil {
  721. br.Msg = "图片上传失败"
  722. br.ErrMsg = "图片上传失败,Err:" + err.Error()
  723. return
  724. }
  725. base := path.Base(h.Filename)
  726. resp := new(response.MediaUploadResp)
  727. resp.Url = mp3Url
  728. resp.FileName = base
  729. br.Data = resp
  730. br.Msg = "上传成功"
  731. br.Ret = 200
  732. br.Success = true
  733. }