12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265 |
- package company_apply
- import (
- "encoding/json"
- "errors"
- "fmt"
- "hongze/hongze_mobile_admin/models/custom"
- "hongze/hongze_mobile_admin/models/tables/admin"
- "hongze/hongze_mobile_admin/models/tables/approval_flow"
- "hongze/hongze_mobile_admin/models/tables/approval_flow_node"
- "hongze/hongze_mobile_admin/models/tables/company"
- "hongze/hongze_mobile_admin/models/tables/company_approval"
- "hongze/hongze_mobile_admin/models/tables/company_approval_message"
- "hongze/hongze_mobile_admin/models/tables/company_contract"
- "hongze/hongze_mobile_admin/models/tables/company_delay_permission"
- "hongze/hongze_mobile_admin/models/tables/company_product"
- "hongze/hongze_mobile_admin/models/tables/company_product_log"
- "hongze/hongze_mobile_admin/models/tables/company_product_try_out_update_log"
- "hongze/hongze_mobile_admin/models/tables/company_product_update_log"
- "hongze/hongze_mobile_admin/models/tables/company_renewal_record"
- "hongze/hongze_mobile_admin/models/tables/company_report_permission"
- "hongze/hongze_mobile_admin/models/tables/contract_approval"
- "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
- "hongze/hongze_mobile_admin/services"
- "hongze/hongze_mobile_admin/services/alarm_msg"
- cygxService "hongze/hongze_mobile_admin/services/cygx"
- "hongze/hongze_mobile_admin/services/flow"
- "hongze/hongze_mobile_admin/utils"
- "strings"
- "time"
- )
- var sourceType = 1 //来源客户
- // 提交审批
- func Apply(companyApprovalId int, messageContent string) (err error) {
- //获取客户单详情
- recodeInfo, err := company_approval.GetCompanyApprovalByApprovalId(companyApprovalId)
- if err != nil {
- err = errors.New("获取合同信息失败,Err:" + err.Error())
- return
- }
- if recodeInfo == nil {
- err = errors.New("客户单异常,找不到客户单信息")
- return
- }
- //获取客户信息
- companyInfo, err := company.GetCompanyById(recodeInfo.CompanyId)
- if err != nil {
- err = errors.New("获取合同信息失败,Err:" + err.Error())
- return
- }
- if companyInfo == nil {
- err = errors.New("客户信息异常,找不到客户信息")
- return
- }
- //合同状态判断
- ignoreStatus := []string{"待审批", "已撤回", "驳回"}
- if !strings.Contains(strings.Join(ignoreStatus, ","), recodeInfo.ApproveStatus) {
- err = errors.New("客户单状态异常,不允许提交审批,当前客户单状态:" + recodeInfo.ApproveStatus)
- return
- }
- //待审内容
- applyContent := `试用转正式`
- switch recodeInfo.ApplyMethod { //"1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新"
- case 1:
- applyContent = `试用转正式`
- case 2:
- applyContent = `冻结转试用`
- case 3:
- applyContent = `试用延期`
- case 4:
- applyContent = `原销售申领客户`
- case 5:
- applyContent = `续约申请`
- case 6:
- applyContent = `补充协议`
- }
- contractDetailByte, err := json.Marshal(recodeInfo)
- if err != nil {
- err = errors.New(fmt.Sprint("合同状态异常,当前合同数据格式化失败:", err))
- return
- }
- //获取基础审批流
- var flowItemInfo *approval_flow.ApprovalFlowItem
- if recodeInfo.ProductId == 1 {
- tmpFlowItemInfo, tmpErr := flow.GetApprovalFlow(1)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("查找审批流程配置失败:", tmpErr))
- return
- }
- flowItemInfo = tmpFlowItemInfo
- } else {
- tmpFlowItemInfo, tmpErr := flow.GetApprovalFlow(2)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("查找审批流程配置失败:", tmpErr))
- return
- }
- flowItemInfo = tmpFlowItemInfo
- }
- //校验是否存在待审批的审批单(其实没有多大意义,只是为了 异常数据校验)
- contractApproval := &contract_approval.ContractApproval{
- ContractId: recodeInfo.CompanyApprovalId,
- Status: "待审批",
- ApprovalType: "company",
- ApplyContent: applyContent,
- ContractDetail: string(contractDetailByte),
- ApplyUserId: recodeInfo.ApplyUserId,
- ApplyUserName: recodeInfo.ApplyRealName,
- FlowId: flowItemInfo.FlowId,
- FlowVersion: flowItemInfo.CurrVersion,
- CurrNodeId: flowItemInfo.NodeList[0].NodeId,
- StartNodeId: flowItemInfo.NodeList[0].NodeId,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- has, err := contractApproval.CheckPendingByContractId(recodeInfo.CompanyApprovalId, "company")
- if err != nil {
- return
- }
- if has {
- err = errors.New("客户单异常,不允许提交审批,存在待审核的客户单")
- return
- }
- contractApprovalRecordList := make([]*contract_approval_record.ContractApprovalRecord, 0)
- ApproveUserMap := make(map[int]approval_flow_node.User)
- for _, nodeItem := range flowItemInfo.NodeList {
- //审批流记录
- if len(nodeItem.UserList) <= 0 {
- err = errors.New(fmt.Sprint("审批流程异常,没有可审批的人员,Err:", err))
- return
- }
- for _, userItem := range nodeItem.UserList {
- contractApprovalRecord := &contract_approval_record.ContractApprovalRecord{
- Status: "待审批",
- ApproveRemark: "",
- ApproveUserId: userItem.AdminId,
- ApproveUserName: userItem.Name,
- ApproveRoleTypeCode: userItem.RoleTypeCode,
- NodeId: nodeItem.NodeId,
- NodeType: nodeItem.NodeType,
- NextNodeId: nodeItem.NextNodeId,
- PrevNodeId: nodeItem.PrevNodeId,
- AuditType: nodeItem.AuditType,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- contractApprovalRecordList = append(contractApprovalRecordList, contractApprovalRecord)
- ApproveUserMap[userItem.AdminId] = userItem
- }
- }
- err = contractApproval.Apply(contractApproval, contractApprovalRecordList)
- if err != nil {
- return
- }
- //待办通知
- {
- //发送消息下级审批人
- go messageToNodeUser(contractApproval.CurrNodeId, recodeInfo.ApplyUserId, companyInfo.CompanyId, contractApproval.ContractId, contractApproval.ContractApprovalId, 1, 1, companyInfo.CompanyName, messageContent)
- }
- return
- }
- // CancelApply 撤回审批
- func CancelApply(recordInfo *company_approval.CompanyApproval, opUserId int) (err error) {
- if recordInfo == nil {
- err = errors.New("客户单异常,没有客户单信息")
- return
- }
- //客户单状态判断
- if recordInfo.ApproveStatus != "待审批" {
- err = errors.New("客户单状态异常,不允许撤回申请,当前客户单状态:" + recordInfo.ApproveStatus)
- return
- }
- //校验操作人与客户单申请人是否同一人
- if recordInfo.ApplyUserId != opUserId {
- err = errors.New(fmt.Sprint("操作人异常,不允许撤回非本人提交的申请;客户单发起人:", recordInfo.ApplyUserId, ";撤销人:", opUserId))
- return
- }
- //获取最近一次待审批的审批单信息
- approval, err := contract_approval.GetLastContractApprovalByContractId(recordInfo.CompanyApprovalId, "company")
- if err != nil {
- return
- }
- //是否一级审批节点判断,如果不是一级审批节点,那么不允许撤回
- if approval.StartNodeId != approval.CurrNodeId {
- err = errors.New("审批单状态异常,不允许撤回申请,当前审批单已被审批操作,进入了下一级审批节点")
- return
- }
- //获取当前待审批的审批流信息
- approvalRecord, err := contract_approval_record.GetCurrContractApprovalRecordByContractId(approval.ContractApprovalId)
- if err != nil {
- return
- }
- //判断当前节点是否存在上级节点,如果存在,那么说明
- if approvalRecord.PrevNodeId > 0 {
- err = errors.New("合同已存在审批操作,不允许撤回申请")
- return
- }
- //获取当前审批单中所有待审批的流程流
- approvalRecordList, err := contract_approval_record.GetPendingContractApprovalRecordListByContractId(approval.ContractApprovalId, approvalRecord.NodeId)
- if err != nil {
- return
- }
- //撤回审批流
- err = approval.Cancel(approval, approvalRecordList)
- if err != nil {
- return
- }
- //添加操作日志 (??这个看情况再说,或许没有必要)
- //作废原有消息
- //for _, contractApprovalRecord := range approvalRecordList {
- // go company_approval_message.CancelCompanyApprovalMessage(contractApprovalRecord.ContractApprovalRecordId, 2)
- //}
- go company_approval_message.CancelCompanyApprovalMessage(approval.ContractId, 2)
- return
- }
- // Reject 驳回审批
- func Reject(approvalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, rejectRemark string) (err error) {
- if approvalRecord == nil {
- err = errors.New("审批流异常,没有审批流信息")
- return
- }
- //审批流状态判断
- if approvalRecord.Status != "待审批" {
- err = errors.New("审批流状态异常,不允许驳回申请,当前审批流状态:" + approvalRecord.Status)
- return
- }
- //判断是否审批类型,如果不是审批类型,那么就没有审批权限
- if approvalRecord.NodeType != "check" {
- err = errors.New("当前账号没有审批权限")
- return
- }
- //操作人
- opUserId := opUser.AdminId
- opUserName := opUser.RealName
- //操作权限校验
- if opUser.RoleTypeCode != approvalRecord.ApproveRoleTypeCode {
- err = errors.New("当前账号没有审批权限")
- return
- }
- if approvalRecord.ApproveUserId > 0 && approvalRecord.ApproveUserId != opUserId {
- err = errors.New("当前账号没有审批权限,需要指定人操作")
- return
- }
- //获取审批单详情
- approval, err := contract_approval.GetContractApprovalById(approvalRecord.ContractApprovalId)
- if err != nil {
- return
- }
- //审批单状态判断
- if approval.Status != "待审批" {
- err = errors.New("审批单状态异常,不允许驳回申请,当前审批单状态:" + approval.Status)
- return
- }
- //获取 客户单 信息
- recordInfo, err := company_approval.GetCompanyApprovalById(approval.ContractId)
- if err != nil {
- return
- }
- //客户单 状态判断
- if recordInfo.ApproveStatus != "待审批" {
- err = errors.New("客户单状态异常,不允许驳回申请,当前客户单状态:" + recordInfo.ApproveStatus)
- return
- }
- //获取 客户 信息
- companyInfo, err := company.GetCompanyById(recordInfo.CompanyId)
- if err != nil {
- return
- }
- err = approval.Reject(approval, approvalRecord, opUserId, opUserName, rejectRemark)
- if err != nil {
- return
- }
- //添加操作日志
- //remark := "驳回申请"
- //_ = contract.AddContractOperationRecord(contractInfo.ContractId, opUserId, approvalRecord.ContractApprovalRecordId, "reject", opUserName, remark)
- //完成审批后的驳回操作
- go afterReject(approval.ContractId, opUserId, opUserName, rejectRemark)
- //待办通知
- {
- //content := contractInfo.CompanyName + " 合同模板已驳回"
- content := companyInfo.CompanyName + " " + recordInfo.ApproveContent + "审批驳回"
- approvalSysUser, _ := admin.GetAdminById(approval.ApplyUserId)
- go services.AddCompanyApprovalMessage(opUserId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, 2, sourceType, 3, companyInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
- }
- return
- }
- // Approved 审批通过
- func Approved(approvalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, approvedRemark string) (err error) {
- if approvalRecord == nil {
- err = errors.New("审批流异常,没有审批流信息")
- return
- }
- //审批流状态判断
- if approvalRecord.Status != "待审批" {
- err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + approvalRecord.Status)
- return
- }
- //判断是否审批类型,如果不是审批类型,那么就没有审批权限
- if approvalRecord.NodeType != "check" {
- err = errors.New("当前账号没有审批权限")
- return
- }
- //操作人
- opUserId := opUser.AdminId
- //opUserName := opUser.RealName
- //操作权限校验
- if opUser.RoleTypeCode != approvalRecord.ApproveRoleTypeCode {
- err = errors.New("当前账号没有审批权限")
- return
- }
- if approvalRecord.ApproveUserId > 0 && approvalRecord.ApproveUserId != opUserId {
- err = errors.New("当前账号没有审批权限,需要指定人操作")
- return
- }
- //获取审批单详情
- approval, err := contract_approval.GetContractApprovalById(approvalRecord.ContractApprovalId)
- if err != nil {
- return
- }
- //审批单状态判断
- if approval.Status != "待审批" {
- err = errors.New("审批单状态异常,不允许审批,当前审批单状态:" + approval.Status)
- return
- }
- //获取 客户单 信息
- recordInfo, err := company_approval.GetCompanyApprovalById(approval.ContractId)
- if err != nil {
- return
- }
- //客户单 状态判断
- if recordInfo.ApproveStatus != "待审批" {
- err = errors.New("客户单状态异常,不允许审批,当前客户单状态:" + recordInfo.ApproveStatus)
- return
- }
- //获取 客户 信息
- companyInfo, err := company.GetCompanyById(recordInfo.CompanyId)
- if err != nil {
- return
- }
- //if contractApprovalRecord
- err = approval.Approved(approval, approvalRecord, approvedRemark)
- if err != nil {
- return
- }
- //添加操作日志
- //remark := "审批通过"
- //_ = contract.AddContractOperationRecord(contractInfo.ContractId, opUserId, approvalRecord.ContractApprovalRecordId, "approval", opUserName, remark)
- //发送信息给所有抄送人
- content := companyInfo.CompanyName + " " + recordInfo.ApproveContent + "审批通过"
- //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
- if approvalRecord.NextNodeId == 0 {
- //待办通知(通知销售已经审核通过了)
- {
- approvalSysUser, _ := admin.GetAdminById(approval.ApplyUserId)
- go services.AddCompanyApprovalMessage(opUserId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, 2, sourceType, 2, companyInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
- }
- // 客户单审批后的异步操作(修改客户单信息、修改合同权限等)
- go func() {
- _ = afterApproved(recordInfo.CompanyApprovalId, opUser.AdminId, opUser.RealName)
- // 注意此处下面的处理原本都是各开了协程,现改为只在一个协程里处理,不然可能会导致数据还没完全更新,紧接着就被错误更新对应的数据
- if recordInfo.ProductId == 2 && recordInfo.CompanyContractId > 0 {
- //处理权益客户的身份信息
- cygxService.UpdateCygxCompanyUserType(recordInfo.CompanyContractId)
- //if recordInfo.ApplyMethod == 1 {
- //获取合同信息
- contractInfo, tmpErr := company_contract.GetCompanyContractDetail(recordInfo.CompanyId, recordInfo.ProductId, recordInfo.CompanyContractId)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("待审批信息不存在,CompanyId:", recordInfo.CompanyId, ";productId:", recordInfo.ProductId, ";Err:"+tmpErr.Error()))
- return
- }
- if contractInfo == nil {
- err = errors.New(fmt.Sprint("待审批信息不存在,CompanyId:", recordInfo.CompanyId, ";productId:", recordInfo.ProductId))
- return
- }
- //格式化合同开始时间
- contractStartDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.StartDate)
- if tmpErr != nil {
- err = errors.New("合同开始时间转换失败,Err:" + tmpErr.Error())
- return
- }
- if time.Now().After(contractStartDate) {
- cygxService.ActivitySpecialCompanyApproval(recordInfo.CompanyId, recordInfo.CompanyContractId, companyInfo.CompanyName) //审批通过的时候专项调研次数更新
- }
- //}
- //权益销售客户申请转正后,消息群发给所有销售
- services.AddCompanyApprovalMessageRai(recordInfo.CompanyId, recordInfo.CompanyContractId, recordInfo.ApplyRealName, companyInfo.CompanyName)
- //客户研选行业转正时(王芳审批通过),模板消息提醒汪洋
- services.AddCompanyApprovalMessageWangYang(recordInfo.CompanyId, recordInfo.CompanyContractId, recordInfo.ApplyRealName, companyInfo.CompanyName)
- cygxService.YanXuanCompanyApproval(recordInfo.CompanyId, recordInfo.CompanyContractId) //研选审批通过的时候研选扣点更新
- //cygxService.HandleAllocationCompanyContractByYanXuan(recordInfo.CompanyContractId) //如果合同只有研选的时候,自动处理派点
- cygxService.HandleCompanyContractPackageDifference(recordInfo.CompanyContractId) // 更新与上一份合同的金额的对比 '增加套餐','减少套餐','维持套餐'
- cygxService.HandleCompanyContractPermissionContractType(recordInfo.CompanyContractId) // 更新合同权限表中的权限名称,以及对应的行业权限类型(行业新签、行业续约)
- }
- }()
- } else {
- //获取下级节点信息
- flowNodeInfo, tmpErr := approval_flow_node.GetByNodeId(approvalRecord.NextNodeId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //如果该级节点是抄送类型,那么需要将该节点给处理掉
- if flowNodeInfo.NodeType == "cc" {
- go approvedByCc(approvedRemark, approvalRecord)
- } else {
- //发送消息下级审批人
- go messageToNodeUser(approvalRecord.NextNodeId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, approval.ContractApprovalId, 1, 1, companyInfo.CompanyName, content)
- }
- }
- return
- }
- // approvedByCc 审批通过(抄送节点)
- func approvedByCc(approvedRemark string, sourceApprovalRecord *contract_approval_record.ContractApprovalRecord) (err error) {
- //下个流程节点id
- nextNodeId := 0
- //获取审批单中抄送节点的所有数据列表
- approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractIdAndNode(sourceApprovalRecord.ContractApprovalId, sourceApprovalRecord.NextNodeId)
- if err != nil {
- return
- }
- //遍历所有的抄送单
- for _, contractApprovalRecord := range approvalRecordList {
- nextNodeId = contractApprovalRecord.NextNodeId
- //审批流状态判断
- if contractApprovalRecord.Status != "待审批" {
- err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + contractApprovalRecord.Status)
- return
- }
- //判断是否审批类型,如果不是审批类型,那么就没有审批权限
- if contractApprovalRecord.NodeType != "cc" {
- err = errors.New("当前账号不是抄送权限")
- return
- }
- }
- //获取审批单详情
- approval, err := contract_approval.GetContractApprovalById(sourceApprovalRecord.ContractApprovalId)
- if err != nil {
- return
- }
- //获取 客户单 信息
- recordInfo, err := company_approval.GetCompanyApprovalById(approval.ContractId)
- if err != nil {
- return
- }
- //客户单 状态判断
- if recordInfo.ApproveStatus != "待审批" {
- err = errors.New("客户单状态异常,不允许审批,当前客户单状态:" + recordInfo.ApproveStatus)
- return
- }
- //获取 客户 信息
- companyInfo, err := company.GetCompanyById(recordInfo.CompanyId)
- if err != nil {
- return
- }
- //if contractApprovalRecord
- err = approval.ApprovedByCc(approval, approvalRecordList, approvedRemark, nextNodeId)
- if err != nil {
- return
- }
- content := companyInfo.CompanyName + " " + recordInfo.ApproveContent + "审批通过"
- //发送消息给抄送人
- go messageToNodeUser(approvalRecordList[0].NodeId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, approval.ContractApprovalId, 1, 1, companyInfo.CompanyName, content)
- //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
- if nextNodeId == 0 {
- //待办通知
- {
- approvalSysUser, _ := admin.GetAdminById(approval.ApplyUserId)
- go services.AddCompanyApprovalMessage(sourceApprovalRecord.ApproveUserId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, 2, sourceType, 2, companyInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
- }
- //客户单审批后的异步操作(修改客户单信息、修改合同权限等)
- go afterApproved(recordInfo.CompanyApprovalId, sourceApprovalRecord.ApproveUserId, sourceApprovalRecord.ApproveUserName)
- } else {
- //获取下级节点信息
- flowNodeInfo, tmpErr := approval_flow_node.GetByNodeId(nextNodeId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //如果下级节点是抄送类型,那么还是需要处理抄送节点逻辑
- if flowNodeInfo.NodeType == "cc" {
- go approvedByCc(approvedRemark, sourceApprovalRecord)
- } else {
- //如果下级级节点是审批类型
- //发送消息下级审批人
- go messageToNodeUser(nextNodeId, approval.ApplyUserId, companyInfo.CompanyId, approval.ContractId, approval.ContractApprovalId, 1, 1, companyInfo.CompanyName, content)
- }
- }
- return
- }
- // messageToNodeUser 抄送信息给用户(根据节点来推送)
- func messageToNodeUser(flowNodeId, createUserId, companyId, companyApprovalId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
- //获取审批单中抄送节点的所有数据列表
- contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractIdAndNode(contractApprovalId, flowNodeId)
- if err != nil {
- return
- }
- err = messageToUser(contractApprovalRecordList, createUserId, companyId, companyApprovalId, messageType, approvalStatus, companyName, content)
- return
- }
- // messageToAllCcUser 发送信息给所有抄送人
- // @param createUserId 消息发起人
- // @param contractApprovalId 审批单id
- // @param messageType 消息类型:1:申请消息,2:审批结果
- // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回"
- // @param companyName 企业客户名称
- // @param content 消息内容
- func messageToAllCcUser(createUserId, companyId, companyApprovalId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
- //获取审批单中抄送节点的所有数据列表
- contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalId(contractApprovalId)
- if err != nil {
- return
- }
- err = messageToUser(contractApprovalRecordList, createUserId, companyId, companyApprovalId, messageType, approvalStatus, companyName, content)
- return
- }
- // messageToUser 抄送信息给用户(发送给所有抄送人)
- // @param createUserId 待审批、抄送记录
- // @param createUserId 消息发起人
- // @param contractApprovalId 审批单id
- // @param messageType 消息类型:1:申请消息,2:审批结果
- // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回"
- // @param companyName 企业客户名称
- // @param content 消息内容
- func messageToUser(contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord, createUserId, companyId, companyApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
- for _, contractApprovalRecord := range contractApprovalRecordList {
- sysUserMobile := ""
- sysUser, _ := admin.GetAdminById(contractApprovalRecord.ApproveUserId)
- if sysUser != nil {
- sysUserMobile = sysUser.Mobile
- }
- //接收人,就是审批记录里面的待审核人(抄送人)
- receiveUserId := contractApprovalRecord.ApproveUserId
- go services.AddCompanyApprovalMessage(createUserId, receiveUserId, companyId, companyApprovalId, messageType, sourceType, approvalStatus, companyName, content, content, sysUserMobile, "")
- }
- return
- }
- // afterApproved 审批完成后操作
- func afterApproved(companyApprovalId int, opUserId int, opUserName string) (err error) {
- defer func() {
- if err != nil {
- //fmt.Println("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
- //go utils.SendEmail("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
- go alarm_msg.SendAlarmMsg("审批完成后操作失败,ERR:"+err.Error()+"", 3)
- }
- }()
- opUser := custom.AdminWx{
- AdminId: opUserId,
- RealName: opUserName,
- }
- //获取客户单详情
- recodeInfo, err := company_approval.GetCompanyApprovalByApprovalId(companyApprovalId)
- if err != nil {
- err = errors.New("获取合同信息失败,Err:" + err.Error())
- return
- }
- if recodeInfo == nil {
- err = errors.New("客户单异常,找不到客户单信息")
- return
- }
- //获取客户信息
- companyInfo, err := company.GetCompanyById(recodeInfo.CompanyId)
- if err != nil {
- err = errors.New("获取合同信息失败,Err:" + err.Error())
- return
- }
- if companyInfo == nil {
- err = errors.New("客户信息异常,找不到客户信息")
- return
- }
- //客户产品信息
- companyProduct, err := company_product.GetCompanyProductByCompanyIdAndProductId(recodeInfo.CompanyId, recodeInfo.ProductId)
- if err != nil {
- err = errors.New(fmt.Sprint("获取信息客户产品失败", ";Err:"+err.Error()))
- return
- }
- if companyProduct == nil {
- err = errors.New("获取信息客户产品失败2")
- return
- }
- logSellerId := recodeInfo.ApplyUserId
- logSellerName := recodeInfo.ApplyRealName
- isAddCompanyProductUpdateLog := true //是否添加变更日志
- companyReportPermissionList := make([]*company_report_permission.CompanyReportPermission, 0) //需要变更的品种列表
- var startDate, endDate string //变更到最新的日期
- switch recodeInfo.ApplyMethod {
- case 1: //试用转正式
- //获取合同信息
- contractInfo, tmpErr := company_contract.GetCompanyContractDetail(recodeInfo.CompanyId, recodeInfo.ProductId, recodeInfo.CompanyContractId)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("待审批信息不存在,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId, ";Err:"+tmpErr.Error()))
- return
- }
- if contractInfo == nil {
- err = errors.New(fmt.Sprint("待审批信息不存在,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId))
- return
- }
- //格式化合同开始时间
- contractStartDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.StartDate)
- if tmpErr != nil {
- err = errors.New("合同开始时间转换失败,Err:" + tmpErr.Error())
- return
- }
- //格式化合同结束日期
- contractEndDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.EndDate)
- if tmpErr != nil {
- err = errors.New("合同结束时间转换失败,Err:" + tmpErr.Error())
- return
- }
- //格式化 产品中 合同结束日期
- //productContractEndDate, err := time.Parse(utils.FormatDate, companyProduct.ContractEndDate)
- //if err != nil {
- // br.Msg = "审批失败"
- // br.ErrMsg = "产品中合同结束时间转换失败,Err:" + err.Error()
- // return
- //}
- //如果是权益的用户客户转正审批单独走
- if companyProduct.Status == utils.COMPANY_STATUS_FOREVER && companyProduct.ProductId == 2 {
- //contractInfo.StartDate = contractStartDate.AddDate(0, 4, 0).Format(utils.FormatDate) // 开始时间与结束时间都加四个月
- //contractInfo.EndDate = contractEndDate.AddDate(0, 4, 0).Format(utils.FormatDate)
- startDate = contractInfo.StartDate
- endDate = contractInfo.EndDate
- 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)
- if tmpErr != nil {
- err = errors.New("试用转正式失败,Err:" + tmpErr.Error())
- return
- }
- } else {
- //如果合同时间小于等于今天,那么立马执行合同内容
- if time.Now().After(contractStartDate) {
- startDate = contractInfo.StartDate
- endDate = contractInfo.EndDate
- 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)
- if tmpErr != nil {
- err = errors.New("试用转正式失败,Err:" + tmpErr.Error())
- return
- }
- } else {
- tmpErr = company_approval.ApplyApproveContract(recodeInfo.CompanyId, recodeInfo.ProductId, recodeInfo.CompanyApprovalId, contractInfo.CompanyContractId)
- if tmpErr != nil {
- err = errors.New("试用转正式失败,Err:" + tmpErr.Error())
- return
- }
- //不添加日志
- isAddCompanyProductUpdateLog = false
- }
- //如果合同中的结束日期大于产品中的合同结束日期,那么就去修改产品中的合同结束日期
- if contractEndDate.After(companyProduct.ContractEndDate) {
- companyProduct.ContractEndDate = contractEndDate
- cols := make([]string, 0)
- cols = append(cols, "ContractEndDate")
- _ = companyProduct.Update(cols)
- }
- }
- case 2: //冻结->试用
- //权益的 x类试用转冻结的申请解冻
- if companyProduct.InitStatus == utils.COMPANY_STATUS_FOREVER && companyProduct.ProductId == utils.COMPANY_PRODUCT_RAI_ID {
- tmpCompanyReportPermissionList, tmpStartDate, tmpEndDate, tmpErr := company_approval.FreezeToTryOutXClassRai(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, recodeInfo.ApplyUserId, opUser.RealName, companyProduct.ProductName)
- if tmpErr != nil {
- err = errors.New("冻结转试用失败,Err:" + tmpErr.Error())
- return
- }
- companyReportPermissionList = tmpCompanyReportPermissionList
- startDate = tmpStartDate
- endDate = tmpEndDate
- } else {
- tmpCompanyReportPermissionList, tmpStartDate, tmpEndDate, tmpErr := company_approval.FreezeToTryOut(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, recodeInfo.ApplyUserId, opUser.RealName, companyProduct.ProductName)
- if tmpErr != nil {
- err = errors.New("冻结转试用失败,Err:" + tmpErr.Error())
- return
- }
- companyReportPermissionList = tmpCompanyReportPermissionList
- startDate = tmpStartDate
- endDate = tmpEndDate
- }
- case 3: //试用延期
- tmpCompanyReportPermissionList, tmpEndDate, tmpErr := company_approval.TryOutDelay(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, opUser.RealName, companyProduct.EndDate, companyProduct.ProductName)
- if tmpErr != nil {
- err = errors.New("试用延期失败,Err:" + tmpErr.Error())
- return
- }
- companyReportPermissionList = tmpCompanyReportPermissionList
- startDate = companyProduct.StartDate
- endDate = tmpEndDate
- case 4: //原销售申请领取流失客户
- companyReportPermissionList, startDate, endDate, err = company_approval.ApplyReceive(recodeInfo.CompanyId, recodeInfo.ProductId, opUser.AdminId, recodeInfo.CompanyApprovalId, recodeInfo.ApplyUserId, opUser.RealName, companyProduct.ProductName)
- if err != nil {
- err = errors.New("原销售申请领取流失客户失败,Err:" + err.Error())
- return
- }
- case 5, 6: //正式客户申请服务更新
- //获取合同信息
- contractInfo, tmpErr := company_contract.GetCompanyContractDetail(recodeInfo.CompanyId, recodeInfo.ProductId, recodeInfo.CompanyContractId)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("正式客户申请服务更新失败,待审批合同信息不存在,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId, ";Err:"+tmpErr.Error()))
- return
- }
- if contractInfo == nil {
- err = errors.New(fmt.Sprint("正式客户申请服务更新失败,待审批合同信息不存在2,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId))
- return
- }
- //格式化合同开始时间
- contractStartDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.StartDate)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("正式客户申请服务更新失败,合同开始时间转换失败;Err:" + tmpErr.Error()))
- return
- }
- //格式化合同结束日期
- contractEndDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.EndDate)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("正式客户申请服务更新失败,合同结束时间转换失败;Err:" + tmpErr.Error()))
- return
- }
- //格式化 产品中 合同结束日期
- //productContractEndDate, err := time.Parse(utils.FormatDate, companyProduct.ContractEndDate)
- //if err != nil {
- // br.Msg = "审批失败"
- // br.ErrMsg = "产品中合同结束时间转换失败,Err:" + err.Error()
- // return
- //}
- //如果合同时间小于等于今天,那么立马执行合同内容
- if time.Now().After(contractStartDate) {
- // 合同处理完成后的续约异常记录
- contactHandleCompanyRenewalRecord(contractInfo)
- 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)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("正式客户申请服务更新失败,执行失败;Err:" + tmpErr.Error()))
- return
- }
- } else {
- tmpErr = company_approval.ApplyApproveContract(recodeInfo.CompanyId, recodeInfo.ProductId, recodeInfo.CompanyApprovalId, recodeInfo.CompanyContractId)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("正式客户申请服务更新失败,执行失败2;Err:" + tmpErr.Error()))
- return
- }
- //不添加日志
- isAddCompanyProductUpdateLog = false
- }
- //如果合同中的结束日期大于产品中的合同结束日期,那么就去修改产品中的合同结束日期
- if contractEndDate.After(companyProduct.ContractEndDate) {
- companyProduct.ContractEndDate = contractEndDate
- cols := make([]string, 0)
- cols = append(cols, "ContractEndDate")
- _ = companyProduct.Update(cols)
- }
- default:
- err = errors.New(fmt.Sprint("无效的审批状态,CompanyId:", recodeInfo.CompanyId, ";productId:", recodeInfo.ProductId, ";ApplyMethod:", recodeInfo.ApplyMethod, ";ApproveStatus:", recodeInfo.ApproveStatus))
- return
- }
- //新增客户产品日志
- {
- productLog := new(company_product_log.CompanyProductLog)
- productLog.CompanyProductId = companyProduct.CompanyProductId
- productLog.CompanyId = companyProduct.CompanyId
- productLog.ProductId = companyProduct.ProductId
- productLog.ProductName = companyProduct.ProductName
- productLog.CompanyName = companyProduct.CompanyName
- productLog.CompanyName = companyProduct.CompanyName
- productLog.Source = companyProduct.Source
- productLog.Reasons = companyProduct.Reasons
- productLog.Status = companyProduct.Status
- productLog.IndustryId = companyProduct.IndustryId
- productLog.IndustryName = companyProduct.IndustryName
- productLog.SellerId = companyProduct.SellerId
- productLog.SellerName = companyProduct.SellerName
- productLog.GroupId = companyProduct.GroupId
- productLog.DepartmentId = companyProduct.DepartmentId
- productLog.IsSuspend = companyProduct.IsSuspend
- productLog.SuspendTime = companyProduct.SuspendTime
- productLog.ApproveStatus = companyProduct.ApproveStatus
- productLog.FreezeTime = companyProduct.FreezeTime
- productLog.Remark = companyProduct.Remark
- productLog.StartDate = companyProduct.StartDate
- productLog.EndDate = companyProduct.EndDate
- productLog.CreateTime = companyProduct.CreateTime
- productLog.ModifyTime = companyProduct.ModifyTime
- productLog.LoseReason = companyProduct.LoseReason
- productLog.LossTime = companyProduct.LossTime
- productLog.LogType = "modify"
- productLog.LogCreateTime = time.Now()
- productLog.AdminId = opUserId
- go company_product_log.AddCompanyProductLog(productLog)
- }
- //新增操作记录
- {
- remark := "审批"
- operation := "approve"
- approveContent := "审批通过"
- if companyProduct.Status == utils.COMPANY_STATUS_FOREVER && companyProduct.InitStatus == utils.COMPANY_STATUS_FOREVER && companyProduct.ProductId == 2 { //权益永续客户申请内容做变更
- //123月份申请转正通过时,将试用期限延长(更新)至4.30。456月份申请转正通过时,将试用期限延长至7.31。789月份申请转正通过时,将试用期限延长至10.31。101112月份申请转正通过时,将试用期限延长至次年1.31。
- endDateContent := utils.GetLastDayOfQuarter(time.Now()).Format(utils.FormatDate)
- approveContent += ",试用延期至" + endDateContent
- }
- services.AddCompanyOperationRecord(recodeInfo.CompanyId, companyProduct.SellerId, opUserId, recodeInfo.ProductId, opUserId, companyProduct.CompanyName,
- companyProduct.ProductName, opUserName, remark, operation, approveContent, opUserName, "", companyProduct.Status)
- }
- //新增客户产品状态变更日志
- if isAddCompanyProductUpdateLog {
- updateSource := ``
- switch recodeInfo.ApplyMethod {
- case 1: //试用转正式
- updateSource = `turn_positive`
- case 2: //冻结->试用
- updateSource = "thaw"
- case 3: //试用延期
- updateSource = "delay"
- case 4: //原销售申请领取流失客户
- updateSource = "apply_receive"
- case 5: //正式客户申请续约
- updateSource = "service_update"
- case 6: //正式客户新增补充协议
- updateSource = "add_agreement"
- }
- startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
- companyProductUpdateLog := &company_product_update_log.CompanyProductUpdateLog{
- Id: 0,
- CompanyId: companyProduct.CompanyId,
- ProductId: companyProduct.ProductId,
- Status: companyProduct.Status,
- SellerId: logSellerId,
- SellerName: logSellerName,
- Source: updateSource,
- IsFormal: companyProduct.IsFormal, //是否已经转正式,0是没有转正式,1是已经转过正式
- StartDate: startDateTime,
- EndDate: endDateTime,
- RealEndDate: startDateTime,
- CreateTime: time.Now(),
- }
- go company_product_update_log.AddCompanyProductUpdateLog(companyProductUpdateLog)
- // 2: //冻结->试用;3: //试用延期;4: //原销售申请领取流失客户;
- if recodeInfo.ApplyMethod == 2 || recodeInfo.ApplyMethod == 3 || recodeInfo.ApplyMethod == 4 {
- companyProductTryOutUpdateLog := &company_product_try_out_update_log.CompanyProductTryOutUpdateLog{
- Id: 0,
- CompanyId: companyProduct.CompanyId,
- ProductId: companyProduct.ProductId,
- SellerId: logSellerId,
- SellerName: logSellerName,
- Source: updateSource,
- StartDate: startDateTime,
- EndDate: endDateTime,
- RealEndDate: startDateTime,
- IsStop: 0,
- CreateTime: time.Now(),
- }
- go company_product_try_out_update_log.AddCompanyProductTryOutUpdateLog(companyProductTryOutUpdateLog, companyReportPermissionList)
- }
- }
- return
- }
- // contactHandleCompanyRenewalRecord
- // @Description: 合同处理完成后的续约异常记录
- // @author: Roc
- // @datetime 2023-12-07 14:24:44
- // @param contractInfo *company_contract.CompanyContract
- // @param day string
- // @return err error
- func contactHandleCompanyRenewalRecord(contractInfo *company_contract.CompanyContractDetail) {
- var err error
- defer func() {
- if err != nil {
- utils.FileLog.Error("合同处理完成后的续约异常记录," + err.Error())
- }
- }()
- // 判断合同类型是否是续约合同,如果不是的话,就不往下走了
- if contractInfo.ContractType != `续约合同` {
- return
- }
- day := time.Now().Format(utils.FormatDate)
- // 获取早于当前合同结束日期的上一份合同
- lastContract, tmpErr := company_contract.GetLastContractListByEndDate(contractInfo.CompanyId, contractInfo.ProductId, contractInfo.EndDate)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";通过最近一份合同的日期获取早于该合同的最晚一份合同失败,ERR:", tmpErr))
- return
- }
- // 校验 上一份合同的结束日期 与 今天 相隔的天数
- betweenDay, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, day, lastContract.EndDate)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";计算两个日期相差的天数失败,ERR:", tmpErr))
- return
- }
- source := 2 // 正常续约
- // 如果间隔时间超过60天,那么标记为超时续约
- if betweenDay > 60 {
- source = 3 // 超时续约
- }
- // 如果间隔时间超过60天,那么标记为超时续约
- companyProductItem, tmpErr := company_product.GetCompanyProductItemByCompanyId(contractInfo.CompanyId, contractInfo.ProductId)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";GetCompanyProductItemByCompanyId失败,ERR:", tmpErr))
- return
- }
- var shareSellerId int
- var shareSellerName string
- if companyProductItem.IsShare == 1 {
- shareSellerId = companyProductItem.ShareSellerId
- shareSellerName = companyProductItem.ShareSeller
- }
- item := &company_renewal_record.CompanyRenewalRecord{
- Id: 0,
- CompanyId: contractInfo.CompanyId,
- ProductId: contractInfo.ProductId,
- Source: source,
- SellerId: companyProductItem.SellerId,
- SellerName: companyProductItem.SellerName,
- ShareSellerId: shareSellerId,
- ShareSellerName: shareSellerName,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- tmpErr = item.Add()
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";添加续约异常记录失败,ERR:", tmpErr))
- }
- return
- }
- // afterReject 驳回完成后操作
- func afterReject(companyApprovalId, opUserId int, opUserName, remark string) (err error) {
- defer func() {
- if err != nil {
- fmt.Println("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
- //go utils.SendEmail("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
- go alarm_msg.SendAlarmMsg("驳回完成后操作失败,ERR:"+err.Error()+"", 3)
- }
- }()
- //获取客户单详情
- recodeInfo, err := company_approval.GetCompanyApprovalByApprovalId(companyApprovalId)
- if err != nil {
- err = errors.New("获取合同信息失败,Err:" + err.Error())
- return
- }
- if recodeInfo == nil {
- err = errors.New("客户单异常,找不到客户单信息")
- return
- }
- //获取客户信息
- companyInfo, err := company.GetCompanyById(recodeInfo.CompanyId)
- if err != nil {
- err = errors.New("获取合同信息失败,Err:" + err.Error())
- return
- }
- if companyInfo == nil {
- err = errors.New("客户信息异常,找不到客户信息")
- return
- }
- //客户产品信息
- companyProduct, err := company_product.GetCompanyProductByCompanyIdAndProductId(recodeInfo.CompanyId, recodeInfo.ProductId)
- if err != nil {
- err = errors.New(fmt.Sprint("获取信息客户产品失败", ";Err:"+err.Error()))
- return
- }
- if companyProduct == nil {
- err = errors.New("获取信息客户产品失败2")
- return
- }
- approveStatus := "驳回"
- err = company_approval.ApproveRefuse(recodeInfo.CompanyId, recodeInfo.ProductId, approveStatus, remark)
- if err != nil {
- err = errors.New("客户单审批驳回失败,Err:" + err.Error())
- return
- }
- //新增客户产品日志
- {
- productLog := new(company_product_log.CompanyProductLog)
- productLog.CompanyProductId = companyProduct.CompanyProductId
- productLog.CompanyId = companyProduct.CompanyId
- productLog.ProductId = companyProduct.ProductId
- productLog.ProductName = companyProduct.ProductName
- productLog.CompanyName = companyProduct.CompanyName
- productLog.CompanyName = companyProduct.CompanyName
- productLog.Source = companyProduct.Source
- productLog.Reasons = companyProduct.Reasons
- productLog.Status = companyProduct.Status
- productLog.IndustryId = companyProduct.IndustryId
- productLog.IndustryName = companyProduct.IndustryName
- productLog.SellerId = companyProduct.SellerId
- productLog.SellerName = companyProduct.SellerName
- productLog.GroupId = companyProduct.GroupId
- productLog.DepartmentId = companyProduct.DepartmentId
- productLog.IsSuspend = companyProduct.IsSuspend
- productLog.SuspendTime = companyProduct.SuspendTime
- productLog.ApproveStatus = companyProduct.ApproveStatus
- productLog.FreezeTime = companyProduct.FreezeTime
- productLog.Remark = companyProduct.Remark
- productLog.StartDate = companyProduct.StartDate
- productLog.EndDate = companyProduct.EndDate
- productLog.CreateTime = companyProduct.CreateTime
- productLog.ModifyTime = companyProduct.ModifyTime
- productLog.LoseReason = companyProduct.LoseReason
- productLog.LossTime = companyProduct.LossTime
- productLog.LogType = "modify"
- productLog.LogCreateTime = time.Now()
- productLog.AdminId = opUserId
- go company_product_log.AddCompanyProductLog(productLog)
- }
- //新增操作记录
- {
- remark := "审批"
- operation := "approve"
- approveContent := "审批驳回"
- services.AddCompanyOperationRecord(recodeInfo.CompanyId, companyProduct.SellerId, opUserId, recodeInfo.ProductId, opUserId, companyProduct.CompanyName,
- companyProduct.ProductName, opUserName, remark, operation, approveContent, opUserName, remark, companyProduct.Status)
- }
- return
- }
- // CheckApproveAuth 校验审批操作权限
- func CheckApproveAuth(companyId, productId int, opUser *custom.AdminWx) (approvalItem *company_approval.CompanyApproval, approvalInfo *contract_approval.ContractApproval, approvalRecord *contract_approval_record.ContractApprovalRecord, err error) {
- //客户单详情
- approvalItem, err = company_approval.GetCompanyApprovalByCompanyId(companyId, productId)
- if err != nil {
- err = errors.New("获取客户单失败,Err:" + err.Error())
- return
- }
- //客户单状态判断
- if approvalItem.ApproveStatus != "待审批" {
- err = errors.New("客户单状态异常,不允许操作申请,当前客户单状态:" + approvalItem.ApproveStatus)
- return
- }
- //获取该最近一条审批单详情
- approvalInfo, err = contract_approval.GetLastContractApprovalByContractId(approvalItem.CompanyApprovalId, "company")
- if err != nil {
- err = errors.New("获取审批单失败,Err:" + err.Error())
- return
- }
- if approvalInfo.Status != "待审批" {
- err = errors.New("当前审批单状态异常,不允许审批,审批单状态:" + approvalInfo.Status)
- return
- }
- //审批流
- approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalIdAndNodeId(approvalInfo.ContractApprovalId, approvalInfo.CurrNodeId)
- if err != nil {
- err = errors.New("获取审批失败,Err:" + err.Error())
- return
- }
- for _, tmpApprovalRecord := range approvalRecordList {
- if tmpApprovalRecord.NodeType == "check" && tmpApprovalRecord.ApproveUserId == opUser.AdminId {
- approvalRecord = tmpApprovalRecord
- }
- }
- if approvalRecord == nil {
- err = errors.New("当前账号没有审批权限")
- return
- }
- return
- }
- // 获取审批流和权限
- func GetOpButton(approvalItem *company_approval.CompanyApprovalList, contractApprovalInfo *contract_approval.ContractApproval, opUser *custom.AdminWx) (flowNodeListResp [][]contract_approval_record.ContractApprovalRecord, err error) {
- //审批流
- approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalId(contractApprovalInfo.ContractApprovalId)
- if err != nil {
- err = errors.New("获取审批失败,Err:" + err.Error())
- return
- }
- flowNodeMap := make(map[int][]contract_approval_record.ContractApprovalRecord, 0)
- keySort := make([]int, 0)
- for _, approvalRecord := range approvalRecordList {
- //如果当前节点正好是该节点,同时审批单状态是待审批状态,然后当前账号又有权限,该账号也正是审批人,那么允许审批操作
- if contractApprovalInfo.CurrNodeId == approvalRecord.NodeId && contractApprovalInfo.Status == "待审批" {
- if opUser.AdminId == approvalRecord.ApproveUserId && approvalRecord.NodeType == "check" {
- approvalItem.OpButton = true
- }
- }
- //如果当前节点正好是该节点,同时审批单状态又是第一层节点
- if contractApprovalInfo.CurrNodeId == approvalRecord.NodeId && approvalRecord.PrevNodeId == 0 && contractApprovalInfo.Status == "待审批" {
- //发起人具有撤回审批单权限
- //if opUser.AdminId == sealInfo.UserId {
- // sealOpButton.Cancel = true
- //}
- }
- if tmpFlowNodeList, ok := flowNodeMap[approvalRecord.NodeId]; ok {
- flowNodeMap[approvalRecord.NodeId] = append(tmpFlowNodeList, *approvalRecord)
- } else {
- tmpFlowNodeList := make([]contract_approval_record.ContractApprovalRecord, 1)
- tmpFlowNodeList[0] = *approvalRecord
- flowNodeMap[approvalRecord.NodeId] = tmpFlowNodeList
- keySort = append(keySort, approvalRecord.NodeId)
- }
- }
- for _, key := range keySort {
- flowNodeListResp = append(flowNodeListResp, flowNodeMap[key])
- }
- return
- }
- // GetApprovalDetailByCompanyApprovalId 根据客户单id获取客户单审批详情
- func GetApprovalDetailByCompanyApprovalId(companyApprovalId int, opUser *custom.AdminWx) (approvalItem *company_approval.CompanyApprovalList, flowNodeListResp [][]contract_approval_record.ContractApprovalRecord, err error) {
- var condition string
- var pars []interface{}
- approvalItem, err = company_approval.GetApprovalInfoByApprovalId(condition, pars, companyApprovalId)
- if err != nil {
- err = errors.New("获取客户单详情失败,ERR:" + err.Error())
- return
- }
- //查询相关的合同详情
- if approvalItem.CompanyContractId > 0 {
- companyContractDetail, e := company_contract.GetCompanyContractDetail(approvalItem.CompanyId, approvalItem.ProductId, approvalItem.CompanyContractId)
- if e == nil {
- if companyContractDetail.Source == "系统合同" {
- approvalItem.ContractSourceTag = "标准合同"
- } else {
- approvalItem.ContractSourceTag = "非标合同"
- }
- }
- }
- if approvalItem.FreezeStartDate != "" && approvalItem.FreezeEndDate != "" && approvalItem.Status == utils.COMPANY_STATUS_FREEZE {
- freezeEndDate, _ := time.Parse(utils.FormatDate, approvalItem.FreezeEndDate)
- if time.Now().Before(freezeEndDate) {
- expireDays := freezeEndDate.Sub(time.Now()).Hours() / 24
- approvalItem.FreezeExpireDays = int(expireDays)
- }
- }
- if approvalItem.StartDate != "" && approvalItem.EndDate != "" {
- endDate, _ := time.Parse(utils.FormatDate, approvalItem.EndDate)
- if time.Now().Before(endDate) {
- expireDays := endDate.Sub(time.Now()).Hours() / 24
- approvalItem.ExpireDay = int(expireDays)
- }
- }
- switch approvalItem.ApplyMethod {
- case 2:
- //客户冻结转试用次数
- approvalCount, tmpErr := company_approval.GetFreezeToTryCompanyApprovalCount(approvalItem.CompanyId, approvalItem.ProductId, approvalItem.ApplyUserId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- err = tmpErr
- return
- }
- approvalItem.ApprovalCount = approvalCount
- case 3:
- delayPermission, tmpErr := company_delay_permission.GetApplyDelayPermission(approvalItem.CompanyApprovalId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- err = tmpErr
- return
- }
- approvalItem.DelayPermission = delayPermission
- //客户审批试用延期次数
- approvalCount, tmpErr := company_approval.GetCompanyTryDelayApprovalCount(approvalItem.CompanyId, approvalItem.ProductId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- err = tmpErr
- return
- }
- approvalItem.ApprovalCount = approvalCount
- }
- //查询最近一次审批单信息
- lastApprovalInfo, err := contract_approval.GetLastContractApprovalByContractId(approvalItem.CompanyApprovalId, "company")
- if err != nil {
- if err.Error() != utils.ErrNoRow() {
- err = errors.New("获取最近一次审批单信息失败,ERR:" + err.Error())
- }
- err = nil
- return
- }
- if lastApprovalInfo != nil {
- //获取审批操作和审批流数据
- tmpFlowNodeListResp, tmpErr := GetOpButton(approvalItem, lastApprovalInfo, opUser)
- if tmpErr != nil {
- err = errors.New("获取客户单按钮权限、审批流程失败,Err:" + tmpErr.Error())
- return
- }
- flowNodeListResp = tmpFlowNodeListResp
- }
- approvalItem.Status = strings.Replace(approvalItem.Status, utils.COMPANY_STATUS_FOREVER, utils.COMPANY_STATUS_X_CLASS_TRY_OUT, -1) //永续替换成 X类试用
- return
- }
|