approval.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. package seal
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "hongze/hz_crm_api/models/contract"
  8. "hongze/hz_crm_api/models/seal"
  9. "hongze/hz_crm_api/models/system"
  10. "hongze/hz_crm_api/services"
  11. "hongze/hz_crm_api/services/flow"
  12. "strings"
  13. "time"
  14. )
  15. // SubmitApproval 提交审批
  16. func SubmitApproval(sealInfo *seal.Seal) (err error) {
  17. // 发起审批
  18. approval, approvalRecordList, err := addApproval(sealInfo)
  19. if err != nil {
  20. return
  21. }
  22. // 待办通知
  23. {
  24. go MessageToNodeUser(approvalRecordList[0].NodeId, approval.ApplyUserId, approval.ContractApprovalId, 1, 1, sealInfo.CompanyName, sealInfo.Use)
  25. }
  26. return
  27. }
  28. // GetFlowTypeBySealType 根据盖章类型(多选,逗号拼接)获取审批流的类型
  29. func GetFlowTypeBySealType(sealType string) (flowType int) {
  30. flowType = 5 // 合同章
  31. longFlow := "公章,法人章" // 盖章类型包含其中之一则审批流变成6
  32. sealTypeArr := strings.Split(sealType, ",")
  33. for _, itemType := range sealTypeArr {
  34. if strings.Contains(longFlow, itemType) {
  35. flowType = 6
  36. }
  37. }
  38. return
  39. }
  40. // addApproval 发起审批
  41. func addApproval(sealInfo *seal.Seal) (approval *contract.ContractApproval, contractApprovalRecordList []*contract.ContractApprovalRecord, err error) {
  42. //用印状态判断
  43. ignoreStatus := []string{"待提交", "已撤回", "已驳回"}
  44. if !strings.Contains(strings.Join(ignoreStatus, ","), sealInfo.Status) {
  45. err = errors.New("用印单状态异常,不允许提交审批,当前用印单状态:" + sealInfo.Status)
  46. return
  47. }
  48. //将用印数据存为快照
  49. sealDetailByte, err := json.Marshal(sealInfo)
  50. if err != nil {
  51. err = errors.New(fmt.Sprint("用印单异常,当前用印单数据格式化失败:", err))
  52. return
  53. }
  54. // 获取审批流ID
  55. flowId := GetFlowTypeBySealType(sealInfo.SealType)
  56. flowItemInfo, err := flow.GetApprovalFlow(flowId)
  57. if err != nil {
  58. return
  59. }
  60. if len(flowItemInfo.NodeList) <= 0 {
  61. err = errors.New("审批流程未配置")
  62. return
  63. }
  64. //校验是否存在待审批的审批单(其实没有多大意义,只是为了 异常数据校验)
  65. approval = &contract.ContractApproval{
  66. ContractId: sealInfo.SealId,
  67. ApprovalType: "seal",
  68. Status: "待审批",
  69. ApplyContent: sealInfo.Use,
  70. ContractDetail: string(sealDetailByte),
  71. ApplyUserId: sealInfo.UserId,
  72. ApplyUserName: sealInfo.UserName,
  73. FlowId: flowItemInfo.FlowId,
  74. FlowVersion: flowItemInfo.CurrVersion,
  75. CurrNodeId: flowItemInfo.NodeList[0].NodeId,
  76. StartNodeId: flowItemInfo.NodeList[0].NodeId,
  77. ModifyTime: time.Now(),
  78. CreateTime: time.Now(),
  79. }
  80. has, err := approval.CheckPendingByContractId(sealInfo.SealId, "seal")
  81. if err != nil {
  82. return
  83. }
  84. if has {
  85. err = errors.New("用印异常,不允许提交审批,存在待审核的审批单")
  86. return
  87. }
  88. //ApproveUserMap := make(map[int]system.User)
  89. for _, nodeItem := range flowItemInfo.NodeList {
  90. //审批流记录
  91. if len(nodeItem.UserList) <= 0 {
  92. err = errors.New(fmt.Sprint("审批流程异常,没有可审批的人员,Err:", err))
  93. return
  94. }
  95. for _, userItem := range nodeItem.UserList {
  96. contractApprovalRecord := &contract.ContractApprovalRecord{
  97. Status: "待审批",
  98. ApproveRemark: "",
  99. ApproveUserId: userItem.AdminId,
  100. ApproveUserName: userItem.Name,
  101. ApproveRoleTypeCode: userItem.RoleTypeCode,
  102. NodeId: nodeItem.NodeId,
  103. NodeType: nodeItem.NodeType,
  104. NextNodeId: nodeItem.NextNodeId,
  105. PrevNodeId: nodeItem.PrevNodeId,
  106. CreateTime: time.Now(),
  107. ModifyTime: time.Now(),
  108. }
  109. contractApprovalRecordList = append(contractApprovalRecordList, contractApprovalRecord)
  110. //ApproveUserMap[userItem.AdminId] = userItem
  111. }
  112. }
  113. err = approval.Apply2(approval, contractApprovalRecordList)
  114. if err != nil {
  115. return
  116. }
  117. return
  118. }
  119. // CheckApprovalAuth 校验审批操作权限
  120. func CheckApprovalAuth(sealId int, sysUser *system.Admin) (sealInfo *seal.Seal, approvalInfo *contract.ContractApproval, approvalRecord *contract.ContractApprovalRecord, err error) {
  121. // 用印详情
  122. sealInfo, err = seal.GetSealInfoById(sealId)
  123. if err != nil {
  124. err = errors.New("获取用印失败,Err:" + err.Error())
  125. return
  126. }
  127. // 合同状态判断
  128. if sealInfo.Status != "待审批" {
  129. err = errors.New("用印状态异常,不允许驳回申请,当前合同状态:" + sealInfo.Status)
  130. return
  131. }
  132. // 获取该最近一条审批单详情
  133. approvalInfo, err = contract.GetLastContractApprovalByContractId(sealInfo.SealId, "seal")
  134. if err != nil {
  135. err = errors.New("获取审批单失败,Err:" + err.Error())
  136. return
  137. }
  138. if approvalInfo.Status != "待审批" {
  139. err = errors.New("当前审批单状态异常,不允许审批,审批单状态:" + approvalInfo.Status)
  140. return
  141. }
  142. // 审批流
  143. approvalRecordList, err := contract.GetContractApprovalRecordListByContractApprovalIdAndNodeId(approvalInfo.ContractApprovalId, approvalInfo.CurrNodeId)
  144. if err != nil {
  145. err = errors.New("获取审批失败,Err:" + err.Error())
  146. return
  147. }
  148. for _, tmpApprovalRecord := range approvalRecordList {
  149. if tmpApprovalRecord.NodeType == "check" && tmpApprovalRecord.ApproveUserId == sysUser.AdminId {
  150. approvalRecord = tmpApprovalRecord
  151. }
  152. }
  153. if approvalRecord == nil {
  154. err = errors.New("当前账号没有审批权限")
  155. return
  156. }
  157. return
  158. }
  159. // ApprovedApproval 通过审批
  160. func ApprovedApproval(sealId int, sysUser *system.Admin, remark string) (err error) {
  161. // 校验审批权限
  162. sealInfo, approvalInfo, approvalRecord, err := CheckApprovalAuth(sealId, sysUser)
  163. if err != nil {
  164. return
  165. }
  166. if approvalRecord == nil {
  167. err = errors.New("审批流异常,没有审批流信息")
  168. return
  169. }
  170. // 用印状态判断
  171. if sealInfo.Status != "待审批" {
  172. err = errors.New("用印状态异常,不允许审批,当前用印状态:" + sealInfo.Status)
  173. return
  174. }
  175. // 审批单状态判断
  176. if approvalInfo.Status != "待审批" {
  177. err = errors.New("审批单状态异常,不允许审批,当前审批单状态:" + approvalInfo.Status)
  178. return
  179. }
  180. // 审批流状态判断
  181. if approvalRecord.Status != "待审批" {
  182. err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + approvalRecord.Status)
  183. return
  184. }
  185. // 判断是否审批类型,如果不是审批类型,那么就没有审批权限
  186. if approvalRecord.NodeType != "check" {
  187. err = errors.New("当前账号没有审批权限")
  188. return
  189. }
  190. // 操作权限校验
  191. if sysUser.RoleTypeCode != approvalRecord.ApproveRoleTypeCode {
  192. err = errors.New("当前账号没有审批权限")
  193. return
  194. }
  195. if approvalRecord.ApproveUserId > 0 && approvalRecord.ApproveUserId != sysUser.AdminId {
  196. err = errors.New("当前账号没有审批权限,需要指定人操作")
  197. return
  198. }
  199. o := orm.NewOrm()
  200. to, err := o.Begin()
  201. if err != nil {
  202. return
  203. }
  204. defer func() {
  205. if err != nil {
  206. _ = to.Rollback()
  207. } else {
  208. _ = to.Commit()
  209. }
  210. }()
  211. // 通过审批
  212. err = approvalInfo.Approved(approvalInfo, approvalRecord, remark)
  213. if err != nil {
  214. return
  215. }
  216. // 添加操作日志
  217. err = seal.AddSealOperationRecord(sealInfo.SealId, sysUser.AdminId, approvalRecord.ContractApprovalRecordId, "approval", sysUser.RealName, "通过申请", "")
  218. if err != nil {
  219. err = errors.New("添加操作日志失败")
  220. return
  221. }
  222. // 推送消息给抄送人
  223. content := sealInfo.CompanyName + " 用印已审核"
  224. // 如果下一个节点属于结束节点,那么通知对应的销售
  225. if approvalRecord.NextNodeId == 0 {
  226. // 待办通知(通知销售已经审核通过了)
  227. {
  228. approvalSysUser, _ := system.GetSysAdminById(sealInfo.UserId)
  229. go services.AddCompanyApprovalMessage(sysUser.AdminId, sealInfo.UserId, 0, approvalRecord.ContractApprovalRecordId, 2, sourceType, 2, sealInfo.CompanyName, content, content, approvalSysUser.Mobile)
  230. }
  231. } else {
  232. // 获取下级节点信息
  233. flowNodeInfo, tmpErr := system.GetByNodeId(approvalRecord.NextNodeId)
  234. if tmpErr != nil {
  235. err = tmpErr
  236. return
  237. }
  238. // 如果该级节点是抄送类型,那么需要将该节点给处理掉
  239. if flowNodeInfo.NodeType == "cc" {
  240. go approvedByCc(remark, approvalRecord, sysUser)
  241. } else {
  242. //发送消息下级审批人
  243. go MessageToNodeUser(approvalRecord.NextNodeId, sealInfo.UserId, approvalInfo.ContractApprovalId, 1, 1, sealInfo.CompanyName, content)
  244. }
  245. }
  246. return
  247. }
  248. // RejectApproval 驳回审批
  249. func RejectApproval(sealId int, sysUser *system.Admin, remark string) (err error) {
  250. // 校验审批权限
  251. sealInfo, approvalInfo, approvalRecord, err := CheckApprovalAuth(sealId, sysUser)
  252. if err != nil {
  253. return
  254. }
  255. o := orm.NewOrm()
  256. to, err := o.Begin()
  257. if err != nil {
  258. return
  259. }
  260. defer func() {
  261. if err != nil {
  262. _ = to.Rollback()
  263. } else {
  264. _ = to.Commit()
  265. }
  266. }()
  267. // 驳回审批
  268. err = reject(sealInfo, approvalInfo, approvalRecord, sysUser, remark)
  269. if err != nil {
  270. return
  271. }
  272. // 待办通知
  273. {
  274. content := sealInfo.CompanyName + " 用印已驳回"
  275. approvalSysUser, _ := system.GetSysAdminById(sealInfo.UserId)
  276. go services.AddCompanyApprovalMessage(sysUser.AdminId, sealInfo.UserId, 0, approvalRecord.ContractApprovalRecordId, 2, sourceType, 3, sealInfo.CompanyName, content, content, approvalSysUser.Mobile)
  277. }
  278. return
  279. }
  280. // reject 驳回操作
  281. func reject(sealInfo *seal.Seal, approvalInfo *contract.ContractApproval, approvalRecord *contract.ContractApprovalRecord, opUser *system.Admin, rejectRemark string) (err error) {
  282. if approvalRecord == nil {
  283. err = errors.New("审批流异常,没有审批流信息")
  284. return
  285. }
  286. //审批流状态判断
  287. if approvalRecord.Status != "待审批" {
  288. err = errors.New("审批流状态异常,不允许驳回申请,当前审批流状态:" + approvalRecord.Status)
  289. return
  290. }
  291. //判断是否审批类型,如果不是审批类型,那么就没有审批权限
  292. if approvalRecord.NodeType != "check" {
  293. err = errors.New("当前账号没有审批权限")
  294. return
  295. }
  296. //操作人
  297. opUserId := opUser.AdminId
  298. opUserName := opUser.RealName
  299. //操作权限校验
  300. if opUser.RoleTypeCode != approvalRecord.ApproveRoleTypeCode {
  301. err = errors.New("当前账号没有审批权限")
  302. return
  303. }
  304. if approvalRecord.ApproveUserId > 0 && approvalRecord.ApproveUserId != opUserId {
  305. err = errors.New("当前账号没有审批权限,需要指定人操作")
  306. return
  307. }
  308. //审批单状态判断
  309. if approvalInfo.Status != "待审批" {
  310. err = errors.New("审批单状态异常,不允许驳回申请,当前审批单状态:" + approvalInfo.Status)
  311. return
  312. }
  313. //用印状态判断
  314. if sealInfo.Status != "待审批" {
  315. err = errors.New("用印状态异常,不允许驳回申请,当前用印状态:" + sealInfo.Status)
  316. return
  317. }
  318. // 驳回
  319. err = approvalInfo.Reject(approvalInfo, approvalRecord, opUserId, opUserName, rejectRemark)
  320. if err != nil {
  321. return
  322. }
  323. // 添加操作日志
  324. err = seal.AddSealOperationRecord(sealInfo.SealId, opUserId, approvalRecord.ContractApprovalRecordId, "reject", opUserName, "驳回申请", rejectRemark)
  325. if err != nil {
  326. err = errors.New("添加操作日志失败")
  327. return
  328. }
  329. return
  330. }
  331. // approvedByCc 审批通过(抄送节点)
  332. func approvedByCc(approvedRemark string, sourceApprovalRecord *contract.ContractApprovalRecord, sysUser *system.Admin) (err error) {
  333. // 下个流程节点id
  334. nextNodeId := 0
  335. // 获取审批单中抄送节点的所有数据列表
  336. approvalRecordList, err := contract.GetContractApprovalRecordListByContractIdAndNode(sourceApprovalRecord.ContractApprovalId, sourceApprovalRecord.NextNodeId)
  337. if err != nil {
  338. return
  339. }
  340. // 遍历所有的抄送单
  341. for _, approvalRecord := range approvalRecordList {
  342. nextNodeId = approvalRecord.NextNodeId
  343. // 审批流状态判断
  344. if approvalRecord.Status != "待审批" {
  345. err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + approvalRecord.Status)
  346. return
  347. }
  348. // 判断是否审批类型,如果不是审批类型,那么就没有审批权限
  349. if approvalRecord.NodeType != "cc" {
  350. err = errors.New("当前账号不是抄送权限")
  351. return
  352. }
  353. }
  354. // 获取审批单详情
  355. approvalInfo, err := contract.GetContractApprovalById(sourceApprovalRecord.ContractApprovalId)
  356. if err != nil {
  357. return
  358. }
  359. // 获取用印信息
  360. sealInfo, err := seal.GetSealInfoById(approvalInfo.ContractId)
  361. if err != nil {
  362. return
  363. }
  364. //if contractApprovalRecord
  365. err = approvalInfo.ApprovedByCc(approvalInfo, approvalRecordList, approvedRemark, nextNodeId)
  366. if err != nil {
  367. return
  368. }
  369. content := sealInfo.CompanyName + " 用印已审核"
  370. // 发送消息给抄送人
  371. go MessageToNodeUser(approvalRecordList[0].NodeId, sealInfo.UserId, approvalInfo.ContractApprovalId, 2, 2, sealInfo.CompanyName, content)
  372. // 如果下一个节点属于结束节点,那么通知对应的销售
  373. if nextNodeId == 0 {
  374. // 待办通知
  375. {
  376. content = sealInfo.CompanyName + " 用印已审核"
  377. approvalSysUser, _ := system.GetSysAdminById(sealInfo.UserId)
  378. go services.AddCompanyApprovalMessage(sourceApprovalRecord.ApproveUserId, sealInfo.UserId, 0, sourceApprovalRecord.ContractApprovalRecordId, 2, sourceType, 2, sealInfo.CompanyName, content, content, approvalSysUser.Mobile)
  379. }
  380. } else {
  381. // 获取下级节点信息
  382. flowNodeInfo, tmpErr := system.GetByNodeId(nextNodeId)
  383. if tmpErr != nil {
  384. err = tmpErr
  385. return
  386. }
  387. // 如果下级节点是抄送类型,那么还是需要处理抄送节点逻辑
  388. if flowNodeInfo.NodeType == "cc" {
  389. go approvedByCc(approvedRemark, sourceApprovalRecord, sysUser)
  390. } else {
  391. //如果下级级节点是审批类型,发送消息下级审批人
  392. go MessageToNodeUser(nextNodeId, sealInfo.UserId, approvalInfo.ContractApprovalId, 1, 1, sealInfo.CompanyName, approvalInfo.ApplyContent)
  393. }
  394. }
  395. return
  396. }