12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270 |
- package controllers
- import (
- "encoding/json"
- "fmt"
- "hongze/hz_crm_api/models"
- "hongze/hz_crm_api/models/company"
- "hongze/hz_crm_api/models/system"
- "hongze/hz_crm_api/services"
- "hongze/hz_crm_api/services/company_apply"
- contractService "hongze/hz_crm_api/services/contract"
- "hongze/hz_crm_api/utils"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- // ApplyServiceUpdate
- // @Title 申请服务更新
- // @Description 申请服务更新接口
- // @Param request body company.CompanyApplyServiceUpdateReq true "type json string"
- // @Success Ret=200 申请成功
- // @router /apply/service/update [post]
- func (this *CompanyApplyController) ApplyServiceUpdate() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req company.CompanyApplyServiceUpdateReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.CompanyId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,客户id小于等于0"
- return
- }
- if req.StartDate == "" {
- br.Msg = "请选择合同开始日期"
- return
- }
- if req.EndDate == "" {
- br.Msg = "请选择合同结束日期"
- return
- }
- if req.Money <= 0 {
- br.Msg = "合同金额不能小于等于0"
- }
- if req.ContractType == "" {
- req.ContractType = "续约合同"
- //br.Msg = "请选择合同类型"
- //return
- }
- if req.PermissionIds == "" {
- br.Msg = "请选择权限"
- br.ErrMsg = "请选择权限"
- return
- }
- roleTypeCode := sysUser.RoleTypeCode
- productId := services.GetProductId(roleTypeCode)
- if productId == 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "客户类型为:" + req.CompanyType
- return
- }
- mapPermissionIdUpgrade := make(map[int]int, 0)
- mapPermissionIdExpensive := make(map[int]int, 0)
- var raicontractType string
- if productId == 2 {
- // CRM13.3改-是否为权益内部人员, 非权益内部人员只能勾选行业不能勾选单独的主客观, 勾选行业(即勾选主观的ID需要加入客观的权限ID)
- isRai, e := services.CheckRaiAdmin(sysUser.AdminId)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "判断是否为权益内部人员失败, GetRaiAdmin Err: " + e.Error()
- return
- }
- var permissionIdsNew string
- permissionIdsNew, mapPermissionIdUpgrade, mapPermissionIdExpensive, err = services.CheckCompanyUpgradeV2(req.PermissionIds, isRai)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取主观客观权限信息失败,Err:" + err.Error()
- return
- }
- req.PermissionIds = permissionIdsNew
- //权益的新签合同重新定义,如果合同起始时间在新签合同起始日期 1年之内的,仍为新签合同。
- raicontractType = req.ContractType
- if raicontractType != "新签合同" {
- //startDateTime, _ := time.Parse(utils.FormatDate, req.StartDate)
- //startDateTime = startDateTime.AddDate(-1, 0, 0)
- totalLastYear, err := contractService.GetCompanyContractCountRaiByLastYear(req.CompanyId, req.StartDate)
- if err != nil {
- br.Msg = "获取合同信息失败"
- br.ErrMsg = "获取合同信息失败,Err:" + err.Error()
- return
- }
- if totalLastYear > 0 {
- raicontractType = "新签合同"
- }
- }
- } else {
- permissionStr, err := company.GetPermissionIdById(req.PermissionIds)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取主观客观权限信息失败,Err:" + err.Error()
- return
- }
- req.PermissionIds = permissionStr
- }
- //添加缓存,避免多次点击提交
- {
- deleteCache := true
- cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
- defer func() {
- if deleteCache {
- utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请勿重复操作"
- br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
- return
- }
- }
- approveRoleTypeCode := ""
- if productId == 1 {
- approveRoleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
- } else {
- approveRoleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
- }
- companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
- if err != nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "获取客户信息失败"
- return
- }
- if companyProduct.Status != utils.COMPANY_STATUS_FORMAL && companyProduct.Status != utils.COMPANY_STATUS_TRY_OUT {
- br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行服务更新申请"
- br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行服务更新申请"
- return
- }
- //客户归属判断-当前销售/咨询组共享销售
- if companyProduct.SellerId != sysUser.AdminId && companyProduct.ShareSellerId != sysUser.AdminId {
- br.Msg = "客户信息异常"
- br.ErrMsg = "客户信息异常,不是当前所属销售,客户所属销售:" + companyProduct.SellerName + ";当前销售:" + sysUser.AdminName
- return
- }
- var approveUserId int
- var approveUserRealName string
- applyMethod := 5 //续约服务
- recordApproveContent := `正式客户申请续约`
- approveContent := `申请续约`
- messageContent := "续约申请单"
- operationRemark := `申请续约`
- operation := "service_update"
- //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议
- if req.ContractType == "补充协议" {
- applyMethod = 6 //补充协议
- recordApproveContent = `正式客户新增补充协议`
- approveContent = `新增补充协议`
- messageContent = "补充协议申请单"
- operationRemark = `新增补充协议`
- operation = "add_agreement"
- }
- approveUser, err := company.GetApproveUser(approveRoleTypeCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取审批信息失败"
- br.ErrMsg = "获取审批人信息失败,Err:" + err.Error()
- return
- }
- if approveUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
- br.Msg = "审批用户不存在"
- br.ErrMsg = "审批用户不存在"
- return
- }
- var companyApprovalId int
- if req.CompanyApprovalId <= 0 {
- contractCode, err := company.GetCompanyContractCode()
- if err != nil {
- br.Msg = "生成合同编码失败"
- br.ErrMsg = "生成合同编码失败,Err:" + err.Error()
- return
- }
- //当前是否存在待审批的单子
- nowCompanyApprovalInfo, err := company.GetCompanyApprovalById(req.CompanyId, productId)
- if (err == nil || nowCompanyApprovalInfo != nil) || err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "此客户已存在待审批的申请单,请在申请单审批完成后重新提交"
- br.ErrMsg = "此客户已存在待审批的申请单,请在申请单审批完成后重新提交"
- return
- }
- //当前待审批的合同,有且只能存在一条
- //count, err := company.GetCompanyContractApproveCount(req.CompanyId, productId)
- //if err != nil {
- // br.Msg = "获取信息失败"
- // br.ErrMsg = "判断是否已存在待审批合同失败,Err:" + err.Error()
- // return
- //}
- //if count > 0 {
- // br.Msg = "此客户已提交过申请,请在客户审批列表重新提交"
- // return
- //}
- contract := new(company.CompanyContract)
- contract.CompanyId = req.CompanyId
- contract.ProductId = productId
- contract.ProductName = companyProduct.ProductName
- contract.CompanyProductId = companyProduct.CompanyProductId
- contract.ContractType = req.ContractType
- contract.RaiContractType = raicontractType
- contract.ContractCode = contractCode
- contract.StartDate = req.StartDate
- contract.EndDate = req.EndDate
- contract.Money = req.Money
- contract.PayMethod = req.PayMethod
- contract.PayChannel = req.PayChannel
- contract.ImgUrl = req.ImgUrl
- contract.Status = 0
- contract.Source = "上传附件"
- contract.CreateTime = time.Now()
- contract.ModifyTime = time.Now()
- contract.PackageType = req.PackageType
- contract.RaiPackageType = req.RaiPackageType
- newId, err := company.AddCompanyContract(contract)
- if err != nil {
- br.Msg = "新增合同失败"
- br.ErrMsg = "新增合同失败,Err:" + err.Error()
- return
- }
- permissionArr := strings.Split(req.PermissionIds, ",")
- permissionMap := make(map[int]int) //权限id的map集合,用来避免重复添加权限校验
- permissionNameMap, err := services.GetPermissionNameMap()
- if err != nil {
- br.Msg = "新增合同失败"
- br.ErrMsg = "获取权限名称失败,Err:" + err.Error()
- return
- }
- for _, v := range permissionArr {
- item := new(company.CompanyContractPermission)
- item.CompanyId = req.CompanyId
- item.CompanyContractId = int(newId)
- chartPermissionId, tmpErr := strconv.Atoi(v)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //权限id判断,避免重复添加权限
- if _, ok := permissionMap[chartPermissionId]; ok {
- continue
- }
- permissionMap[chartPermissionId] = chartPermissionId
- item.ChartPermissionId = chartPermissionId
- item.PermissionName = permissionNameMap[chartPermissionId]
- //如果是研选扣点包的权限,写入扣点点数
- if chartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
- item.Points = req.Points
- }
- item.StartDate = req.StartDate
- item.EndDate = req.EndDate
- item.IsUpgrade = mapPermissionIdUpgrade[chartPermissionId]
- item.ExpensiveYx = mapPermissionIdExpensive[chartPermissionId]
- item.ModifyTime = time.Now()
- item.CreateTime = time.Now()
- err = company.AddCompanyContractPermission(item)
- if err != nil {
- br.Msg = "新增合同失败"
- br.ErrMsg = "新增合同权限失败,Err:" + err.Error()
- return
- }
- //新增日志
- {
- itemLog := new(company.CompanyContractPermissionLog)
- itemLog.CompanyId = req.CompanyId
- itemLog.CompanyContractId = int(newId)
- itemLog.ChartPermissionId = v
- itemLog.StartDate = req.StartDate
- itemLog.EndDate = req.EndDate
- itemLog.ModifyTime = time.Now()
- itemLog.CreateTime = time.Now()
- itemLog.LogCreateTime = time.Now()
- go company.AddCompanyContractPermissionLog(itemLog)
- }
- }
- approval := new(company.CompanyApproval)
- approval.CompanyId = req.CompanyId
- approval.ProductId = productId
- approval.ApproveStatus = "待审批"
- approval.ApproveUserId = 0
- approval.ApproveRoleTypeCode = approveRoleTypeCode
- approval.ApproveRealName = ""
- approval.ApproveTime = time.Now()
- approval.ApproveContent = approveContent
- approval.ApplyMethod = applyMethod
- approval.ApplyReasons = ""
- approval.ApplyUserId = sysUser.AdminId
- approval.ApplyRealName = sysUser.RealName
- approval.CompanyContractId = int(newId)
- approval.CreateTime = time.Now()
- approval.ModifyTime = time.Now()
- approval.IsFormal = companyProduct.IsFormal
- companyApprovalId, err = company.AddCompanyApproval(approval)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- } else {
- // 有审批单
- recode, err := company.GetCompanyApprovalByApprovalId(req.CompanyApprovalId)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "获取审批记录失败,Err:" + err.Error()
- return
- }
- err = company.ModifyCompanyApplyServiceUpdate(&req, productId, recode.CompanyContractId, companyProduct.IsFormal, mapPermissionIdUpgrade, mapPermissionIdExpensive)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- companyApprovalId = req.CompanyApprovalId
- }
- //新增操作记录
- {
- services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, approveUserId, companyProduct.CompanyName,
- companyProduct.ProductName, sysUser.RealName, operationRemark, operation, recordApproveContent, approveUserRealName, "", companyProduct.Status)
- }
- //新增客户产品日志
- {
- logType := "modify"
- go services.AddCompanyProductLog(companyProduct, this.SysUser.AdminId, logType)
- }
- //新增申请单
- err = company_apply.Apply(companyApprovalId, messageContent)
- if err != nil {
- br.Msg = "发起审批异常,请联系管理员"
- br.ErrMsg = "客户审批发起异常,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "申请成功"
- }
- // ApplyTurnPositive
- // @Title 申请转正
- // @Description 申请转正接口
- // @Param request body company.CompanyApplyTurnPositiveReq true "type json string"
- // @Success Ret=200 申请成功
- // @router /apply/turn/positive [post]
- func (this *CompanyApplyController) ApplyTurnPositive() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req company.CompanyApplyTurnPositiveReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.CompanyId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,客户id小于等于0"
- return
- }
- if req.StartDate == "" {
- br.Msg = "请选择合同开始日期"
- return
- }
- if req.EndDate == "" {
- br.Msg = "请选择合同结束日期"
- return
- }
- if req.Money <= 0 {
- br.Msg = "合同金额不能小于等于0"
- return
- }
- if req.PermissionIds == "" {
- br.Msg = "请选择权限"
- return
- }
- roleTypeCode := sysUser.RoleTypeCode
- productId := services.GetProductId(roleTypeCode)
- if productId == 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "客户类型为:" + roleTypeCode
- return
- }
- //添加缓存,避免多次点击提交
- {
- deleteCache := true
- cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
- defer func() {
- if deleteCache {
- utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请勿重复操作"
- br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
- return
- }
- }
- companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
- if err != nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "获取客户信息失败"
- return
- }
- if companyProduct == nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "客户不存在,不可提交审批:companyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
- return
- }
- if companyProduct.Status != utils.COMPANY_STATUS_TRY_OUT && companyProduct.InitStatus != utils.COMPANY_STATUS_FOREVER {
- br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行转正申请"
- br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行转正申请"
- return
- }
- mapPermissionIdUpgrade := make(map[int]int) // 权益主客观、行业升级、买方研选的处理
- permissionNameMap := make(map[int]string) // 权益行业名称处理
- permissionNames := req.PermissionNames
- expensiveYx := 0
- if productId == 2 {
- // 给升级的产业ID加一个极大值判断处理
- permissionSlice := strings.Split(req.PermissionIds, ",")
- mapPermissionId := make(map[int]int)
- //var permissionIdsNew string
- var permissionIdOnly int //处理普通权限与升级权限同时勾选的时候的拦截
- expId := utils.PERMISSION_ID_YANXUAN_DIFF + utils.CHART_PERMISSION_ID_YANXUAN
- // 获取行业主观对应的客观ID
- subObjMap, e := services.GetRaiSubObjPermissionIdMap()
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取行业主观对应的客观ID失败, Err: " + e.Error()
- return
- }
- strPermissionIdArr := make([]string, 0) // 最终过滤后的权限IDs
- for _, v := range permissionSlice {
- permissionId, _ := strconv.Atoi(v)
- // 研选5w的处理
- if permissionId == expId {
- //permissionIdsNew += strconv.Itoa(utils.CHART_PERMISSION_ID_YANXUAN) + ","
- strPermissionIdArr = append(strPermissionIdArr, strconv.Itoa(utils.CHART_PERMISSION_ID_YANXUAN))
- expensiveYx = 1
- continue
- }
- // 研选10w的处理
- if permissionId == utils.PERMISSION_ID_YANXUAN_10W_DIFF {
- //permissionIdsNew += strconv.Itoa(utils.CHART_PERMISSION_ID_YANXUAN) + ","
- strPermissionIdArr = append(strPermissionIdArr, strconv.Itoa(utils.CHART_PERMISSION_ID_YANXUAN))
- expensiveYx = 2
- continue
- }
- if permissionId > utils.PERMISSION_ID_UPGRADE {
- //permissionIdsNew += strconv.Itoa(permissionId-utils.PERMISSION_ID_UPGRADE) + ","
- permissionIdOnly = permissionId - utils.PERMISSION_ID_UPGRADE
- // 升级权限, 需要加入客观的权限
- subId := permissionId - utils.PERMISSION_ID_UPGRADE
- strPermissionIdArr = append(strPermissionIdArr, strconv.Itoa(subId))
- if subObjMap[subId] > 0 {
- strPermissionIdArr = append(strPermissionIdArr, strconv.Itoa(subObjMap[subId]))
- }
- } else {
- //permissionIdsNew += strconv.Itoa(permissionId) + ","
- strPermissionIdArr = append(strPermissionIdArr, v)
- permissionIdOnly = permissionId
- }
- //权益的永续客户主客观权限都开
- if companyProduct.InitStatus == utils.COMPANY_STATUS_FOREVER && subObjMap[permissionId] > 0 {
- strPermissionIdArr = append(strPermissionIdArr, strconv.Itoa(subObjMap[permissionId]))
- }
- if _, ok := mapPermissionId[permissionIdOnly]; ok {
- br.Msg = "权限设置有误,请重新选择"
- br.ErrMsg = "升级权限与普通权限重复选择!" + req.PermissionIds
- return
- }
- mapPermissionId[permissionIdOnly] = permissionIdOnly
- }
- req.PermissionIds = strings.Join(strPermissionIdArr, ",")
- //req.PermissionIds = strings.TrimRight(permissionIdsNew, ",")
- // 是否为权益内部人员, 非权益内部人员只能勾选行业不能勾选单独的主客观, 需要通过主观的ID去查客观的ID
- //isRai, e := services.CheckRaiAdmin(sysUser.AdminId)
- //if e != nil {
- // br.Msg = "获取失败"
- // br.ErrMsg = "判断是否为权益内部人员失败, GetRaiAdmin Err: " + e.Error()
- // return
- //}
- //if !isRai {
- //permissionStr, err := company.GetPermissionIdById(req.PermissionIds)
- //if err != nil {
- // br.Msg = "获取信息失败"
- // br.ErrMsg = "获取主观客观权限信息失败,Err:" + err.Error()
- // return
- //}
- //req.PermissionIds = permissionStr
- //}
- // 升级map
- if permissionNames != "" {
- mapPermissionIdUpgrade, err = services.PermissionIdUpgrade(permissionNames)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "处理升级信息失败,Err:" + err.Error()
- return
- }
- }
- permissionNameMap, err = services.GetPermissionNameMap()
- if err != nil {
- br.Msg = "新增合同失败"
- br.ErrMsg = "获取权限名称失败,Err:" + err.Error()
- return
- }
- }
- //客户归属判断
- if companyProduct.SellerId != sysUser.AdminId && companyProduct.ShareSellerId != sysUser.AdminId {
- br.Msg = "客户信息异常"
- br.ErrMsg = "客户信息异常,不是当前所属销售,客户所属销售:" + companyProduct.SellerName + ";当前销售:" + sysUser.AdminName
- return
- }
- if companyProduct.ApproveStatus == "待审批" {
- br.Msg = "存在待审批订单,不可重复提交"
- return
- }
- approveContent := `试用转正式`
- var approveUserId int
- var approveUserRealName string
- approveRoleTypeCode := ""
- if productId == 1 {
- approveRoleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
- } else {
- approveRoleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
- }
- approveUser, err := company.GetApproveUser(approveRoleTypeCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取审批信息失败"
- br.ErrMsg = "获取审批人信息失败,Err:" + err.Error()
- return
- }
- if approveUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
- br.Msg = "审批用户不存在"
- br.ErrMsg = "审批用户不存在"
- return
- }
- // 合同类型
- contractType := req.ContractType
- var raicontractType string // 权益合同类型
- {
- if contractType == "" {
- contractType = "新签合同"
- }
- var condition string
- var pars []interface{}
- condition += " AND product_id = ?"
- pars = append(pars, productId)
- condition += " AND status = ?"
- pars = append(pars, 1)
- list, e := company.GetCompanyContractListByCompanyId(req.CompanyId, condition, pars)
- if e != nil {
- br.Msg = "获取合同信息失败"
- br.ErrMsg = "获取合同信息失败,Err:" + e.Error()
- return
- }
- if len(list) > 0 {
- contractType = "续约合同"
- }
- if productId == 2 {
- //权益的新签合同重新定义,如果合同起始时间在新签合同起始日期 1年之内的,仍为新签合同。
- raicontractType = contractType
- if raicontractType != "新签合同" {
- //startDateTime, _ := time.Parse(utils.FormatDate, req.StartDate)
- //startDateTime = startDateTime.AddDate(-1, 0, 0)
- totalLastYear, err := contractService.GetCompanyContractCountRaiByLastYear(req.CompanyId, req.StartDate)
- if err != nil {
- br.Msg = "获取合同信息失败"
- br.ErrMsg = "获取合同信息失败,Err:" + err.Error()
- return
- }
- if totalLastYear > 0 {
- raicontractType = "新签合同"
- }
- }
- }
- // 若上面的判断不是续约合同, 则继续查询是否存在正式转试用的记录,如果存在,那么合同类型是续约合同
- if contractType != "续约合同" {
- total, e := company.GetCompanyProductOperationRecordCount(companyProduct.CompanyId, companyProduct.ProductId)
- if e != nil {
- br.Msg = "查询用户信息异常"
- br.ErrMsg = "查询用户变更记录异常, GetCompanyProductOperationRecordCount err: " + e.Error()
- return
- }
- if total > 0 {
- contractType = "续约合同"
- }
- }
- }
- var companyApprovalId int
- // 全新的申请转正
- if req.CompanyApprovalId <= 0 {
- contractCode, err := company.GetCompanyContractCode()
- if err != nil {
- br.Msg = "生成合同编码失败"
- br.ErrMsg = "生成合同编码失败,Err:" + err.Error()
- return
- }
- //当前是否存在待审批的单子
- nowCompanyApprovalInfo, err := company.GetCompanyApprovalById(req.CompanyId, productId)
- if (err == nil || nowCompanyApprovalInfo != nil) || err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "此客户已存在待审批的申请单,请在申请单审批完成后重新提交"
- br.ErrMsg = "此客户已存在待审批的申请单,请在申请单审批完成后重新提交"
- return
- }
- //当前待审批的合同,有且只能存在一条
- //count, err := company.GetCompanyContractApproveCount(req.CompanyId, productId)
- //if err != nil {
- // br.Msg = "获取信息失败"
- // br.ErrMsg = "判断是否已存在待审批合同失败,Err:" + err.Error()
- // return
- //}
- //if count > 0 {
- // br.Msg = "此客户已提交过申请,请在客户审批列表重新提交"
- // return
- //}
- contract := new(company.CompanyContract)
- contract.CompanyId = req.CompanyId
- contract.ProductId = productId
- contract.ContractType = contractType
- contract.RaiContractType = raicontractType
- contract.ProductName = companyProduct.ProductName
- contract.CompanyProductId = companyProduct.CompanyProductId
- contract.ContractCode = contractCode
- contract.StartDate = req.StartDate
- contract.EndDate = req.EndDate
- contract.Money = req.Money
- contract.PayMethod = req.PayMethod
- contract.PayChannel = req.PayChannel
- contract.ImgUrl = req.ImgUrl
- contract.Status = 0
- contract.Source = "上传附件"
- contract.PackageType = req.PackageType
- contract.RaiPackageType = req.RaiPackageType
- contract.CreateTime = time.Now()
- contract.ModifyTime = time.Now()
- newId, err := company.AddCompanyContract(contract)
- if err != nil {
- br.Msg = "新增合同失败"
- br.ErrMsg = "新增合同失败,Err:" + err.Error()
- return
- }
- permissionArr := strings.Split(req.PermissionIds, ",")
- permissionMap := make(map[int]int) //权限id的map集合,用来避免重复添加权限校验
- permissionItems := make([]*company.CompanyContractPermission, 0)
- permissionLogs := make([]*company.CompanyContractPermissionLog, 0)
- for _, v := range permissionArr {
- item := new(company.CompanyContractPermission)
- item.CompanyId = req.CompanyId
- item.CompanyContractId = int(newId)
- chartPermissionId, tmpErr := strconv.Atoi(v)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //权限id判断,避免重复添加权限
- if _, ok := permissionMap[chartPermissionId]; ok {
- continue
- }
- permissionMap[chartPermissionId] = chartPermissionId
- item.ChartPermissionId = chartPermissionId
- if _, ok := mapPermissionIdUpgrade[chartPermissionId]; ok {
- item.IsUpgrade = 1
- }
- if chartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN {
- item.ExpensiveYx = expensiveYx
- }
- //如果是研选扣点包,这个行业权限的ID;那么就对点数进行赋值。目前就研选扣点包 这一个行业有点数,先不用数组类型处理。v:查研观向13.2
- if chartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
- if req.Points <= 0 {
- br.Msg = "点数不能为空"
- return
- }
- item.Points = req.Points
- }
- item.PermissionName = permissionNameMap[chartPermissionId]
- item.StartDate = req.StartDate
- item.EndDate = req.EndDate
- item.ModifyTime = time.Now()
- item.CreateTime = time.Now()
- permissionItems = append(permissionItems, item)
- //新增日志
- itemLog := new(company.CompanyContractPermissionLog)
- itemLog.CompanyId = req.CompanyId
- itemLog.CompanyContractId = int(newId)
- itemLog.ChartPermissionId = v
- itemLog.StartDate = req.StartDate
- itemLog.EndDate = req.EndDate
- itemLog.ModifyTime = time.Now()
- itemLog.CreateTime = time.Now()
- itemLog.LogCreateTime = time.Now()
- permissionLogs = append(permissionLogs, itemLog)
- }
- // 批量新增company_contract_permission和log
- if e := company.MultiAddCompanyContractPermission(permissionItems); e != nil {
- br.Msg = "新增合同失败"
- br.ErrMsg = "批量新增合同权限失败, MultiAddCompanyContractPermission Err:" + e.Error()
- return
- }
- go company.MultiAddCompanyContractPermissionLog(permissionLogs)
- applyMethod := 1
- approval := new(company.CompanyApproval)
- approval.CompanyId = req.CompanyId
- approval.ProductId = productId
- approval.ApproveStatus = "待审批"
- approval.ApproveUserId = 0
- approval.ApproveRoleTypeCode = approveRoleTypeCode
- approval.ApproveRealName = ""
- approval.ApproveTime = time.Now()
- approval.ApproveContent = "试用转正式"
- approval.ApplyMethod = applyMethod
- approval.ApplyReasons = ""
- approval.ApplyUserId = sysUser.AdminId
- approval.ApplyRealName = sysUser.RealName
- approval.CreateTime = time.Now()
- approval.ModifyTime = time.Now()
- approval.CompanyContractId = int(newId)
- approval.IsFormal = companyProduct.IsFormal
- companyApprovalId, err = company.AddCompanyApproval(approval)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- } else {
- // 重新申请
- recode, err := company.GetCompanyApprovalByApprovalId(req.CompanyApprovalId)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "获取审批记录失败,Err:" + err.Error()
- return
- }
- err = company.ModifyCompanyTurnPositive(&req, productId, recode.CompanyContractId, companyProduct.IsFormal, mapPermissionIdUpgrade, expensiveYx)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- companyApprovalId = req.CompanyApprovalId
- }
- //新增操作记录
- {
- remark := "申请转正"
- operation := "turn_positive"
- services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, approveUserId, companyProduct.CompanyName,
- companyProduct.ProductName, sysUser.RealName, remark, operation, approveContent, approveUserRealName, "", companyProduct.Status)
- }
- //新增客户产品日志
- {
- logType := "modify"
- go services.AddCompanyProductLog(companyProduct, this.SysUser.AdminId, logType)
- }
- //新增申请单
- err = company_apply.Apply(companyApprovalId, "试用转正式申请单")
- if err != nil {
- br.Msg = "发起审批异常,请联系管理员"
- br.ErrMsg = "客户审批发起异常,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "申请成功"
- }
- // ApplyBySystemContract
- // @Title 通过系统合同申请转正/服务更新
- // @Description 通过系统合同申请转正/服务更新接口
- // @Param request body company.CompanyApplyBySystemContractReq true "type json string"
- // @Success Ret=200 申请成功
- // @router /apply/systemContract [post]
- func (this *CompanyApplyController) ApplyBySystemContract() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req company.CompanyApplyBySystemContractReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.CompanyId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,客户id小于等于0"
- return
- }
- //获取客户信息
- companyInfo, err := company.GetCompanyById(req.CompanyId)
- if err != nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "获取客户信息失败"
- return
- }
- roleTypeCode := sysUser.RoleTypeCode
- productId := services.GetProductId(roleTypeCode)
- if productId == 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "客户类型为:" + roleTypeCode
- return
- }
- //添加缓存,避免多次点击提交
- {
- deleteCache := true
- cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
- defer func() {
- if deleteCache {
- utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请勿重复操作"
- br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
- return
- }
- }
- companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
- if err != nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "获取客户信息失败"
- return
- }
- if companyProduct == nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "客户不存在,不可提交审批:companyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
- return
- }
- if companyProduct.SellerId != sysUser.AdminId && companyProduct.ShareSellerId != sysUser.AdminId {
- br.Msg = "客户信息异常"
- br.ErrMsg = "客户信息异常,不是当前所属销售,客户所属销售:" + companyProduct.SellerName + ";当前销售:" + sysUser.AdminName
- return
- }
- if companyProduct.ApproveStatus == "待审批" {
- br.Msg = "存在待审批订单,不可重复提交"
- return
- }
- var approveUserId int
- var approveUserRealName string
- approveRoleTypeCode := ""
- if productId == 1 {
- approveRoleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
- } else {
- approveRoleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
- }
- approveUser, err := company.GetApproveUser(approveRoleTypeCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取审批信息失败"
- br.ErrMsg = "获取审批人信息失败,Err:" + err.Error()
- return
- }
- if approveUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
- br.Msg = "审批用户不存在"
- br.ErrMsg = "审批用户不存在"
- return
- }
- //获取合同详情
- contractDetail, err := contractService.GetContractDetail(req.ContractId)
- if err != nil {
- br.Msg = "查询合同异常"
- br.ErrMsg = fmt.Sprint("查询合同异常,合同id:", req.ContractId, ";Err:", err.Error())
- return
- }
- if contractDetail.ContractBusinessType != "业务合同" {
- br.Msg = "合同类型异常"
- br.ErrMsg = "合同类型异常,当前合同类型为:" + contractDetail.ContractBusinessType
- return
- }
- //判断产品类型是否一致
- if productId != contractDetail.ProductId {
- br.Msg = "合同产品类型异常"
- br.ErrMsg = fmt.Sprint("合同产品类型异常,合同产品类型Id:", contractDetail.ProductId)
- return
- }
- //判断销售归属
- if contractDetail.SellerId != sysUser.AdminId {
- br.Msg = "合同异常"
- br.ErrMsg = fmt.Sprint("合同归属人异常,合同申请人Id:", contractDetail.SellerId, ";当前账号:", sysUser.AdminId)
- return
- }
- //判断合同是否该客户的(组织社会信用代码是否一致)
- if contractDetail.CreditCode != companyInfo.CreditCode {
- br.Msg = "合同异常,请选择社会信用码一致的合同"
- br.ErrMsg = fmt.Sprint("该合同组织社会信用码与客户不符,合同的社会信用码:", contractDetail.CreditCode, ";客户的社会信用码:", companyInfo.CreditCode)
- return
- }
- if contractDetail.Status != "已签回" {
- br.Msg = "合同状态异常"
- br.ErrMsg = fmt.Sprint("合同状态异常,合同状态:", contractDetail.Status)
- return
- }
- //当前客户需要的合同状态
- needContractType, err := services.GetApplyTurnContractType(req.CompanyId, productId)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取合同类型失败,Err:" + err.Error()
- return
- }
- //判断 系统需要的合同类型 与 实际提交的合同类型是否一致,如果不一致,那么不支持
- if needContractType != "新签合同" && contractDetail.ContractType == "新签合同" {
- br.Msg = "申请失败,该客户曾经开通过正式服务,不允许签署新签合同"
- br.ErrMsg = "申请失败,该客户曾经开通过正式服务,不允许签署新签合同"
- return
- }
- if contractDetail.ContractType == "新签合同" {
- if companyProduct.Status != utils.COMPANY_STATUS_TRY_OUT {
- br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行转正申请"
- br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行转正申请"
- return
- }
- } else {
- if companyProduct.Status != utils.COMPANY_STATUS_FORMAL && companyProduct.Status != utils.COMPANY_STATUS_TRY_OUT {
- br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行服务更新申请"
- br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行服务更新申请"
- return
- }
- }
- //合同类型
- contractType := contractDetail.ContractType
- var hasContract bool //是否已经存在合同,如果存在,那么就不能是新签合同
- var condition string
- var pars []interface{}
- condition += " AND product_id = ?"
- pars = append(pars, productId)
- condition += " AND status = ?"
- pars = append(pars, 1)
- list, err := company.GetCompanyContractListByCompanyId(req.CompanyId, condition, pars)
- if err != nil {
- br.Msg = "获取合同信息失败"
- br.ErrMsg = "获取合同信息失败,Err:" + err.Error()
- return
- }
- if len(list) > 0 {
- hasContract = true
- }
- //查询是否存在正式转试用的记录,如果存在,那么合同类型是续约合同
- total, err := company.GetCompanyProductOperationRecordCount(companyProduct.CompanyId, companyProduct.ProductId)
- if err != nil {
- br.Msg = "查询用户信息异常"
- br.ErrMsg = "查询用户变更记录异常"
- return
- }
- if total > 0 {
- hasContract = true
- }
- if hasContract && contractType == "新签合同" {
- br.Msg = "客户已存在老合同,不允许签署新签合同"
- br.ErrMsg = "客户已存在老合同,不允许签署新签合同"
- return
- }
- //合同类型 end
- //获取权限id列表
- permissionMap, err := contractService.GetServicePermissionMap(contractDetail.Service)
- permissionArr := make([]int, 0)
- for _, permissionId := range permissionMap {
- permissionArr = append(permissionArr, permissionId)
- }
- //对权限排序
- sort.Ints(permissionArr)
- // CRM8.8-权益主客观此处拆分成了三级套餐, 根据contract_service写权限
- // CRM8.8-如果是权益客户那么主观客观权限一起带上(暂弃)
- //if companyProduct.ProductId == 2 {
- // var permissionIds string
- // var permissionNames string
- // for _, v := range permissionArr {
- // permissionIds += strconv.Itoa(v) + ","
- // }
- // permissionIds = strings.TrimRight(permissionIds, ",")
- // listSandO, err := company.GetPermissionLookItemsSandO(permissionIds)
- // if err != nil {
- // br.Msg = "获取现有的合同权限信息失败"
- // br.ErrMsg = "获取现有的合同权限ID信息失败,Err:" + err.Error()
- // return
- // }
- // for _, v := range listSandO {
- // permissionNames += "'" + v.PermissionName + "'" + ","
- // }
- // permissionNames = strings.TrimRight(permissionNames, ",")
- // listPermissionByname, err := company.GetPermissionLookItemsSandOByName(permissionNames)
- // if err != nil {
- // br.Msg = "获取现有的合同权限信息失败"
- // br.ErrMsg = "通过名称获取主观客观权限ID信息失败,Err:" + err.Error()
- // return
- // }
- // permissionArr = make([]int, 0)
- // for _, v := range listPermissionByname {
- // permissionArr = append(permissionArr, v.ChartPermissionId)
- // }
- // //对权限排序
- // sort.Ints(permissionArr)
- //}
- //当前已经存在的权限
- currPermissionMap, err := services.GetLastContractPermissionList(companyProduct.CompanyId, companyProduct.ProductId)
- if err != nil {
- br.Msg = "获取现有的合同权限信息失败"
- br.ErrMsg = "获取现有的合同权限信息失败,Err:" + err.Error()
- return
- }
- //权限内日期判断校验
- for _, permissionId := range permissionArr {
- //研选服务不做校验
- if permissionId == 31 {
- continue
- }
- if currPermission, ok := currPermissionMap[permissionId]; ok {
- currStartDate, _ := time.Parse(utils.FormatDate, currPermission.StartDate)
- currEndDate, _ := time.Parse(utils.FormatDate, currPermission.EndDate)
- if !(contractDetail.StartDate.After(currEndDate) || currStartDate.After(contractDetail.EndDate)) {
- br.Msg = currPermission.PermissionName + "合同日期存在重复,不允许申请"
- br.ErrMsg = "权限合同日期存在重复,不允许申请"
- return
- }
- }
- }
- //获取客户产品套餐类型(大套餐、小套餐、无)
- packageType, err := contractService.GetServicePackageType(contractDetail.Service)
- raiPackageType := contractService.GetRaiServicePackageType(contractDetail.Service)
- recordApproveContent := `试用转正式`
- applyMethod := 1
- approveContent := "试用转正式"
- operationRemark := `申请转正`
- operation := "turn_positive"
- messageContent := "试用转正式申请单"
- //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议
- if contractType != "新签合同" {
- if contractType == "补充协议" {
- applyMethod = 6 //补充协议
- recordApproveContent = `正式客户新增补充协议`
- approveContent = `新增补充协议`
- operationRemark = `新增补充协议`
- operation = "add_agreement"
- messageContent = "补充协议申请单"
- } else {
- applyMethod = 5 //续约服务
- recordApproveContent = `正式客户申请续约`
- approveContent = `申请续约`
- operationRemark = `申请续约`
- operation = "service_update"
- messageContent = "续约申请单"
- }
- }
- var raicontractType string
- if productId == 2 {
- //权益的新签合同重新定义,如果合同起始时间在新签合同起始日期 1年之内的,仍为新签合同。
- raicontractType = contractType
- if raicontractType != "新签合同" {
- //startDateTime := contractDetail.StartDate.AddDate(-1, 0, 0)
- totalLastYear, err := contractService.GetCompanyContractCountRaiByLastYear(req.CompanyId, contractDetail.StartDate.Format(utils.FormatDate))
- if err != nil {
- br.Msg = "获取合同信息失败"
- br.ErrMsg = "获取合同信息失败,Err:" + err.Error()
- return
- }
- if totalLastYear > 0 {
- raicontractType = "新签合同"
- }
- }
- }
- var companyApprovalId int
- if req.CompanyApprovalId <= 0 {
- //当前是否存在待审批的单子
- nowCompanyApprovalInfo, err := company.GetCompanyApprovalById(req.CompanyId, productId)
- if (err == nil || nowCompanyApprovalInfo != nil) || err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "此客户已存在待审批的申请单,请在申请单审批完成后重新提交"
- br.ErrMsg = "此客户已存在待审批的申请单,请在申请单审批完成后重新提交"
- return
- }
- ////当前待审批的合同,有且只能存在一条
- //count, err := company.GetCompanyContractApproveCount(req.CompanyId, productId)
- //if err != nil {
- // br.Msg = "获取信息失败"
- // br.ErrMsg = "判断是否已存在待审批合同失败,Err:" + err.Error()
- // return
- //}
- //if count > 0 {
- // br.Msg = "此客户已提交过申请,请在客户审批列表重新提交"
- // return
- //}
- companyContract := &company.CompanyContract{
- CompanyId: req.CompanyId,
- ProductId: contractDetail.ProductId,
- ContractType: contractType,
- RaiContractType: raicontractType,
- ProductName: companyProduct.ProductName,
- CompanyProductId: companyProduct.CompanyProductId,
- ContractCode: contractDetail.ContractCode,
- StartDate: contractDetail.StartDate.Format(utils.FormatDate),
- EndDate: contractDetail.EndDate.Format(utils.FormatDate),
- Money: contractDetail.Price,
- PayMethod: contractDetail.PayRemark,
- PayChannel: contractDetail.PayChannel,
- ImgUrl: contractDetail.CheckBackFileUrl,
- Status: 0,
- Source: "系统合同",
- PackageType: packageType,
- RaiPackageType: raiPackageType,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- newId, err := company.AddCompanyContract(companyContract)
- if err != nil {
- br.Msg = "新增合同失败"
- br.ErrMsg = "新增合同失败,Err:" + err.Error()
- return
- }
- upgradeMap := make(map[int]int)
- if utils.RunMode == "release" {
- upgradeMap = map[int]int{22: 39, 21: 38, 20: 37, 19: 36}
- } else {
- upgradeMap = map[int]int{22: 37, 21: 36, 20: 35, 19: 34}
- }
- for _, chartPermissionId := range permissionArr {
- if chartPermissionId < utils.PERMISSION_ID_UPGRADE {
- item := new(company.CompanyContractPermission)
- item.CompanyId = req.CompanyId
- item.CompanyContractId = int(newId)
- item.ChartPermissionId = chartPermissionId
- item.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
- item.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
- item.ModifyTime = time.Now()
- item.CreateTime = time.Now()
- err = company.AddCompanyContractPermission(item)
- if err != nil {
- br.Msg = "新增合同失败"
- br.ErrMsg = "新增合同权限失败,Err:" + err.Error()
- return
- }
- //新增日志
- {
- itemLog := new(company.CompanyContractPermissionLog)
- itemLog.CompanyId = req.CompanyId
- itemLog.CompanyContractId = int(newId)
- itemLog.ChartPermissionId = strconv.Itoa(chartPermissionId)
- itemLog.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
- itemLog.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
- itemLog.ModifyTime = time.Now()
- itemLog.CreateTime = time.Now()
- itemLog.LogCreateTime = time.Now()
- go company.AddCompanyContractPermissionLog(itemLog)
- }
- } else {
- // 升级套餐,手动补充主客观套餐
- chartPermissionId -= utils.PERMISSION_ID_UPGRADE
- item := new(company.CompanyContractPermission)
- item.CompanyId = req.CompanyId
- item.CompanyContractId = int(newId)
- item.ChartPermissionId = chartPermissionId
- item.IsUpgrade = 1
- item.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
- item.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
- item.ModifyTime = time.Now()
- item.CreateTime = time.Now()
- err = company.AddCompanyContractPermission(item)
- if err != nil {
- br.Msg = "新增合同失败"
- br.ErrMsg = "新增合同权限失败,Err:" + err.Error()
- return
- }
- //新增日志
- {
- itemLog := new(company.CompanyContractPermissionLog)
- itemLog.CompanyId = req.CompanyId
- itemLog.CompanyContractId = int(newId)
- itemLog.ChartPermissionId = strconv.Itoa(chartPermissionId)
- itemLog.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
- itemLog.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
- itemLog.ModifyTime = time.Now()
- itemLog.CreateTime = time.Now()
- itemLog.LogCreateTime = time.Now()
- go company.AddCompanyContractPermissionLog(itemLog)
- }
- // 客观
- upgradeChartPermissionId := upgradeMap[chartPermissionId]
- upItem := new(company.CompanyContractPermission)
- upItem.CompanyId = req.CompanyId
- upItem.CompanyContractId = int(newId)
- upItem.ChartPermissionId = upgradeChartPermissionId
- upItem.IsUpgrade = 1
- upItem.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
- upItem.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
- upItem.ModifyTime = time.Now()
- upItem.CreateTime = time.Now()
- err = company.AddCompanyContractPermission(upItem)
- if err != nil {
- br.Msg = "新增合同失败"
- br.ErrMsg = "新增合同权限失败,Err:" + err.Error()
- return
- }
- //新增日志
- {
- itemLog := new(company.CompanyContractPermissionLog)
- itemLog.CompanyId = req.CompanyId
- itemLog.CompanyContractId = int(newId)
- itemLog.ChartPermissionId = strconv.Itoa(upgradeChartPermissionId)
- itemLog.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
- itemLog.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
- itemLog.ModifyTime = time.Now()
- itemLog.CreateTime = time.Now()
- itemLog.LogCreateTime = time.Now()
- go company.AddCompanyContractPermissionLog(itemLog)
- }
- }
- }
- approval := new(company.CompanyApproval)
- approval.CompanyId = req.CompanyId
- approval.ProductId = productId
- approval.ApproveStatus = "待审批"
- approval.ApproveUserId = 0
- approval.ApproveRoleTypeCode = approveRoleTypeCode
- approval.ApproveRealName = ""
- approval.ApproveTime = time.Now()
- approval.ApproveContent = approveContent
- approval.ApplyMethod = applyMethod
- approval.ApplyReasons = ""
- approval.ApplyUserId = sysUser.AdminId
- approval.ApplyRealName = sysUser.RealName
- approval.CreateTime = time.Now()
- approval.ModifyTime = time.Now()
- approval.CompanyContractId = int(newId)
- approval.IsFormal = companyProduct.IsFormal
- companyApprovalId, err = company.AddCompanyApproval(approval)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- } else {
- recode, err := company.GetCompanyApprovalByApprovalId(req.CompanyApprovalId)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "获取审批记录失败,Err:" + err.Error()
- return
- }
- err = company.ModifyCompanyTurnPositiveBySystemContract(contractDetail, permissionArr, req.CompanyId, productId, req.CompanyApprovalId, recode.CompanyContractId, companyProduct.IsFormal, packageType, raiPackageType)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- companyApprovalId = req.CompanyApprovalId
- }
- //新增操作记录
- {
- services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, approveUserId, companyProduct.CompanyName,
- companyProduct.ProductName, sysUser.RealName, operationRemark, operation, recordApproveContent, approveUserRealName, "", companyProduct.Status)
- }
- //新增客户产品日志
- {
- logType := "modify"
- go services.AddCompanyProductLog(companyProduct, this.SysUser.AdminId, logType)
- }
- err = company_apply.Apply(companyApprovalId, messageContent)
- if err != nil {
- br.Msg = "发起审批异常,请联系管理员"
- br.ErrMsg = "客户审批发起异常,Err:" + err.Error()
- return
- }
- //待办通知
- //{
- // for _, v := range approveUser {
- // approveUserId = v.AdminId
- // approveUserRealName = v.RealName
- // mobile := v.Mobile
- // err = services.AddCompanyApprovalMessage(sysUser.AdminId, approveUserId, req.CompanyId, companyApprovalId, 1, 1, 1, companyProduct.CompanyName, messageContent, messageContent, mobile)
- // if err != nil {
- // br.Msg = "申请失败"
- // br.ErrMsg = "新增待办通知失败,Err:" + err.Error()
- // return
- // }
- // }
- //}
- br.Ret = 200
- br.Success = true
- br.Msg = "申请成功"
- }
- // ApplyThaw
- // @Title 申请解冻
- // @Description 申请解冻接口
- // @Param request body company.CompanyApplyThawReq true "type json string"
- // @Success Ret=200 申请成功
- // @router /apply/thaw [post]
- func (this *CompanyApplyController) ApplyThaw() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req company.CompanyApplyThawReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.CompanyId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,客户id小于等于0"
- return
- }
- roleTypeCode := sysUser.RoleTypeCode
- productId := services.GetProductId(roleTypeCode)
- if productId == 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "客户类型为:" + roleTypeCode
- return
- }
- if productId == utils.COMPANY_PRODUCT_FICC_ID && req.PermissionIds == "" {
- br.Msg = "请勾选需要解冻的品种"
- br.ErrMsg = "请勾选需要解冻的品种"
- return
- }
- // 查询当前销售是否有重新申请的权限
- receiveEnabled, _ := services.CheckCompanyReceiveButton(sysUser.AdminId)
- if !receiveEnabled {
- br.Msg = "无权申请"
- br.ErrMsg = "无权申请"
- return
- }
- //添加缓存,避免多次点击提交
- {
- deleteCache := true
- cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
- defer func() {
- if deleteCache {
- utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请勿重复操作"
- br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
- return
- }
- }
- var companyApprovalCount int //某一客户申请的次数
- approveRoleTypeCode := ""
- if productId == 1 {
- approveRoleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
- } else {
- approveRoleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
- companyApprovalCount, err = company.GetCompanyApprovalCount(req.CompanyId, productId, 2)
- if err != nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "获取客户申请的次数失败,Err:" + err.Error()
- return
- }
- permissionArr := strings.Split(req.PermissionIds, ",")
- //如果所选行业不属于研选订阅跟研选扣点包,那就是非研选试用客户
- var isNoResearch bool
- for _, v := range permissionArr {
- if v != strconv.Itoa(utils.CHART_PERMISSION_ID_YANXUAN) && v != strconv.Itoa(utils.YAN_XUAN_KOU_DIAN_BAO_ID) {
- isNoResearch = true
- continue
- }
- }
- if isNoResearch {
- tryOutCountRai, err := company.GetRaiCompanyTryOutCountNoResearch(sysUser.AdminId)
- if err != nil {
- br.Msg = "判断试用客户量失败"
- br.ErrMsg = "判断试用客户量失败,Err:" + err.Error()
- return
- }
- if tryOutCountRai >= utils.COMPANY_STATUS_TRY_OUT_COUNT_RAI {
- br.Msg = "非研选试用客户最多40家"
- return
- }
- }
- }
- companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
- if err != nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "获取客户信息失败"
- return
- }
- if companyProduct == nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "客户不存在,不可提交审批:companyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
- return
- }
- if companyProduct.Status != utils.COMPANY_STATUS_FREEZE {
- br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行解冻申请"
- br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行解冻申请"
- return
- }
- if companyProduct.ApproveStatus == utils.COMPANY_APPROVE_STATUS {
- br.Msg = "存在待审批订单,不可重复提交"
- return
- }
- //客户归属判断
- if companyProduct.SellerId != sysUser.AdminId && companyProduct.ShareSellerId != sysUser.AdminId {
- br.Msg = "客户信息异常"
- br.ErrMsg = "客户信息异常,不是当前所属销售,客户所属销售:" + companyProduct.SellerName + ";当前销售:" + sysUser.AdminName
- return
- }
- // CRM8.8(仅针对权益客户)-此处同申请试用的逻辑
- if companyProduct.ProductId == utils.COMPANY_PRODUCT_RAI_ID {
- reportPermissions, e := company.GetCompanyReportPermission(req.CompanyId, utils.COMPANY_PRODUCT_RAI_ID)
- if e != nil {
- br.Msg = "客户权限信息异常"
- br.ErrMsg = "客户权限信息异常, Err: " + e.Error()
- return
- }
- reportPermissionArr := make([]string, 0)
- for i := range reportPermissions {
- reportPermissionArr = append(reportPermissionArr, strconv.Itoa(reportPermissions[i].ChartPermissionId))
- }
- if len(reportPermissionArr) > 0 {
- req.PermissionIds = strings.Join(reportPermissionArr, ",")
- }
- }
- approveUser, err := company.GetApproveUser(approveRoleTypeCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取审批信息失败"
- br.ErrMsg = "获取审批人信息失败,Err:" + err.Error()
- return
- }
- if approveUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
- br.Msg = "审批用户不存在"
- br.ErrMsg = "审批用户不存在"
- return
- }
- //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议
- approveContent := `冻结转试用`
- var companyApprovalId int
- if req.CompanyApprovalId <= 0 {
- applyMethod := 2
- approval := new(company.CompanyApproval)
- approval.CompanyId = req.CompanyId
- approval.ProductId = productId
- approval.ApproveStatus = "待审批"
- approval.ApproveUserId = 0
- approval.ApproveRoleTypeCode = approveRoleTypeCode
- approval.ApproveRealName = ""
- approval.ApproveTime = time.Now()
- approval.ApproveContent = approveContent
- approval.ApplyMethod = applyMethod
- approval.ApplyReasons = req.ApplyRemark
- approval.ApplyUserId = sysUser.AdminId
- approval.ApplyRealName = sysUser.RealName
- approval.CreateTime = time.Now()
- approval.ModifyTime = time.Now()
- companyApprovalId, err = company.AddCompanyApproval(approval)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- } else {
- err = company.ModifyCompanyApproval(req.CompanyId, productId, req.CompanyApprovalId, approveContent, req.ApplyRemark)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- companyApprovalId = req.CompanyApprovalId
- //先删除之前申请的权限
- _ = company.DeleteCompanyDelayPermission(req.CompanyApprovalId)
- }
- //处理权限
- {
- if req.PermissionIds != "" {
- // CRM8.8(仅针对权益客户)-此处不再去查询主观对应的客观, 而是根据客户之前拥有的权限来操作
- //permissionStr, err := company.GetPermissionIdById(req.PermissionIds)
- //if err != nil {
- // br.Msg = "获取信息失败"
- // br.ErrMsg = "获取主观客观权限信息失败,Err:" + err.Error()
- // return
- //}
- //req.PermissionIds = permissionStr
- permissionArr := strings.Split(req.PermissionIds, ",")
- for _, v := range permissionArr {
- //fmt.Println(k, v)
- //新增
- if v != "" {
- permissionId, err := strconv.Atoi(v)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "权限格式转换失败,Err:" + err.Error()
- return
- }
- delayItem := new(company.CompanyDelayPermission)
- delayItem.CompanyId = req.CompanyId
- delayItem.CompanyApplyId = companyApprovalId
- delayItem.ChartPermissionId = permissionId
- delayItem.StartDate = time.Now().Format(utils.FormatDate)
- delayItem.EndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
- delayItem.CreateTime = time.Now()
- delayItem.ModifyTime = time.Now()
- err = company.AddCompanyDelayPermission(delayItem)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "新增冻结转试用审批权限失败,Err:" + err.Error()
- return
- }
- }
- }
- }
- }
- //新增操作记录
- {
- remark := "申请解冻"
- operation := "thaw"
- services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, 0, companyProduct.CompanyName,
- companyProduct.ProductName, sysUser.RealName, remark, operation, approveContent, "", "", companyProduct.Status)
- }
- //新增客户产品日志
- {
- logType := "modify"
- go services.AddCompanyProductLog(companyProduct, this.SysUser.AdminId, logType)
- }
- err = company_apply.Apply(companyApprovalId, "冻结转试用申请单")
- if err != nil {
- br.Msg = "发起审批异常,请联系管理员"
- br.ErrMsg = "客户审批发起异常,Err:" + err.Error()
- return
- }
- if companyApprovalCount == 0 && productId == 2 {
- //获取王芳信息,自动审批使用
- approvalUserInfo, err := system.CheckSysUserByName("王芳", utils.ROLE_TYPE_CODE_RAI_ADMIN)
- if err != nil {
- br.Msg = "权益客户自动审批失败,请联系管理员"
- br.ErrMsg = "审批失败,Err:" + err.Error()
- return
- }
- //数据校验(校验是否具有审批权限)
- _, _, approvalRecord, err := company_apply.CheckApproveAuth(req.CompanyId, productId, approvalUserInfo)
- if err != nil {
- br.Msg = "权益客户自动审批失败,请联系管理员"
- br.ErrMsg = "校验是否具有审批权限失败,Err:" + err.Error()
- return
- }
- err = company_apply.Approved(approvalRecord, approvalUserInfo, "权益客首次申请解冻自动审批通过")
- if err != nil {
- br.Msg = "权益客户自动审批失败,请联系管理员!"
- br.ErrMsg = "审核失败,Err:" + err.Error()
- return
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "申请成功"
- }
- // ApplyDelay
- // @Title 申请延期
- // @Description 申请延期接口
- // @Param request body company.CompanyApplyDelayReq true "type json string"
- // @Success Ret=200 申请成功
- // @router /apply/delay [post]
- func (this *CompanyApplyController) ApplyDelay() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req company.CompanyApplyDelayReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.CompanyId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,客户id小于等于0"
- return
- }
- roleTypeCode := sysUser.RoleTypeCode
- productId := services.GetProductId(roleTypeCode)
- if productId == 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "客户类型为:" + roleTypeCode
- return
- }
- if productId == utils.COMPANY_PRODUCT_FICC_ID && req.PermissionIds == "" {
- br.Msg = "请勾选需要延期的品种"
- br.ErrMsg = "请勾选需要延期的品种"
- return
- }
- //添加缓存,避免多次点击提交
- {
- deleteCache := true
- cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
- defer func() {
- if deleteCache {
- utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请勿重复操作"
- br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
- return
- }
- }
- approveRoleTypeCode := ""
- if productId == 1 {
- approveRoleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
- } else {
- approveRoleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
- }
- companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
- if err != nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "获取客户信息失败"
- return
- }
- if companyProduct == nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "客户不存在,不可提交审批:companyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
- return
- }
- if companyProduct.Status != utils.COMPANY_STATUS_TRY_OUT {
- br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行延期申请"
- br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行延期申请"
- return
- }
- if companyProduct.ApproveStatus == "待审批" {
- br.Msg = "存在待审批订单,不可重复提交"
- return
- }
- //客户归属判断
- if companyProduct.SellerId != sysUser.AdminId && companyProduct.ShareSellerId != sysUser.AdminId {
- br.Msg = "客户信息异常"
- br.ErrMsg = "客户信息异常,不是当前所属销售,客户所属销售:" + companyProduct.SellerName + ";当前销售:" + sysUser.AdminName
- return
- }
- // CRM8.8(仅针对权益客户)-申请延期的权限不再进行选择, 而是自动将所有的试用权限延期
- if companyProduct.ProductId == utils.COMPANY_PRODUCT_RAI_ID {
- reportPermissions, e := company.GetCompanyReportPermission(req.CompanyId, utils.COMPANY_PRODUCT_RAI_ID)
- if e != nil {
- br.Msg = "客户权限信息异常"
- br.ErrMsg = "客户权限信息异常, Err: " + e.Error()
- return
- }
- reportPermissionArr := make([]string, 0)
- for i := range reportPermissions {
- reportPermissionArr = append(reportPermissionArr, strconv.Itoa(reportPermissions[i].ChartPermissionId))
- }
- if len(reportPermissionArr) > 0 {
- req.PermissionIds = strings.Join(reportPermissionArr, ",")
- }
- }
- approveUser, err := company.GetApproveUser(approveRoleTypeCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取审批信息失败"
- br.ErrMsg = "获取审批人信息失败,Err:" + err.Error()
- return
- }
- if approveUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
- br.Msg = "审批用户不存在"
- br.ErrMsg = "审批用户不存在"
- return
- }
- //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议
- approveContent := `试用延期`
- var companyApprovalId int
- utils.FileLog.Info("试用延期申请:%d", req.CompanyApprovalId)
- if req.CompanyApprovalId > 0 {
- utils.FileLog.Info("试用延期申请:%d", req.CompanyApprovalId)
- err = company.ModifyCompanyApproval(req.CompanyId, productId, req.CompanyApprovalId, approveContent, req.ApplyRemark)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- companyApprovalId = req.CompanyApprovalId
- } else {
- applyMethod := 3
- approval := new(company.CompanyApproval)
- approval.CompanyId = req.CompanyId
- approval.ProductId = productId
- approval.ApproveStatus = "待审批"
- approval.ApproveUserId = 0
- approval.ApproveRoleTypeCode = approveRoleTypeCode
- approval.ApproveRealName = ""
- approval.ApproveTime = time.Now()
- approval.ApproveContent = "试用延期"
- approval.ApplyMethod = applyMethod
- approval.ApplyReasons = req.ApplyRemark
- approval.ApplyUserId = sysUser.AdminId
- approval.ApplyRealName = sysUser.RealName
- approval.CreateTime = time.Now()
- approval.ModifyTime = time.Now()
- companyApprovalId, err = company.AddCompanyApproval(approval)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- }
- //处理权限
- {
- if req.PermissionIds != "" {
- // CRM8.8(仅针对权益客户)-此处不再去查询主观对应的客观, 而是根据客户之前拥有的权限来操作
- //permissionStr, err := company.GetPermissionIdById(req.PermissionIds)
- //if err != nil {
- // br.Msg = "获取信息失败"
- // br.ErrMsg = "获取主观客观权限信息失败,Err:" + err.Error()
- // return
- //}
- //req.PermissionIds = permissionStr
- permissionArr := strings.Split(req.PermissionIds, ",")
- //如果是编辑的话,删除原先相关的所有品种权限
- if req.CompanyApprovalId > 0 {
- err = company.DeleteCompanyDelayPermission(req.CompanyApprovalId)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "删除之前的申请记录失败,Err:" + err.Error()
- return
- }
- }
- for k, v := range permissionArr {
- fmt.Println(k, v)
- permissionId, _ := strconv.Atoi(v)
- //companyId, companyApplyId, chartPermissionId int
- count, err := company.GetDelayPermissionCount(req.CompanyId, companyApprovalId, permissionId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "申请失败"
- br.ErrMsg = "判断权限是否存在失败,Err:" + err.Error()
- return
- }
- if count > 0 { //修改
- err = company.ModifyCompanyDelayPermission(req.CompanyId, companyApprovalId, permissionId)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "修改权限失败,Err:" + err.Error()
- return
- }
- } else { //新增
- if v != "" {
- permissionId, err := strconv.Atoi(v)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "权限格式转换失败,Err:" + err.Error()
- return
- }
- delayItem := new(company.CompanyDelayPermission)
- delayItem.CompanyId = req.CompanyId
- delayItem.CompanyApplyId = companyApprovalId
- delayItem.ChartPermissionId = permissionId
- delayItem.StartDate = time.Now().Format(utils.FormatDate)
- delayItem.EndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
- delayItem.CreateTime = time.Now()
- delayItem.ModifyTime = time.Now()
- err = company.AddCompanyDelayPermission(delayItem)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "新增延期审批权限失败,Err:" + err.Error()
- return
- }
- }
- }
- }
- }
- }
- //新增操作记录
- {
- remark := "申请延期"
- operation := "delay"
- go services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, 0, companyProduct.CompanyName,
- companyProduct.ProductName, sysUser.RealName, remark, operation, approveContent, "", "", companyProduct.Status)
- }
- //新增客户产品日志
- {
- logType := "modify"
- go services.AddCompanyProductLog(companyProduct, this.SysUser.AdminId, logType)
- }
- err = company_apply.Apply(companyApprovalId, "试用延期申请单")
- if err != nil {
- br.Msg = "发起审批异常,请联系管理员"
- br.ErrMsg = "客户审批发起异常,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "申请成功"
- }
- // ApplyReceive
- // @Title 申请领取
- // @Description 申请领取接口
- // @Param request body company.CompanyApplyReceiveReq true "type json string"
- // @Success Ret=200 申请成功
- // @router /apply/receive [post]
- func (this *CompanyApplyController) ApplyReceive() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req company.CompanyApplyReceiveReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.CompanyId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,客户id小于等于0"
- return
- }
- // 查询当前销售是否有领取客户的权限
- receiveEnabled, _ := services.CheckCompanyReceiveButton(sysUser.AdminId)
- if !receiveEnabled {
- br.Msg = "领取失败,您目前没有领取客户的权限"
- br.ErrMsg = "领取失败,您目前没有领取客户的权限"
- return
- }
- roleTypeCode := sysUser.RoleTypeCode
- productId := services.GetProductId(roleTypeCode)
- if productId == 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "客户类型为:" + roleTypeCode
- return
- }
- //如果是权益客户,在选择非研选行业试用时,需要判断当前有多少非研选的试用客户(试用状态,且行业是非研选)
- if productId == utils.COMPANY_PRODUCT_RAI_ID {
- permissionArr := strings.Split(req.PermissionIds, ",")
- //如果所选行业不属于研选订阅跟研选扣点包,那就是非研选试用客户
- var isNoResearch bool
- for _, v := range permissionArr {
- if v != strconv.Itoa(utils.CHART_PERMISSION_ID_YANXUAN) && v != strconv.Itoa(utils.YAN_XUAN_KOU_DIAN_BAO_ID) {
- isNoResearch = true
- continue
- }
- }
- if isNoResearch {
- tryOutCountRai, err := company.GetRaiCompanyTryOutCountNoResearch(sysUser.AdminId)
- if err != nil {
- br.Msg = "判断试用客户量失败"
- br.ErrMsg = "判断试用客户量失败,Err:" + err.Error()
- return
- }
- if tryOutCountRai >= utils.COMPANY_STATUS_TRY_OUT_COUNT_RAI {
- br.Msg = "非研选试用客户最多40家"
- return
- }
- }
- }
- //添加缓存,避免多次点击提交
- {
- deleteCache := true
- cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
- defer func() {
- if deleteCache {
- utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请勿重复操作"
- br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
- return
- }
- }
- approveRoleTypeCode := ""
- if productId == 1 {
- approveRoleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
- } else {
- approveRoleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
- }
- companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
- if err != nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "获取客户信息失败"
- return
- }
- if companyProduct == nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "客户不存在,不可提交审批:companyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
- return
- }
- if companyProduct.Status != utils.COMPANY_STATUS_LOSE {
- br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行延期申请"
- br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行延期申请"
- return
- }
- if companyProduct.ApproveStatus == "待审批" {
- br.Msg = "存在待审批订单,不可重复提交"
- return
- }
- approveUser, err := company.GetApproveUser(approveRoleTypeCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取审批信息失败"
- br.ErrMsg = "获取审批人信息失败,Err:" + err.Error()
- return
- }
- if approveUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
- br.Msg = "审批用户不存在"
- br.ErrMsg = "审批用户不存在"
- return
- }
- approveContent := `原销售申领客户`
- applyMethod := 4
- var companyApprovalId int
- //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议
- if req.CompanyApprovalId <= 0 {
- approval := new(company.CompanyApproval)
- approval.CompanyId = req.CompanyId
- approval.ProductId = productId
- approval.ApproveStatus = "待审批"
- approval.ApproveUserId = 0
- approval.ApproveRoleTypeCode = approveRoleTypeCode
- approval.ApproveRealName = ""
- approval.ApproveTime = time.Now()
- approval.ApproveContent = approveContent
- approval.ApplyMethod = applyMethod
- approval.ApplyReasons = req.ApplyRemark
- approval.ApplyUserId = sysUser.AdminId
- approval.ApplyRealName = sysUser.RealName
- approval.CreateTime = time.Now()
- approval.ModifyTime = time.Now()
- companyApprovalId, err = company.AddCompanyApproval(approval)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- } else {
- err = company.ModifyCompanyApproval(req.CompanyId, productId, req.CompanyApprovalId, approveContent, req.ApplyRemark)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- companyApprovalId = req.CompanyApprovalId
- //先删除之前申请的权限
- _ = company.DeleteCompanyDelayPermission(req.CompanyApprovalId)
- }
- //处理权限
- {
- if req.PermissionIds != "" {
- permissionStr, err := company.GetPermissionIdById(req.PermissionIds)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取主观客观权限信息失败,Err:" + err.Error()
- return
- }
- req.PermissionIds = permissionStr
- permissionArr := strings.Split(req.PermissionIds, ",")
- for _, v := range permissionArr {
- if v != "" {
- permissionId, err := strconv.Atoi(v)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "权限格式转换失败,Err:" + err.Error()
- return
- }
- delayItem := new(company.CompanyDelayPermission)
- delayItem.CompanyId = req.CompanyId
- delayItem.CompanyApplyId = companyApprovalId
- delayItem.ChartPermissionId = permissionId
- delayItem.StartDate = time.Now().Format(utils.FormatDate)
- delayItem.EndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
- delayItem.CreateTime = time.Now()
- delayItem.ModifyTime = time.Now()
- err = company.AddCompanyDelayPermission(delayItem)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "新增领取客户审批权限失败,Err:" + err.Error()
- return
- }
- }
- }
- }
- }
- //新增操作记录
- {
- remark := "申请领取"
- operation := "apply_receive"
- services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, 0, companyProduct.CompanyName,
- companyProduct.ProductName, sysUser.RealName, remark, operation, approveContent, "", "", companyProduct.Status)
- }
- //新增客户产品日志
- {
- logType := "modify"
- go services.AddCompanyProductLog(companyProduct, this.SysUser.AdminId, logType)
- }
- err = company_apply.Apply(companyApprovalId, "申请领取申请单")
- if err != nil {
- br.Msg = "发起审批异常,请联系管理员"
- br.ErrMsg = "客户审批发起异常,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "申请成功"
- }
- // ApplyRevoke
- // @Title 申请撤回
- // @Description 申请撤回接口
- // @Param request body company.CompanyApplyRevokeReq true "type json string"
- // @Success Ret=200 成功
- // @router /apply/revoke [post]
- func (this *CompanyApprovalController) ApplyRevoke() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req company.CompanyApplyRevokeReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.CompanyId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "客户Id小于等于0:"
- return
- }
- roleTypeCode := sysUser.RoleTypeCode
- productId := services.GetProductId(roleTypeCode)
- if productId == 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "客户类型为:" + roleTypeCode
- return
- }
- //添加缓存,避免多次点击提交
- {
- deleteCache := true
- cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
- defer func() {
- if deleteCache {
- utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请勿重复操作"
- br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
- return
- }
- }
- companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
- if err != nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "获取客户信息失败"
- return
- }
- if companyProduct == nil {
- br.Msg = "获取客户信息失败"
- br.ErrMsg = "客户不存在,不可提交审批:companyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
- return
- }
- approvalItem, err := company.GetCompanyApprovalById(req.CompanyId, productId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "待审批信息不存在"
- br.ErrMsg = "待审批信息不存在,CompanyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
- return
- }
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,Err:" + err.Error()
- return
- }
- if approvalItem == nil {
- br.Msg = "待审批信息不存在"
- br.ErrMsg = "待审批信息不存在,CompanyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
- return
- }
- if approvalItem.ApproveStatus != "待审批" {
- br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行撤回操作"
- br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行撤回操作"
- return
- }
- //撤回审批单
- err = company_apply.CancelApply(approvalItem, sysUser.AdminId)
- if err != nil {
- br.Msg = "撤回失败"
- br.ErrMsg = "撤回失败,Err:" + err.Error()
- return
- }
- //真正撤回操作
- err = company.ApplyRevoke(approvalItem.CompanyApprovalId, approvalItem.CompanyId, approvalItem.ProductId)
- if err != nil {
- br.Msg = "撤回失败"
- br.ErrMsg = "撤回失败,Err:" + err.Error()
- return
- }
- //新增操作记录
- {
- remark := "撤回"
- operation := "apply_revoke"
- services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, 0, companyProduct.CompanyName,
- companyProduct.ProductName, sysUser.RealName, remark, operation, "", "", "", companyProduct.Status)
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "撤回成功"
- }
|