company_approval.go 51 KB

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