product.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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: req.ProductId,
  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. sortColumn := this.GetString("SortColumn")
  401. KeyWord := this.GetString("KeyWord")
  402. CreatedTime := this.GetString("CreatedTime")
  403. UpdatedTime := this.GetString("UpdatedTime")
  404. IsSingle, _ := this.GetBool("IsSingle", true)
  405. ProductType := this.GetString("ProductType")
  406. SaleStatus := this.GetString("SaleStatus")
  407. var condition string
  408. if pageSize <= 0 {
  409. pageSize = utils.PageSize20
  410. }
  411. if currentIndex <= 0 {
  412. currentIndex = 1
  413. }
  414. if KeyWord != "" {
  415. condition += " AND title like '%" + KeyWord + "%'"
  416. }
  417. var sortCondition string
  418. if sortColumn == "" {
  419. sortCondition = " ORDER BY created_time "
  420. } else {
  421. sortCondition = " ORDER BY " + sortColumn + " "
  422. }
  423. if sortType == "" {
  424. sortType = "DESC"
  425. }
  426. if CreatedTime != "" {
  427. condition += " AND Date(created_time) = '" + CreatedTime + "'"
  428. }
  429. if UpdatedTime != "" {
  430. condition += " AND Date(updated_time) = '" + UpdatedTime + "'"
  431. }
  432. if SaleStatus != "" {
  433. switch SaleStatus {
  434. case "onSale":
  435. condition += " AND sale_status='on_sale'"
  436. case "offSale":
  437. condition += " AND sale_status='off_sale'"
  438. default:
  439. br.Msg = "无效的销售状态"
  440. br.ErrMsg = "无效的销售状态:" + SaleStatus
  441. return
  442. }
  443. }
  444. if IsSingle {
  445. if ProductType != "" {
  446. switch ProductType {
  447. case "report":
  448. condition += " AND type='" + string(models.ProductReport) + "'"
  449. case "audio":
  450. condition += " AND type='" + string(models.ProductAudio) + "'"
  451. case "video":
  452. condition += " AND type='" + string(models.ProductVideo) + "'"
  453. default:
  454. br.Msg = "无效的产品类型"
  455. br.ErrMsg = "无效的产品类型:" + ProductType
  456. return
  457. }
  458. } else {
  459. condition += " AND type != '" + string(models.ProductPackage) + "'"
  460. }
  461. } else {
  462. condition += " AND type = '" + string(models.ProductPackage) + "'"
  463. }
  464. sortCondition = sortCondition + sortType
  465. total, err := models.GetProductCountByCondition(condition)
  466. if err != nil {
  467. br.Msg = "获取产品列表失败"
  468. br.ErrMsg = "获取产品列表失败,Err:" + err.Error()
  469. return
  470. }
  471. startSize := utils.StartIndex(currentIndex, pageSize)
  472. List, err := models.GetProductByCondition(condition, sortCondition, startSize, pageSize)
  473. if err != nil {
  474. br.Msg = "获取产品列表失败"
  475. br.ErrMsg = "获取产品列表失败,Err:" + err.Error()
  476. return
  477. }
  478. var ListView []*services.ProductView
  479. for _, product := range List {
  480. view := &services.ProductView{
  481. Id: product.Id,
  482. ProductName: product.Title,
  483. ProductType: CNProductMap[product.Type],
  484. PublishedTime: product.CreatedTime.Format(time.DateTime),
  485. Price: fmt.Sprintf("¥%s", product.Price),
  486. SaleStatus: CNSaleStatusMap[product.SaleStatus],
  487. CoverSrc: product.CoverUrl,
  488. ValidDays: product.ValidDays,
  489. Creator: product.Creator,
  490. IsPermanent: product.IsPermanent,
  491. Description: product.Description,
  492. SourceId: product.SourceId,
  493. }
  494. if product.CoverUrl == "" && product.CoverSrc > 0 {
  495. image, imageErr := models.GetImageById(product.CoverSrc)
  496. if err != nil {
  497. utils.FileLog.Warn("获取图片资源失败,err:%s,imageId:%d", imageErr, product.CoverSrc)
  498. } else {
  499. view.CoverSrc = image.SrcUrl
  500. }
  501. }
  502. if !product.UpdatedTime.IsZero() {
  503. view.UpdatedTime = product.UpdatedTime.Format(time.DateTime)
  504. }
  505. view.RiskLevel, _, _, _ = services.GetRiskLevel(string(product.Type), product.SourceId)
  506. ListView = append(ListView, view)
  507. }
  508. page := paging.GetPaging(currentIndex, pageSize, total)
  509. resp := new(response.ProductListResp)
  510. resp.List = ListView
  511. resp.Paging = page
  512. br.Ret = 200
  513. br.Success = true
  514. br.Data = resp
  515. br.Msg = "获取成功"
  516. }
  517. // ProductRisk
  518. // @Title 产品列表
  519. // @Description pdf研报列表
  520. // @Param PageSize query int true "每页数据条数"
  521. // @Param CurrentIndex query int true "当前页页码,从1开始"
  522. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  523. // @Param KeyWord query string true "报告标题/创建人"
  524. // @Param SortType query string true "排序方式"
  525. // @Success 200 {object} models.ReportAuthorResp
  526. // @router /productRisk [get]
  527. func (this *ProductController) ProductRisk() {
  528. br := new(models.BaseResponse).Init()
  529. defer func() {
  530. this.Data["json"] = br
  531. this.ServeJSON()
  532. }()
  533. SourceId, _ := this.GetInt("SourceId", 0)
  534. ProductType := this.GetString("ProductType")
  535. if SourceId <= 0 {
  536. br.Msg = "无效的产品ID"
  537. br.ErrMsg = "无效的产品ID:" + strconv.Itoa(SourceId)
  538. return
  539. }
  540. riskLevel, _, _, err := services.GetRiskLevel(ProductType, SourceId)
  541. if err != nil {
  542. utils.FileLog.Error("查询产品风险等级失败", err.Error)
  543. return
  544. }
  545. resp := new(response.ProductRiskResp)
  546. resp.RiskLevel = riskLevel
  547. resp.SourceId = SourceId
  548. resp.ProductType = ProductType
  549. br.Ret = 200
  550. br.Success = true
  551. br.Data = resp
  552. br.Msg = "获取成功"
  553. }
  554. // EditProduct @Title 编辑产品
  555. // @Description 编辑产品
  556. // @Param File query file true "文件"
  557. // @Success 200 {object} models.ReportAuthorResp
  558. // @router /editProduct [post]
  559. func (this *ProductController) EditProduct() {
  560. br := new(models.BaseResponse).Init()
  561. defer func() {
  562. this.Data["json"] = br
  563. this.ServeJSON()
  564. }()
  565. var req request.ProductReq
  566. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  567. if err != nil {
  568. br.Msg = "参数解析异常!"
  569. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  570. return
  571. }
  572. var product models.MerchantProduct
  573. if req.Type == "package" {
  574. if req.ValidDays <= 0 {
  575. br.Msg = "套餐有效期非法"
  576. br.ErrMsg = "套餐有效期非法,天数不能是负数"
  577. return
  578. }
  579. if req.ProductName == "" {
  580. br.Msg = "套餐名称不能为空"
  581. br.ErrMsg = "套餐名称不能为空"
  582. return
  583. }
  584. product.Title = req.ProductName
  585. product.ValidDays = req.ValidDays
  586. product.Description = req.Description
  587. if req.CoverSrc == "" {
  588. var imageList []models.ImageSource
  589. imageList, err = models.GetImageByPermissionId(req.SourceId)
  590. if err != nil {
  591. utils.FileLog.Error("套餐封面获取失败", err.Error())
  592. //br.Msg = "默认套餐封面获取失败,请上传封面"
  593. //br.ErrMsg = "默认套餐封面获取失败,请上传封面,Err:" + err.Error()
  594. //return
  595. } else {
  596. if len(imageList) > 0 {
  597. var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
  598. index := rnd.Intn(len(imageList))
  599. product.CoverSrc = imageList[index].Id
  600. }
  601. }
  602. } else {
  603. product.CoverUrl = req.CoverSrc
  604. }
  605. }
  606. switch req.Type {
  607. case "report":
  608. _, product.Title, _, err = services.GetRiskLevel("report", req.SourceId)
  609. case "audio":
  610. _, product.Title, _, err = services.GetRiskLevel("audio", req.SourceId)
  611. case "video":
  612. _, product.Title, _, err = services.GetRiskLevel("video", req.SourceId)
  613. case "package":
  614. _, _, _, err = services.GetRiskLevel("package", req.SourceId)
  615. default:
  616. br.Msg = "产品类型错误"
  617. br.ErrMsg = "获取产品列表失败,Err:产品类型错误"
  618. return
  619. }
  620. if err != nil {
  621. utils.FileLog.Error("编辑产品失败", err.Error())
  622. br.Msg = "编辑产品失败"
  623. if strings.Contains(err.Error(), "<QuerySeter> no row found") {
  624. br.Msg = "编辑产品失败,产品信息不存在"
  625. } else {
  626. br.Msg = "编辑产品失败" + err.Error()
  627. }
  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.Id = req.ProductId
  648. product.SaleStatus = models.OnSale
  649. product.CreatedTime = time.Now()
  650. product.Price = req.Price
  651. product.SourceId = req.SourceId
  652. product.Type = models.MerchantProductType(req.Type)
  653. product.Creator = this.SysUser.SysRealName
  654. if product.Type == "" {
  655. br.Msg = "编辑产品失败"
  656. br.ErrMsg = "产品类型为空"
  657. return
  658. }
  659. err = product.EditProduct()
  660. if err != nil {
  661. utils.FileLog.Error("编辑产品失败,err:" + err.Error())
  662. br.Msg = "编辑产品失败"
  663. br.ErrMsg = "编辑产品失败,err:" + err.Error()
  664. return
  665. }
  666. br.Ret = 200
  667. br.Success = true
  668. br.Msg = "编辑产品成功"
  669. return
  670. }
  671. // UploadFile @Title 上传图片
  672. // @Description 上传图片
  673. // @Param File query file true "文件"
  674. // @Success 200 {object} models.ReportAuthorResp
  675. // @router /uploadFile [post]
  676. func (this *ProductController) UploadFile() {
  677. br := new(models.BaseResponse).Init()
  678. defer func() {
  679. this.Data["json"] = br
  680. this.ServeJSON()
  681. }()
  682. f, h, err := this.GetFile("File")
  683. if err != nil {
  684. br.Msg = "获取资源信息失败"
  685. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  686. return
  687. }
  688. defer f.Close()
  689. size, err := strconv.Atoi(utils.UPLOAD_IMG_SIZE)
  690. if err != nil {
  691. size = 100
  692. }
  693. if h.Size > 1024*1024*int64(size) {
  694. br.Msg = fmt.Sprintf("图片大小不能超过%dK", size)
  695. br.ErrMsg = "图片上传失败,Err:" + err.Error()
  696. return
  697. }
  698. ext := path.Ext(h.Filename)
  699. dateDir := time.Now().Format("20060102")
  700. uploadDir := utils.STATIC_DIR + "ht/product" + dateDir
  701. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  702. if err != nil {
  703. br.Msg = "存储目录创建失败"
  704. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  705. return
  706. }
  707. randStr := utils.GetRandStringNoSpecialChar(28)
  708. fileName := randStr + ext
  709. fpath := uploadDir + "/" + fileName
  710. err = this.SaveToFile("File", fpath)
  711. if err != nil {
  712. br.Msg = "图片上传失败"
  713. br.ErrMsg = "图片上传失败,Err:" + err.Error()
  714. return
  715. }
  716. audioUploadDir := utils.RESOURCE_DIR + "img/"
  717. savePdfToOssPath := audioUploadDir + time.Now().Format("200601/20060102/")
  718. audioName := utils.GetRandStringNoSpecialChar(28)
  719. savePdfToOssPath += audioName + ext
  720. defer func() {
  721. err = os.Remove(fpath)
  722. fmt.Sprintf("删除文件失败:%v", err)
  723. }()
  724. ossClient := services.NewOssClient()
  725. if ossClient == nil {
  726. br.Msg = "图片上传失败"
  727. br.ErrMsg = "初始化OSS服务失败"
  728. return
  729. }
  730. mp3Url, err := ossClient.UploadFile("", fpath, savePdfToOssPath)
  731. if err != nil {
  732. br.Msg = "图片上传失败"
  733. br.ErrMsg = "图片上传失败,Err:" + err.Error()
  734. return
  735. }
  736. base := path.Base(h.Filename)
  737. resp := new(response.MediaUploadResp)
  738. resp.Url = mp3Url
  739. resp.FileName = base
  740. br.Data = resp
  741. br.Msg = "上传成功"
  742. br.Ret = 200
  743. br.Success = true
  744. }