company_approval.go 45 KB

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