classify.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. package services
  2. import (
  3. "errors"
  4. "eta/eta_mobile/models"
  5. "eta/eta_mobile/models/report_approve"
  6. "eta/eta_mobile/utils"
  7. "fmt"
  8. "sort"
  9. "time"
  10. )
  11. // MoveReportClassify 移动分类
  12. func MoveReportClassify(req models.ClassifyMoveReq) (err error, errMsg string) {
  13. classifyId := req.ClassifyId
  14. prevClassifyId := req.PrevClassifyId
  15. nextClassifyId := req.NextClassifyId
  16. //如果有传入 上一个兄弟节点分类id
  17. var (
  18. classifyInfo *models.Classify
  19. prevClassify *models.Classify
  20. nextClassify *models.Classify
  21. prevSort int
  22. nextSort int
  23. )
  24. // 移动对象为分类, 判断权限
  25. classifyInfo, err = models.GetClassifyById(classifyId)
  26. if err != nil {
  27. if err.Error() == utils.ErrNoRow() {
  28. errMsg = "分类不存在, 请刷新页面"
  29. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  30. return
  31. }
  32. errMsg = "移动失败"
  33. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  34. return
  35. } else if classifyInfo.Id == 0 {
  36. errMsg = "分类不存在, 请刷新页面"
  37. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  38. return
  39. }
  40. parentClassifyId := classifyInfo.ParentId
  41. if prevClassifyId > 0 {
  42. prevClassify, err = models.GetClassifyById(prevClassifyId)
  43. if err != nil {
  44. if err.Error() == utils.ErrNoRow() {
  45. errMsg = "上一个分类不存在, 请刷新页面"
  46. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  47. return
  48. }
  49. errMsg = "移动失败"
  50. err = fmt.Errorf("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  51. return
  52. }
  53. if prevClassify.ParentId != parentClassifyId {
  54. errMsg = "禁止拖动到其他节点"
  55. err = fmt.Errorf(errMsg)
  56. return
  57. }
  58. prevSort = prevClassify.Sort
  59. }
  60. if nextClassifyId > 0 {
  61. //下一个兄弟节点
  62. nextClassify, err = models.GetClassifyById(nextClassifyId)
  63. if err != nil {
  64. if err.Error() == utils.ErrNoRow() {
  65. errMsg = "下一个分类不存在, 请刷新页面"
  66. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  67. return
  68. }
  69. errMsg = "移动失败"
  70. err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  71. return
  72. }
  73. if nextClassify.ParentId != parentClassifyId {
  74. errMsg = "禁止拖动到其他节点"
  75. err = fmt.Errorf(errMsg)
  76. return
  77. }
  78. nextSort = nextClassify.Sort
  79. }
  80. err, errMsg = moveReportClassify(classifyInfo, prevClassify, nextClassify, parentClassifyId, prevSort, nextSort)
  81. return
  82. }
  83. // moveReportClassify 移动分类
  84. func moveReportClassify(classifyInfo, prevClassify, nextClassify *models.Classify, parentId, prevSort, nextSort int) (err error, errMsg string) {
  85. ob := new(models.Classify)
  86. updateCol := make([]string, 0)
  87. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  88. if classifyInfo.ParentId != parentId {
  89. errMsg = "移动失败"
  90. err = fmt.Errorf("不支持目录层级变更")
  91. return
  92. }
  93. if prevSort > 0 {
  94. //如果是移动在两个兄弟节点之间
  95. if nextSort > 0 {
  96. //下一个兄弟节点
  97. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  98. if prevSort == nextSort || prevSort == classifyInfo.Sort {
  99. //变更兄弟节点的排序
  100. updateSortStr := `sort + 2`
  101. //变更分类
  102. if prevClassify != nil {
  103. _ = models.UpdateClassifySortByParentId(parentId, prevClassify.Id, prevClassify.Sort, updateSortStr)
  104. } else {
  105. _ = models.UpdateClassifySortByParentId(parentId, 0, prevSort, updateSortStr)
  106. }
  107. } else {
  108. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  109. if nextSort-prevSort == 1 {
  110. //变更兄弟节点的排序
  111. updateSortStr := `sort + 1`
  112. //变更分类
  113. if prevClassify != nil {
  114. _ = models.UpdateClassifySortByParentId(parentId, prevClassify.Id, prevSort, updateSortStr)
  115. } else {
  116. _ = models.UpdateClassifySortByParentId(parentId, 0, prevSort, updateSortStr)
  117. }
  118. }
  119. }
  120. }
  121. classifyInfo.Sort = prevSort + 1
  122. classifyInfo.ModifyTime = time.Now()
  123. updateCol = append(updateCol, "Sort", "ModifyTime")
  124. } else if prevClassify == nil && nextClassify == nil && parentId > 0 {
  125. //处理只拖动到目录里,默认放到目录底部的情况
  126. var maxSort int
  127. maxSort, err = ob.GetMaxSortByParentId(parentId)
  128. if err != nil {
  129. errMsg = "移动失败"
  130. err = fmt.Errorf("查询组内排序信息失败,Err:" + err.Error())
  131. return
  132. }
  133. classifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  134. classifyInfo.ModifyTime = time.Now()
  135. updateCol = append(updateCol, "Sort", "ModifyTime")
  136. } else {
  137. // 拖动到父级分类的第一位
  138. firstPermission, tmpErr := ob.GetFirstClassifyByParentId(parentId)
  139. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  140. errMsg = "移动失败"
  141. err = fmt.Errorf("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  142. return
  143. }
  144. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  145. if firstPermission != nil && firstPermission.Id != 0 && firstPermission.Sort == 0 {
  146. updateSortStr := ` sort + 1 `
  147. _ = models.UpdateClassifySortByParentId(parentId, firstPermission.Id-1, 0, updateSortStr)
  148. }
  149. classifyInfo.Sort = 0 //那就是排在第一位
  150. classifyInfo.ModifyTime = time.Now()
  151. updateCol = append(updateCol, "Sort", "ModifyTime")
  152. }
  153. //更新
  154. if len(updateCol) > 0 {
  155. err = classifyInfo.UpdateClassify(updateCol)
  156. if err != nil {
  157. errMsg = "移动失败"
  158. err = fmt.Errorf("修改失败,Err:" + err.Error())
  159. return
  160. }
  161. }
  162. return
  163. }
  164. // AddReportClassify
  165. // @Description: 添加报告分类
  166. // @author: Roc
  167. // @datetime 2024-06-17 11:01:21
  168. // @param classifyName string
  169. // @param parentId int
  170. // @param chartPermissionIdList []int
  171. // @return err error
  172. // @return errMsg string
  173. // @return isSendEmail bool
  174. func AddReportClassify(classifyName string, parentId int, chartPermissionIdList []int) (err error, errMsg string, isSendEmail bool) {
  175. isSendEmail = true
  176. errMsg = `添加失败`
  177. item, err := models.GetClassifyByName(classifyName, parentId)
  178. if err != nil && err.Error() != utils.ErrNoRow() {
  179. errMsg = "获取分类信息失败"
  180. if err.Error() == utils.ErrNoRow() {
  181. errMsg = "分类名称:" + classifyName + "已存在"
  182. isSendEmail = false
  183. }
  184. return
  185. }
  186. if item != nil {
  187. errMsg = "分类名称:" + classifyName + "已存在"
  188. isSendEmail = false
  189. err = errors.New(errMsg)
  190. return
  191. }
  192. level := 1
  193. // 父级分类
  194. var parentClassifyItem *models.Classify
  195. // 父级分类下的子分类数量
  196. var childClassifyCount int
  197. if parentId > 0 {
  198. // 获取父级分类信息
  199. parentClassifyItem, err = models.GetClassifyById(parentId)
  200. if err != nil {
  201. errMsg = "获取父级分类信息失败"
  202. if err.Error() == utils.ErrNoRow() {
  203. errMsg = "父级分类不存在"
  204. }
  205. return
  206. }
  207. level = parentClassifyItem.Level + 1
  208. if level > 3 {
  209. errMsg = "分类层级不可超过三级"
  210. isSendEmail = false
  211. return
  212. }
  213. // 判断是否分类存在待操作的审批单
  214. err, errMsg = checkClassifyApprove(parentClassifyItem)
  215. if err != nil {
  216. return
  217. }
  218. // 获取父级分类下的子分类数量
  219. childClassifyCount, err = models.GetCountClassifyChildByParentId(parentId)
  220. if err != nil {
  221. errMsg = "获取父级分类的子分类信息失败"
  222. return
  223. }
  224. }
  225. nowTime := time.Now().Local()
  226. classify := new(models.Classify)
  227. maxSort, err := classify.GetMaxSort()
  228. if err != nil {
  229. errMsg = "操作失败"
  230. err = errors.New("查询品种排序失败, Err: " + err.Error())
  231. return
  232. }
  233. classify.ClassifyName = classifyName
  234. classify.ParentId = parentId
  235. classify.CreateTime = nowTime
  236. classify.ModifyTime = nowTime
  237. classify.Sort = maxSort + 1
  238. classify.Enabled = 1
  239. classify.ShowType = 1 //默认列表格式
  240. classify.IsShow = 1
  241. classify.Level = level
  242. /*classify.Abstract = req.Abstract
  243. classify.Descript = req.Descript
  244. classify.Abstract = req.Abstract
  245. classify.Descript = req.Descript
  246. classify.ReportAuthor = req.ReportAuthor
  247. classify.AuthorDescript = req.AuthorDescript
  248. classify.ColumnImgUrl = req.ColumnImgUrl
  249. classify.ReportImgUrl = req.ReportImgUrl
  250. classify.HeadImgUrl = req.HeadImgUrl
  251. classify.AvatarImgUrl = req.AvatarImgUrl
  252. classify.HomeImgUrl = req.HomeImgUrl
  253. classify.ClassifyLabel = req.ClassifyLabel
  254. classify.ShowType = req.ShowType
  255. classify.HasTeleconference = req.HasTeleconference
  256. classify.VipTitle = req.VipTitle
  257. classify.IsShow = req.IsShow
  258. classify.YbFiccSort = req.YbFiccSort
  259. classify.YbFiccIcon = req.YbFiccIcon
  260. classify.YbFiccPcIcon = req.YbFiccPcIcon
  261. classify.YbIconUrl = req.YbIconUrl
  262. classify.YbBgUrl = req.YbBgUrl
  263. classify.YbListImg = req.YbListImg
  264. classify.YbShareBgImg = req.YbShareBgImg
  265. classify.YbRightBanner = req.YbRightBanner
  266. classify.RelateTel = req.RelateTel
  267. classify.RelateVideo = req.RelateVideo
  268. if req.ParentId > 0 {
  269. parentClassify := new(models.Classify)
  270. if parentClassify, err = models.GetClassifyById(req.ParentId); err != nil {
  271. br.Msg = "获取父级分类信息失败"
  272. br.ErrMsg = "获取父级分类信息失败, Err:" + err.Error()
  273. return
  274. }
  275. updateParent := false
  276. updateCols := make([]string, 0)
  277. updateCols = append(updateCols, "HasTeleconference")
  278. if req.HasTeleconference == 1 {
  279. // 二级分类包含电话会,则一级分类也默认包含电话会
  280. if parentClassify.HasTeleconference == 0 {
  281. parentClassify.HasTeleconference = 1
  282. updateParent = true
  283. }
  284. } else {
  285. // 二级分类均无电话会,则一级分类也无电话会
  286. if parentClassify.HasTeleconference == 1 {
  287. child, err := models.GetClassifyChild(parentClassify.Id, "")
  288. if err != nil {
  289. br.Msg = "获取子分类失败"
  290. br.ErrMsg = "获取子分类失败,Err:" + err.Error()
  291. return
  292. }
  293. // 存在同一级分类下的二级分类有电话会则不变动
  294. hasTel := false
  295. for i := 0; i < len(child); i++ {
  296. if child[i].HasTeleconference == 1 {
  297. hasTel = true
  298. break
  299. }
  300. }
  301. if !hasTel {
  302. parentClassify.HasTeleconference = 0
  303. updateParent = true
  304. }
  305. }
  306. }
  307. if updateParent {
  308. if err = parentClassify.UpdateClassify(updateCols); err != nil {
  309. br.Msg = "更新父级分类失败"
  310. br.ErrMsg = "更新父级分类失败, Err:" + err.Error()
  311. return
  312. }
  313. }
  314. }*/
  315. err = models.AddClassify(classify)
  316. if err != nil {
  317. return
  318. }
  319. //获取报告分类权限列表
  320. err = models.EditChartPermissionSearchKeyWordMappingMulti(classifyName, chartPermissionIdList, classify.Id)
  321. if err != nil {
  322. errMsg = "修改分类权限失败"
  323. return
  324. }
  325. // 修改CRM权限
  326. go func() {
  327. _ = EditClassifyChartPermissionSync(classifyName, classify.Id)
  328. }()
  329. // 如果父级分类不为空的话,那么就标记有子级分类,同时
  330. if parentClassifyItem != nil {
  331. parentClassifyItem.HasChild = 1
  332. parentClassifyItem.UpdateClassify([]string{"HasChild"})
  333. // 如果以前没有子级分类,那么就继承父级分类下的章节类型(创建新的章节与分类的关系)
  334. if childClassifyCount <= 0 {
  335. // 继承父级分类下的章节类型(创建新的章节与分类的关系)
  336. tmpErr := inheritReportChapterType(parentId, classify.Id)
  337. if tmpErr != nil {
  338. return
  339. }
  340. // 继承父级分类审批流
  341. go inheritReportApproveFlow(parentClassifyItem, classify)
  342. moveReportByAddClassify(parentClassifyItem, classify)
  343. }
  344. }
  345. return
  346. }
  347. // checkClassifyApprove
  348. // @Description: 判断分类是否存在待操作的审批单
  349. // @author: Roc
  350. // @datetime 2024-06-27 13:19:15
  351. // @param currClassify *models.Classify
  352. // @return err error
  353. // @return errMsg string
  354. func checkClassifyApprove(currClassify *models.Classify) (err error, errMsg string) {
  355. errMsg = `判断是否有审批流关联失败`
  356. var firstClassifyId, secondClassifyId int
  357. if currClassify.ParentId > 0 {
  358. parentClassifyItem, tmpErr := models.GetClassifyById(currClassify.ParentId)
  359. if tmpErr != nil {
  360. err = tmpErr
  361. errMsg = "获取父级分类信息失败"
  362. if tmpErr.Error() == utils.ErrNoRow() {
  363. errMsg = "父级分类不存在"
  364. }
  365. return
  366. }
  367. firstClassifyId = parentClassifyItem.Id
  368. secondClassifyId = currClassify.Id
  369. } else {
  370. firstClassifyId = currClassify.Id
  371. }
  372. // 校验审批流是否关联了进行中的审批
  373. {
  374. flowOb := new(report_approve.ReportApproveFlow)
  375. existCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ? AND %s = ?`, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifyFirstId, report_approve.ReportApproveFlowCols.ClassifySecondId, report_approve.ReportApproveFlowCols.ClassifyThirdId)
  376. existPars := make([]interface{}, 0)
  377. existPars = append(existPars, report_approve.FlowReportTypeChinese, firstClassifyId, secondClassifyId, 0)
  378. flowItem, e := flowOb.GetItemByCondition(existCond, existPars, "")
  379. if e != nil {
  380. // 父级分类如果没有审批流,那么就正常进行就好了
  381. if e.Error() != utils.ErrNoRow() {
  382. err = errors.New("获取审批流是否已存在失败, Err: " + e.Error())
  383. return
  384. }
  385. err = nil
  386. return
  387. }
  388. if flowItem == nil {
  389. return
  390. }
  391. approvingOb := new(report_approve.ReportApprove)
  392. approvingCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, report_approve.ReportApproveCols.FlowId, report_approve.ReportApproveCols.FlowVersion, report_approve.ReportApproveCols.State)
  393. approvingPars := make([]interface{}, 0)
  394. approvingPars = append(approvingPars, flowItem.ReportApproveFlowId, flowItem.CurrVersion, report_approve.ReportApproveStateApproving)
  395. count, e := approvingOb.GetCountByCondition(approvingCond, approvingPars)
  396. if e != nil {
  397. err = errors.New("获取审批流关联进行中的审批数失败. Err: " + e.Error())
  398. return
  399. }
  400. if count > 0 {
  401. errMsg = "当前有未走完流程的报告,请走完流程后再做变更"
  402. err = errors.New(errMsg)
  403. return
  404. }
  405. }
  406. return
  407. }
  408. // 关于研报分类,因只允许报告或品种关联在最小分类下,所以当某个父分类(非三级分类)已关联报告或品种,需要在该父分类下增加子分类,则第一个子分类添加成功时,默认把该父分类关联的品种和报告转移至新创建的子分类(第一个子分类)下
  409. // moveReportByAddClassify
  410. // @Description: 报告和章节的转移
  411. // @author: Roc
  412. // @datetime 2024-06-17 16:29:56
  413. // @param parentClassifyInfo *models.Classify
  414. // @param currClassifyInfo *models.Classify
  415. // @return err error
  416. func moveReportByAddClassify(parentClassifyInfo, currClassifyInfo *models.Classify) (err error) {
  417. defer func() {
  418. if err != nil {
  419. utils.FileLog.Error(fmt.Sprint("历史报告更改分类失败,父级分类ID:", parentClassifyInfo.Id, ";当前分类ID:", currClassifyInfo.Id, ";错误信息:", err.Error()))
  420. }
  421. }()
  422. if currClassifyInfo.Level > 3 {
  423. err = errors.New("父级分类不支持三级分类以上")
  424. return
  425. }
  426. // 报告的分类归属调整,转为下一级的分类
  427. var condition, updateStr string
  428. pars := make([]interface{}, 0)
  429. switch currClassifyInfo.Level {
  430. case 3: // 当前分类是3级分类
  431. updateStr += ` classify_id_third = ?,classify_name_third = ?`
  432. condition += ` AND classify_id_second = ? `
  433. case 2: // 当前分类是2级分类
  434. updateStr += ` classify_id_second = ?,classify_name_second = ?`
  435. condition += ` AND classify_id_first = ? `
  436. default:
  437. err = errors.New("错误的分类层级")
  438. return
  439. }
  440. pars = append(pars, currClassifyInfo.Id, currClassifyInfo.ClassifyName, parentClassifyInfo.Id)
  441. // 获取当前分类下的所有章节类型
  442. currReportChapterTypeList, err := models.GetAllReportChapterTypeListByClassifyId(currClassifyInfo.Id)
  443. if err != nil {
  444. return
  445. }
  446. // 当前的章节类型ID ---> 继承的章节类型ID
  447. chapterTypeIdMap := make(map[int]int)
  448. for _, v := range currReportChapterTypeList {
  449. chapterTypeIdMap[v.ReportChapterTypeId] = v.InheritReportChapterTypeId
  450. }
  451. // 报告转移后,历史章节报告中的type_id也要修复成最新的type_id
  452. err = models.ModifyReportClassifyAndReportChapterTypeByCondition(condition, pars, updateStr, chapterTypeIdMap, parentClassifyInfo.Id, currClassifyInfo.Id, currClassifyInfo.ClassifyName)
  453. if err != nil {
  454. return
  455. }
  456. return
  457. }
  458. // inheritReportChapterType
  459. // @Description: 继承父级分类下的章节类型
  460. // @author: Roc
  461. // @datetime 2024-06-17 14:41:04
  462. // @param parentClassifyId int
  463. // @param currClassifyId int
  464. // @return err error
  465. func inheritReportChapterType(parentClassifyId, currClassifyId int) (err error) {
  466. defer func() {
  467. if err != nil {
  468. utils.FileLog.Error(fmt.Sprint("继承父级分类下的章节类型失败,父级分类ID:", parentClassifyId, ";当前分类ID:", currClassifyId, ";错误信息:", err.Error()))
  469. }
  470. }()
  471. parentReportChapterTypeList, err := models.GetAllReportChapterTypeListByClassifyId(parentClassifyId)
  472. if err != nil {
  473. return
  474. }
  475. // 如果没有章节类型,那么就直接返回
  476. if len(parentReportChapterTypeList) <= 0 {
  477. return
  478. }
  479. addList := make([]*models.ReportChapterType, 0)
  480. for _, v := range parentReportChapterTypeList {
  481. addList = append(addList, &models.ReportChapterType{
  482. //ReportChapterTypeId: 0,
  483. ReportChapterTypeKey: v.ReportChapterTypeKey,
  484. ReportChapterTypeThumb: v.ReportChapterTypeThumb,
  485. BannerUrl: v.BannerUrl,
  486. ReportChapterTypeName: v.ReportChapterTypeName,
  487. Sort: v.Sort,
  488. Enabled: v.Enabled,
  489. CreatedTime: time.Now(),
  490. LastUpdatedTime: time.Now(),
  491. ResearchType: v.ResearchType,
  492. SelectedImage: v.SelectedImage,
  493. UnselectedImage: v.UnselectedImage,
  494. PcSelectedImage: v.PcSelectedImage,
  495. PcUnselectedImage: v.PcUnselectedImage,
  496. EditImgUrl: v.EditImgUrl,
  497. TickerTitle: v.TickerTitle,
  498. IsShow: v.IsShow,
  499. PauseStartTime: v.PauseStartTime,
  500. PauseEndTime: v.PauseEndTime,
  501. IsSet: v.IsSet,
  502. YbIconUrl: v.YbIconUrl,
  503. YbBottomIcon: v.YbBottomIcon,
  504. ReportClassifyId: currClassifyId,
  505. InheritReportChapterTypeId: v.ReportChapterTypeId,
  506. })
  507. }
  508. obj := models.ReportChapterType{}
  509. err = obj.MultiCreate(addList)
  510. // 修改CRM权限
  511. go func() {
  512. var syncReq ChapterTypeSyncReq
  513. _, _ = ReportChapterTypeSync(&syncReq)
  514. }()
  515. return
  516. }
  517. // inheritReportApproveFlow
  518. // @Description: 继承父级分类下的审批流
  519. // @author: Roc
  520. // @datetime 2024-06-17 14:41:04
  521. // @param parentClassifyId int
  522. // @param currClassifyId int
  523. // @return err error
  524. func inheritReportApproveFlow(parentClassifyItem, currClassifyItem *models.Classify) (err error) {
  525. defer func() {
  526. if err != nil {
  527. utils.FileLog.Error(fmt.Sprint("继承父级分类下的审批流失败,父级分类ID:", parentClassifyItem.Id, ";当前分类ID:", currClassifyItem.Id, ";错误信息:", err.Error()))
  528. }
  529. }()
  530. var firstClassify, secondClassify, thirdClassify *models.Classify
  531. if parentClassifyItem.ParentId > 0 {
  532. // 获取父级分类信息
  533. firstClassify, err = models.GetClassifyById(parentClassifyItem.ParentId)
  534. if err != nil {
  535. return
  536. }
  537. secondClassify = parentClassifyItem
  538. thirdClassify = currClassifyItem
  539. } else {
  540. firstClassify = parentClassifyItem
  541. secondClassify = currClassifyItem
  542. }
  543. flowObj := report_approve.ReportApproveFlow{}
  544. // 获取父级的审批流
  545. existCond := fmt.Sprintf(` AND %s = ? AND %s = ? `, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifyFirstId)
  546. existPars := make([]interface{}, 0)
  547. existPars = append(existPars, report_approve.FlowReportTypeChinese, firstClassify.Id)
  548. // 如果这是第三级,那么说明只需要查找第二级的审批配置就好了
  549. if thirdClassify != nil {
  550. existCond = fmt.Sprintf(`%s AND %s = ?`, existCond, report_approve.ReportApproveFlowCols.ClassifySecondId)
  551. existPars = append(existPars, secondClassify.Id)
  552. }
  553. //if thirdClassify != nil {
  554. // existCond = fmt.Sprintf(`%s AND %s = ?`, existCond, report_approve.ReportApproveFlowCols.ClassifyThirdId)
  555. // existPars = append(existPars, thirdClassify.Id)
  556. //}
  557. parentFlow, err := flowObj.GetItemByCondition(existCond, existPars, "")
  558. if err != nil {
  559. // 如果没有配置审批流,那么就直接返回
  560. if err.Error() == utils.ErrNoRow() {
  561. err = nil
  562. }
  563. return
  564. }
  565. // 获取父级的审批节点
  566. nodeObj := report_approve.ReportApproveNode{}
  567. nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, report_approve.ReportApproveNodeCols.ReportApproveFlowId, report_approve.ReportApproveNodeCols.CurrVersion)
  568. nodePars := make([]interface{}, 0)
  569. nodePars = append(nodePars, parentFlow.ReportApproveFlowId, parentFlow.CurrVersion)
  570. parentNodeList, err := nodeObj.GetItemsByCondition(nodeCond, nodePars, []string{}, "")
  571. if err != nil {
  572. return
  573. }
  574. // 新审批流
  575. currFlow := &report_approve.ReportApproveFlow{
  576. ReportApproveFlowId: 0,
  577. FlowName: currClassifyItem.ClassifyName,
  578. ReportType: parentFlow.ReportType,
  579. ClassifyFirstId: firstClassify.Id,
  580. ClassifySecondId: secondClassify.Id,
  581. //ClassifyThirdId: 0,
  582. CurrVersion: 1,
  583. Enabled: 1,
  584. CreateTime: time.Now().Local(),
  585. ModifyTime: time.Now().Local(),
  586. }
  587. if thirdClassify != nil {
  588. currFlow.ClassifyThirdId = thirdClassify.Id
  589. }
  590. // 新审批流的节点
  591. nodeItems := make([]*report_approve.ReportApproveNode, 0)
  592. for _, v := range parentNodeList {
  593. n := &report_approve.ReportApproveNode{
  594. //ReportApproveNodeId: 0,
  595. //ReportApproveFlowId: 0,
  596. PrevNodeId: 0,
  597. NextNodeId: 0,
  598. NodeType: v.NodeType,
  599. ApproveType: v.ApproveType,
  600. Users: v.Users,
  601. CurrVersion: 1,
  602. CreateTime: time.Now().Local(),
  603. }
  604. nodeItems = append(nodeItems, n)
  605. }
  606. // 新增审批流和节点
  607. err = flowObj.CreateFlowAndNodes(currFlow, nodeItems)
  608. if err != nil {
  609. return
  610. }
  611. parentFlow.Enabled = 0
  612. err = parentFlow.Update([]string{"Enabled"})
  613. return
  614. }
  615. // EditReportClassify
  616. // @Description: 编辑报告分类
  617. // @author: Roc
  618. // @datetime 2024-06-17 13:31:33
  619. // @param classifyId int
  620. // @param classifyName string
  621. // @param chartPermissionIdList []int
  622. // @return err error
  623. // @return errMsg string
  624. // @return isSendEmail bool
  625. func EditReportClassify(classifyId int, classifyName string, chartPermissionIdList []int) (err error, errMsg string, isSendEmail bool) {
  626. isSendEmail = true
  627. errMsg = `修改失败`
  628. item, err := models.GetClassifyById(classifyId)
  629. if err != nil {
  630. errMsg = "获取分类信息失败"
  631. if err.Error() == utils.ErrNoRow() {
  632. errMsg = "分类不存在, 或已被删除"
  633. isSendEmail = false
  634. }
  635. return
  636. }
  637. originName := item.ClassifyName
  638. // 重名校验
  639. existName, e := models.GetClassifyByName(classifyName, item.ParentId)
  640. if e != nil && e.Error() != utils.ErrNoRow() {
  641. errMsg = "获取信息失败"
  642. err = errors.New("获取重名分类失败, Err: " + err.Error())
  643. return
  644. }
  645. if existName != nil && existName.Id != item.Id {
  646. errMsg = "分类名称:" + classifyName + "已存在"
  647. err = errors.New(errMsg)
  648. isSendEmail = false
  649. return
  650. }
  651. item.ClassifyName = classifyName
  652. // ETA1.8.3:不允许修改上级分类 2024-6-17 13:21:01
  653. //item.ParentId = req.ParentId
  654. item.ModifyTime = time.Now().Local()
  655. cols := make([]string, 0)
  656. cols = append(cols, "ClassifyName", "ModifyTime")
  657. err = item.UpdateClassify(cols)
  658. if err != nil {
  659. return
  660. }
  661. err = models.EditChartPermissionSearchKeyWordMappingMulti(item.ClassifyName, chartPermissionIdList, item.Id)
  662. if err != nil {
  663. errMsg = "修改分类权限失败"
  664. return
  665. }
  666. // 修改CRM权限
  667. go func() {
  668. _ = EditClassifyChartPermissionSync(item.ClassifyName, item.Id)
  669. }()
  670. // TODO 修改分类的关联品种时,历史报告中关联的品种怎么处理?
  671. // 更新报告分类名称/父级分类后
  672. go func() {
  673. _ = AfterUpdateClassifyNameOrParent(item.Id, item.ParentId, item.ParentId, originName, item.ClassifyName, item.Level)
  674. }()
  675. return
  676. }
  677. // GetClassifyTreeRecursive 递归获取分类树形结构
  678. func GetClassifyTreeRecursive(list []*models.ClassifyItem, parentId int) []*models.ClassifyItem {
  679. res := make([]*models.ClassifyItem, 0)
  680. for _, v := range list {
  681. if v.ParentId == parentId {
  682. v.Child = GetClassifyTreeRecursive(list, v.Id)
  683. res = append(res, v)
  684. }
  685. }
  686. return res
  687. }
  688. // GetParentClassifyListByParentIdList
  689. // @Description: 递归获取父级分类信息,正常来讲只有三次
  690. // @author: Roc
  691. // @datetime 2024-06-19 13:23:33
  692. // @param parentClassifyIdList []int
  693. // @return list []*models.ClassifyList
  694. // @return err error
  695. func GetParentClassifyListByParentIdList(parentClassifyIdList []int) (list []*models.ClassifyList, err error) {
  696. num := len(parentClassifyIdList)
  697. if num <= 0 {
  698. return
  699. }
  700. list, err = models.GetClassifyListByParentIdList(parentClassifyIdList)
  701. if err != nil {
  702. return
  703. }
  704. // 是否还有上级
  705. {
  706. currParentClassifyIdList := make([]int, 0)
  707. for _, v := range list {
  708. if v.ParentId > 0 {
  709. currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  710. }
  711. }
  712. if len(currParentClassifyIdList) > 0 {
  713. tmpList, tmpErr := GetParentClassifyListByParentIdList(currParentClassifyIdList)
  714. if tmpErr != nil {
  715. err = tmpErr
  716. return
  717. }
  718. list = append(tmpList, list...)
  719. }
  720. }
  721. return
  722. }
  723. // GetClassifyListTreeRecursive
  724. // @Description: 递归获取分类树形结构
  725. // @author: Roc
  726. // @datetime 2024-06-19 13:23:28
  727. // @param list []*models.ClassifyList
  728. // @param parentId int
  729. // @return []*models.ClassifyList
  730. func GetClassifyListTreeRecursive(list []*models.ClassifyList, parentId int) []*models.ClassifyList {
  731. res := make([]*models.ClassifyList, 0)
  732. for _, v := range list {
  733. if v.ParentId == parentId {
  734. v.Child = GetClassifyListTreeRecursive(list, v.Id)
  735. res = append(res, v)
  736. }
  737. }
  738. // 前端的JP需要我这么返回
  739. if len(res) <= 0 {
  740. res = nil
  741. }
  742. return res
  743. }
  744. // BySortAndCreateTime 用来排序,先按Sort字段升序排序,若Sort相同,则按照CreateTime字段升序排序。
  745. type BySortAndCreateTime []*models.ClassifyList
  746. func (a BySortAndCreateTime) Len() int {
  747. return len(a)
  748. }
  749. func (a BySortAndCreateTime) Swap(i, j int) {
  750. a[i], a[j] = a[j], a[i]
  751. }
  752. func (a BySortAndCreateTime) Less(i, j int) bool {
  753. if a[i].Sort == a[j].Sort {
  754. return a[i].CreateTime.Before(a[j].CreateTime)
  755. }
  756. return a[i].Sort < a[j].Sort
  757. }
  758. // SortClassifyListBySortAndCreateTime sorts the ClassifyList slice by Sort and then CreateTime in ascending order.
  759. func SortClassifyListBySortAndCreateTime(classifyList []*models.ClassifyList) {
  760. sort.Sort(BySortAndCreateTime(classifyList))
  761. }