company_approval.go 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. package company_apply
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_mobile_admin/models/custom"
  7. "hongze/hongze_mobile_admin/models/tables/admin"
  8. "hongze/hongze_mobile_admin/models/tables/approval_flow"
  9. "hongze/hongze_mobile_admin/models/tables/approval_flow_node"
  10. "hongze/hongze_mobile_admin/models/tables/company"
  11. "hongze/hongze_mobile_admin/models/tables/company_approval"
  12. "hongze/hongze_mobile_admin/models/tables/company_approval_message"
  13. "hongze/hongze_mobile_admin/models/tables/company_contract"
  14. "hongze/hongze_mobile_admin/models/tables/company_delay_permission"
  15. "hongze/hongze_mobile_admin/models/tables/company_product"
  16. "hongze/hongze_mobile_admin/models/tables/company_product_log"
  17. "hongze/hongze_mobile_admin/models/tables/company_product_try_out_update_log"
  18. "hongze/hongze_mobile_admin/models/tables/company_product_update_log"
  19. "hongze/hongze_mobile_admin/models/tables/company_renewal_record"
  20. "hongze/hongze_mobile_admin/models/tables/company_report_permission"
  21. "hongze/hongze_mobile_admin/models/tables/contract_approval"
  22. "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
  23. "hongze/hongze_mobile_admin/services"
  24. "hongze/hongze_mobile_admin/services/alarm_msg"
  25. contractService "hongze/hongze_mobile_admin/services/contract"
  26. cygxService "hongze/hongze_mobile_admin/services/cygx"
  27. "hongze/hongze_mobile_admin/services/flow"
  28. "hongze/hongze_mobile_admin/utils"
  29. "strings"
  30. "time"
  31. )
  32. var sourceType = 1 //来源客户
  33. // 提交审批
  34. func Apply(companyApprovalId int, messageContent string) (err error) {
  35. //获取客户单详情
  36. recodeInfo, err := company_approval.GetCompanyApprovalByApprovalId(companyApprovalId)
  37. if err != nil {
  38. err = errors.New("获取合同信息失败,Err:" + err.Error())
  39. return
  40. }
  41. if recodeInfo == nil {
  42. err = errors.New("客户单异常,找不到客户单信息")
  43. return
  44. }
  45. //获取客户信息
  46. companyInfo, err := company.GetCompanyById(recodeInfo.CompanyId)
  47. if err != nil {
  48. err = errors.New("获取合同信息失败,Err:" + err.Error())
  49. return
  50. }
  51. if companyInfo == nil {
  52. err = errors.New("客户信息异常,找不到客户信息")
  53. return
  54. }
  55. //合同状态判断
  56. ignoreStatus := []string{"待审批", "已撤回", "驳回"}
  57. if !strings.Contains(strings.Join(ignoreStatus, ","), recodeInfo.ApproveStatus) {
  58. err = errors.New("客户单状态异常,不允许提交审批,当前客户单状态:" + recodeInfo.ApproveStatus)
  59. return
  60. }
  61. //待审内容
  62. applyContent := `试用转正式`
  63. switch recodeInfo.ApplyMethod { //"1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新"
  64. case 1:
  65. applyContent = `试用转正式`
  66. case 2:
  67. applyContent = `冻结转试用`
  68. case 3:
  69. applyContent = `试用延期`
  70. case 4:
  71. applyContent = `原销售申领客户`
  72. case 5:
  73. applyContent = `续约申请`
  74. case 6:
  75. applyContent = `补充协议`
  76. }
  77. contractDetailByte, err := json.Marshal(recodeInfo)
  78. if err != nil {
  79. err = errors.New(fmt.Sprint("合同状态异常,当前合同数据格式化失败:", err))
  80. return
  81. }
  82. //获取基础审批流
  83. var flowItemInfo *approval_flow.ApprovalFlowItem
  84. if recodeInfo.ProductId == 1 {
  85. tmpFlowItemInfo, tmpErr := flow.GetApprovalFlow(1)
  86. if tmpErr != nil {
  87. err = errors.New(fmt.Sprint("查找审批流程配置失败:", tmpErr))
  88. return
  89. }
  90. flowItemInfo = tmpFlowItemInfo
  91. } else {
  92. tmpFlowItemInfo, tmpErr := flow.GetApprovalFlow(2)
  93. if tmpErr != nil {
  94. err = errors.New(fmt.Sprint("查找审批流程配置失败:", tmpErr))
  95. return
  96. }
  97. flowItemInfo = tmpFlowItemInfo
  98. }
  99. //校验是否存在待审批的审批单(其实没有多大意义,只是为了 异常数据校验)
  100. contractApproval := &contract_approval.ContractApproval{
  101. ContractId: recodeInfo.CompanyApprovalId,
  102. Status: "待审批",
  103. ApprovalType: "company",
  104. ApplyContent: applyContent,
  105. ContractDetail: string(contractDetailByte),
  106. ApplyUserId: recodeInfo.ApplyUserId,
  107. ApplyUserName: recodeInfo.ApplyRealName,
  108. FlowId: flowItemInfo.FlowId,
  109. FlowVersion: flowItemInfo.CurrVersion,
  110. CurrNodeId: flowItemInfo.NodeList[0].NodeId,
  111. StartNodeId: flowItemInfo.NodeList[0].NodeId,
  112. ModifyTime: time.Now(),
  113. CreateTime: time.Now(),
  114. }
  115. has, err := contractApproval.CheckPendingByContractId(recodeInfo.CompanyApprovalId, "company")
  116. if err != nil {
  117. return
  118. }
  119. if has {
  120. err = errors.New("客户单异常,不允许提交审批,存在待审核的客户单")
  121. return
  122. }
  123. contractApprovalRecordList := make([]*contract_approval_record.ContractApprovalRecord, 0)
  124. ApproveUserMap := make(map[int]approval_flow_node.User)
  125. for _, nodeItem := range flowItemInfo.NodeList {
  126. //审批流记录
  127. if len(nodeItem.UserList) <= 0 {
  128. err = errors.New(fmt.Sprint("审批流程异常,没有可审批的人员,Err:", err))
  129. return
  130. }
  131. for _, userItem := range nodeItem.UserList {
  132. contractApprovalRecord := &contract_approval_record.ContractApprovalRecord{
  133. Status: "待审批",
  134. ApproveRemark: "",
  135. ApproveUserId: userItem.AdminId,
  136. ApproveUserName: userItem.Name,
  137. ApproveRoleTypeCode: userItem.RoleTypeCode,
  138. NodeId: nodeItem.NodeId,
  139. NodeType: nodeItem.NodeType,
  140. NextNodeId: nodeItem.NextNodeId,
  141. PrevNodeId: nodeItem.PrevNodeId,
  142. AuditType: nodeItem.AuditType,
  143. CreateTime: time.Now(),
  144. ModifyTime: time.Now(),
  145. }
  146. contractApprovalRecordList = append(contractApprovalRecordList, contractApprovalRecord)
  147. ApproveUserMap[userItem.AdminId] = userItem
  148. }
  149. }
  150. err = contractApproval.Apply(contractApproval, contractApprovalRecordList)
  151. if err != nil {
  152. return
  153. }
  154. //待办通知
  155. {
  156. //发送消息下级审批人
  157. go messageToNodeUser(contractApproval.CurrNodeId, recodeInfo.ApplyUserId, companyInfo.CompanyId, contractApproval.ContractId, contractApproval.ContractApprovalId, 1, 1, companyInfo.CompanyName, messageContent)
  158. }
  159. return
  160. }
  161. // CancelApply 撤回审批
  162. func CancelApply(recordInfo *company_approval.CompanyApproval, opUserId int) (err error) {
  163. if recordInfo == nil {
  164. err = errors.New("客户单异常,没有客户单信息")
  165. return
  166. }
  167. //客户单状态判断
  168. if recordInfo.ApproveStatus != "待审批" {
  169. err = errors.New("客户单状态异常,不允许撤回申请,当前客户单状态:" + recordInfo.ApproveStatus)
  170. return
  171. }
  172. //校验操作人与客户单申请人是否同一人
  173. if recordInfo.ApplyUserId != opUserId {
  174. err = errors.New(fmt.Sprint("操作人异常,不允许撤回非本人提交的申请;客户单发起人:", recordInfo.ApplyUserId, ";撤销人:", opUserId))
  175. return
  176. }
  177. //获取最近一次待审批的审批单信息
  178. approval, err := contract_approval.GetLastContractApprovalByContractId(recordInfo.CompanyApprovalId, "company")
  179. if err != nil {
  180. return
  181. }
  182. //是否一级审批节点判断,如果不是一级审批节点,那么不允许撤回
  183. if approval.StartNodeId != approval.CurrNodeId {
  184. err = errors.New("审批单状态异常,不允许撤回申请,当前审批单已被审批操作,进入了下一级审批节点")
  185. return
  186. }
  187. //获取当前待审批的审批流信息
  188. approvalRecord, err := contract_approval_record.GetCurrContractApprovalRecordByContractId(approval.ContractApprovalId)
  189. if err != nil {
  190. return
  191. }
  192. //判断当前节点是否存在上级节点,如果存在,那么说明
  193. if approvalRecord.PrevNodeId > 0 {
  194. err = errors.New("合同已存在审批操作,不允许撤回申请")
  195. return
  196. }
  197. //获取当前审批单中所有待审批的流程流
  198. approvalRecordList, err := contract_approval_record.GetPendingContractApprovalRecordListByContractId(approval.ContractApprovalId, approvalRecord.NodeId)
  199. if err != nil {
  200. return
  201. }
  202. //撤回审批流
  203. err = approval.Cancel(approval, approvalRecordList)
  204. if err != nil {
  205. return
  206. }
  207. //添加操作日志 (??这个看情况再说,或许没有必要)
  208. //作废原有消息
  209. //for _, contractApprovalRecord := range approvalRecordList {
  210. // go company_approval_message.CancelCompanyApprovalMessage(contractApprovalRecord.ContractApprovalRecordId, 2)
  211. //}
  212. go company_approval_message.CancelCompanyApprovalMessage(approval.ContractId, 2)
  213. return
  214. }
  215. // Reject 驳回审批
  216. func Reject(approvalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, rejectRemark string) (err error) {
  217. if approvalRecord == nil {
  218. err = errors.New("审批流异常,没有审批流信息")
  219. return
  220. }
  221. //审批流状态判断
  222. if approvalRecord.Status != "待审批" {
  223. err = errors.New("审批流状态异常,不允许驳回申请,当前审批流状态:" + approvalRecord.Status)
  224. return
  225. }
  226. //判断是否审批类型,如果不是审批类型,那么就没有审批权限
  227. if approvalRecord.NodeType != "check" {
  228. err = errors.New("当前账号没有审批权限")
  229. return
  230. }
  231. //操作人
  232. opUserId := opUser.AdminId
  233. opUserName := opUser.RealName
  234. //操作权限校验
  235. if opUser.RoleTypeCode != approvalRecord.ApproveRoleTypeCode {
  236. err = errors.New("当前账号没有审批权限")
  237. return
  238. }
  239. if approvalRecord.ApproveUserId > 0 && approvalRecord.ApproveUserId != opUserId {
  240. err = errors.New("当前账号没有审批权限,需要指定人操作")
  241. return
  242. }
  243. //获取审批单详情
  244. approval, err := contract_approval.GetContractApprovalById(approvalRecord.ContractApprovalId)
  245. if err != nil {
  246. return
  247. }
  248. //审批单状态判断
  249. if approval.Status != "待审批" {
  250. err = errors.New("审批单状态异常,不允许驳回申请,当前审批单状态:" + approval.Status)
  251. return
  252. }
  253. //获取 客户单 信息
  254. recordInfo, err := company_approval.GetCompanyApprovalById(approval.ContractId)
  255. if err != nil {
  256. return
  257. }
  258. //客户单 状态判断
  259. if recordInfo.ApproveStatus != "待审批" {
  260. err = errors.New("客户单状态异常,不允许驳回申请,当前客户单状态:" + recordInfo.ApproveStatus)
  261. return
  262. }
  263. //获取 客户 信息
  264. companyInfo, err := company.GetCompanyById(recordInfo.CompanyId)
  265. if err != nil {
  266. return
  267. }
  268. err = approval.Reject(approval, approvalRecord, opUserId, opUserName, rejectRemark)
  269. if err != nil {
  270. return
  271. }
  272. //添加操作日志
  273. //remark := "驳回申请"
  274. //_ = contract.AddContractOperationRecord(contractInfo.ContractId, opUserId, approvalRecord.ContractApprovalRecordId, "reject", opUserName, remark)
  275. //完成审批后的驳回操作
  276. go afterReject(approval.ContractId, opUserId, opUserName, rejectRemark)
  277. //待办通知
  278. {
  279. //content := contractInfo.CompanyName + " 合同模板已驳回"
  280. content := companyInfo.CompanyName + " " + recordInfo.ApproveContent + "审批驳回"
  281. approvalSysUser, _ := admin.GetAdminById(approval.ApplyUserId)
  282. go services.AddCompanyApprovalMessage(opUserId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, 2, sourceType, 3, companyInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
  283. }
  284. return
  285. }
  286. // Approved 审批通过
  287. func Approved(approvalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, approvedRemark string) (err error) {
  288. if approvalRecord == nil {
  289. err = errors.New("审批流异常,没有审批流信息")
  290. return
  291. }
  292. //审批流状态判断
  293. if approvalRecord.Status != "待审批" {
  294. err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + approvalRecord.Status)
  295. return
  296. }
  297. //判断是否审批类型,如果不是审批类型,那么就没有审批权限
  298. if approvalRecord.NodeType != "check" {
  299. err = errors.New("当前账号没有审批权限")
  300. return
  301. }
  302. //操作人
  303. opUserId := opUser.AdminId
  304. //opUserName := opUser.RealName
  305. //操作权限校验
  306. if opUser.RoleTypeCode != approvalRecord.ApproveRoleTypeCode {
  307. err = errors.New("当前账号没有审批权限")
  308. return
  309. }
  310. if approvalRecord.ApproveUserId > 0 && approvalRecord.ApproveUserId != opUserId {
  311. err = errors.New("当前账号没有审批权限,需要指定人操作")
  312. return
  313. }
  314. //获取审批单详情
  315. approval, err := contract_approval.GetContractApprovalById(approvalRecord.ContractApprovalId)
  316. if err != nil {
  317. return
  318. }
  319. //审批单状态判断
  320. if approval.Status != "待审批" {
  321. err = errors.New("审批单状态异常,不允许审批,当前审批单状态:" + approval.Status)
  322. return
  323. }
  324. //获取 客户单 信息
  325. recordInfo, err := company_approval.GetCompanyApprovalById(approval.ContractId)
  326. if err != nil {
  327. return
  328. }
  329. //客户单 状态判断
  330. if recordInfo.ApproveStatus != "待审批" {
  331. err = errors.New("客户单状态异常,不允许审批,当前客户单状态:" + recordInfo.ApproveStatus)
  332. return
  333. }
  334. //获取 客户 信息
  335. companyInfo, err := company.GetCompanyById(recordInfo.CompanyId)
  336. if err != nil {
  337. return
  338. }
  339. //if contractApprovalRecord
  340. err = approval.Approved(approval, approvalRecord, approvedRemark)
  341. if err != nil {
  342. return
  343. }
  344. //添加操作日志
  345. //remark := "审批通过"
  346. //_ = contract.AddContractOperationRecord(contractInfo.ContractId, opUserId, approvalRecord.ContractApprovalRecordId, "approval", opUserName, remark)
  347. //发送信息给所有抄送人
  348. content := companyInfo.CompanyName + " " + recordInfo.ApproveContent + "审批通过"
  349. //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
  350. if approvalRecord.NextNodeId == 0 {
  351. //待办通知(通知销售已经审核通过了)
  352. {
  353. approvalSysUser, _ := admin.GetAdminById(approval.ApplyUserId)
  354. go services.AddCompanyApprovalMessage(opUserId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, 2, sourceType, 2, companyInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
  355. }
  356. // 客户单审批后的异步操作(修改客户单信息、修改合同权限等)
  357. go func() {
  358. _ = afterApproved(recordInfo.CompanyApprovalId, opUser.AdminId, opUser.RealName)
  359. // 注意此处下面的处理原本都是各开了协程,现改为只在一个协程里处理,不然可能会导致数据还没完全更新,紧接着就被错误更新对应的数据
  360. if recordInfo.ProductId == 2 && recordInfo.CompanyContractId > 0 {
  361. //处理权益客户的身份信息
  362. cygxService.UpdateCygxCompanyUserType(recordInfo.CompanyContractId)
  363. //if recordInfo.ApplyMethod == 1 {
  364. //获取合同信息
  365. contractInfo, tmpErr := company_contract.GetCompanyContractDetail(recordInfo.CompanyId, recordInfo.ProductId, recordInfo.CompanyContractId)
  366. if tmpErr != nil {
  367. err = errors.New(fmt.Sprint("待审批信息不存在,CompanyId:", recordInfo.CompanyId, ";productId:", recordInfo.ProductId, ";Err:"+tmpErr.Error()))
  368. return
  369. }
  370. if contractInfo == nil {
  371. err = errors.New(fmt.Sprint("待审批信息不存在,CompanyId:", recordInfo.CompanyId, ";productId:", recordInfo.ProductId))
  372. return
  373. }
  374. //格式化合同开始时间
  375. contractStartDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.StartDate)
  376. if tmpErr != nil {
  377. err = errors.New("合同开始时间转换失败,Err:" + tmpErr.Error())
  378. return
  379. }
  380. if time.Now().After(contractStartDate) {
  381. cygxService.ActivitySpecialCompanyApproval(recordInfo.CompanyId, recordInfo.CompanyContractId, companyInfo.CompanyName) //审批通过的时候专项调研次数更新
  382. }
  383. //}
  384. //权益销售客户申请转正后,消息群发给所有销售
  385. services.AddCompanyApprovalMessageRai(recordInfo.CompanyId, recordInfo.CompanyContractId, recordInfo.ApplyRealName, companyInfo.CompanyName)
  386. //客户研选行业转正时(王芳审批通过),模板消息提醒汪洋
  387. services.AddCompanyApprovalMessageWangYang(recordInfo.CompanyId, recordInfo.CompanyContractId, recordInfo.ApplyRealName, companyInfo.CompanyName)
  388. cygxService.YanXuanCompanyApproval(recordInfo.CompanyId, recordInfo.CompanyContractId) //研选审批通过的时候研选扣点更新
  389. //cygxService.HandleAllocationCompanyContractByYanXuan(recordInfo.CompanyContractId) //如果合同只有研选的时候,自动处理派点
  390. cygxService.HandleCompanyContractPackageDifference(recordInfo.CompanyContractId) // 更新与上一份合同的金额的对比 '增加套餐','减少套餐','维持套餐'
  391. cygxService.HandleCompanyContractPermissionContractType(recordInfo.CompanyContractId) // 更新合同权限表中的权限名称,以及对应的行业权限类型(行业新签、行业续约)
  392. contractService.UpdateCompanyProductSellerMove(recordInfo.CompanyId, recordInfo.ProductId) // 权益客户创建新的有效合同之后,更新所关联的销售信息
  393. }
  394. }()
  395. } else {
  396. //获取下级节点信息
  397. flowNodeInfo, tmpErr := approval_flow_node.GetByNodeId(approvalRecord.NextNodeId)
  398. if tmpErr != nil {
  399. err = tmpErr
  400. return
  401. }
  402. //如果该级节点是抄送类型,那么需要将该节点给处理掉
  403. if flowNodeInfo.NodeType == "cc" {
  404. go approvedByCc(approvedRemark, approvalRecord)
  405. } else {
  406. //发送消息下级审批人
  407. go messageToNodeUser(approvalRecord.NextNodeId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, approval.ContractApprovalId, 1, 1, companyInfo.CompanyName, content)
  408. }
  409. }
  410. return
  411. }
  412. // approvedByCc 审批通过(抄送节点)
  413. func approvedByCc(approvedRemark string, sourceApprovalRecord *contract_approval_record.ContractApprovalRecord) (err error) {
  414. //下个流程节点id
  415. nextNodeId := 0
  416. //获取审批单中抄送节点的所有数据列表
  417. approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractIdAndNode(sourceApprovalRecord.ContractApprovalId, sourceApprovalRecord.NextNodeId)
  418. if err != nil {
  419. return
  420. }
  421. //遍历所有的抄送单
  422. for _, contractApprovalRecord := range approvalRecordList {
  423. nextNodeId = contractApprovalRecord.NextNodeId
  424. //审批流状态判断
  425. if contractApprovalRecord.Status != "待审批" {
  426. err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + contractApprovalRecord.Status)
  427. return
  428. }
  429. //判断是否审批类型,如果不是审批类型,那么就没有审批权限
  430. if contractApprovalRecord.NodeType != "cc" {
  431. err = errors.New("当前账号不是抄送权限")
  432. return
  433. }
  434. }
  435. //获取审批单详情
  436. approval, err := contract_approval.GetContractApprovalById(sourceApprovalRecord.ContractApprovalId)
  437. if err != nil {
  438. return
  439. }
  440. //获取 客户单 信息
  441. recordInfo, err := company_approval.GetCompanyApprovalById(approval.ContractId)
  442. if err != nil {
  443. return
  444. }
  445. //客户单 状态判断
  446. if recordInfo.ApproveStatus != "待审批" {
  447. err = errors.New("客户单状态异常,不允许审批,当前客户单状态:" + recordInfo.ApproveStatus)
  448. return
  449. }
  450. //获取 客户 信息
  451. companyInfo, err := company.GetCompanyById(recordInfo.CompanyId)
  452. if err != nil {
  453. return
  454. }
  455. //if contractApprovalRecord
  456. err = approval.ApprovedByCc(approval, approvalRecordList, approvedRemark, nextNodeId)
  457. if err != nil {
  458. return
  459. }
  460. content := companyInfo.CompanyName + " " + recordInfo.ApproveContent + "审批通过"
  461. //发送消息给抄送人
  462. go messageToNodeUser(approvalRecordList[0].NodeId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, approval.ContractApprovalId, 1, 1, companyInfo.CompanyName, content)
  463. //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
  464. if nextNodeId == 0 {
  465. //待办通知
  466. {
  467. approvalSysUser, _ := admin.GetAdminById(approval.ApplyUserId)
  468. go services.AddCompanyApprovalMessage(sourceApprovalRecord.ApproveUserId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, 2, sourceType, 2, companyInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
  469. }
  470. //客户单审批后的异步操作(修改客户单信息、修改合同权限等)
  471. go afterApproved(recordInfo.CompanyApprovalId, sourceApprovalRecord.ApproveUserId, sourceApprovalRecord.ApproveUserName)
  472. } else {
  473. //获取下级节点信息
  474. flowNodeInfo, tmpErr := approval_flow_node.GetByNodeId(nextNodeId)
  475. if tmpErr != nil {
  476. err = tmpErr
  477. return
  478. }
  479. //如果下级节点是抄送类型,那么还是需要处理抄送节点逻辑
  480. if flowNodeInfo.NodeType == "cc" {
  481. go approvedByCc(approvedRemark, sourceApprovalRecord)
  482. } else {
  483. //如果下级级节点是审批类型
  484. //发送消息下级审批人
  485. go messageToNodeUser(nextNodeId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, approval.ContractApprovalId, 1, 1, companyInfo.CompanyName, content)
  486. }
  487. }
  488. return
  489. }
  490. // messageToNodeUser 抄送信息给用户(根据节点来推送)
  491. func messageToNodeUser(flowNodeId, createUserId, companyId, companyApprovalId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
  492. //获取审批单中抄送节点的所有数据列表
  493. contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractIdAndNode(contractApprovalId, flowNodeId)
  494. if err != nil {
  495. return
  496. }
  497. err = messageToUser(contractApprovalRecordList, createUserId, companyId, companyApprovalId, messageType, approvalStatus, companyName, content)
  498. return
  499. }
  500. // messageToAllCcUser 发送信息给所有抄送人
  501. // @param createUserId 消息发起人
  502. // @param contractApprovalId 审批单id
  503. // @param messageType 消息类型:1:申请消息,2:审批结果
  504. // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回"
  505. // @param companyName 企业客户名称
  506. // @param content 消息内容
  507. func messageToAllCcUser(createUserId, companyId, companyApprovalId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
  508. //获取审批单中抄送节点的所有数据列表
  509. contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalId(contractApprovalId)
  510. if err != nil {
  511. return
  512. }
  513. err = messageToUser(contractApprovalRecordList, createUserId, companyId, companyApprovalId, messageType, approvalStatus, companyName, content)
  514. return
  515. }
  516. // messageToUser 抄送信息给用户(发送给所有抄送人)
  517. // @param createUserId 待审批、抄送记录
  518. // @param createUserId 消息发起人
  519. // @param contractApprovalId 审批单id
  520. // @param messageType 消息类型:1:申请消息,2:审批结果
  521. // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回"
  522. // @param companyName 企业客户名称
  523. // @param content 消息内容
  524. func messageToUser(contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord, createUserId, companyId, companyApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
  525. for _, contractApprovalRecord := range contractApprovalRecordList {
  526. sysUserMobile := ""
  527. sysUser, _ := admin.GetAdminById(contractApprovalRecord.ApproveUserId)
  528. if sysUser != nil {
  529. sysUserMobile = sysUser.Mobile
  530. }
  531. //接收人,就是审批记录里面的待审核人(抄送人)
  532. receiveUserId := contractApprovalRecord.ApproveUserId
  533. go services.AddCompanyApprovalMessage(createUserId, receiveUserId, companyId, companyApprovalId, messageType, sourceType, approvalStatus, companyName, content, content, sysUserMobile, "")
  534. }
  535. return
  536. }
  537. // afterApproved 审批完成后操作
  538. func afterApproved(companyApprovalId int, opUserId int, opUserName string) (err error) {
  539. defer func() {
  540. if err != nil {
  541. //fmt.Println("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  542. //go utils.SendEmail("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  543. go alarm_msg.SendAlarmMsg("审批完成后操作失败,ERR:"+err.Error()+"", 3)
  544. }
  545. }()
  546. opUser := custom.AdminWx{
  547. AdminId: opUserId,
  548. RealName: opUserName,
  549. }
  550. //获取客户单详情
  551. recodeInfo, err := company_approval.GetCompanyApprovalByApprovalId(companyApprovalId)
  552. if err != nil {
  553. err = errors.New("获取合同信息失败,Err:" + err.Error())
  554. return
  555. }
  556. if recodeInfo == nil {
  557. err = errors.New("客户单异常,找不到客户单信息")
  558. return
  559. }
  560. //获取客户信息
  561. companyInfo, err := company.GetCompanyById(recodeInfo.CompanyId)
  562. if err != nil {
  563. err = errors.New("获取合同信息失败,Err:" + err.Error())
  564. return
  565. }
  566. if companyInfo == nil {
  567. err = errors.New("客户信息异常,找不到客户信息")
  568. return
  569. }
  570. //客户产品信息
  571. companyProduct, err := company_product.GetCompanyProductByCompanyIdAndProductId(recodeInfo.CompanyId, recodeInfo.ProductId)
  572. if err != nil {
  573. err = errors.New(fmt.Sprint("获取信息客户产品失败", ";Err:"+err.Error()))
  574. return
  575. }
  576. if companyProduct == nil {
  577. err = errors.New("获取信息客户产品失败2")
  578. return
  579. }
  580. logSellerId := recodeInfo.ApplyUserId
  581. logSellerName := recodeInfo.ApplyRealName
  582. isAddCompanyProductUpdateLog := true //是否添加变更日志
  583. companyReportPermissionList := make([]*company_report_permission.CompanyReportPermission, 0) //需要变更的品种列表
  584. var startDate, endDate string //变更到最新的日期
  585. switch recodeInfo.ApplyMethod {
  586. case 1: //试用转正式
  587. //获取合同信息
  588. contractInfo, tmpErr := company_contract.GetCompanyContractDetail(recodeInfo.CompanyId, recodeInfo.ProductId, recodeInfo.CompanyContractId)
  589. if tmpErr != nil {
  590. err = errors.New(fmt.Sprint("待审批信息不存在,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId, ";Err:"+tmpErr.Error()))
  591. return
  592. }
  593. if contractInfo == nil {
  594. err = errors.New(fmt.Sprint("待审批信息不存在,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId))
  595. return
  596. }
  597. //格式化合同开始时间
  598. contractStartDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.StartDate)
  599. if tmpErr != nil {
  600. err = errors.New("合同开始时间转换失败,Err:" + tmpErr.Error())
  601. return
  602. }
  603. //格式化合同结束日期
  604. contractEndDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.EndDate)
  605. if tmpErr != nil {
  606. err = errors.New("合同结束时间转换失败,Err:" + tmpErr.Error())
  607. return
  608. }
  609. //格式化 产品中 合同结束日期
  610. //productContractEndDate, err := time.Parse(utils.FormatDate, companyProduct.ContractEndDate)
  611. //if err != nil {
  612. // br.Msg = "审批失败"
  613. // br.ErrMsg = "产品中合同结束时间转换失败,Err:" + err.Error()
  614. // return
  615. //}
  616. //如果是权益的用户客户转正审批单独走
  617. if companyProduct.Status == utils.COMPANY_STATUS_FOREVER && companyProduct.ProductId == 2 {
  618. //contractInfo.StartDate = contractStartDate.AddDate(0, 4, 0).Format(utils.FormatDate) // 开始时间与结束时间都加四个月
  619. //contractInfo.EndDate = contractEndDate.AddDate(0, 4, 0).Format(utils.FormatDate)
  620. startDate = contractInfo.StartDate
  621. endDate = contractInfo.EndDate
  622. companyReportPermissionList, tmpErr = company_approval.TryOutToFormalXClassRai(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, contractInfo.CompanyContractId, contractInfo.StartDate, contractInfo.EndDate, opUser.RealName, companyProduct.ProductName, contractInfo.RaiPackageType)
  623. if tmpErr != nil {
  624. err = errors.New("试用转正式失败,Err:" + tmpErr.Error())
  625. return
  626. }
  627. } else {
  628. //如果合同时间小于等于今天,那么立马执行合同内容
  629. if time.Now().After(contractStartDate) {
  630. startDate = contractInfo.StartDate
  631. endDate = contractInfo.EndDate
  632. companyReportPermissionList, tmpErr = company_approval.TryOutToFormal(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, contractInfo.CompanyContractId, contractInfo.StartDate, contractInfo.EndDate, opUser.RealName, companyProduct.ProductName, contractInfo.PackageType, contractInfo.RaiPackageType)
  633. if tmpErr != nil {
  634. err = errors.New("试用转正式失败,Err:" + tmpErr.Error())
  635. return
  636. }
  637. } else {
  638. tmpErr = company_approval.ApplyApproveContract(recodeInfo.CompanyId, recodeInfo.ProductId, recodeInfo.CompanyApprovalId, contractInfo.CompanyContractId)
  639. if tmpErr != nil {
  640. err = errors.New("试用转正式失败,Err:" + tmpErr.Error())
  641. return
  642. }
  643. //不添加日志
  644. isAddCompanyProductUpdateLog = false
  645. }
  646. //如果合同中的结束日期大于产品中的合同结束日期,那么就去修改产品中的合同结束日期
  647. if contractEndDate.After(companyProduct.ContractEndDate) {
  648. companyProduct.ContractEndDate = contractEndDate
  649. cols := make([]string, 0)
  650. cols = append(cols, "ContractEndDate")
  651. _ = companyProduct.Update(cols)
  652. }
  653. }
  654. case 2: //冻结->试用
  655. //权益的 x类试用转冻结的申请解冻
  656. if companyProduct.InitStatus == utils.COMPANY_STATUS_FOREVER && companyProduct.ProductId == utils.COMPANY_PRODUCT_RAI_ID {
  657. tmpCompanyReportPermissionList, tmpStartDate, tmpEndDate, tmpErr := company_approval.FreezeToTryOutXClassRai(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, recodeInfo.ApplyUserId, opUser.RealName, companyProduct.ProductName)
  658. if tmpErr != nil {
  659. err = errors.New("冻结转试用失败,Err:" + tmpErr.Error())
  660. return
  661. }
  662. companyReportPermissionList = tmpCompanyReportPermissionList
  663. startDate = tmpStartDate
  664. endDate = tmpEndDate
  665. } else {
  666. tmpCompanyReportPermissionList, tmpStartDate, tmpEndDate, tmpErr := company_approval.FreezeToTryOut(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, recodeInfo.ApplyUserId, opUser.RealName, companyProduct.ProductName)
  667. if tmpErr != nil {
  668. err = errors.New("冻结转试用失败,Err:" + tmpErr.Error())
  669. return
  670. }
  671. companyReportPermissionList = tmpCompanyReportPermissionList
  672. startDate = tmpStartDate
  673. endDate = tmpEndDate
  674. }
  675. case 3: //试用延期
  676. tmpCompanyReportPermissionList, tmpEndDate, tmpErr := company_approval.TryOutDelay(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, opUser.RealName, companyProduct.EndDate, companyProduct.ProductName)
  677. if tmpErr != nil {
  678. err = errors.New("试用延期失败,Err:" + tmpErr.Error())
  679. return
  680. }
  681. companyReportPermissionList = tmpCompanyReportPermissionList
  682. startDate = companyProduct.StartDate
  683. endDate = tmpEndDate
  684. case 4: //原销售申请领取流失客户
  685. companyReportPermissionList, startDate, endDate, err = company_approval.ApplyReceive(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, recodeInfo.ApplyUserId, opUser.RealName, companyProduct.ProductName)
  686. if err != nil {
  687. err = errors.New("原销售申请领取流失客户失败,Err:" + err.Error())
  688. return
  689. }
  690. case 5, 6: //正式客户申请服务更新
  691. //获取合同信息
  692. contractInfo, tmpErr := company_contract.GetCompanyContractDetail(recodeInfo.CompanyId, recodeInfo.ProductId, recodeInfo.CompanyContractId)
  693. if tmpErr != nil {
  694. err = errors.New(fmt.Sprint("正式客户申请服务更新失败,待审批合同信息不存在,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId, ";Err:"+tmpErr.Error()))
  695. return
  696. }
  697. if contractInfo == nil {
  698. err = errors.New(fmt.Sprint("正式客户申请服务更新失败,待审批合同信息不存在2,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId))
  699. return
  700. }
  701. //格式化合同开始时间
  702. contractStartDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.StartDate)
  703. if tmpErr != nil {
  704. err = errors.New(fmt.Sprint("正式客户申请服务更新失败,合同开始时间转换失败;Err:" + tmpErr.Error()))
  705. return
  706. }
  707. //格式化合同结束日期
  708. contractEndDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.EndDate)
  709. if tmpErr != nil {
  710. err = errors.New(fmt.Sprint("正式客户申请服务更新失败,合同结束时间转换失败;Err:" + tmpErr.Error()))
  711. return
  712. }
  713. //格式化 产品中 合同结束日期
  714. //productContractEndDate, err := time.Parse(utils.FormatDate, companyProduct.ContractEndDate)
  715. //if err != nil {
  716. // br.Msg = "审批失败"
  717. // br.ErrMsg = "产品中合同结束时间转换失败,Err:" + err.Error()
  718. // return
  719. //}
  720. //如果合同时间小于等于今天,那么立马执行合同内容
  721. if time.Now().After(contractStartDate) {
  722. // 合同处理完成后的续约异常记录
  723. contactHandleCompanyRenewalRecord(contractInfo)
  724. companyReportPermissionList, err = company_approval.ApplyServiceUpdate(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, recodeInfo.CompanyContractId, companyProduct.StartDate, contractInfo.EndDate, opUser.RealName, companyProduct.ProductName, contractInfo.PackageType, contractInfo.RaiPackageType)
  725. if tmpErr != nil {
  726. err = errors.New(fmt.Sprint("正式客户申请服务更新失败,执行失败;Err:" + tmpErr.Error()))
  727. return
  728. }
  729. } else {
  730. tmpErr = company_approval.ApplyApproveContract(recodeInfo.CompanyId, recodeInfo.ProductId, recodeInfo.CompanyApprovalId, recodeInfo.CompanyContractId)
  731. if tmpErr != nil {
  732. err = errors.New(fmt.Sprint("正式客户申请服务更新失败,执行失败2;Err:" + tmpErr.Error()))
  733. return
  734. }
  735. //不添加日志
  736. isAddCompanyProductUpdateLog = false
  737. }
  738. //如果合同中的结束日期大于产品中的合同结束日期,那么就去修改产品中的合同结束日期
  739. if contractEndDate.After(companyProduct.ContractEndDate) {
  740. companyProduct.ContractEndDate = contractEndDate
  741. cols := make([]string, 0)
  742. cols = append(cols, "ContractEndDate")
  743. _ = companyProduct.Update(cols)
  744. }
  745. default:
  746. err = errors.New(fmt.Sprint("无效的审批状态,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId, ";ApplyMethod:", recodeInfo.ApplyMethod, ";ApproveStatus:", recodeInfo.ApproveStatus))
  747. return
  748. }
  749. //新增客户产品日志
  750. {
  751. productLog := new(company_product_log.CompanyProductLog)
  752. productLog.CompanyProductId = companyProduct.CompanyProductId
  753. productLog.CompanyId = companyProduct.CompanyId
  754. productLog.ProductId = companyProduct.ProductId
  755. productLog.ProductName = companyProduct.ProductName
  756. productLog.CompanyName = companyProduct.CompanyName
  757. productLog.CompanyName = companyProduct.CompanyName
  758. productLog.Source = companyProduct.Source
  759. productLog.Reasons = companyProduct.Reasons
  760. productLog.Status = companyProduct.Status
  761. productLog.IndustryId = companyProduct.IndustryId
  762. productLog.IndustryName = companyProduct.IndustryName
  763. productLog.SellerId = companyProduct.SellerId
  764. productLog.SellerName = companyProduct.SellerName
  765. productLog.GroupId = companyProduct.GroupId
  766. productLog.DepartmentId = companyProduct.DepartmentId
  767. productLog.IsSuspend = companyProduct.IsSuspend
  768. productLog.SuspendTime = companyProduct.SuspendTime
  769. productLog.ApproveStatus = companyProduct.ApproveStatus
  770. productLog.FreezeTime = companyProduct.FreezeTime
  771. productLog.Remark = companyProduct.Remark
  772. productLog.StartDate = companyProduct.StartDate
  773. productLog.EndDate = companyProduct.EndDate
  774. productLog.CreateTime = companyProduct.CreateTime
  775. productLog.ModifyTime = companyProduct.ModifyTime
  776. productLog.LoseReason = companyProduct.LoseReason
  777. productLog.LossTime = companyProduct.LossTime
  778. productLog.LogType = "modify"
  779. productLog.LogCreateTime = time.Now()
  780. productLog.AdminId = opUserId
  781. go company_product_log.AddCompanyProductLog(productLog)
  782. }
  783. //新增操作记录
  784. {
  785. remark := "审批"
  786. operation := "approve"
  787. approveContent := "审批通过"
  788. if companyProduct.Status == utils.COMPANY_STATUS_FOREVER && companyProduct.InitStatus == utils.COMPANY_STATUS_FOREVER && companyProduct.ProductId == 2 { //权益永续客户申请内容做变更
  789. //123月份申请转正通过时,将试用期限延长(更新)至4.30。456月份申请转正通过时,将试用期限延长至7.31。789月份申请转正通过时,将试用期限延长至10.31。101112月份申请转正通过时,将试用期限延长至次年1.31。
  790. endDateContent := utils.GetLastDayOfQuarter(time.Now()).Format(utils.FormatDate)
  791. approveContent += ",试用延期至" + endDateContent
  792. }
  793. services.AddCompanyOperationRecord(recodeInfo.CompanyId, companyProduct.SellerId, opUserId, recodeInfo.ProductId, opUserId, companyProduct.CompanyName,
  794. companyProduct.ProductName, opUserName, remark, operation, approveContent, opUserName, "", companyProduct.Status)
  795. }
  796. //新增客户产品状态变更日志
  797. if isAddCompanyProductUpdateLog {
  798. updateSource := ``
  799. switch recodeInfo.ApplyMethod {
  800. case 1: //试用转正式
  801. updateSource = `turn_positive`
  802. case 2: //冻结->试用
  803. updateSource = "thaw"
  804. case 3: //试用延期
  805. updateSource = "delay"
  806. case 4: //原销售申请领取流失客户
  807. updateSource = "apply_receive"
  808. case 5: //正式客户申请续约
  809. updateSource = "service_update"
  810. case 6: //正式客户新增补充协议
  811. updateSource = "add_agreement"
  812. }
  813. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  814. endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  815. companyProductUpdateLog := &company_product_update_log.CompanyProductUpdateLog{
  816. Id: 0,
  817. CompanyId: companyProduct.CompanyId,
  818. ProductId: companyProduct.ProductId,
  819. Status: companyProduct.Status,
  820. SellerId: logSellerId,
  821. SellerName: logSellerName,
  822. Source: updateSource,
  823. IsFormal: companyProduct.IsFormal, //是否已经转正式,0是没有转正式,1是已经转过正式
  824. StartDate: startDateTime,
  825. EndDate: endDateTime,
  826. RealEndDate: startDateTime,
  827. CreateTime: time.Now(),
  828. }
  829. go company_product_update_log.AddCompanyProductUpdateLog(companyProductUpdateLog)
  830. // 2: //冻结->试用;3: //试用延期;4: //原销售申请领取流失客户;
  831. if recodeInfo.ApplyMethod == 2 || recodeInfo.ApplyMethod == 3 || recodeInfo.ApplyMethod == 4 {
  832. companyProductTryOutUpdateLog := &company_product_try_out_update_log.CompanyProductTryOutUpdateLog{
  833. Id: 0,
  834. CompanyId: companyProduct.CompanyId,
  835. ProductId: companyProduct.ProductId,
  836. SellerId: logSellerId,
  837. SellerName: logSellerName,
  838. Source: updateSource,
  839. StartDate: startDateTime,
  840. EndDate: endDateTime,
  841. RealEndDate: startDateTime,
  842. IsStop: 0,
  843. CreateTime: time.Now(),
  844. }
  845. go company_product_try_out_update_log.AddCompanyProductTryOutUpdateLog(companyProductTryOutUpdateLog, companyReportPermissionList)
  846. }
  847. }
  848. return
  849. }
  850. // contactHandleCompanyRenewalRecord
  851. // @Description: 合同处理完成后的续约异常记录
  852. // @author: Roc
  853. // @datetime 2023-12-07 14:24:44
  854. // @param contractInfo *company_contract.CompanyContract
  855. // @param day string
  856. // @return err error
  857. func contactHandleCompanyRenewalRecord(contractInfo *company_contract.CompanyContractDetail) {
  858. var err error
  859. defer func() {
  860. if err != nil {
  861. utils.FileLog.Error("合同处理完成后的续约异常记录," + err.Error())
  862. }
  863. }()
  864. // 判断合同类型是否是续约合同,如果不是的话,就不往下走了
  865. if contractInfo.ContractType != `续约合同` {
  866. return
  867. }
  868. day := time.Now().Format(utils.FormatDate)
  869. // 获取早于当前合同结束日期的上一份合同
  870. lastContract, tmpErr := company_contract.GetLastContractListByEndDate(contractInfo.CompanyId, contractInfo.ProductId, contractInfo.EndDate)
  871. if tmpErr != nil {
  872. err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";通过最近一份合同的日期获取早于该合同的最晚一份合同失败,ERR:", tmpErr))
  873. return
  874. }
  875. // 校验 上一份合同的结束日期 与 今天 相隔的天数
  876. betweenDay, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, day, lastContract.EndDate)
  877. if tmpErr != nil {
  878. err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";计算两个日期相差的天数失败,ERR:", tmpErr))
  879. return
  880. }
  881. source := 2 // 正常续约
  882. // 如果间隔时间超过60天,那么标记为超时续约
  883. if betweenDay > 60 {
  884. source = 3 // 超时续约
  885. }
  886. // 如果间隔时间超过60天,那么标记为超时续约
  887. companyProductItem, tmpErr := company_product.GetCompanyProductItemByCompanyId(contractInfo.CompanyId, contractInfo.ProductId)
  888. if tmpErr != nil {
  889. err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";GetCompanyProductItemByCompanyId失败,ERR:", tmpErr))
  890. return
  891. }
  892. var shareSellerId int
  893. var shareSellerName string
  894. if companyProductItem.IsShare == 1 {
  895. shareSellerId = companyProductItem.ShareSellerId
  896. shareSellerName = companyProductItem.ShareSeller
  897. }
  898. item := &company_renewal_record.CompanyRenewalRecord{
  899. Id: 0,
  900. CompanyId: contractInfo.CompanyId,
  901. ProductId: contractInfo.ProductId,
  902. Source: source,
  903. SellerId: companyProductItem.SellerId,
  904. SellerName: companyProductItem.SellerName,
  905. ShareSellerId: shareSellerId,
  906. ShareSellerName: shareSellerName,
  907. CreateTime: time.Now(),
  908. ModifyTime: time.Now(),
  909. }
  910. tmpErr = item.Add()
  911. if tmpErr != nil {
  912. err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";添加续约异常记录失败,ERR:", tmpErr))
  913. }
  914. return
  915. }
  916. // afterReject 驳回完成后操作
  917. func afterReject(companyApprovalId, opUserId int, opUserName, remark string) (err error) {
  918. defer func() {
  919. if err != nil {
  920. fmt.Println("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  921. //go utils.SendEmail("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  922. go alarm_msg.SendAlarmMsg("驳回完成后操作失败,ERR:"+err.Error()+"", 3)
  923. }
  924. }()
  925. //获取客户单详情
  926. recodeInfo, err := company_approval.GetCompanyApprovalByApprovalId(companyApprovalId)
  927. if err != nil {
  928. err = errors.New("获取合同信息失败,Err:" + err.Error())
  929. return
  930. }
  931. if recodeInfo == nil {
  932. err = errors.New("客户单异常,找不到客户单信息")
  933. return
  934. }
  935. //获取客户信息
  936. companyInfo, err := company.GetCompanyById(recodeInfo.CompanyId)
  937. if err != nil {
  938. err = errors.New("获取合同信息失败,Err:" + err.Error())
  939. return
  940. }
  941. if companyInfo == nil {
  942. err = errors.New("客户信息异常,找不到客户信息")
  943. return
  944. }
  945. //客户产品信息
  946. companyProduct, err := company_product.GetCompanyProductByCompanyIdAndProductId(recodeInfo.CompanyId, recodeInfo.ProductId)
  947. if err != nil {
  948. err = errors.New(fmt.Sprint("获取信息客户产品失败", ";Err:"+err.Error()))
  949. return
  950. }
  951. if companyProduct == nil {
  952. err = errors.New("获取信息客户产品失败2")
  953. return
  954. }
  955. approveStatus := "驳回"
  956. err = company_approval.ApproveRefuse(recodeInfo.CompanyId, recodeInfo.ProductId, approveStatus, remark)
  957. if err != nil {
  958. err = errors.New("客户单审批驳回失败,Err:" + err.Error())
  959. return
  960. }
  961. //新增客户产品日志
  962. {
  963. productLog := new(company_product_log.CompanyProductLog)
  964. productLog.CompanyProductId = companyProduct.CompanyProductId
  965. productLog.CompanyId = companyProduct.CompanyId
  966. productLog.ProductId = companyProduct.ProductId
  967. productLog.ProductName = companyProduct.ProductName
  968. productLog.CompanyName = companyProduct.CompanyName
  969. productLog.CompanyName = companyProduct.CompanyName
  970. productLog.Source = companyProduct.Source
  971. productLog.Reasons = companyProduct.Reasons
  972. productLog.Status = companyProduct.Status
  973. productLog.IndustryId = companyProduct.IndustryId
  974. productLog.IndustryName = companyProduct.IndustryName
  975. productLog.SellerId = companyProduct.SellerId
  976. productLog.SellerName = companyProduct.SellerName
  977. productLog.GroupId = companyProduct.GroupId
  978. productLog.DepartmentId = companyProduct.DepartmentId
  979. productLog.IsSuspend = companyProduct.IsSuspend
  980. productLog.SuspendTime = companyProduct.SuspendTime
  981. productLog.ApproveStatus = companyProduct.ApproveStatus
  982. productLog.FreezeTime = companyProduct.FreezeTime
  983. productLog.Remark = companyProduct.Remark
  984. productLog.StartDate = companyProduct.StartDate
  985. productLog.EndDate = companyProduct.EndDate
  986. productLog.CreateTime = companyProduct.CreateTime
  987. productLog.ModifyTime = companyProduct.ModifyTime
  988. productLog.LoseReason = companyProduct.LoseReason
  989. productLog.LossTime = companyProduct.LossTime
  990. productLog.LogType = "modify"
  991. productLog.LogCreateTime = time.Now()
  992. productLog.AdminId = opUserId
  993. go company_product_log.AddCompanyProductLog(productLog)
  994. }
  995. //新增操作记录
  996. {
  997. remark := "审批"
  998. operation := "approve"
  999. approveContent := "审批驳回"
  1000. services.AddCompanyOperationRecord(recodeInfo.CompanyId, companyProduct.SellerId, opUserId, recodeInfo.ProductId, opUserId, companyProduct.CompanyName,
  1001. companyProduct.ProductName, opUserName, remark, operation, approveContent, opUserName, remark, companyProduct.Status)
  1002. }
  1003. return
  1004. }
  1005. // CheckApproveAuth 校验审批操作权限
  1006. func CheckApproveAuth(companyId, productId int, opUser *custom.AdminWx) (approvalItem *company_approval.CompanyApproval, approvalInfo *contract_approval.ContractApproval, approvalRecord *contract_approval_record.ContractApprovalRecord, err error) {
  1007. //客户单详情
  1008. approvalItem, err = company_approval.GetCompanyApprovalByCompanyId(companyId, productId)
  1009. if err != nil {
  1010. err = errors.New("获取客户单失败,Err:" + err.Error())
  1011. return
  1012. }
  1013. //客户单状态判断
  1014. if approvalItem.ApproveStatus != "待审批" {
  1015. err = errors.New("客户单状态异常,不允许操作申请,当前客户单状态:" + approvalItem.ApproveStatus)
  1016. return
  1017. }
  1018. //获取该最近一条审批单详情
  1019. approvalInfo, err = contract_approval.GetLastContractApprovalByContractId(approvalItem.CompanyApprovalId, "company")
  1020. if err != nil {
  1021. err = errors.New("获取审批单失败,Err:" + err.Error())
  1022. return
  1023. }
  1024. if approvalInfo.Status != "待审批" {
  1025. err = errors.New("当前审批单状态异常,不允许审批,审批单状态:" + approvalInfo.Status)
  1026. return
  1027. }
  1028. //审批流
  1029. approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalIdAndNodeId(approvalInfo.ContractApprovalId, approvalInfo.CurrNodeId)
  1030. if err != nil {
  1031. err = errors.New("获取审批失败,Err:" + err.Error())
  1032. return
  1033. }
  1034. for _, tmpApprovalRecord := range approvalRecordList {
  1035. if tmpApprovalRecord.NodeType == "check" && tmpApprovalRecord.ApproveUserId == opUser.AdminId {
  1036. approvalRecord = tmpApprovalRecord
  1037. }
  1038. }
  1039. if approvalRecord == nil {
  1040. err = errors.New("当前账号没有审批权限")
  1041. return
  1042. }
  1043. return
  1044. }
  1045. // 获取审批流和权限
  1046. func GetOpButton(approvalItem *company_approval.CompanyApprovalList, contractApprovalInfo *contract_approval.ContractApproval, opUser *custom.AdminWx) (flowNodeListResp [][]contract_approval_record.ContractApprovalRecord, err error) {
  1047. //审批流
  1048. approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalId(contractApprovalInfo.ContractApprovalId)
  1049. if err != nil {
  1050. err = errors.New("获取审批失败,Err:" + err.Error())
  1051. return
  1052. }
  1053. flowNodeMap := make(map[int][]contract_approval_record.ContractApprovalRecord, 0)
  1054. keySort := make([]int, 0)
  1055. for _, approvalRecord := range approvalRecordList {
  1056. //如果当前节点正好是该节点,同时审批单状态是待审批状态,然后当前账号又有权限,该账号也正是审批人,那么允许审批操作
  1057. if contractApprovalInfo.CurrNodeId == approvalRecord.NodeId && contractApprovalInfo.Status == "待审批" {
  1058. if opUser.AdminId == approvalRecord.ApproveUserId && approvalRecord.NodeType == "check" {
  1059. approvalItem.OpButton = true
  1060. }
  1061. }
  1062. //如果当前节点正好是该节点,同时审批单状态又是第一层节点
  1063. if contractApprovalInfo.CurrNodeId == approvalRecord.NodeId && approvalRecord.PrevNodeId == 0 && contractApprovalInfo.Status == "待审批" {
  1064. //发起人具有撤回审批单权限
  1065. //if opUser.AdminId == sealInfo.UserId {
  1066. // sealOpButton.Cancel = true
  1067. //}
  1068. }
  1069. if tmpFlowNodeList, ok := flowNodeMap[approvalRecord.NodeId]; ok {
  1070. flowNodeMap[approvalRecord.NodeId] = append(tmpFlowNodeList, *approvalRecord)
  1071. } else {
  1072. tmpFlowNodeList := make([]contract_approval_record.ContractApprovalRecord, 1)
  1073. tmpFlowNodeList[0] = *approvalRecord
  1074. flowNodeMap[approvalRecord.NodeId] = tmpFlowNodeList
  1075. keySort = append(keySort, approvalRecord.NodeId)
  1076. }
  1077. }
  1078. for _, key := range keySort {
  1079. flowNodeListResp = append(flowNodeListResp, flowNodeMap[key])
  1080. }
  1081. return
  1082. }
  1083. // GetApprovalDetailByCompanyApprovalId 根据客户单id获取客户单审批详情
  1084. func GetApprovalDetailByCompanyApprovalId(companyApprovalId int, opUser *custom.AdminWx) (approvalItem *company_approval.CompanyApprovalList, flowNodeListResp [][]contract_approval_record.ContractApprovalRecord, err error) {
  1085. var condition string
  1086. var pars []interface{}
  1087. approvalItem, err = company_approval.GetApprovalInfoByApprovalId(condition, pars, companyApprovalId)
  1088. if err != nil {
  1089. err = errors.New("获取客户单详情失败,ERR:" + err.Error())
  1090. return
  1091. }
  1092. //查询相关的合同详情
  1093. if approvalItem.CompanyContractId > 0 {
  1094. companyContractDetail, e := company_contract.GetCompanyContractDetail(approvalItem.CompanyId, approvalItem.ProductId, approvalItem.CompanyContractId)
  1095. if e == nil {
  1096. if companyContractDetail.Source == "系统合同" {
  1097. approvalItem.ContractSourceTag = "标准合同"
  1098. } else {
  1099. approvalItem.ContractSourceTag = "非标合同"
  1100. }
  1101. }
  1102. }
  1103. if approvalItem.FreezeStartDate != "" && approvalItem.FreezeEndDate != "" && approvalItem.Status == utils.COMPANY_STATUS_FREEZE {
  1104. freezeEndDate, _ := time.Parse(utils.FormatDate, approvalItem.FreezeEndDate)
  1105. if time.Now().Before(freezeEndDate) {
  1106. expireDays := freezeEndDate.Sub(time.Now()).Hours() / 24
  1107. approvalItem.FreezeExpireDays = int(expireDays)
  1108. }
  1109. }
  1110. if approvalItem.StartDate != "" && approvalItem.EndDate != "" {
  1111. endDate, _ := time.Parse(utils.FormatDate, approvalItem.EndDate)
  1112. if time.Now().Before(endDate) {
  1113. expireDays := endDate.Sub(time.Now()).Hours() / 24
  1114. approvalItem.ExpireDay = int(expireDays)
  1115. }
  1116. }
  1117. switch approvalItem.ApplyMethod {
  1118. case 2:
  1119. //客户冻结转试用次数
  1120. approvalCount, tmpErr := company_approval.GetFreezeToTryCompanyApprovalCount(approvalItem.CompanyId, approvalItem.ProductId, approvalItem.ApplyUserId)
  1121. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1122. err = tmpErr
  1123. return
  1124. }
  1125. approvalItem.ApprovalCount = approvalCount
  1126. case 3:
  1127. delayPermission, tmpErr := company_delay_permission.GetApplyDelayPermission(approvalItem.CompanyApprovalId)
  1128. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1129. err = tmpErr
  1130. return
  1131. }
  1132. approvalItem.DelayPermission = delayPermission
  1133. //客户审批试用延期次数
  1134. approvalCount, tmpErr := company_approval.GetCompanyTryDelayApprovalCount(approvalItem.CompanyId, approvalItem.ProductId)
  1135. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1136. err = tmpErr
  1137. return
  1138. }
  1139. approvalItem.ApprovalCount = approvalCount
  1140. }
  1141. //查询最近一次审批单信息
  1142. lastApprovalInfo, err := contract_approval.GetLastContractApprovalByContractId(approvalItem.CompanyApprovalId, "company")
  1143. if err != nil {
  1144. if err.Error() != utils.ErrNoRow() {
  1145. err = errors.New("获取最近一次审批单信息失败,ERR:" + err.Error())
  1146. }
  1147. err = nil
  1148. return
  1149. }
  1150. if lastApprovalInfo != nil {
  1151. //获取审批操作和审批流数据
  1152. tmpFlowNodeListResp, tmpErr := GetOpButton(approvalItem, lastApprovalInfo, opUser)
  1153. if tmpErr != nil {
  1154. err = errors.New("获取客户单按钮权限、审批流程失败,Err:" + tmpErr.Error())
  1155. return
  1156. }
  1157. flowNodeListResp = tmpFlowNodeListResp
  1158. }
  1159. approvalItem.Status = strings.Replace(approvalItem.Status, utils.COMPANY_STATUS_FOREVER, utils.COMPANY_STATUS_X_CLASS_TRY_OUT, -1) //永续替换成 X类试用
  1160. return
  1161. }