company_approval.go 51 KB

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