seal.go 22 KB

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