company_approval.go 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. services.UpdateCompanyProductSellerMove(recordInfo.CompanyId, recordInfo.ProductId) // 权益客户创建新的有效合同之后,更新所关联的销售信息
  379. services.UpdateCompanyContracthideBycompanyId(recordInfo.CompanyId, recordInfo.ProductId) // 权益客户创建新的有效合同之后,对相关合同进行隐藏
  380. }
  381. }()
  382. } else {
  383. //获取下级节点信息
  384. flowNodeInfo, tmpErr := system.GetByNodeId(approvalRecord.NextNodeId)
  385. if tmpErr != nil {
  386. err = tmpErr
  387. return
  388. }
  389. //如果该级节点是抄送类型,那么需要将该节点给处理掉
  390. if flowNodeInfo.NodeType == "cc" {
  391. go approvedByCc(approvedRemark, approvalRecord)
  392. } else {
  393. //发送消息下级审批人
  394. go messageToNodeUser(approvalRecord.NextNodeId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, approval.ContractApprovalId, 1, 1, companyInfo.CompanyName, content)
  395. }
  396. }
  397. return
  398. }
  399. // approvedByCc 审批通过(抄送节点)
  400. func approvedByCc(approvedRemark string, sourceApprovalRecord *contract.ContractApprovalRecord) (err error) {
  401. //下个流程节点id
  402. nextNodeId := 0
  403. //获取审批单中抄送节点的所有数据列表
  404. approvalRecordList, err := contract.GetContractApprovalRecordListByContractIdAndNode(sourceApprovalRecord.ContractApprovalId, sourceApprovalRecord.NextNodeId)
  405. if err != nil {
  406. return
  407. }
  408. //遍历所有的抄送单
  409. for _, contractApprovalRecord := range approvalRecordList {
  410. nextNodeId = contractApprovalRecord.NextNodeId
  411. //审批流状态判断
  412. if contractApprovalRecord.Status != "待审批" {
  413. err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + contractApprovalRecord.Status)
  414. return
  415. }
  416. //判断是否审批类型,如果不是审批类型,那么就没有审批权限
  417. if contractApprovalRecord.NodeType != "cc" {
  418. err = errors.New("当前账号不是抄送权限")
  419. return
  420. }
  421. }
  422. //获取审批单详情
  423. approval, err := contract.GetContractApprovalById(sourceApprovalRecord.ContractApprovalId)
  424. if err != nil {
  425. return
  426. }
  427. //获取 客户单 信息
  428. recordInfo, err := company.GetCompanyApprovalByCompanyApprovalId(approval.ContractId)
  429. if err != nil {
  430. return
  431. }
  432. //客户单 状态判断
  433. if recordInfo.ApproveStatus != "待审批" {
  434. err = errors.New("客户单状态异常,不允许审批,当前客户单状态:" + recordInfo.ApproveStatus)
  435. return
  436. }
  437. //获取 客户 信息
  438. companyInfo, err := company.GetCompanyById(recordInfo.CompanyId)
  439. if err != nil {
  440. return
  441. }
  442. //if contractApprovalRecord
  443. err = approval.ApprovedByCc(approval, approvalRecordList, approvedRemark, nextNodeId)
  444. if err != nil {
  445. return
  446. }
  447. content := companyInfo.CompanyName + " " + recordInfo.ApproveContent + "审批通过"
  448. //发送消息给抄送人
  449. go messageToNodeUser(approvalRecordList[0].NodeId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, approval.ContractApprovalId, 1, 1, companyInfo.CompanyName, content)
  450. //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
  451. if nextNodeId == 0 {
  452. //待办通知
  453. {
  454. approvalSysUser, _ := system.GetSysAdminById(approval.ApplyUserId)
  455. go services.AddCompanyApprovalMessage(sourceApprovalRecord.ApproveUserId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, 2, sourceType, 2, companyInfo.CompanyName, content, content, approvalSysUser.Mobile)
  456. }
  457. //客户单审批后的异步操作(修改客户单信息、修改合同权限等)
  458. go afterApproved(recordInfo.CompanyApprovalId, sourceApprovalRecord.ApproveUserId, sourceApprovalRecord.ApproveUserName)
  459. } else {
  460. //获取下级节点信息
  461. flowNodeInfo, tmpErr := system.GetByNodeId(nextNodeId)
  462. if tmpErr != nil {
  463. err = tmpErr
  464. return
  465. }
  466. //如果下级节点是抄送类型,那么还是需要处理抄送节点逻辑
  467. if flowNodeInfo.NodeType == "cc" {
  468. go approvedByCc(approvedRemark, sourceApprovalRecord)
  469. } else {
  470. //如果下级级节点是审批类型
  471. //发送消息下级审批人
  472. go messageToNodeUser(nextNodeId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, approval.ContractApprovalId, 1, 1, companyInfo.CompanyName, content)
  473. }
  474. }
  475. return
  476. }
  477. // messageToNodeUser 抄送信息给用户(根据节点来推送)
  478. func messageToNodeUser(flowNodeId, createUserId, companyId, companyApprovalId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
  479. //获取审批单中抄送节点的所有数据列表
  480. contractApprovalRecordList, err := contract.GetContractApprovalRecordListByContractIdAndNode(contractApprovalId, flowNodeId)
  481. if err != nil {
  482. return
  483. }
  484. err = messageToUser(contractApprovalRecordList, createUserId, companyId, companyApprovalId, messageType, approvalStatus, companyName, content)
  485. return
  486. }
  487. // messageToAllCcUser 发送信息给所有抄送人
  488. // @param createUserId 消息发起人
  489. // @param contractApprovalId 审批单id
  490. // @param messageType 消息类型:1:申请消息,2:审批结果
  491. // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回"
  492. // @param companyName 企业客户名称
  493. // @param content 消息内容
  494. func messageToAllCcUser(createUserId, companyId, companyApprovalId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
  495. //获取审批单中抄送节点的所有数据列表
  496. contractApprovalRecordList, err := contract.GetContractApprovalRecordListByContractApprovalId(contractApprovalId)
  497. if err != nil {
  498. return
  499. }
  500. err = messageToUser(contractApprovalRecordList, createUserId, companyId, companyApprovalId, messageType, approvalStatus, companyName, content)
  501. return
  502. }
  503. // messageToUser 抄送信息给用户(发送给所有抄送人)
  504. // @param createUserId 待审批、抄送记录
  505. // @param createUserId 消息发起人
  506. // @param contractApprovalId 审批单id
  507. // @param messageType 消息类型:1:申请消息,2:审批结果
  508. // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回"
  509. // @param companyName 企业客户名称
  510. // @param content 消息内容
  511. func messageToUser(contractApprovalRecordList []*contract.ContractApprovalRecord, createUserId, companyId, companyApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
  512. for _, contractApprovalRecord := range contractApprovalRecordList {
  513. sysUserMobile := ""
  514. sysUser, _ := system.GetSysAdminById(contractApprovalRecord.ApproveUserId)
  515. if sysUser != nil {
  516. sysUserMobile = sysUser.Mobile
  517. }
  518. //接收人,就是审批记录里面的待审核人(抄送人)
  519. receiveUserId := contractApprovalRecord.ApproveUserId
  520. go services.AddCompanyApprovalMessage(createUserId, receiveUserId, companyId, companyApprovalId, messageType, sourceType, approvalStatus, companyName, content, content, sysUserMobile)
  521. }
  522. return
  523. }
  524. // afterApproved 审批完成后操作
  525. func afterApproved(companyApprovalId int, opUserId int, opUserName string) (err error) {
  526. defer func() {
  527. if err != nil {
  528. //fmt.Println("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  529. go alarm_msg.SendAlarmMsg("客户审批通过后,处理客户失败,Err:"+err.Error(), 3)
  530. //go utils.SendEmail("异常提醒:"+utils.RunMode, "客户审批通过后,处理客户失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  531. }
  532. }()
  533. opUser := system.Admin{
  534. AdminId: opUserId,
  535. RealName: opUserName,
  536. }
  537. //获取客户单详情
  538. recodeInfo, err := company.GetCompanyApprovalByApprovalId(companyApprovalId)
  539. if err != nil {
  540. err = errors.New("获取合同信息失败,Err:" + err.Error())
  541. return
  542. }
  543. if recodeInfo == nil {
  544. err = errors.New("客户单异常,找不到客户单信息")
  545. return
  546. }
  547. //获取客户信息
  548. companyInfo, err := company.GetCompanyById(recodeInfo.CompanyId)
  549. if err != nil {
  550. err = errors.New("获取合同信息失败,Err:" + err.Error())
  551. return
  552. }
  553. if companyInfo == nil {
  554. err = errors.New("客户信息异常,找不到客户信息")
  555. return
  556. }
  557. //客户产品信息
  558. companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(recodeInfo.CompanyId, recodeInfo.ProductId)
  559. if err != nil {
  560. err = errors.New(fmt.Sprint("获取信息客户产品失败", ";Err:"+err.Error()))
  561. return
  562. }
  563. if companyProduct == nil {
  564. err = errors.New("获取信息客户产品失败2")
  565. return
  566. }
  567. //recodeInfo.
  568. logSellerId := recodeInfo.ApplyUserId
  569. logSellerName := recodeInfo.ApplyRealName
  570. isAddCompanyProductUpdateLog := true //是否添加变更日志
  571. companyReportPermissionList := make([]*company.CompanyReportPermission, 0) //需要变更的品种列表
  572. var startDate, endDate string //变更到最新的日期
  573. switch recodeInfo.ApplyMethod {
  574. case 1: //试用转正式
  575. //获取合同信息
  576. contractInfo, tmpErr := company.GetCompanyContractDetail(recodeInfo.CompanyId, recodeInfo.ProductId, recodeInfo.CompanyContractId)
  577. if tmpErr != nil {
  578. err = errors.New(fmt.Sprint("待审批信息不存在,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId, ";Err:"+tmpErr.Error()))
  579. return
  580. }
  581. if contractInfo == nil {
  582. err = errors.New(fmt.Sprint("待审批信息不存在,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId))
  583. return
  584. }
  585. //格式化合同开始时间
  586. contractStartDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.StartDate)
  587. if tmpErr != nil {
  588. err = errors.New("合同开始时间转换失败,Err:" + tmpErr.Error())
  589. return
  590. }
  591. //格式化合同结束日期
  592. contractEndDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.EndDate)
  593. if tmpErr != nil {
  594. err = errors.New("合同结束时间转换失败,Err:" + tmpErr.Error())
  595. return
  596. }
  597. //格式化 产品中 合同结束日期
  598. //productContractEndDate, err := time.Parse(utils.FormatDate, companyProduct.ContractEndDate)
  599. //if err != nil {
  600. // br.Msg = "审批失败"
  601. // br.ErrMsg = "产品中合同结束时间转换失败,Err:" + err.Error()
  602. // return
  603. //}
  604. //如果是权益的用户客户转正审批单独走
  605. if companyProduct.Status == utils.COMPANY_STATUS_FOREVER && companyProduct.ProductId == 2 {
  606. //contractInfo.StartDate = contractStartDate.AddDate(0, 4, 0).Format(utils.FormatDate) // 开始时间与结束时间都加三个月
  607. //contractInfo.EndDate = contractEndDate.AddDate(0, 4, 0).Format(utils.FormatDate)
  608. startDate = contractInfo.StartDate
  609. endDate = contractInfo.EndDate
  610. companyReportPermissionList, tmpErr = company.TryOutToFormalXClassRai(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, contractInfo.CompanyContractId, contractInfo.StartDate, contractInfo.EndDate, opUser.RealName, companyProduct.ProductName, contractInfo.RaiPackageType)
  611. if tmpErr != nil {
  612. err = errors.New("试用转正式失败,Err:" + tmpErr.Error())
  613. return
  614. }
  615. } else {
  616. //如果合同时间小于等于今天,那么立马执行合同内容
  617. if time.Now().After(contractStartDate) {
  618. startDate = contractInfo.StartDate
  619. endDate = contractInfo.EndDate
  620. 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)
  621. if tmpErr != nil {
  622. err = errors.New("试用转正式失败,Err:" + tmpErr.Error())
  623. return
  624. }
  625. } else {
  626. tmpErr = company.ApplyApproveContract(recodeInfo.CompanyId, recodeInfo.ProductId, recodeInfo.CompanyApprovalId, contractInfo.CompanyContractId)
  627. if tmpErr != nil {
  628. err = errors.New("试用转正式失败,Err:" + tmpErr.Error())
  629. return
  630. }
  631. //不添加日志
  632. isAddCompanyProductUpdateLog = false
  633. }
  634. //如果合同中的结束日期大于产品中的合同结束日期,那么就去修改产品中的合同结束日期
  635. if contractEndDate.After(companyProduct.ContractEndDate) {
  636. companyProduct.ContractEndDate = contractEndDate
  637. cols := make([]string, 0)
  638. cols = append(cols, "ContractEndDate")
  639. _ = companyProduct.Update(cols)
  640. }
  641. }
  642. case 2: //冻结->试用
  643. //权益的 x类试用转冻结的申请解冻
  644. if companyProduct.InitStatus == utils.COMPANY_STATUS_FOREVER && companyProduct.ProductId == utils.COMPANY_PRODUCT_RAI_ID {
  645. tmpCompanyReportPermissionList, tmpStartDate, tmpEndDate, tmpErr := company.FreezeToTryOutXClassRai(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, recodeInfo.ApplyUserId, opUser.RealName, companyProduct.ProductName)
  646. if tmpErr != nil {
  647. err = errors.New("冻结转试用失败,Err:" + tmpErr.Error())
  648. return
  649. }
  650. companyReportPermissionList = tmpCompanyReportPermissionList
  651. startDate = tmpStartDate
  652. endDate = tmpEndDate
  653. } else {
  654. tmpCompanyReportPermissionList, tmpStartDate, tmpEndDate, tmpErr := company.FreezeToTryOut(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, recodeInfo.ApplyUserId, opUser.RealName, companyProduct.ProductName)
  655. if tmpErr != nil {
  656. err = errors.New("冻结转试用失败,Err:" + tmpErr.Error())
  657. return
  658. }
  659. companyReportPermissionList = tmpCompanyReportPermissionList
  660. startDate = tmpStartDate
  661. endDate = tmpEndDate
  662. }
  663. case 3: //试用延期
  664. tmpCompanyReportPermissionList, tmpEndDate, tmpErr := company.TryOutDelay(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, opUser.RealName, companyProduct.EndDate, companyProduct.ProductName)
  665. if tmpErr != nil {
  666. err = errors.New("试用延期失败,Err:" + tmpErr.Error())
  667. return
  668. }
  669. companyReportPermissionList = tmpCompanyReportPermissionList
  670. startDate = companyProduct.StartDate
  671. endDate = tmpEndDate
  672. case 4: //原销售申请领取流失客户
  673. sellerItem, tmpErr := system.GetSysAdminById(recodeInfo.ApplyUserId)
  674. if tmpErr != nil {
  675. err = errors.New("找不到申请的销售,Err:" + tmpErr.Error())
  676. return
  677. }
  678. companyReportPermissionList, startDate, endDate, tmpErr = company.ApplyReceive(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, sellerItem, opUser.RealName, companyProduct.ProductName, sellerItem.RealName, sellerItem.AdminId)
  679. if tmpErr != nil {
  680. err = errors.New("原销售申请领取流失客户失败,Err:" + tmpErr.Error())
  681. return
  682. }
  683. logSellerId = sellerItem.AdminId
  684. logSellerName = sellerItem.RealName
  685. case 5, 6: //正式客户申请服务更新
  686. //获取合同信息
  687. contractInfo, tmpErr := company.GetCompanyContractDetail(recodeInfo.CompanyId, recodeInfo.ProductId, recodeInfo.CompanyContractId)
  688. if tmpErr != nil {
  689. err = errors.New(fmt.Sprint("正式客户申请服务更新失败,待审批合同信息不存在,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId, ";Err:"+tmpErr.Error()))
  690. return
  691. }
  692. if contractInfo == nil {
  693. err = errors.New(fmt.Sprint("正式客户申请服务更新失败,待审批合同信息不存在2,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId))
  694. return
  695. }
  696. //格式化合同开始时间
  697. contractStartDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.StartDate)
  698. if tmpErr != nil {
  699. err = errors.New(fmt.Sprint("正式客户申请服务更新失败,合同开始时间转换失败;Err:" + tmpErr.Error()))
  700. return
  701. }
  702. //格式化合同结束日期
  703. contractEndDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.EndDate)
  704. if tmpErr != nil {
  705. err = errors.New(fmt.Sprint("正式客户申请服务更新失败,合同结束时间转换失败;Err:" + tmpErr.Error()))
  706. return
  707. }
  708. //格式化 产品中 合同结束日期
  709. //productContractEndDate, err := time.Parse(utils.FormatDate, companyProduct.ContractEndDate)
  710. //if err != nil {
  711. // br.Msg = "审批失败"
  712. // br.ErrMsg = "产品中合同结束时间转换失败,Err:" + err.Error()
  713. // return
  714. //}
  715. //如果合同时间小于等于今天,那么立马执行合同内容
  716. if time.Now().After(contractStartDate) {
  717. // 合同处理完成后的续约异常记录
  718. contactHandleCompanyRenewalRecord(contractInfo)
  719. startDate = contractInfo.StartDate
  720. endDate = contractInfo.EndDate
  721. 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)
  722. if err != nil {
  723. err = errors.New(fmt.Sprint("正式客户申请服务更新失败,执行失败;Err:" + err.Error()))
  724. return
  725. }
  726. } else {
  727. tmpErr = company.ApplyApproveContract(recodeInfo.CompanyId, recodeInfo.ProductId, recodeInfo.CompanyApprovalId, recodeInfo.CompanyContractId)
  728. if tmpErr != nil {
  729. err = errors.New(fmt.Sprint("正式客户申请服务更新失败,执行失败2;Err:" + tmpErr.Error()))
  730. return
  731. }
  732. //不添加日志
  733. isAddCompanyProductUpdateLog = false
  734. }
  735. //如果合同中的结束日期大于产品中的合同结束日期,那么就去修改产品中的合同结束日期
  736. if contractEndDate.After(companyProduct.ContractEndDate) {
  737. companyProduct.ContractEndDate = contractEndDate
  738. cols := make([]string, 0)
  739. cols = append(cols, "ContractEndDate")
  740. _ = companyProduct.Update(cols)
  741. }
  742. default:
  743. err = errors.New(fmt.Sprint("无效的审批状态,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId, ";ApplyMethod:", recodeInfo.ApplyMethod, ";ApproveStatus:", recodeInfo.ApproveStatus))
  744. return
  745. }
  746. //新增客户产品日志
  747. {
  748. productLog := new(company.CompanyProductLog)
  749. productLog.CompanyProductId = companyProduct.CompanyProductId
  750. productLog.CompanyId = companyProduct.CompanyId
  751. productLog.ProductId = companyProduct.ProductId
  752. productLog.ProductName = companyProduct.ProductName
  753. productLog.CompanyName = companyProduct.CompanyName
  754. productLog.CompanyName = companyProduct.CompanyName
  755. productLog.Source = companyProduct.Source
  756. productLog.Reasons = companyProduct.Reasons
  757. productLog.Status = companyProduct.Status
  758. productLog.IndustryId = companyProduct.IndustryId
  759. productLog.IndustryName = companyProduct.IndustryName
  760. productLog.SellerId = companyProduct.SellerId
  761. productLog.SellerName = companyProduct.SellerName
  762. productLog.GroupId = companyProduct.GroupId
  763. productLog.DepartmentId = companyProduct.DepartmentId
  764. productLog.IsSuspend = companyProduct.IsSuspend
  765. productLog.SuspendTime = companyProduct.SuspendTime
  766. productLog.ApproveStatus = companyProduct.ApproveStatus
  767. productLog.FreezeTime = companyProduct.FreezeTime
  768. productLog.Remark = companyProduct.Remark
  769. productLog.StartDate = companyProduct.StartDate
  770. productLog.EndDate = companyProduct.EndDate
  771. productLog.CreateTime = companyProduct.CreateTime
  772. productLog.ModifyTime = companyProduct.ModifyTime
  773. productLog.LoseReason = companyProduct.LoseReason
  774. productLog.LossTime = companyProduct.LossTime
  775. productLog.LogType = "modify"
  776. productLog.LogCreateTime = time.Now()
  777. productLog.AdminId = opUserId
  778. go company.AddCompanyProductLog(productLog)
  779. }
  780. //新增操作记录
  781. {
  782. remark := "审批"
  783. operation := "approve"
  784. approveContent := "审批通过"
  785. if companyProduct.Status == utils.COMPANY_STATUS_FOREVER && companyProduct.InitStatus == utils.COMPANY_STATUS_FOREVER && companyProduct.ProductId == 2 { //权益永续客户申请内容做变更
  786. //123月份申请转正通过时,将试用期限延长(更新)至4.30。456月份申请转正通过时,将试用期限延长至7.31。789月份申请转正通过时,将试用期限延长至10.31。101112月份申请转正通过时,将试用期限延长至次年1.31。
  787. endDateContent := utils.GetLastDayOfQuarter(time.Now()).Format(utils.FormatDate)
  788. approveContent += ",试用延期至" + endDateContent
  789. }
  790. services.AddCompanyOperationRecord(recodeInfo.CompanyId, companyProduct.SellerId, opUserId, recodeInfo.ProductId, opUserId, companyProduct.CompanyName,
  791. companyProduct.ProductName, opUserName, remark, operation, approveContent, opUserName, "", companyProduct.Status)
  792. }
  793. //新增客户产品状态变更日志
  794. if isAddCompanyProductUpdateLog {
  795. updateSource := ``
  796. switch recodeInfo.ApplyMethod {
  797. case 1: //试用转正式
  798. updateSource = `turn_positive`
  799. case 2: //冻结->试用
  800. updateSource = "thaw"
  801. case 3: //试用延期
  802. updateSource = "delay"
  803. case 4: //原销售申请领取流失客户
  804. updateSource = "apply_receive"
  805. case 5: //正式客户申请续约
  806. updateSource = "service_update"
  807. case 6: //正式客户新增补充协议
  808. updateSource = "add_agreement"
  809. }
  810. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  811. endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  812. companyProductUpdateLog := &company.CompanyProductUpdateLog{
  813. Id: 0,
  814. CompanyId: companyProduct.CompanyId,
  815. ProductId: companyProduct.ProductId,
  816. Status: companyProduct.Status,
  817. SellerId: logSellerId,
  818. SellerName: logSellerName,
  819. Source: updateSource,
  820. StartDate: startDateTime,
  821. EndDate: endDateTime,
  822. RealEndDate: startDateTime,
  823. IsFormal: companyProduct.IsFormal, //是否已经转正式,0是没有转正式,1是已经转过正式
  824. CreateTime: time.Now(),
  825. }
  826. go company.AddCompanyProductUpdateLog(companyProductUpdateLog)
  827. // 2: //冻结->试用;3: //试用延期;4: //原销售申请领取流失客户;
  828. if recodeInfo.ApplyMethod == 2 || recodeInfo.ApplyMethod == 3 || recodeInfo.ApplyMethod == 4 {
  829. companyProductTryOutUpdateLog := &company.CompanyProductTryOutUpdateLog{
  830. Id: 0,
  831. CompanyId: companyProduct.CompanyId,
  832. ProductId: companyProduct.ProductId,
  833. SellerId: logSellerId,
  834. SellerName: logSellerName,
  835. Source: updateSource,
  836. StartDate: startDateTime,
  837. EndDate: endDateTime,
  838. RealEndDate: startDateTime,
  839. IsStop: 0,
  840. CreateTime: time.Now(),
  841. }
  842. go company.AddCompanyProductTryOutUpdateLog(companyProductTryOutUpdateLog, companyReportPermissionList)
  843. }
  844. }
  845. return
  846. }
  847. // contactHandleCompanyRenewalRecord
  848. // @Description: 合同处理完成后的续约异常记录
  849. // @author: Roc
  850. // @datetime 2023-12-07 14:24:44
  851. // @param contractInfo *company_contract.CompanyContract
  852. // @param day string
  853. // @return err error
  854. func contactHandleCompanyRenewalRecord(contractInfo *company.CompanyContractDetail) {
  855. var err error
  856. defer func() {
  857. if err != nil {
  858. utils.FileLog.Error("合同处理完成后的续约异常记录," + err.Error())
  859. }
  860. }()
  861. // 判断合同类型是否是续约合同,如果不是的话,就不往下走了
  862. if contractInfo.ContractType != `续约合同` {
  863. return
  864. }
  865. day := time.Now().Format(utils.FormatDate)
  866. // 获取早于当前合同结束日期的上一份合同
  867. lastContract, tmpErr := company.GetLastContractListByEndDate(contractInfo.CompanyId, contractInfo.ProductId, contractInfo.EndDate)
  868. if tmpErr != nil {
  869. err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";通过最近一份合同的日期获取早于该合同的最晚一份合同失败,ERR:", tmpErr))
  870. return
  871. }
  872. // 校验 上一份合同的结束日期 与 今天 相隔的天数
  873. betweenDay, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, day, lastContract.EndDate)
  874. if tmpErr != nil {
  875. err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";计算两个日期相差的天数失败,ERR:", tmpErr))
  876. return
  877. }
  878. source := 2 // 正常续约
  879. // 如果间隔时间超过60天,那么标记为超时续约
  880. if betweenDay > 60 {
  881. source = 3 // 超时续约
  882. }
  883. // 如果间隔时间超过60天,那么标记为超时续约
  884. companyProductItem, tmpErr := company.GetCompanyProductItemByCompanyId(contractInfo.CompanyId, contractInfo.ProductId)
  885. if tmpErr != nil {
  886. err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";GetCompanyProductItemByCompanyId失败,ERR:", tmpErr))
  887. return
  888. }
  889. var shareSellerId int
  890. var shareSellerName string
  891. if companyProductItem.IsShare == 1 {
  892. shareSellerId = companyProductItem.ShareSellerId
  893. shareSellerName = companyProductItem.ShareSeller
  894. }
  895. item := &company.CompanyRenewalRecord{
  896. Id: 0,
  897. CompanyId: contractInfo.CompanyId,
  898. ProductId: contractInfo.ProductId,
  899. Source: source,
  900. SellerId: companyProductItem.SellerId,
  901. SellerName: companyProductItem.SellerName,
  902. ShareSellerId: shareSellerId,
  903. ShareSellerName: shareSellerName,
  904. CreateTime: time.Now(),
  905. ModifyTime: time.Now(),
  906. }
  907. tmpErr = item.Add()
  908. if tmpErr != nil {
  909. err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";添加续约异常记录失败,ERR:", tmpErr))
  910. }
  911. return
  912. }
  913. // afterReject 驳回完成后操作
  914. func afterReject(companyApprovalId, opUserId int, opUserName, remark string) (err error) {
  915. defer func() {
  916. if err != nil {
  917. fmt.Println("异常提醒:"+utils.RunMode, "客户审批失败后,客户操作异常,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  918. go alarm_msg.SendAlarmMsg("客户审批驳回后,客户操作异常,Err:"+err.Error(), 3)
  919. //go utils.SendEmail("异常提醒:"+utils.RunMode, "合同审批失败后,合同操作异常,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  920. }
  921. }()
  922. //获取客户单详情
  923. recodeInfo, err := company.GetCompanyApprovalByApprovalId(companyApprovalId)
  924. if err != nil {
  925. err = errors.New("获取合同信息失败,Err:" + err.Error())
  926. return
  927. }
  928. if recodeInfo == nil {
  929. err = errors.New("客户单异常,找不到客户单信息")
  930. return
  931. }
  932. //获取客户信息
  933. companyInfo, err := company.GetCompanyById(recodeInfo.CompanyId)
  934. if err != nil {
  935. err = errors.New("获取合同信息失败,Err:" + err.Error())
  936. return
  937. }
  938. if companyInfo == nil {
  939. err = errors.New("客户信息异常,找不到客户信息")
  940. return
  941. }
  942. //客户产品信息
  943. companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(recodeInfo.CompanyId, recodeInfo.ProductId)
  944. if err != nil {
  945. err = errors.New(fmt.Sprint("获取信息客户产品失败", ";Err:"+err.Error()))
  946. return
  947. }
  948. if companyProduct == nil {
  949. err = errors.New("获取信息客户产品失败2")
  950. return
  951. }
  952. approveStatus := "驳回"
  953. err = company.ApproveRefuse(recodeInfo.CompanyId, recodeInfo.ProductId, approveStatus, remark)
  954. if err != nil {
  955. err = errors.New("客户单审批驳回失败,Err:" + err.Error())
  956. return
  957. }
  958. //新增客户产品日志
  959. {
  960. productLog := new(company.CompanyProductLog)
  961. productLog.CompanyProductId = companyProduct.CompanyProductId
  962. productLog.CompanyId = companyProduct.CompanyId
  963. productLog.ProductId = companyProduct.ProductId
  964. productLog.ProductName = companyProduct.ProductName
  965. productLog.CompanyName = companyProduct.CompanyName
  966. productLog.CompanyName = companyProduct.CompanyName
  967. productLog.Source = companyProduct.Source
  968. productLog.Reasons = companyProduct.Reasons
  969. productLog.Status = companyProduct.Status
  970. productLog.IndustryId = companyProduct.IndustryId
  971. productLog.IndustryName = companyProduct.IndustryName
  972. productLog.SellerId = companyProduct.SellerId
  973. productLog.SellerName = companyProduct.SellerName
  974. productLog.GroupId = companyProduct.GroupId
  975. productLog.DepartmentId = companyProduct.DepartmentId
  976. productLog.IsSuspend = companyProduct.IsSuspend
  977. productLog.SuspendTime = companyProduct.SuspendTime
  978. productLog.ApproveStatus = companyProduct.ApproveStatus
  979. productLog.FreezeTime = companyProduct.FreezeTime
  980. productLog.Remark = companyProduct.Remark
  981. productLog.StartDate = companyProduct.StartDate
  982. productLog.EndDate = companyProduct.EndDate
  983. productLog.CreateTime = companyProduct.CreateTime
  984. productLog.ModifyTime = companyProduct.ModifyTime
  985. productLog.LoseReason = companyProduct.LoseReason
  986. productLog.LossTime = companyProduct.LossTime
  987. productLog.LogType = "modify"
  988. productLog.LogCreateTime = time.Now()
  989. productLog.AdminId = opUserId
  990. go company.AddCompanyProductLog(productLog)
  991. }
  992. //新增操作记录
  993. {
  994. remark := "审批"
  995. operation := "approve"
  996. approveContent := "审批驳回"
  997. services.AddCompanyOperationRecord(recodeInfo.CompanyId, companyProduct.SellerId, opUserId, recodeInfo.ProductId, opUserId, companyProduct.CompanyName,
  998. companyProduct.ProductName, opUserName, remark, operation, approveContent, opUserName, remark, companyProduct.Status)
  999. }
  1000. return
  1001. }
  1002. // CheckApproveAuth 校验审批操作权限
  1003. func CheckApproveAuth(companyId, productId int, opUser *system.Admin) (approvalItem *company.CompanyApproval, approvalInfo *contract.ContractApproval, approvalRecord *contract.ContractApprovalRecord, err error) {
  1004. //客户单详情
  1005. approvalItem, err = company.GetCompanyApprovalById(companyId, productId)
  1006. if err != nil {
  1007. err = errors.New("获取客户单失败,Err:" + err.Error())
  1008. return
  1009. }
  1010. //客户单状态判断
  1011. if approvalItem.ApproveStatus != "待审批" {
  1012. err = errors.New("客户单状态异常,不允许操作申请,当前客户单状态:" + approvalItem.ApproveStatus)
  1013. return
  1014. }
  1015. //获取该最近一条审批单详情
  1016. approvalInfo, err = contract.GetLastContractApprovalByContractId(approvalItem.CompanyApprovalId, "company")
  1017. if err != nil {
  1018. err = errors.New("获取审批单失败,Err:" + err.Error())
  1019. return
  1020. }
  1021. if approvalInfo.Status != "待审批" {
  1022. err = errors.New("当前审批单状态异常,不允许审批,审批单状态:" + approvalInfo.Status)
  1023. return
  1024. }
  1025. //审批流
  1026. approvalRecordList, err := contract.GetContractApprovalRecordListByContractApprovalIdAndNodeId(approvalInfo.ContractApprovalId, approvalInfo.CurrNodeId)
  1027. if err != nil {
  1028. err = errors.New("获取审批失败,Err:" + err.Error())
  1029. return
  1030. }
  1031. for _, tmpApprovalRecord := range approvalRecordList {
  1032. if tmpApprovalRecord.NodeType == "check" && tmpApprovalRecord.ApproveUserId == opUser.AdminId {
  1033. approvalRecord = tmpApprovalRecord
  1034. }
  1035. }
  1036. if approvalRecord == nil {
  1037. err = errors.New("当前账号没有审批权限")
  1038. return
  1039. }
  1040. return
  1041. }