company_approval.go 45 KB

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