product_interior.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. package cygx
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "github.com/tealeg/xlsx"
  6. "hongze/hz_crm_api/controllers"
  7. "hongze/hz_crm_api/models"
  8. "hongze/hz_crm_api/models/cygx"
  9. "hongze/hz_crm_api/models/system"
  10. cygxService "hongze/hz_crm_api/services/cygx"
  11. "hongze/hz_crm_api/utils"
  12. "os"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // 产品内测
  19. type ProductInteriorController struct {
  20. controllers.BaseAuthController
  21. }
  22. // @Title 新增
  23. // @Description 新增产品内测接口
  24. // @Param request body cygx.AddProductInteriorReq true "type json string"
  25. // @Success 200 {object} "保存成功"
  26. // @router /productInterior/preserveAndPublish [post]
  27. func (this *ProductInteriorController) PreserveAndPublish() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. sysUser := this.SysUser
  34. if sysUser == nil {
  35. br.Msg = "请登录"
  36. br.ErrMsg = "请登录,SysUser Is Empty"
  37. br.Ret = 408
  38. return
  39. }
  40. var req cygx.AddProductInteriorReq
  41. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  42. if err != nil {
  43. br.Msg = "参数解析异常!"
  44. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  45. return
  46. }
  47. publishTime := utils.StrDateToDate(req.PublishTime) //时间字符串格式转时间格式
  48. productInteriorId := req.ProductInteriorId
  49. columnName := req.ColumnName
  50. title := req.Title
  51. abstract := req.Abstract
  52. department := req.Department
  53. body := req.Body
  54. industrialManagementIds := req.IndustrialManagementIds
  55. industrialSubjectIds := req.IndustrialSubjectIds
  56. matchTypeId := req.MatchTypeId
  57. chartPermissionId := req.ChartPermissionId
  58. // 产业ID校验
  59. if industrialManagementIds != "" {
  60. industrialManagementIdList := strings.Split(industrialManagementIds, ",")
  61. for _, v := range industrialManagementIdList {
  62. _, err := strconv.Atoi(v)
  63. if err != nil {
  64. br.Msg = "参数解析异常!"
  65. br.ErrMsg = "产业ID不规范,Err:" + err.Error() + industrialManagementIds
  66. return
  67. }
  68. }
  69. }
  70. if industrialSubjectIds != "" {
  71. industrialSubjectIdList := strings.Split(industrialSubjectIds, ",")
  72. for _, v := range industrialSubjectIdList {
  73. _, err := strconv.Atoi(v)
  74. if err != nil {
  75. br.Msg = "参数解析异常!"
  76. br.ErrMsg = "标的ID不规范,Err:" + err.Error() + industrialSubjectIds
  77. return
  78. }
  79. }
  80. }
  81. charInfo, errCategory := cygx.GetCategoryInfoById(chartPermissionId)
  82. if errCategory != nil {
  83. br.Msg = "获取品种信息失败"
  84. br.ErrMsg = "获取品种信息失败,Err:" + errCategory.Error()
  85. return
  86. }
  87. item := new(cygx.CygxProductInterior)
  88. item.Status = req.DoType
  89. item.ColumnName = columnName
  90. item.Title = title
  91. item.PublishTime = publishTime
  92. item.CreateTime = time.Now()
  93. item.ModifyTime = time.Now()
  94. item.Body = body
  95. item.Abstract = abstract
  96. item.Department = department
  97. item.AdminId = sysUser.AdminId
  98. item.MatchTypeId = matchTypeId
  99. item.ChartPermissionId = chartPermissionId
  100. item.ChartPermissionName = charInfo.PermissionName
  101. if req.DoType == 1 {
  102. item.IsCancel = 0
  103. }
  104. if productInteriorId == 0 {
  105. //新增
  106. err = cygx.AddProductInterior(item, industrialManagementIds, industrialSubjectIds)
  107. } else {
  108. //更新
  109. detail, err := cygx.GetCygxProductInteriorDetail(productInteriorId)
  110. if err != nil {
  111. br.Msg = "详情不存在"
  112. br.ErrMsg = "获取失败,Err:" + err.Error()
  113. return
  114. }
  115. if req.DoType == 1 {
  116. item.Status = 1
  117. item.IsCancel = 0
  118. } else {
  119. item.IsCancel = detail.IsCancel
  120. item.Status = detail.Status
  121. }
  122. item.ProductInteriorId = productInteriorId
  123. item.VisibleRange = detail.VisibleRange
  124. err = cygx.UpdateProductInterior(item, industrialManagementIds, industrialSubjectIds)
  125. }
  126. if err != nil {
  127. br.Msg = "保存失败"
  128. br.ErrMsg = "保存失败,Err:" + err.Error()
  129. return
  130. }
  131. br.Ret = 200
  132. br.Success = true
  133. br.IsAddLog = true
  134. br.Msg = "操作成功"
  135. }
  136. // @Title 列表
  137. // @Description 列表接口
  138. // @Param PageSize query int true "每页数据条数"
  139. // @Param CurrentIndex query int true "当前页页码,从1开始"
  140. // @Param Status query int false "发布状态 ,0未发布,1已发布,传2查询所有"
  141. // @Param StartDate query string false "开始时间 ,列如2021-03-06 "
  142. // @Param EndDate query string false "结束时间,列如2021-03-06 "
  143. // @Success Ret=200 {object} cygx.GetCygxTacticsTimeLineResp
  144. // @router /productInterior/list [get]
  145. func (this *ProductInteriorController) List() {
  146. br := new(models.BaseResponse).Init()
  147. defer func() {
  148. this.Data["json"] = br
  149. this.ServeJSON()
  150. }()
  151. sysUser := this.SysUser
  152. if sysUser == nil {
  153. br.Msg = "请登录"
  154. br.ErrMsg = "请登录,SysUser Is Empty"
  155. br.Ret = 408
  156. return
  157. }
  158. resp := new(cygx.GetCygxProductInteriorResp)
  159. pageSize, _ := this.GetInt("PageSize")
  160. currentIndex, _ := this.GetInt("CurrentIndex")
  161. status, _ := this.GetInt("Status")
  162. startDate := this.GetString("StartDate")
  163. endDate := this.GetString("EndDate")
  164. var startSize int
  165. if pageSize <= 0 {
  166. pageSize = utils.PageSize20
  167. }
  168. if currentIndex <= 0 {
  169. currentIndex = 1
  170. }
  171. startSize = utils.StartIndex(currentIndex, pageSize)
  172. var condition string
  173. var pars []interface{}
  174. if status == 0 || status == 1 {
  175. condition += ` AND art.status = ? `
  176. pars = append(pars, status)
  177. }
  178. if startDate != "" && endDate != "" {
  179. condition += ` AND art.publish_time BETWEEN ? AND ? `
  180. pars = append(pars, startDate, endDate)
  181. }
  182. total, err := cygx.GetCygxProductInteriorCount(condition, pars)
  183. if err != nil {
  184. br.Msg = "获取失败"
  185. br.ErrMsg = "获取失败,Err:" + err.Error()
  186. return
  187. }
  188. condition += " ORDER BY art.publish_time DESC , art.product_interior_id DESC "
  189. list, err := cygx.GetCygxProductInteriorList(condition, pars, startSize, pageSize)
  190. if err != nil {
  191. br.Msg = "获取失败"
  192. br.ErrMsg = "获取失败,Err:" + err.Error()
  193. return
  194. }
  195. var productInteriorIds []int
  196. for _, v := range list {
  197. v.PublishTime = utils.TimeRemoveHms(v.PublishTime)
  198. if v.IsCancel == 1 {
  199. v.Status = 3
  200. }
  201. productInteriorIds = append(productInteriorIds, v.ProductInteriorId)
  202. }
  203. //获取pv/Uv map
  204. mapPv, mapUv := cygxService.GetCygxProductInteriorHistoryListMap(productInteriorIds)
  205. //mapMsg := cygxService.GetCygxProductInteriorMsgListMap(productInteriorIds) // 留言数据
  206. mapLabel := cygxService.GetCygxProductInteriorLabelListMap(productInteriorIds) // 标签
  207. mapMatchTypeName := cygxService.GetCygxReportMappingCygxListMap() //报告匹配类型
  208. for _, v := range list {
  209. v.Pv = mapPv[v.ProductInteriorId]
  210. v.Uv = mapUv[v.ProductInteriorId]
  211. v.Label = mapLabel[v.ProductInteriorId]
  212. v.MatchTypeName = mapMatchTypeName[v.MatchTypeId]
  213. }
  214. page := paging.GetPaging(currentIndex, pageSize, total)
  215. resp.List = list
  216. resp.Paging = page
  217. br.Ret = 200
  218. br.Success = true
  219. br.Msg = "获取成功"
  220. br.Data = resp
  221. }
  222. // @Title 详情
  223. // @Description 获取详情接口
  224. // @Param ProductInteriorId query int true "ID"
  225. // @Success Ret=200 {object} cygx.GetCygxProductInteriorDetailResp
  226. // @router /productInterior/detail [get]
  227. func (this *ProductInteriorController) Detail() {
  228. br := new(models.BaseResponse).Init()
  229. defer func() {
  230. this.Data["json"] = br
  231. this.ServeJSON()
  232. }()
  233. AdminUser := this.SysUser
  234. if AdminUser == nil {
  235. br.Msg = "请登录"
  236. br.ErrMsg = "请登录,用户信息为空"
  237. br.Ret = 408
  238. return
  239. }
  240. resp := new(cygx.GetCygxProductInteriorDetailResp)
  241. productInteriorId, _ := this.GetInt("ProductInteriorId")
  242. if productInteriorId < 1 {
  243. br.Msg = "请输入详情ID"
  244. return
  245. }
  246. detail, err := cygx.GetCygxProductInteriorDetail(productInteriorId)
  247. if err != nil {
  248. br.Msg = "详情不存在"
  249. br.ErrMsg = "获取失败,Err:" + err.Error()
  250. return
  251. }
  252. detail.PublishTime = utils.TimeRemoveHms2(detail.PublishTime)
  253. industrialList, err := cygx.GetProductInteriorIndustrialGroupManagementList(productInteriorId)
  254. if err != nil && err.Error() != utils.ErrNoRow() {
  255. br.Msg = "获取信息失败"
  256. br.ErrMsg = "GetProductInteriorIndustrialGroupManagementList,Err:" + err.Error() + "productInteriorId:" + strconv.Itoa(productInteriorId)
  257. return
  258. }
  259. subjectList, err := cygx.GetProductInteriorIndustrialGroupSubjecttList(productInteriorId)
  260. if err != nil && err.Error() != utils.ErrNoRow() {
  261. br.Msg = "获取信息失败"
  262. br.ErrMsg = "GetProductInteriorIndustrialGroupSubjecttList,Err:" + err.Error() + "productInteriorId:" + strconv.Itoa(productInteriorId)
  263. return
  264. }
  265. if detail.MatchTypeId > 0 {
  266. matchDetail, err := cygx.GetCygxReportMappingCygxDetail(detail.MatchTypeId)
  267. if err != nil {
  268. br.Msg = "获取信息失败"
  269. br.ErrMsg = "GetCygxReportMappingCygxDetail,Err:" + err.Error()
  270. return
  271. }
  272. if matchDetail != nil {
  273. detail.MatchTypeName = matchDetail.MatchTypeName
  274. }
  275. }
  276. detail.ListIndustrial = industrialList
  277. detail.ListSubject = subjectList
  278. resp.Detail = detail
  279. br.Ret = 200
  280. br.Success = true
  281. br.Msg = "获取成功"
  282. br.Data = resp
  283. }
  284. // @Title 删除
  285. // @Description 获取详情接口
  286. // @Param request body cygx.TacticsTimeLineTimeLineIdReq true "type json string"
  287. // @Success 200 {object} "操作成功"
  288. // @router /productInterior/delete [POST]
  289. func (this *ProductInteriorController) Delete() {
  290. br := new(models.BaseResponse).Init()
  291. defer func() {
  292. this.Data["json"] = br
  293. this.ServeJSON()
  294. }()
  295. AdminUser := this.SysUser
  296. if AdminUser == nil {
  297. br.Msg = "请登录"
  298. br.ErrMsg = "请登录,用户信息为空"
  299. br.Ret = 408
  300. return
  301. }
  302. var req cygx.ProductInteriorIdReq
  303. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  304. if err != nil {
  305. br.Msg = "参数解析异常!"
  306. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  307. return
  308. }
  309. productInteriorId := req.ProductInteriorId
  310. if productInteriorId == 0 {
  311. br.Msg = "参数错误"
  312. br.ErrMsg = "参数错误,id不可为空"
  313. return
  314. }
  315. err = cygx.DeleteProductInterior(productInteriorId)
  316. if err != nil {
  317. br.Msg = "详情不存在"
  318. br.ErrMsg = "获取失败,Err:" + err.Error()
  319. return
  320. }
  321. br.Ret = 200
  322. br.Success = true
  323. br.IsAddLog = true
  324. br.Msg = "删除成功"
  325. }
  326. // @Title 发布/取消发布报告
  327. // @Description 发布/取消发布报告接口
  328. // @Param request body cygx.ProductInteriorIdReq true "type json string"
  329. // @Success 200 Ret=200 发布成功
  330. // @router /productInterior/publishAndcancel [post]
  331. func (this *ProductInteriorController) PublishReport() {
  332. br := new(models.BaseResponse).Init()
  333. defer func() {
  334. this.Data["json"] = br
  335. this.ServeJSON()
  336. }()
  337. var req cygx.ProductInteriorIdReq
  338. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  339. if err != nil {
  340. br.Msg = "参数解析异常!"
  341. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  342. return
  343. }
  344. productInteriorId := req.ProductInteriorId
  345. if productInteriorId == 0 {
  346. br.Msg = "参数错误"
  347. br.ErrMsg = "参数错误,id不可为空"
  348. return
  349. }
  350. detail, err := cygx.GetCygxProductInteriorDetail(productInteriorId)
  351. if err != nil {
  352. br.Msg = "详情不存在"
  353. br.ErrMsg = "获取失败,Err:" + err.Error()
  354. return
  355. }
  356. var status int
  357. var isCancel int
  358. if detail.Status == 0 {
  359. status = 1
  360. isCancel = 0
  361. } else {
  362. status = 0
  363. isCancel = 1
  364. go cygxService.UpdateResourceData(productInteriorId, "productinterior", "delete", time.Now().Format(utils.FormatDateTime))
  365. }
  366. err = cygx.EditProductInteriorStatus(status, isCancel, productInteriorId)
  367. if err != nil {
  368. br.Msg = "操作失败"
  369. br.ErrMsg = "获取失败,Err:" + err.Error()
  370. return
  371. }
  372. br.Ret = 200
  373. br.Success = true
  374. br.Msg = "操作成功"
  375. }
  376. // @Title 可见范围修改
  377. // @Description 可见范围修改接口
  378. // @Param request body cygx.ProductInteriorIdReq true "type json string"
  379. // @Success 200 操作成功
  380. // @router /productInterior/visibleRange [post]
  381. func (this *ProductInteriorController) VisibleRange() {
  382. br := new(models.BaseResponse).Init()
  383. defer func() {
  384. this.Data["json"] = br
  385. this.ServeJSON()
  386. }()
  387. AdminUser := this.SysUser
  388. if AdminUser == nil {
  389. br.Msg = "请登录"
  390. br.ErrMsg = "请登录,用户信息为空"
  391. br.Ret = 408
  392. return
  393. }
  394. var req cygx.ProductInteriorIdReq
  395. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  396. if err != nil {
  397. br.Msg = "参数解析异常!"
  398. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  399. return
  400. }
  401. productInteriorId := req.ProductInteriorId
  402. detail, err := cygx.GetCygxProductInteriorDetail(productInteriorId)
  403. if err != nil {
  404. br.Msg = "详情不存在"
  405. br.ErrMsg = "获取失败,Err:" + err.Error()
  406. return
  407. }
  408. var visibleRange int
  409. if detail.VisibleRange == 0 {
  410. visibleRange = 1
  411. go cygxService.UpdateResourceData(productInteriorId, "productinterior", "add", time.Now().Format(utils.FormatDateTime))
  412. go cygxService.SendWxMsgWithCygxProductInterior(productInteriorId)
  413. } else {
  414. visibleRange = 0
  415. go cygxService.UpdateResourceData(productInteriorId, "productinterior", "delete", time.Now().Format(utils.FormatDateTime))
  416. }
  417. err = cygx.ProductInteriorVisibleRange(visibleRange, productInteriorId)
  418. if err != nil {
  419. br.Msg = "操作失败"
  420. br.ErrMsg = "操作失败,Err:" + err.Error()
  421. return
  422. }
  423. br.Ret = 200
  424. br.Success = true
  425. br.Msg = "操作成功"
  426. br.IsAddLog = true
  427. }
  428. // @Title 下载PV
  429. // @Description 下载PV接口
  430. // @Param ProductInteriorId query int true "ID"
  431. // @router /productInterior/PvExport [get]
  432. func (this *ProductInteriorController) PvExport() {
  433. br := new(models.BaseResponse).Init()
  434. defer func() {
  435. this.Data["json"] = br
  436. this.ServeJSON()
  437. }()
  438. AdminUser := this.SysUser
  439. if AdminUser == nil {
  440. br.Msg = "请登录"
  441. br.ErrMsg = "请登录,用户信息为空"
  442. br.Ret = 408
  443. return
  444. }
  445. productInteriorId, _ := this.GetInt("ProductInteriorId")
  446. if productInteriorId < 1 {
  447. br.Msg = "请输入详情ID"
  448. return
  449. }
  450. var condition string
  451. var pars []interface{}
  452. condition = ` AND product_interior_id = ? `
  453. pars = append(pars, productInteriorId)
  454. var respList []*cygx.CygxProductInteriorHistory
  455. list, err := cygx.GetCygxProductInteriorHistoryList(condition, pars)
  456. if err != nil {
  457. br.Msg = "获取失败"
  458. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  459. return
  460. }
  461. //超级管理员和权益管理员、权益研究员可以下载所有客户,销售组长能下载本组客户,销售只能下载本人名下客户
  462. resp := new(cygx.CanDownload)
  463. adminInfo, errAdmin := system.GetSysUserById(AdminUser.AdminId)
  464. if errAdmin != nil {
  465. br.Msg = "获取失败"
  466. br.ErrMsg = "获取失败,Err:" + errAdmin.Error()
  467. return
  468. }
  469. if adminInfo.Role == "admin" || adminInfo.Role == "researcher" {
  470. resp.IsCanDownload = true
  471. }
  472. //销售查看自己客户,销售组长查看组员
  473. if resp.IsCanDownload == false {
  474. mapMobile, err := cygxService.GetAdminLookUserMobile(adminInfo)
  475. if err != nil {
  476. br.Msg = "获取失败"
  477. br.ErrMsg = "获取失败,销售对应权限,Err:" + err.Error()
  478. return
  479. }
  480. for _, v := range list {
  481. if _, ok := mapMobile[v.Mobile]; ok {
  482. respList = append(respList, v)
  483. }
  484. }
  485. } else {
  486. respList = list
  487. }
  488. //创建excel
  489. dir, err := os.Executable()
  490. exPath := filepath.Dir(dir)
  491. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  492. xlsxFile := xlsx.NewFile()
  493. if err != nil {
  494. br.Msg = "生成文件失败"
  495. br.ErrMsg = "生成文件失败"
  496. return
  497. }
  498. style := xlsx.NewStyle()
  499. alignment := xlsx.Alignment{
  500. Horizontal: "center",
  501. Vertical: "center",
  502. WrapText: true,
  503. }
  504. style.Alignment = alignment
  505. style.ApplyAlignment = true
  506. sheet, err := xlsxFile.AddSheet("阅读明细")
  507. if err != nil {
  508. br.Msg = "新增Sheet失败"
  509. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  510. return
  511. }
  512. rowTitle := sheet.AddRow()
  513. cellA := rowTitle.AddCell()
  514. cellA.Value = "姓名"
  515. cellB := rowTitle.AddCell()
  516. cellB.Value = "手机号"
  517. cellC := rowTitle.AddCell()
  518. cellC.Value = "公司名称"
  519. cellD := rowTitle.AddCell()
  520. cellD.Value = "所属权益销售"
  521. cellE := rowTitle.AddCell()
  522. cellE.Value = "阅读时间"
  523. for _, item := range respList {
  524. row := sheet.AddRow()
  525. cellA := row.AddCell()
  526. cellA.Value = item.RealName
  527. cellB := row.AddCell()
  528. cellB.Value = item.Mobile
  529. cellC := row.AddCell()
  530. cellC.Value = item.CompanyName
  531. cellD := row.AddCell()
  532. cellD.Value = item.SellerName
  533. cellE := row.AddCell()
  534. cellE.Value = item.CreateTime.Format(utils.FormatDateTime)
  535. }
  536. err = xlsxFile.Save(downLoadnFilePath)
  537. if err != nil {
  538. br.Msg = "保存文件失败"
  539. br.ErrMsg = "保存文件失败"
  540. return
  541. }
  542. downloadFileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  543. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  544. defer func() {
  545. os.Remove(downLoadnFilePath)
  546. }()
  547. br.Ret = 200
  548. br.Success = true
  549. br.Msg = "获取成功"
  550. }