product_interior.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. go cygxService.UpdateProductInteriorResourceData(productInteriorId) //写入首页最新 cygx_resource_data 表
  132. br.Ret = 200
  133. br.Success = true
  134. br.IsAddLog = true
  135. br.Msg = "操作成功"
  136. }
  137. // @Title 列表
  138. // @Description 列表接口
  139. // @Param PageSize query int true "每页数据条数"
  140. // @Param CurrentIndex query int true "当前页页码,从1开始"
  141. // @Param Status query int false "发布状态 ,0未发布,1已发布,传2查询所有"
  142. // @Param StartDate query string false "开始时间 ,列如2021-03-06 "
  143. // @Param EndDate query string false "结束时间,列如2021-03-06 "
  144. // @Success Ret=200 {object} cygx.GetCygxTacticsTimeLineResp
  145. // @router /productInterior/list [get]
  146. func (this *ProductInteriorController) List() {
  147. br := new(models.BaseResponse).Init()
  148. defer func() {
  149. this.Data["json"] = br
  150. this.ServeJSON()
  151. }()
  152. sysUser := this.SysUser
  153. if sysUser == nil {
  154. br.Msg = "请登录"
  155. br.ErrMsg = "请登录,SysUser Is Empty"
  156. br.Ret = 408
  157. return
  158. }
  159. resp := new(cygx.GetCygxProductInteriorResp)
  160. pageSize, _ := this.GetInt("PageSize")
  161. currentIndex, _ := this.GetInt("CurrentIndex")
  162. status, _ := this.GetInt("Status")
  163. startDate := this.GetString("StartDate")
  164. endDate := this.GetString("EndDate")
  165. var startSize int
  166. if pageSize <= 0 {
  167. pageSize = utils.PageSize20
  168. }
  169. if currentIndex <= 0 {
  170. currentIndex = 1
  171. }
  172. startSize = utils.StartIndex(currentIndex, pageSize)
  173. var condition string
  174. var pars []interface{}
  175. if status == 0 || status == 1 {
  176. condition += ` AND art.status = ? `
  177. pars = append(pars, status)
  178. }
  179. if startDate != "" && endDate != "" {
  180. condition += ` AND art.publish_time BETWEEN ? AND ? `
  181. pars = append(pars, startDate, endDate)
  182. }
  183. total, err := cygx.GetCygxProductInteriorCount(condition, pars)
  184. if err != nil {
  185. br.Msg = "获取失败"
  186. br.ErrMsg = "获取失败,Err:" + err.Error()
  187. return
  188. }
  189. condition += " ORDER BY art.publish_time DESC , art.product_interior_id DESC "
  190. list, err := cygx.GetCygxProductInteriorList(condition, pars, startSize, pageSize)
  191. if err != nil {
  192. br.Msg = "获取失败"
  193. br.ErrMsg = "获取失败,Err:" + err.Error()
  194. return
  195. }
  196. var productInteriorIds []int
  197. for _, v := range list {
  198. v.PublishTime = utils.TimeRemoveHms(v.PublishTime)
  199. if v.IsCancel == 1 {
  200. v.Status = 3
  201. }
  202. productInteriorIds = append(productInteriorIds, v.ProductInteriorId)
  203. }
  204. //获取pv/Uv map
  205. mapPv, mapUv := cygxService.GetCygxProductInteriorHistoryListMap(productInteriorIds)
  206. //mapMsg := cygxService.GetCygxProductInteriorMsgListMap(productInteriorIds) // 留言数据
  207. mapLabel := cygxService.GetCygxProductInteriorLabelListMap(productInteriorIds) // 标签
  208. mapMatchTypeName := cygxService.GetCygxReportMappingCygxListMap() //报告匹配类型
  209. for _, v := range list {
  210. v.Pv = mapPv[v.ProductInteriorId]
  211. v.Uv = mapUv[v.ProductInteriorId]
  212. v.Label = mapLabel[v.ProductInteriorId]
  213. v.MatchTypeName = mapMatchTypeName[v.MatchTypeId]
  214. }
  215. page := paging.GetPaging(currentIndex, pageSize, total)
  216. resp.List = list
  217. resp.Paging = page
  218. br.Ret = 200
  219. br.Success = true
  220. br.Msg = "获取成功"
  221. br.Data = resp
  222. }
  223. // @Title 详情
  224. // @Description 获取详情接口
  225. // @Param ProductInteriorId query int true "ID"
  226. // @Success Ret=200 {object} cygx.GetCygxProductInteriorDetailResp
  227. // @router /productInterior/detail [get]
  228. func (this *ProductInteriorController) Detail() {
  229. br := new(models.BaseResponse).Init()
  230. defer func() {
  231. this.Data["json"] = br
  232. this.ServeJSON()
  233. }()
  234. AdminUser := this.SysUser
  235. if AdminUser == nil {
  236. br.Msg = "请登录"
  237. br.ErrMsg = "请登录,用户信息为空"
  238. br.Ret = 408
  239. return
  240. }
  241. resp := new(cygx.GetCygxProductInteriorDetailResp)
  242. productInteriorId, _ := this.GetInt("ProductInteriorId")
  243. if productInteriorId < 1 {
  244. br.Msg = "请输入详情ID"
  245. return
  246. }
  247. detail, err := cygx.GetCygxProductInteriorDetail(productInteriorId)
  248. if err != nil {
  249. br.Msg = "详情不存在"
  250. br.ErrMsg = "获取失败,Err:" + err.Error()
  251. return
  252. }
  253. detail.PublishTime = utils.TimeRemoveHms2(detail.PublishTime)
  254. industrialList, err := cygx.GetProductInteriorIndustrialGroupManagementList(productInteriorId)
  255. if err != nil && err.Error() != utils.ErrNoRow() {
  256. br.Msg = "获取信息失败"
  257. br.ErrMsg = "GetProductInteriorIndustrialGroupManagementList,Err:" + err.Error() + "productInteriorId:" + strconv.Itoa(productInteriorId)
  258. return
  259. }
  260. subjectList, err := cygx.GetProductInteriorIndustrialGroupSubjecttList(productInteriorId)
  261. if err != nil && err.Error() != utils.ErrNoRow() {
  262. br.Msg = "获取信息失败"
  263. br.ErrMsg = "GetProductInteriorIndustrialGroupSubjecttList,Err:" + err.Error() + "productInteriorId:" + strconv.Itoa(productInteriorId)
  264. return
  265. }
  266. if detail.MatchTypeId > 0 {
  267. matchDetail, err := cygx.GetCygxReportMappingCygxDetail(detail.MatchTypeId)
  268. if err != nil {
  269. br.Msg = "获取信息失败"
  270. br.ErrMsg = "GetCygxReportMappingCygxDetail,Err:" + err.Error()
  271. return
  272. }
  273. if matchDetail != nil {
  274. detail.MatchTypeName = matchDetail.MatchTypeName
  275. }
  276. }
  277. detail.ListIndustrial = industrialList
  278. detail.ListSubject = subjectList
  279. resp.Detail = detail
  280. br.Ret = 200
  281. br.Success = true
  282. br.Msg = "获取成功"
  283. br.Data = resp
  284. }
  285. // @Title 删除
  286. // @Description 获取详情接口
  287. // @Param request body cygx.TacticsTimeLineTimeLineIdReq true "type json string"
  288. // @Success 200 {object} "操作成功"
  289. // @router /productInterior/delete [POST]
  290. func (this *ProductInteriorController) Delete() {
  291. br := new(models.BaseResponse).Init()
  292. defer func() {
  293. this.Data["json"] = br
  294. this.ServeJSON()
  295. }()
  296. AdminUser := this.SysUser
  297. if AdminUser == nil {
  298. br.Msg = "请登录"
  299. br.ErrMsg = "请登录,用户信息为空"
  300. br.Ret = 408
  301. return
  302. }
  303. var req cygx.ProductInteriorIdReq
  304. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  305. if err != nil {
  306. br.Msg = "参数解析异常!"
  307. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  308. return
  309. }
  310. productInteriorId := req.ProductInteriorId
  311. if productInteriorId == 0 {
  312. br.Msg = "参数错误"
  313. br.ErrMsg = "参数错误,id不可为空"
  314. return
  315. }
  316. err = cygx.DeleteProductInterior(productInteriorId)
  317. if err != nil {
  318. br.Msg = "详情不存在"
  319. br.ErrMsg = "获取失败,Err:" + err.Error()
  320. return
  321. }
  322. br.Ret = 200
  323. br.Success = true
  324. br.IsAddLog = true
  325. br.Msg = "删除成功"
  326. }
  327. // @Title 发布/取消发布报告
  328. // @Description 发布/取消发布报告接口
  329. // @Param request body cygx.ProductInteriorIdReq true "type json string"
  330. // @Success 200 Ret=200 发布成功
  331. // @router /productInterior/publishAndcancel [post]
  332. func (this *ProductInteriorController) PublishReport() {
  333. br := new(models.BaseResponse).Init()
  334. defer func() {
  335. this.Data["json"] = br
  336. this.ServeJSON()
  337. }()
  338. var req cygx.ProductInteriorIdReq
  339. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  340. if err != nil {
  341. br.Msg = "参数解析异常!"
  342. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  343. return
  344. }
  345. productInteriorId := req.ProductInteriorId
  346. if productInteriorId == 0 {
  347. br.Msg = "参数错误"
  348. br.ErrMsg = "参数错误,id不可为空"
  349. return
  350. }
  351. detail, err := cygx.GetCygxProductInteriorDetail(productInteriorId)
  352. if err != nil {
  353. br.Msg = "详情不存在"
  354. br.ErrMsg = "获取失败,Err:" + err.Error()
  355. return
  356. }
  357. var status int
  358. var isCancel int
  359. if detail.Status == 0 {
  360. status = 1
  361. isCancel = 0
  362. } else {
  363. status = 0
  364. isCancel = 1
  365. //go cygxService.UpdateResourceData(productInteriorId, "productinterior", "delete", time.Now().Format(utils.FormatDateTime))
  366. go cygxService.UpdateProductInteriorResourceData(productInteriorId) //写入首页最新 cygx_resource_data 表
  367. }
  368. err = cygx.EditProductInteriorStatus(status, isCancel, productInteriorId)
  369. if err != nil {
  370. br.Msg = "操作失败"
  371. br.ErrMsg = "获取失败,Err:" + err.Error()
  372. return
  373. }
  374. br.Ret = 200
  375. br.Success = true
  376. br.Msg = "操作成功"
  377. }
  378. // @Title 可见范围修改
  379. // @Description 可见范围修改接口
  380. // @Param request body cygx.ProductInteriorIdReq true "type json string"
  381. // @Success 200 操作成功
  382. // @router /productInterior/visibleRange [post]
  383. func (this *ProductInteriorController) VisibleRange() {
  384. br := new(models.BaseResponse).Init()
  385. defer func() {
  386. this.Data["json"] = br
  387. this.ServeJSON()
  388. }()
  389. AdminUser := this.SysUser
  390. if AdminUser == nil {
  391. br.Msg = "请登录"
  392. br.ErrMsg = "请登录,用户信息为空"
  393. br.Ret = 408
  394. return
  395. }
  396. var req cygx.ProductInteriorIdReq
  397. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  398. if err != nil {
  399. br.Msg = "参数解析异常!"
  400. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  401. return
  402. }
  403. productInteriorId := req.ProductInteriorId
  404. detail, err := cygx.GetCygxProductInteriorDetail(productInteriorId)
  405. if err != nil {
  406. br.Msg = "详情不存在"
  407. br.ErrMsg = "获取失败,Err:" + err.Error()
  408. return
  409. }
  410. var visibleRange int
  411. if detail.VisibleRange == 0 {
  412. visibleRange = 1
  413. //go cygxService.UpdateResourceData(productInteriorId, "productinterior", "add", time.Now().Format(utils.FormatDateTime))
  414. go cygxService.SendWxMsgWithCygxProductInterior(productInteriorId)
  415. } else {
  416. visibleRange = 0
  417. //go cygxService.UpdateResourceData(productInteriorId, "productinterior", "delete", time.Now().Format(utils.FormatDateTime))
  418. }
  419. err = cygx.ProductInteriorVisibleRange(visibleRange, productInteriorId)
  420. if err != nil {
  421. br.Msg = "操作失败"
  422. br.ErrMsg = "操作失败,Err:" + err.Error()
  423. return
  424. }
  425. go cygxService.UpdateProductInteriorResourceData(productInteriorId) //写入首页最新 cygx_resource_data 表
  426. br.Ret = 200
  427. br.Success = true
  428. br.Msg = "操作成功"
  429. br.IsAddLog = true
  430. }
  431. // @Title 下载PV
  432. // @Description 下载PV接口
  433. // @Param ProductInteriorId query int true "ID"
  434. // @router /productInterior/PvExport [get]
  435. func (this *ProductInteriorController) PvExport() {
  436. br := new(models.BaseResponse).Init()
  437. defer func() {
  438. this.Data["json"] = br
  439. this.ServeJSON()
  440. }()
  441. AdminUser := this.SysUser
  442. if AdminUser == nil {
  443. br.Msg = "请登录"
  444. br.ErrMsg = "请登录,用户信息为空"
  445. br.Ret = 408
  446. return
  447. }
  448. productInteriorId, _ := this.GetInt("ProductInteriorId")
  449. if productInteriorId < 1 {
  450. br.Msg = "请输入详情ID"
  451. return
  452. }
  453. var condition string
  454. var pars []interface{}
  455. condition = ` AND product_interior_id = ? `
  456. pars = append(pars, productInteriorId)
  457. var respList []*cygx.CygxProductInteriorHistory
  458. list, err := cygx.GetCygxProductInteriorHistoryList(condition, pars)
  459. if err != nil {
  460. br.Msg = "获取失败"
  461. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  462. return
  463. }
  464. //超级管理员和权益管理员、权益研究员可以下载所有客户,销售组长能下载本组客户,销售只能下载本人名下客户
  465. resp := new(cygx.CanDownload)
  466. adminInfo, errAdmin := system.GetSysUserById(AdminUser.AdminId)
  467. if errAdmin != nil {
  468. br.Msg = "获取失败"
  469. br.ErrMsg = "获取失败,Err:" + errAdmin.Error()
  470. return
  471. }
  472. if adminInfo.Role == "admin" || adminInfo.Role == "researcher" {
  473. resp.IsCanDownload = true
  474. }
  475. //销售查看自己客户,销售组长查看组员
  476. if resp.IsCanDownload == false {
  477. mapMobile, err := cygxService.GetAdminLookUserMobile(adminInfo)
  478. if err != nil {
  479. br.Msg = "获取失败"
  480. br.ErrMsg = "获取失败,销售对应权限,Err:" + err.Error()
  481. return
  482. }
  483. for _, v := range list {
  484. if _, ok := mapMobile[v.Mobile]; ok {
  485. respList = append(respList, v)
  486. }
  487. }
  488. } else {
  489. respList = list
  490. }
  491. //创建excel
  492. dir, err := os.Executable()
  493. exPath := filepath.Dir(dir)
  494. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  495. xlsxFile := xlsx.NewFile()
  496. if err != nil {
  497. br.Msg = "生成文件失败"
  498. br.ErrMsg = "生成文件失败"
  499. return
  500. }
  501. style := xlsx.NewStyle()
  502. alignment := xlsx.Alignment{
  503. Horizontal: "center",
  504. Vertical: "center",
  505. WrapText: true,
  506. }
  507. style.Alignment = alignment
  508. style.ApplyAlignment = true
  509. sheet, err := xlsxFile.AddSheet("阅读明细")
  510. if err != nil {
  511. br.Msg = "新增Sheet失败"
  512. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  513. return
  514. }
  515. rowTitle := sheet.AddRow()
  516. cellA := rowTitle.AddCell()
  517. cellA.Value = "姓名"
  518. cellB := rowTitle.AddCell()
  519. cellB.Value = "手机号"
  520. cellC := rowTitle.AddCell()
  521. cellC.Value = "公司名称"
  522. cellD := rowTitle.AddCell()
  523. cellD.Value = "所属权益销售"
  524. cellE := rowTitle.AddCell()
  525. cellE.Value = "阅读时间"
  526. for _, item := range respList {
  527. row := sheet.AddRow()
  528. cellA := row.AddCell()
  529. cellA.Value = item.RealName
  530. cellB := row.AddCell()
  531. cellB.Value = item.Mobile
  532. cellC := row.AddCell()
  533. cellC.Value = item.CompanyName
  534. cellD := row.AddCell()
  535. cellD.Value = item.SellerName
  536. cellE := row.AddCell()
  537. cellE.Value = item.CreateTime.Format(utils.FormatDateTime)
  538. }
  539. err = xlsxFile.Save(downLoadnFilePath)
  540. if err != nil {
  541. br.Msg = "保存文件失败"
  542. br.ErrMsg = "保存文件失败"
  543. return
  544. }
  545. downloadFileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  546. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  547. defer func() {
  548. os.Remove(downLoadnFilePath)
  549. }()
  550. br.Ret = 200
  551. br.Success = true
  552. br.Msg = "获取成功"
  553. }