seal.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. package seal
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "hongze/hongze_mobile_admin/models/custom"
  7. sealResp "hongze/hongze_mobile_admin/models/response/seal"
  8. "hongze/hongze_mobile_admin/models/tables/admin"
  9. "hongze/hongze_mobile_admin/models/tables/contract"
  10. "hongze/hongze_mobile_admin/models/tables/contract_approval"
  11. "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
  12. "hongze/hongze_mobile_admin/models/tables/seal"
  13. "hongze/hongze_mobile_admin/services"
  14. contractService "hongze/hongze_mobile_admin/services/contract"
  15. "hongze/hongze_mobile_admin/utils"
  16. "mime/multipart"
  17. "reflect"
  18. "strings"
  19. "time"
  20. )
  21. // AddSeal 添加用印
  22. func AddSeal(userId, contractId, fileNum int, userName, use, useCompanyName, companyName, creditCode, serviceType, sealType, remark string, fileUrls []string) (sealInfo *seal.Seal, err error) {
  23. o := orm.NewOrm()
  24. to, err := o.Begin()
  25. if err != nil {
  26. return
  27. }
  28. defer func() {
  29. if err != nil {
  30. _ = to.Rollback()
  31. } else {
  32. _ = to.Commit()
  33. }
  34. }()
  35. if !strings.Contains(strings.Join(seal.EnumUse, ","), use) {
  36. err = errors.New("用印用途异常")
  37. return
  38. }
  39. if !strings.Contains(strings.Join(seal.EnumServiceType, ","), serviceType) {
  40. err = errors.New("业务类型异常")
  41. return
  42. }
  43. sealCode, err := seal.GetSealCode()
  44. if err != nil {
  45. return
  46. }
  47. fileUrl := ""
  48. now := time.Now()
  49. attachments := make([]*seal.Attachment,0)
  50. if len(fileUrls) == 1 {
  51. fileUrl = fileUrls[0]
  52. }
  53. sealInfo = &seal.Seal{
  54. Code: sealCode,
  55. UserId: userId,
  56. UserName: userName,
  57. Use: use,
  58. CompanyName: companyName,
  59. UseCompanyName: useCompanyName,
  60. CreditCode: creditCode,
  61. ServiceType: serviceType,
  62. SealType: sealType,
  63. Status: "待提交",
  64. Remark: remark,
  65. FileUrl: fileUrl,
  66. FileNum: fileNum,
  67. ContractId: contractId,
  68. ModifyTime: now,
  69. CreateTime: now,
  70. }
  71. err = seal.AddSeal(sealInfo)
  72. if err != nil {
  73. err = errors.New("新增用印失败")
  74. return
  75. }
  76. if len(fileUrls) > 1 {
  77. for _, v := range fileUrls {
  78. tmp := &seal.Attachment{
  79. SealId: sealInfo.SealId,
  80. FileUrl: v,
  81. ModifyTime: now,
  82. CreateTime: now,
  83. }
  84. attachments = append(attachments, tmp)
  85. }
  86. err = seal.AddAttachments(attachments)
  87. if err != nil {
  88. err = errors.New("添加用印附件失败")
  89. return
  90. }
  91. }
  92. // 操作日志
  93. err = seal.AddSealOperationRecord(sealInfo.SealId, userId, 0, "apply", userName, "提交审批", "")
  94. if err != nil {
  95. err = errors.New("新增用印日志失败")
  96. return
  97. }
  98. return
  99. }
  100. // Edit 修改用印
  101. func Edit(sealId, userId, contractId, fileNum int, use, companyName, userCompanyName, creditCode, serviceType, sealType, remark string, fileUrls []string, userName string) (sealInfo *seal.Seal, err error) {
  102. o := orm.NewOrm()
  103. to, err := o.Begin()
  104. if err != nil {
  105. return
  106. }
  107. defer func() {
  108. if err != nil {
  109. _ = to.Rollback()
  110. } else {
  111. _ = to.Commit()
  112. }
  113. }()
  114. if !strings.Contains(strings.Join(seal.EnumUse, ","), use) {
  115. err = errors.New("用印用途异常")
  116. return
  117. }
  118. if !strings.Contains(strings.Join(seal.EnumServiceType, ","), serviceType) {
  119. err = errors.New("业务类型异常")
  120. return
  121. }
  122. //查询当前合同信息
  123. sealInfo, err = seal.GetSealInfoById(sealId)
  124. if err != nil {
  125. if err.Error() == utils.ErrNoRow() {
  126. err = errors.New(fmt.Sprint("根据用印编号:", sealId, " 找不到该用印"))
  127. }
  128. return
  129. }
  130. if sealInfo.UserId != userId {
  131. err = errors.New("当前账号无操作权限")
  132. return
  133. }
  134. ignoreStatus := []string{"待提交", "已撤回", "已驳回"}
  135. if !strings.Contains(strings.Join(ignoreStatus, ","), sealInfo.Status) {
  136. err = errors.New("用印状态异常,不允许修改,当前用印状态:" + sealInfo.Status)
  137. return
  138. }
  139. //sealId,userId int, userName, use, companyName, creditCode, serviceType, sealType, remark, fileUrl string
  140. fileUrl := ""
  141. now := time.Now()
  142. attachments := make([]*seal.Attachment,0)
  143. if len(fileUrls) == 1 {
  144. fileUrl = fileUrls[0]
  145. }
  146. sealInfo.Use = use
  147. sealInfo.CompanyName = companyName
  148. sealInfo.UseCompanyName = userCompanyName
  149. sealInfo.CreditCode = creditCode
  150. sealInfo.ServiceType = serviceType
  151. sealInfo.SealType = sealType
  152. sealInfo.Remark = remark
  153. sealInfo.FileUrl = fileUrl
  154. sealInfo.FileNum = fileNum
  155. sealInfo.ContractId = contractId
  156. sealInfo.CreateTime = now // 重提更新提交时间
  157. sealInfo.ModifyTime = now
  158. sealInfo.Status = "待提交" //用印状态
  159. err = sealInfo.Update([]string{"Use", "CompanyName", "UseCompanyName", "CreditCode", "ServiceType", "SealType", "Remark", "FileUrl", "FileNum", "ContractId", "CreateTime", "ModifyTime", "Status"})
  160. if err != nil {
  161. err = errors.New("新增用印日志失败")
  162. return
  163. }
  164. if len(fileUrls) > 1 {
  165. for _, v := range fileUrls {
  166. tmp := &seal.Attachment{
  167. SealId: sealInfo.SealId,
  168. FileUrl: v,
  169. ModifyTime: now,
  170. CreateTime: now,
  171. }
  172. attachments = append(attachments, tmp)
  173. }
  174. err = seal.AddAttachments(attachments)
  175. if err != nil {
  176. err = errors.New("添加用印附件失败")
  177. return
  178. }
  179. }
  180. // 操作日志
  181. err = seal.AddSealOperationRecord(sealInfo.SealId, userId, 0, "edit", userName, "重提审批", "")
  182. if err != nil {
  183. err = errors.New("编辑用印日志失败")
  184. return
  185. }
  186. return
  187. }
  188. // CheckEdit 审批者修改用印
  189. func CheckEdit(sealInfo *seal.Seal, approvalInfo *contract_approval.ContractApproval, approvalRecord *contract_approval_record.ContractApprovalRecord, fileNum int, fileUrls []string, use, sealType, remark string, opUser *custom.AdminWx) (err error) {
  190. if !strings.Contains(strings.Join(seal.EnumUse, ","), use) {
  191. err = errors.New("用印用途异常")
  192. return
  193. }
  194. //判断是否是合规账号
  195. if opUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE {
  196. err = errors.New("当前账号无修改权限")
  197. return
  198. }
  199. //用印状态判断
  200. if sealInfo.Status != "待审批" {
  201. err = errors.New("用印状态异常,不允许修改,当前用印状态:" + sealInfo.Status)
  202. return
  203. }
  204. //变更字段判断
  205. updateCol := make([]string, 0)
  206. updateContent := make([]string, 0)
  207. if sealInfo.Use != use {
  208. updateCol = append(updateCol, "Use")
  209. updateContent = append(updateContent, "用印用途")
  210. }
  211. if sealInfo.SealType != sealType {
  212. updateCol = append(updateCol, "SealType")
  213. updateContent = append(updateContent, "加盖何种印章")
  214. }
  215. if sealInfo.Remark != remark {
  216. updateCol = append(updateCol, "Remark")
  217. updateContent = append(updateContent, "备注")
  218. }
  219. if sealInfo.FileNum != fileNum {
  220. updateCol = append(updateCol, "FileNum")
  221. updateContent = append(updateContent, "文件份数")
  222. }
  223. //判断附件地址是否相等:
  224. sealAttachements, err := seal.GetAttachmentBySealId(sealInfo.SealId)
  225. if err != nil {
  226. err = errors.New(fmt.Sprint("获取用印附件失败,Err:"+err.Error(), err))
  227. return
  228. }
  229. oldFileUrls := make([]string, 0)
  230. if sealInfo.FileUrl != "" {
  231. oldFileUrls = append(oldFileUrls, sealInfo.FileUrl)
  232. }
  233. if len(sealAttachements) > 0 {
  234. for _, v := range sealAttachements{
  235. oldFileUrls = append(oldFileUrls, v.FileUrl)
  236. }
  237. }
  238. attachmentFlag := false
  239. if !reflect.DeepEqual(oldFileUrls, fileUrls) {
  240. updateCol = append(updateCol, "FileUrl")
  241. updateContent = append(updateContent, "文件附件")
  242. attachmentFlag = true
  243. }
  244. fmt.Println(updateCol)
  245. if len(updateCol) <= 0 {
  246. err = errors.New("没有变更信息")
  247. return
  248. }
  249. content := "用印修改:" + opUser.RealName + "修改了" + strings.Join(updateContent, "、")
  250. // 通过盖章类型判断是否需要变更审批流程
  251. originFlowType := GetFlowTypeBySealType(sealInfo.SealType)
  252. reqFlowType := GetFlowTypeBySealType(sealType)
  253. newFileUrl := ""
  254. if len(fileUrls) == 1 {
  255. newFileUrl = fileUrls[0]
  256. }
  257. //如果变更了盖章类型(涉及到审批流程变更),那么需要变更审批流
  258. //变更审批流:先驳回用印申请,再修改用印,再发消息给申请人,再替申请人重新发起申请,
  259. //然后遍历审批流程,当前操作人节点之前的全部审核通过,最后通过当前操作人的节点(改成如果一级审批人如果是自己,那么直接通过这一级审批,否则不通过,让审批单重走流程)
  260. if originFlowType != reqFlowType {
  261. //先驳回
  262. err = CheckReject(sealInfo, approvalInfo, approvalRecord, opUser, "")
  263. if err != nil {
  264. return
  265. }
  266. //修改用印申请,给申请人发消息
  267. sealInfo.Use = use
  268. sealInfo.SealType = sealType
  269. sealInfo.Remark = remark
  270. sealInfo.FileNum = fileNum
  271. sealInfo.FileUrl = newFileUrl
  272. sealInfo.ModifyTime = time.Now()
  273. checkEdit(sealInfo, updateCol, approvalRecord.ContractApprovalRecordId, content, opUser, attachmentFlag, fileUrls)
  274. //重新获取最新的用印单
  275. sealInfo, tmpErr := seal.GetSealInfoById(sealInfo.SealId)
  276. if tmpErr != nil {
  277. err = tmpErr
  278. return
  279. }
  280. //重新发起申请
  281. err = CheckApply(sealInfo, opUser)
  282. if err != nil {
  283. return
  284. }
  285. } else { //修改用印基础信息 并 审批通过
  286. //修改用印申请,给申请人发消息
  287. sealInfo.Use = use
  288. sealInfo.SealType = sealType
  289. sealInfo.Remark = remark
  290. sealInfo.FileNum = fileNum
  291. sealInfo.FileUrl = newFileUrl
  292. sealInfo.ModifyTime = time.Now()
  293. checkEdit(sealInfo, updateCol, approvalRecord.ContractApprovalRecordId, content, opUser, attachmentFlag, fileUrls)
  294. //审核通过
  295. err = Approved(sealInfo, approvalInfo, approvalRecord, opUser, "")
  296. }
  297. return
  298. }
  299. // checkEdit 审批人修改
  300. func checkEdit(sealInfo *seal.Seal, updateCol []string, approvalRecordId int, content string, opUser *custom.AdminWx, attachmentFlag bool, fileUrls []string) {
  301. if attachmentFlag {
  302. // 同时更新用印申请和附件
  303. _ = updateSealAndAttachment(sealInfo, updateCol, fileUrls)
  304. }else{
  305. _ = sealInfo.Update(updateCol)
  306. }
  307. // 操作日志
  308. _ = seal.AddSealOperationRecord(sealInfo.SealId, opUser.AdminId, approvalRecordId, "edit", opUser.RealName, "审批人修改信息", "")
  309. //给用印人,发送修改消息
  310. sysUserMobile := ""
  311. sysUser, _ := admin.GetAdminById(opUser.AdminId)
  312. if sysUser != nil {
  313. sysUserMobile = sysUser.Mobile
  314. }
  315. go services.AddCompanyApprovalMessage(opUser.AdminId, sealInfo.UserId, 0, approvalRecordId, 3, sourceType, 2, sealInfo.CompanyName, content, content, sysUserMobile, "")
  316. return
  317. }
  318. // Invalid 作废用印
  319. func Invalid(sealId int, opUser *custom.AdminWx, isInvalidContract bool) (err error) {
  320. //查询当前用印信息
  321. sealInfo, err := seal.GetSealInfoById(sealId)
  322. if err != nil {
  323. if err.Error() == utils.ErrNoRow() {
  324. err = errors.New(fmt.Sprint("根据用印编号:", sealId, " 找不到初始用印"))
  325. }
  326. return
  327. }
  328. if sealInfo.UserId != opUser.AdminId && opUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE {
  329. err = errors.New("当前账号无操作权限")
  330. return
  331. }
  332. if sealInfo.Status != "已审批" {
  333. err = errors.New("用印状态异常,不允许作废,当前用印状态:" + sealInfo.Status)
  334. return
  335. }
  336. //如果删除状态 >0,那么代表已经被删除了
  337. if sealInfo.IsDelete > 0 {
  338. err = errors.New("该用印已删除")
  339. return
  340. }
  341. //用印作废
  342. err = seal.Invalid(sealInfo)
  343. if err != nil {
  344. return
  345. }
  346. // 操作日志
  347. err = seal.AddSealOperationRecord(sealInfo.SealId, opUser.AdminId, 0, "invalid", opUser.RealName, "作废审批", "")
  348. if err != nil {
  349. err = errors.New("新增用印日志失败")
  350. return
  351. }
  352. //如果是系统合同,同时需要同步作废合同时,那么逻辑处理
  353. if sealInfo.ContractId > 0 && isInvalidContract {
  354. err = contractService.InvalidContract(sealInfo.ContractId, opUser)
  355. if err != nil {
  356. utils.ApiLog.Println("作废用印时,同步作废合同失败,Err:", err.Error())
  357. }
  358. }
  359. //获取最近一次提交的审批单信息
  360. contractApproval, err := contract_approval.GetLastContractApprovalByContractId(sealInfo.SealId, "seal")
  361. if err != nil {
  362. err = nil
  363. return
  364. }
  365. //如果有提测信息,那么需要给对应的审批人发送消息
  366. if contractApproval != nil {
  367. //作废后需要给审批者发送消息
  368. contractApprovalRecordList, tmpErr := contract_approval_record.GetContractApprovalRecordListByContractApprovalId(contractApproval.ContractApprovalId)
  369. if tmpErr != nil {
  370. return
  371. }
  372. content := fmt.Sprint("作废", sealInfo.CompanyName, "用印申请")
  373. for _, contractApprovalRecord := range contractApprovalRecordList {
  374. if contractApprovalRecord.NodeType == "check" { //如果是审批人,那么需要发送消息给对方
  375. //操作人信息
  376. sysUserMobile := ""
  377. sysUser, _ := admin.GetAdminById(contractApprovalRecord.ApproveUserId)
  378. if sysUser != nil {
  379. sysUserMobile = sysUser.Mobile
  380. }
  381. go services.AddCompanyApprovalMessage(opUser.AdminId, contractApprovalRecord.ApproveUserId, 0, contractApprovalRecord.ContractApprovalRecordId, 3, sourceType, 2, sealInfo.CompanyName, content, content, sysUserMobile, "")
  382. }
  383. }
  384. }
  385. //添加操作日志
  386. //remark := "作废合同模板"
  387. //_ = contract_operation_record.AddContractOperationRecord(nowContract.ContractId, opUser.AdminId, 0, "invalid", opUser.RealName, remark)
  388. return
  389. }
  390. // UploadCheckBackFileByFile 上传签回用印附件(实际操作)
  391. func UploadCheckBackFileByFile(sealId int, ext string, fileMulti multipart.File, opUser *custom.AdminWx) (sealInfo *seal.Seal, err error) {
  392. //查询当前用印信息
  393. sealInfo, err = seal.GetSealInfoById(sealId)
  394. if err != nil {
  395. return
  396. }
  397. //用印状态判断
  398. if sealInfo.Status != "已审批" && sealInfo.Status != "已签回" {
  399. err = errors.New("用印状态异常,不允许上传签回用印附件,当前用印状态:" + sealInfo.Status)
  400. return
  401. }
  402. fileName := ``
  403. //保存的文件名
  404. fileName = sealInfo.CompanyName + "_" + sealInfo.Code + "(签回)"
  405. if sealInfo.ContractId > 0 {
  406. //获取合同信息
  407. contractInfo, tmpErr := contract.GetContractById(sealInfo.ContractId)
  408. if tmpErr != nil {
  409. err = tmpErr
  410. return
  411. }
  412. //保存的文件名
  413. fileName = contractInfo.CompanyName + "_" + contractInfo.ContractCode + "(签回)"
  414. }
  415. //非正式环境下,文件名上面还是加上随机数
  416. if utils.RunMode != "release" {
  417. fileName += "_" + utils.GetRandStringNoSpecialChar(10)
  418. }
  419. fileName += ext
  420. //上传到阿里云
  421. resourceUrl, err := services.UploadToOssAndFileName(fileMulti, fileName)
  422. if err != nil {
  423. err = errors.New("文件保存失败,Err:" + err.Error())
  424. return
  425. }
  426. sealInfo.CheckBackFileUrl = resourceUrl
  427. sealInfo.Status = "已签回"
  428. sealInfo.ModifyTime = time.Now()
  429. sealInfo.CheckBackFileTime = time.Now()
  430. err = sealInfo.Update([]string{"CheckBackFileUrl", "Status", "ModifyTime", "CheckBackFileTime"})
  431. if err != nil {
  432. return
  433. }
  434. // 发送模版消息
  435. sellerInfo, tErr := admin.GetAdminById(sealInfo.UserId)
  436. if tErr == nil {
  437. if sellerInfo.Mobile != "" {
  438. go services.SendSealFinishedWxTemplateMsg(sellerInfo.Mobile, sealInfo.CompanyName, sealInfo.SealId)
  439. }
  440. }
  441. return
  442. }
  443. // UploadCheckBackFile 上传签回用印附件(实际操作)
  444. func UploadCheckBackFile(sealId int, fileUrl string, opUser *custom.AdminWx) (sealInfo *seal.Seal, err error) {
  445. //查询当前用印信息
  446. sealInfo, err = seal.GetSealInfoByContractId(sealId)
  447. if err != nil {
  448. return
  449. }
  450. //用印状态判断
  451. if sealInfo.Status != "已审批" && sealInfo.Status != "已签回" {
  452. err = errors.New("用印状态异常,不允许上传签回用印附件,当前用印状态:" + sealInfo.Status)
  453. return
  454. }
  455. sealInfo.CheckBackFileUrl = fileUrl
  456. sealInfo.Status = "已签回"
  457. sealInfo.ModifyTime = time.Now()
  458. sealInfo.CheckBackFileTime = time.Now()
  459. err = sealInfo.Update([]string{"CheckBackFileUrl", "Status", "ModifyTime", "CheckBackFileTime"})
  460. if err != nil {
  461. return
  462. }
  463. // 发送模版消息
  464. sellerInfo, tErr := admin.GetAdminById(sealInfo.UserId)
  465. if tErr == nil {
  466. if sellerInfo.Mobile != "" {
  467. go services.SendSealFinishedWxTemplateMsg(sellerInfo.Mobile, sealInfo.CompanyName, sealInfo.SealId)
  468. }
  469. }
  470. return
  471. }
  472. // GetOpButton 获取审批流和权限
  473. func GetOpButton(sealInfo *seal.Seal, contractApprovalInfo *contract_approval.ContractApproval, opUser *custom.AdminWx) (sealOpButton sealResp.OpButton, flowNodeListResp [][]contract_approval_record.ContractApprovalRecord, err error) {
  474. //审批流
  475. approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalId(contractApprovalInfo.ContractApprovalId)
  476. if err != nil {
  477. err = errors.New("获取审批失败,Err:" + err.Error())
  478. return
  479. }
  480. //待审批状态下,如果是自己发起的审批单,同时已经经过了一轮审批,那么标记为处理中
  481. if contractApprovalInfo.Status == "待审批" {
  482. if contractApprovalInfo.ApplyUserId == opUser.AdminId && contractApprovalInfo.CurrNodeId > contractApprovalInfo.StartNodeId {
  483. sealInfo.Status = "处理中"
  484. }
  485. }
  486. flowNodeMap := make(map[int][]contract_approval_record.ContractApprovalRecord, 0)
  487. keySort := make([]int, 0)
  488. for _, approvalRecord := range approvalRecordList {
  489. //如果当前节点正好是该节点,同时审批单状态是待审批状态,然后当前账号又有权限,该账号也正是审批人,那么允许审批操作
  490. if contractApprovalInfo.CurrNodeId == approvalRecord.NodeId && contractApprovalInfo.Status == "待审批" {
  491. if opUser.AdminId == approvalRecord.ApproveUserId && approvalRecord.NodeType == "check" {
  492. sealOpButton.Approval = true
  493. }
  494. //合规在审批过程中具有 部分修改权限
  495. if opUser.AdminId == approvalRecord.ApproveUserId && opUser.RoleTypeCode == utils.ROLE_TYPE_CODE_COMPLIANCE && approvalRecord.NodeType == "check" {
  496. sealOpButton.CheckEdit = true
  497. }
  498. }
  499. //如果当前节点正好是该节点,同时审批单状态又是第一层节点
  500. if contractApprovalInfo.CurrNodeId == approvalRecord.NodeId && approvalRecord.PrevNodeId == 0 && contractApprovalInfo.Status == "待审批" {
  501. //发起人具有撤回审批单权限
  502. if opUser.AdminId == sealInfo.UserId {
  503. sealOpButton.Cancel = true
  504. }
  505. }
  506. if tmpFlowNodeList, ok := flowNodeMap[approvalRecord.NodeId]; ok {
  507. flowNodeMap[approvalRecord.NodeId] = append(tmpFlowNodeList, *approvalRecord)
  508. } else {
  509. tmpFlowNodeList := make([]contract_approval_record.ContractApprovalRecord, 1)
  510. tmpFlowNodeList[0] = *approvalRecord
  511. flowNodeMap[approvalRecord.NodeId] = tmpFlowNodeList
  512. keySort = append(keySort, approvalRecord.NodeId)
  513. }
  514. //待审批状态下,如果当前审批节点就是操作人,审批节点超过当前审批节点,且节点类型是抄送人,,那么标记为处理中
  515. if contractApprovalInfo.Status == "待审批" {
  516. if approvalRecord.ApproveUserId == opUser.AdminId && approvalRecord.NodeId < contractApprovalInfo.CurrNodeId && approvalRecord.NodeType != "cc" {
  517. sealInfo.Status = "处理中"
  518. }
  519. }
  520. }
  521. for _, key := range keySort {
  522. flowNodeListResp = append(flowNodeListResp, flowNodeMap[key])
  523. }
  524. //作废权限
  525. if opUser.AdminId == sealInfo.UserId && sealInfo.Status == "已审批" {
  526. sealOpButton.Invalid = true
  527. }
  528. //是否具有签回合同用印权限
  529. uploadStatus := []string{"已审批", "已签回"}
  530. if opUser.RoleTypeCode == utils.ROLE_TYPE_CODE_COMPLIANCE && strings.Contains(strings.Join(uploadStatus, ","), sealInfo.Status) {
  531. sealOpButton.UploadFile = true
  532. }
  533. //编辑权限
  534. editStatus := []string{"已驳回", "待提交", "已撤回"}
  535. if opUser.AdminId == sealInfo.UserId && strings.Contains(strings.Join(editStatus, ","), sealInfo.Status) {
  536. sealOpButton.Edit = true
  537. }
  538. return
  539. }
  540. //根据用印id获取用印详情
  541. func GetSealDetailBySealId(sealId int, opUser *custom.AdminWx) (sealMore *sealResp.SealMoreResp, flowNodeListResp [][]contract_approval_record.ContractApprovalRecord, opButton sealResp.OpButton, err error) {
  542. sealInfo, err := seal.GetSealInfoById(sealId)
  543. if err != nil {
  544. err = errors.New("获取合同详情失败,ERR:" + err.Error())
  545. return
  546. }
  547. sealAttachements, err := seal.GetAttachmentBySealId(sealId)
  548. if err != nil {
  549. err = errors.New(fmt.Sprint("获取用印附件失败,Err:"+err.Error(), err))
  550. return
  551. }
  552. fileUrls := make([]string, 0)
  553. if sealInfo.FileUrl != "" {
  554. fileUrls = append(fileUrls, sealInfo.FileUrl)
  555. }
  556. for _, v := range sealAttachements{
  557. fileUrls = append(fileUrls, v.FileUrl)
  558. }
  559. sealMore = &sealResp.SealMoreResp{
  560. Seal: sealInfo,
  561. FileUrls: fileUrls,
  562. }
  563. //查询最近一次审批单信息
  564. lastApprovalInfo, err := contract_approval.GetLastContractApprovalByContractId(sealInfo.SealId, "seal")
  565. if err != nil {
  566. if err.Error() != utils.ErrNoRow() {
  567. err = errors.New("获取最近一次审批单信息失败,ERR:" + err.Error())
  568. }
  569. err = nil
  570. return
  571. }
  572. if lastApprovalInfo != nil {
  573. //获取审批操作和审批流数据
  574. tmpSealOpButton, tmpFlowNodeListResp, tmpErr := GetOpButton(sealInfo, lastApprovalInfo, opUser)
  575. if tmpErr != nil {
  576. err = errors.New("获取用印按钮权限、审批流程失败,Err:" + tmpErr.Error())
  577. return
  578. }
  579. opButton = tmpSealOpButton
  580. flowNodeListResp = tmpFlowNodeListResp
  581. }
  582. return
  583. }
  584. // updateSealAndAttachment 更新用印申请,并更新附件
  585. func updateSealAndAttachment(sealInfo *seal.Seal, updateCol []string, fileUrls []string) (err error){
  586. o := orm.NewOrm()
  587. to, err := o.Begin()
  588. if err != nil {
  589. return
  590. }
  591. defer func() {
  592. if err != nil {
  593. _ = to.Rollback()
  594. } else {
  595. _ = to.Commit()
  596. }
  597. }()
  598. err = sealInfo.Update(updateCol)
  599. if err != nil {
  600. return
  601. }
  602. now := time.Now()
  603. //删除原有的附件,新增最新的附件
  604. err = seal.DelAttachmentBySealId(sealInfo.SealId)
  605. if err != nil {
  606. return
  607. }
  608. attachments := make([]*seal.Attachment, 0)
  609. if len(fileUrls) > 1 {
  610. for _, v := range fileUrls {
  611. tmp := &seal.Attachment{
  612. SealId: sealInfo.SealId,
  613. FileUrl: v,
  614. ModifyTime: now,
  615. CreateTime: now,
  616. }
  617. attachments = append(attachments, tmp)
  618. }
  619. err = seal.AddAttachments(attachments)
  620. }
  621. return
  622. }