company_apply_v2.go 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/models/seal"
  8. "hongze/hz_crm_api/models/system"
  9. "hongze/hz_crm_api/services"
  10. "hongze/hz_crm_api/services/company_apply"
  11. contractService "hongze/hz_crm_api/services/contract"
  12. "hongze/hz_crm_api/utils"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // ApplyServiceUpdate
  19. // @Title 申请服务更新
  20. // @Description 申请服务更新接口
  21. // @Param request body company.CompanyApplyServiceUpdateReq true "type json string"
  22. // @Success Ret=200 申请成功
  23. // @router /apply/service/update [post]
  24. func (this *CompanyApplyController) ApplyServiceUpdate() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. sysUser := this.SysUser
  31. if sysUser == nil {
  32. br.Msg = "请登录"
  33. br.ErrMsg = "请登录,SysUser Is Empty"
  34. br.Ret = 408
  35. return
  36. }
  37. var req company.CompanyApplyServiceUpdateReq
  38. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  39. if err != nil {
  40. br.Msg = "参数解析异常!"
  41. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  42. return
  43. }
  44. if req.CompanyId <= 0 {
  45. br.Msg = "参数错误"
  46. br.ErrMsg = "参数错误,客户id小于等于0"
  47. return
  48. }
  49. if req.StartDate == "" {
  50. br.Msg = "请选择合同开始日期"
  51. return
  52. }
  53. if req.EndDate == "" {
  54. br.Msg = "请选择合同结束日期"
  55. return
  56. }
  57. if req.Money <= 0 {
  58. br.Msg = "合同金额不能小于等于0"
  59. }
  60. if req.ContractType == "" {
  61. req.ContractType = "续约合同"
  62. //br.Msg = "请选择合同类型"
  63. //return
  64. }
  65. if req.PermissionIds == "" {
  66. br.Msg = "请选择权限"
  67. br.ErrMsg = "请选择权限"
  68. return
  69. }
  70. roleTypeCode := sysUser.RoleTypeCode
  71. productId := services.GetProductId(roleTypeCode)
  72. if productId == 0 {
  73. br.Msg = "参数错误"
  74. br.ErrMsg = "客户类型为:" + req.CompanyType
  75. return
  76. }
  77. //添加缓存,避免多次点击提交
  78. {
  79. deleteCache := true
  80. cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
  81. defer func() {
  82. if deleteCache {
  83. utils.Rc.Delete(cacheKey)
  84. }
  85. }()
  86. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  87. deleteCache = false
  88. br.Msg = "系统处理中,请勿重复操作"
  89. br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  90. return
  91. }
  92. }
  93. approveRoleTypeCode := ""
  94. if productId == 1 {
  95. approveRoleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
  96. } else {
  97. approveRoleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
  98. }
  99. companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
  100. if err != nil {
  101. br.Msg = "获取客户信息失败"
  102. br.ErrMsg = "获取客户信息失败"
  103. return
  104. }
  105. if companyProduct.Status != utils.COMPANY_STATUS_FORMAL && companyProduct.Status != utils.COMPANY_STATUS_TRY_OUT {
  106. br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行服务更新申请"
  107. br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行服务更新申请"
  108. return
  109. }
  110. //客户归属判断-当前销售/咨询组共享销售
  111. if companyProduct.SellerId != sysUser.AdminId && companyProduct.ShareSellerId != sysUser.AdminId {
  112. br.Msg = "客户信息异常"
  113. br.ErrMsg = "客户信息异常,不是当前所属销售,客户所属销售:" + companyProduct.SellerName + ";当前销售:" + sysUser.AdminName
  114. return
  115. }
  116. mapPermissionIdUpgrade := make(map[int]int, 0)
  117. mapPermissionIdExpensive := make(map[int]int, 0)
  118. var raicontractType string
  119. if productId == 2 {
  120. // CRM13.3改-是否为权益内部人员, 非权益内部人员只能勾选行业不能勾选单独的主客观, 勾选行业(即勾选主观的ID需要加入客观的权限ID)
  121. isRai, e := services.CheckRaiAdmin(sysUser.AdminId)
  122. if e != nil {
  123. br.Msg = "获取失败"
  124. br.ErrMsg = "判断是否为权益内部人员失败, GetRaiAdmin Err: " + e.Error()
  125. return
  126. }
  127. var permissionIdsNew string
  128. permissionIdsNew, mapPermissionIdUpgrade, mapPermissionIdExpensive, err = services.CheckCompanyUpgradeV2(req.PermissionIds, isRai)
  129. if err != nil {
  130. br.Msg = "获取信息失败"
  131. br.ErrMsg = "获取主观客观权限信息失败,Err:" + err.Error()
  132. return
  133. }
  134. req.PermissionIds = permissionIdsNew
  135. //权益的新签合同重新定义,如果合同起始时间在新签合同起始日期 1年之内的,仍为新签合同。
  136. raicontractType = req.ContractType
  137. if raicontractType != "新签合同" && companyProduct.Status != utils.COMPANY_STATUS_FOREVER {
  138. //startDateTime, _ := time.Parse(utils.FormatDate, req.StartDate)
  139. //startDateTime = startDateTime.AddDate(-1, 0, 0)
  140. totalLastYear, err := contractService.GetCompanyContractCountRaiByLastYear(req.CompanyId, req.StartDate)
  141. if err != nil {
  142. br.Msg = "获取合同信息失败"
  143. br.ErrMsg = "获取合同信息失败,Err:" + err.Error()
  144. return
  145. }
  146. if totalLastYear > 0 {
  147. raicontractType = "新签合同"
  148. req.ContractType = "新签合同"
  149. }
  150. }
  151. } else {
  152. permissionStr, err := company.GetPermissionIdById(req.PermissionIds)
  153. if err != nil {
  154. br.Msg = "获取信息失败"
  155. br.ErrMsg = "获取主观客观权限信息失败,Err:" + err.Error()
  156. return
  157. }
  158. req.PermissionIds = permissionStr
  159. }
  160. var approveUserId int
  161. var approveUserRealName string
  162. applyMethod := 5 //续约服务
  163. recordApproveContent := `正式客户申请续约`
  164. approveContent := `申请续约`
  165. messageContent := "续约申请单"
  166. operationRemark := `申请续约`
  167. operation := "service_update"
  168. //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议
  169. if req.ContractType == "补充协议" {
  170. applyMethod = 6 //补充协议
  171. recordApproveContent = `正式客户新增补充协议`
  172. approveContent = `新增补充协议`
  173. messageContent = "补充协议申请单"
  174. operationRemark = `新增补充协议`
  175. operation = "add_agreement"
  176. }
  177. approveUser, err := company.GetApproveUser(approveRoleTypeCode)
  178. if err != nil && err.Error() != utils.ErrNoRow() {
  179. br.Msg = "获取审批信息失败"
  180. br.ErrMsg = "获取审批人信息失败,Err:" + err.Error()
  181. return
  182. }
  183. if approveUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  184. br.Msg = "审批用户不存在"
  185. br.ErrMsg = "审批用户不存在"
  186. return
  187. }
  188. var companyApprovalId int
  189. if req.CompanyApprovalId <= 0 {
  190. contractCode, err := company.GetCompanyContractCode()
  191. if err != nil {
  192. br.Msg = "生成合同编码失败"
  193. br.ErrMsg = "生成合同编码失败,Err:" + err.Error()
  194. return
  195. }
  196. var sealId int
  197. if productId == 2 {
  198. contractCode, err = seal.GetSealCodeRai() //权益客户合同编号格式替换
  199. if err != nil {
  200. br.Msg = "生成合同编码失败"
  201. br.ErrMsg = "生成合同编码失败,Err:" + err.Error()
  202. return
  203. }
  204. sealCount, err := seal.GetCountByDateAndCompanyId(req.StartDate, req.EndDate, req.CompanyId)
  205. if err != nil {
  206. br.Msg = "生成合同编码失败"
  207. br.ErrMsg = "获取用印合同数量信息失败,Err:" + err.Error()
  208. return
  209. }
  210. crmConfig, err := company.GetConfigDetailByCode("rai_seal_check") //权益非标合同是否校验用印提交
  211. if err != nil {
  212. br.Msg = "获取配置失败"
  213. br.ErrMsg = "获取配置失败,Err:" + err.Error()
  214. return
  215. }
  216. if sealCount == 0 && crmConfig.ConfigValue == "1" {
  217. br.Msg = "此合同暂未申请用印,请核对合同期限是否有误"
  218. return
  219. }
  220. if sealCount > 0 {
  221. sealInfo, err := seal.GetSealInfoByDateAndCompanyId(req.StartDate, req.EndDate, req.CompanyId)
  222. if err != nil {
  223. br.Msg = "生成合同编码失败"
  224. br.ErrMsg = "获取用印合同信息失败,Err:" + err.Error()
  225. return
  226. }
  227. contractCode = sealInfo.Code
  228. sealId = sealInfo.SealId
  229. }
  230. }
  231. //当前是否存在待审批的单子
  232. nowCompanyApprovalInfo, err := company.GetCompanyApprovalById(req.CompanyId, productId)
  233. if (err == nil || nowCompanyApprovalInfo != nil) || err != nil && err.Error() != utils.ErrNoRow() {
  234. br.Msg = "此客户已存在待审批的申请单,请在申请单审批完成后重新提交"
  235. br.ErrMsg = "此客户已存在待审批的申请单,请在申请单审批完成后重新提交"
  236. return
  237. }
  238. //当前待审批的合同,有且只能存在一条
  239. //count, err := company.GetCompanyContractApproveCount(req.CompanyId, productId)
  240. //if err != nil {
  241. // br.Msg = "获取信息失败"
  242. // br.ErrMsg = "判断是否已存在待审批合同失败,Err:" + err.Error()
  243. // return
  244. //}
  245. //if count > 0 {
  246. // br.Msg = "此客户已提交过申请,请在客户审批列表重新提交"
  247. // return
  248. //}
  249. contract := new(company.CompanyContract)
  250. contract.CompanyId = req.CompanyId
  251. contract.ProductId = productId
  252. contract.ProductName = companyProduct.ProductName
  253. contract.CompanyProductId = companyProduct.CompanyProductId
  254. contract.ContractType = req.ContractType
  255. contract.RaiContractType = raicontractType
  256. contract.ContractCode = contractCode
  257. contract.StartDate = req.StartDate
  258. contract.EndDate = req.EndDate
  259. contract.Money = req.Money
  260. contract.PayMethod = req.PayMethod
  261. contract.PayChannel = req.PayChannel
  262. contract.ImgUrl = req.ImgUrl
  263. contract.Status = 0
  264. contract.Source = "上传附件"
  265. contract.CreateTime = time.Now()
  266. contract.ModifyTime = time.Now()
  267. contract.PackageType = req.PackageType
  268. contract.RaiPackageType = req.RaiPackageType
  269. contract.SellerIdInit = companyProduct.SellerId
  270. contract.SellerNameInit = companyProduct.SellerName
  271. contract.ShareSellerInit = companyProduct.ShareSeller
  272. contract.ShareSellerIdInit = companyProduct.ShareSellerId
  273. contract.SellerIdLast = companyProduct.SellerId
  274. contract.SellerNameLast = companyProduct.SellerName
  275. contract.ShareSellerLast = companyProduct.ShareSeller
  276. contract.ShareSellerIdLast = companyProduct.ShareSellerId
  277. newId, err := company.AddCompanyContract(contract)
  278. if err != nil {
  279. br.Msg = "新增合同失败"
  280. br.ErrMsg = "新增合同失败,Err:" + err.Error()
  281. return
  282. }
  283. //如果用印ID > 0 建立合同ID,与用印申请表的关系
  284. if sealId > 0 {
  285. //用印作废
  286. err = seal.UpdateSealCompanyContractId(int(newId), sealId)
  287. if err != nil {
  288. br.Msg = "操作失败"
  289. br.ErrMsg = "添加用印与合同的关系失败,Err:" + err.Error()
  290. return
  291. }
  292. }
  293. permissionArr := strings.Split(req.PermissionIds, ",")
  294. permissionMap := make(map[int]int) //权限id的map集合,用来避免重复添加权限校验
  295. permissionNameMap, err := services.GetPermissionNameMap()
  296. if err != nil {
  297. br.Msg = "新增合同失败"
  298. br.ErrMsg = "获取权限名称失败,Err:" + err.Error()
  299. return
  300. }
  301. for _, v := range permissionArr {
  302. item := new(company.CompanyContractPermission)
  303. item.CompanyId = req.CompanyId
  304. item.CompanyContractId = int(newId)
  305. chartPermissionId, tmpErr := strconv.Atoi(v)
  306. if tmpErr != nil {
  307. err = tmpErr
  308. return
  309. }
  310. //权限id判断,避免重复添加权限
  311. if _, ok := permissionMap[chartPermissionId]; ok {
  312. continue
  313. }
  314. permissionMap[chartPermissionId] = chartPermissionId
  315. item.ChartPermissionId = chartPermissionId
  316. item.PermissionName = permissionNameMap[chartPermissionId]
  317. //如果是研选扣点包的权限,写入扣点点数
  318. if chartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
  319. item.Points = req.Points
  320. }
  321. item.StartDate = req.StartDate
  322. item.EndDate = req.EndDate
  323. item.IsUpgrade = mapPermissionIdUpgrade[chartPermissionId]
  324. item.ExpensiveYx = mapPermissionIdExpensive[chartPermissionId]
  325. item.ModifyTime = time.Now()
  326. item.CreateTime = time.Now()
  327. err = company.AddCompanyContractPermission(item)
  328. if err != nil {
  329. br.Msg = "新增合同失败"
  330. br.ErrMsg = "新增合同权限失败,Err:" + err.Error()
  331. return
  332. }
  333. //新增日志
  334. {
  335. itemLog := new(company.CompanyContractPermissionLog)
  336. itemLog.CompanyId = req.CompanyId
  337. itemLog.CompanyContractId = int(newId)
  338. itemLog.ChartPermissionId = v
  339. itemLog.StartDate = req.StartDate
  340. itemLog.EndDate = req.EndDate
  341. itemLog.ModifyTime = time.Now()
  342. itemLog.CreateTime = time.Now()
  343. itemLog.LogCreateTime = time.Now()
  344. go company.AddCompanyContractPermissionLog(itemLog)
  345. }
  346. }
  347. approval := new(company.CompanyApproval)
  348. approval.CompanyId = req.CompanyId
  349. approval.ProductId = productId
  350. approval.ApproveStatus = "待审批"
  351. approval.ApproveUserId = 0
  352. approval.ApproveRoleTypeCode = approveRoleTypeCode
  353. approval.ApproveRealName = ""
  354. approval.ApproveTime = time.Now()
  355. approval.ApproveContent = approveContent
  356. approval.ApplyMethod = applyMethod
  357. approval.ApplyReasons = ""
  358. approval.ApplyUserId = sysUser.AdminId
  359. approval.ApplyRealName = sysUser.RealName
  360. approval.CompanyContractId = int(newId)
  361. approval.CreateTime = time.Now()
  362. approval.ModifyTime = time.Now()
  363. approval.IsFormal = companyProduct.IsFormal
  364. companyApprovalId, err = company.AddCompanyApproval(approval)
  365. if err != nil {
  366. br.Msg = "申请失败"
  367. br.ErrMsg = "申请失败,Err:" + err.Error()
  368. return
  369. }
  370. } else {
  371. // 有审批单
  372. recode, err := company.GetCompanyApprovalByApprovalId(req.CompanyApprovalId)
  373. if err != nil {
  374. br.Msg = "申请失败"
  375. br.ErrMsg = "获取审批记录失败,Err:" + err.Error()
  376. return
  377. }
  378. err = company.ModifyCompanyApplyServiceUpdate(&req, productId, recode.CompanyContractId, companyProduct.IsFormal, mapPermissionIdUpgrade, mapPermissionIdExpensive)
  379. if err != nil {
  380. br.Msg = "申请失败"
  381. br.ErrMsg = "申请失败,Err:" + err.Error()
  382. return
  383. }
  384. companyApprovalId = req.CompanyApprovalId
  385. }
  386. //新增操作记录
  387. {
  388. services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, approveUserId, companyProduct.CompanyName,
  389. companyProduct.ProductName, sysUser.RealName, operationRemark, operation, recordApproveContent, approveUserRealName, "", companyProduct.Status)
  390. }
  391. //新增客户产品日志
  392. {
  393. logType := "modify"
  394. go services.AddCompanyProductLog(companyProduct, this.SysUser.AdminId, logType)
  395. }
  396. //新增申请单
  397. err = company_apply.Apply(companyApprovalId, messageContent)
  398. if err != nil {
  399. br.Msg = "发起审批异常,请联系管理员"
  400. br.ErrMsg = "客户审批发起异常,Err:" + err.Error()
  401. return
  402. }
  403. br.Ret = 200
  404. br.Success = true
  405. br.Msg = "申请成功"
  406. }
  407. // ApplyTurnPositive
  408. // @Title 申请转正
  409. // @Description 申请转正接口
  410. // @Param request body company.CompanyApplyTurnPositiveReq true "type json string"
  411. // @Success Ret=200 申请成功
  412. // @router /apply/turn/positive [post]
  413. func (this *CompanyApplyController) ApplyTurnPositive() {
  414. br := new(models.BaseResponse).Init()
  415. defer func() {
  416. this.Data["json"] = br
  417. this.ServeJSON()
  418. }()
  419. sysUser := this.SysUser
  420. if sysUser == nil {
  421. br.Msg = "请登录"
  422. br.ErrMsg = "请登录,SysUser Is Empty"
  423. br.Ret = 408
  424. return
  425. }
  426. var req company.CompanyApplyTurnPositiveReq
  427. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  428. if err != nil {
  429. br.Msg = "参数解析异常!"
  430. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  431. return
  432. }
  433. if req.CompanyId <= 0 {
  434. br.Msg = "参数错误"
  435. br.ErrMsg = "参数错误,客户id小于等于0"
  436. return
  437. }
  438. if req.StartDate == "" {
  439. br.Msg = "请选择合同开始日期"
  440. return
  441. }
  442. if req.EndDate == "" {
  443. br.Msg = "请选择合同结束日期"
  444. return
  445. }
  446. if req.Money <= 0 {
  447. br.Msg = "合同金额不能小于等于0"
  448. return
  449. }
  450. if req.PermissionIds == "" {
  451. br.Msg = "请选择权限"
  452. return
  453. }
  454. roleTypeCode := sysUser.RoleTypeCode
  455. productId := services.GetProductId(roleTypeCode)
  456. if productId == 0 {
  457. br.Msg = "参数错误"
  458. br.ErrMsg = "客户类型为:" + roleTypeCode
  459. return
  460. }
  461. //添加缓存,避免多次点击提交
  462. {
  463. deleteCache := true
  464. cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
  465. defer func() {
  466. if deleteCache {
  467. utils.Rc.Delete(cacheKey)
  468. }
  469. }()
  470. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  471. deleteCache = false
  472. br.Msg = "系统处理中,请勿重复操作"
  473. br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  474. return
  475. }
  476. }
  477. companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
  478. if err != nil {
  479. br.Msg = "获取客户信息失败"
  480. br.ErrMsg = "获取客户信息失败"
  481. return
  482. }
  483. if companyProduct == nil {
  484. br.Msg = "获取客户信息失败"
  485. br.ErrMsg = "客户不存在,不可提交审批:companyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
  486. return
  487. }
  488. if companyProduct.Status != utils.COMPANY_STATUS_TRY_OUT && companyProduct.InitStatus != utils.COMPANY_STATUS_FOREVER {
  489. br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行转正申请"
  490. br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行转正申请"
  491. return
  492. }
  493. mapPermissionIdUpgrade := make(map[int]int) // 权益主客观、行业升级、买方研选的处理
  494. permissionNameMap := make(map[int]string) // 权益行业名称处理
  495. permissionNames := req.PermissionNames
  496. expensiveYx := 0
  497. if productId == 2 {
  498. // 给升级的产业ID加一个极大值判断处理
  499. permissionSlice := strings.Split(req.PermissionIds, ",")
  500. mapPermissionId := make(map[int]int)
  501. //var permissionIdsNew string
  502. var permissionIdOnly int //处理普通权限与升级权限同时勾选的时候的拦截
  503. expId := utils.PERMISSION_ID_YANXUAN_DIFF + utils.CHART_PERMISSION_ID_YANXUAN
  504. // 获取行业主观对应的客观ID
  505. subObjMap, e := services.GetRaiSubObjPermissionIdMap()
  506. if e != nil {
  507. br.Msg = "操作失败"
  508. br.ErrMsg = "获取行业主观对应的客观ID失败, Err: " + e.Error()
  509. return
  510. }
  511. strPermissionIdArr := make([]string, 0) // 最终过滤后的权限IDs
  512. for _, v := range permissionSlice {
  513. permissionId, _ := strconv.Atoi(v)
  514. // 研选5w的处理
  515. if permissionId == expId {
  516. //permissionIdsNew += strconv.Itoa(utils.CHART_PERMISSION_ID_YANXUAN) + ","
  517. strPermissionIdArr = append(strPermissionIdArr, strconv.Itoa(utils.CHART_PERMISSION_ID_YANXUAN))
  518. expensiveYx = 1
  519. continue
  520. }
  521. // 研选10w的处理
  522. if permissionId == utils.PERMISSION_ID_YANXUAN_10W_DIFF {
  523. //permissionIdsNew += strconv.Itoa(utils.CHART_PERMISSION_ID_YANXUAN) + ","
  524. strPermissionIdArr = append(strPermissionIdArr, strconv.Itoa(utils.CHART_PERMISSION_ID_YANXUAN))
  525. expensiveYx = 2
  526. continue
  527. }
  528. if permissionId > utils.PERMISSION_ID_UPGRADE {
  529. //permissionIdsNew += strconv.Itoa(permissionId-utils.PERMISSION_ID_UPGRADE) + ","
  530. permissionIdOnly = permissionId - utils.PERMISSION_ID_UPGRADE
  531. // 升级权限, 需要加入客观的权限
  532. subId := permissionId - utils.PERMISSION_ID_UPGRADE
  533. strPermissionIdArr = append(strPermissionIdArr, strconv.Itoa(subId))
  534. if subObjMap[subId] > 0 {
  535. strPermissionIdArr = append(strPermissionIdArr, strconv.Itoa(subObjMap[subId]))
  536. }
  537. } else {
  538. //permissionIdsNew += strconv.Itoa(permissionId) + ","
  539. strPermissionIdArr = append(strPermissionIdArr, v)
  540. permissionIdOnly = permissionId
  541. }
  542. //权益的永续客户主客观权限都开
  543. if companyProduct.InitStatus == utils.COMPANY_STATUS_FOREVER && subObjMap[permissionId] > 0 {
  544. strPermissionIdArr = append(strPermissionIdArr, strconv.Itoa(subObjMap[permissionId]))
  545. }
  546. if _, ok := mapPermissionId[permissionIdOnly]; ok {
  547. br.Msg = "权限设置有误,请重新选择"
  548. br.ErrMsg = "升级权限与普通权限重复选择!" + req.PermissionIds
  549. return
  550. }
  551. mapPermissionId[permissionIdOnly] = permissionIdOnly
  552. }
  553. req.PermissionIds = strings.Join(strPermissionIdArr, ",")
  554. //req.PermissionIds = strings.TrimRight(permissionIdsNew, ",")
  555. // 是否为权益内部人员, 非权益内部人员只能勾选行业不能勾选单独的主客观, 需要通过主观的ID去查客观的ID
  556. //isRai, e := services.CheckRaiAdmin(sysUser.AdminId)
  557. //if e != nil {
  558. // br.Msg = "获取失败"
  559. // br.ErrMsg = "判断是否为权益内部人员失败, GetRaiAdmin Err: " + e.Error()
  560. // return
  561. //}
  562. //if !isRai {
  563. //permissionStr, err := company.GetPermissionIdById(req.PermissionIds)
  564. //if err != nil {
  565. // br.Msg = "获取信息失败"
  566. // br.ErrMsg = "获取主观客观权限信息失败,Err:" + err.Error()
  567. // return
  568. //}
  569. //req.PermissionIds = permissionStr
  570. //}
  571. // 升级map
  572. if permissionNames != "" {
  573. mapPermissionIdUpgrade, err = services.PermissionIdUpgrade(permissionNames)
  574. if err != nil {
  575. br.Msg = "获取信息失败"
  576. br.ErrMsg = "处理升级信息失败,Err:" + err.Error()
  577. return
  578. }
  579. }
  580. permissionNameMap, err = services.GetPermissionNameMap()
  581. if err != nil {
  582. br.Msg = "新增合同失败"
  583. br.ErrMsg = "获取权限名称失败,Err:" + err.Error()
  584. return
  585. }
  586. }
  587. //客户归属判断
  588. if companyProduct.SellerId != sysUser.AdminId && companyProduct.ShareSellerId != sysUser.AdminId {
  589. br.Msg = "客户信息异常"
  590. br.ErrMsg = "客户信息异常,不是当前所属销售,客户所属销售:" + companyProduct.SellerName + ";当前销售:" + sysUser.AdminName
  591. return
  592. }
  593. if companyProduct.ApproveStatus == "待审批" {
  594. br.Msg = "存在待审批订单,不可重复提交"
  595. return
  596. }
  597. approveContent := `试用转正式`
  598. var approveUserId int
  599. var approveUserRealName string
  600. approveRoleTypeCode := ""
  601. if productId == 1 {
  602. approveRoleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
  603. } else {
  604. approveRoleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
  605. }
  606. approveUser, err := company.GetApproveUser(approveRoleTypeCode)
  607. if err != nil && err.Error() != utils.ErrNoRow() {
  608. br.Msg = "获取审批信息失败"
  609. br.ErrMsg = "获取审批人信息失败,Err:" + err.Error()
  610. return
  611. }
  612. if approveUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  613. br.Msg = "审批用户不存在"
  614. br.ErrMsg = "审批用户不存在"
  615. return
  616. }
  617. // 合同类型
  618. contractType := req.ContractType
  619. var raicontractType string // 权益合同类型
  620. {
  621. if contractType == "" {
  622. contractType = "新签合同"
  623. }
  624. var condition string
  625. var pars []interface{}
  626. condition += " AND product_id = ?"
  627. pars = append(pars, productId)
  628. condition += " AND status = ?"
  629. pars = append(pars, 1)
  630. list, e := company.GetCompanyContractListByCompanyId(req.CompanyId, condition, pars)
  631. if e != nil {
  632. br.Msg = "获取合同信息失败"
  633. br.ErrMsg = "获取合同信息失败,Err:" + e.Error()
  634. return
  635. }
  636. if len(list) > 0 {
  637. contractType = "续约合同"
  638. }
  639. if productId == 2 {
  640. //权益的新签合同重新定义,如果合同起始时间在新签合同起始日期 1年之内的,仍为新签合同。
  641. raicontractType = contractType
  642. if raicontractType != "新签合同" && companyProduct.Status != utils.COMPANY_STATUS_FOREVER {
  643. //startDateTime, _ := time.Parse(utils.FormatDate, req.StartDate)
  644. //startDateTime = startDateTime.AddDate(-1, 0, 0)
  645. totalLastYear, err := contractService.GetCompanyContractCountRaiByLastYear(req.CompanyId, req.StartDate)
  646. if err != nil {
  647. br.Msg = "获取合同信息失败"
  648. br.ErrMsg = "获取合同信息失败,Err:" + err.Error()
  649. return
  650. }
  651. if totalLastYear > 0 {
  652. raicontractType = "新签合同"
  653. }
  654. }
  655. }
  656. // 若上面的判断不是续约合同, 则继续查询是否存在正式转试用的记录,如果存在,那么合同类型是续约合同
  657. if contractType != "续约合同" {
  658. total, e := company.GetCompanyProductOperationRecordCount(companyProduct.CompanyId, companyProduct.ProductId)
  659. if e != nil {
  660. br.Msg = "查询用户信息异常"
  661. br.ErrMsg = "查询用户变更记录异常, GetCompanyProductOperationRecordCount err: " + e.Error()
  662. return
  663. }
  664. if total > 0 {
  665. contractType = "续约合同"
  666. }
  667. }
  668. }
  669. //权益合同类型,有一套单独的逻辑
  670. if productId == 2 {
  671. contractType = raicontractType
  672. }
  673. var companyApprovalId int
  674. // 全新的申请转正
  675. if req.CompanyApprovalId <= 0 {
  676. contractCode, err := company.GetCompanyContractCode()
  677. if err != nil {
  678. br.Msg = "生成合同编码失败"
  679. br.ErrMsg = "生成合同编码失败,Err:" + err.Error()
  680. return
  681. }
  682. //如果是权益的合同,合同编号另 取
  683. var sealId int
  684. if productId == 2 {
  685. contractCode, err = seal.GetSealCodeRai() //权益客户合同编号格式替换
  686. if err != nil {
  687. br.Msg = "生成合同编码失败"
  688. br.ErrMsg = "生成合同编码失败,Err:" + err.Error()
  689. return
  690. }
  691. sealCount, err := seal.GetCountByDateAndCompanyId(req.StartDate, req.EndDate, req.CompanyId)
  692. if err != nil {
  693. br.Msg = "生成合同编码失败"
  694. br.ErrMsg = "获取用印合同数量信息失败,Err:" + err.Error()
  695. return
  696. }
  697. crmConfig, err := company.GetConfigDetailByCode("rai_seal_check") //权益非标合同是否校验用印提交
  698. if err != nil {
  699. br.Msg = "获取配置失败"
  700. br.ErrMsg = "获取配置失败,Err:" + err.Error()
  701. return
  702. }
  703. if sealCount == 0 && crmConfig.ConfigValue == "1" {
  704. br.Msg = "此合同暂未申请用印,请核对合同期限是否有误"
  705. return
  706. }
  707. if sealCount > 0 {
  708. sealInfo, err := seal.GetSealInfoByDateAndCompanyId(req.StartDate, req.EndDate, req.CompanyId)
  709. if err != nil {
  710. br.Msg = "生成合同编码失败"
  711. br.ErrMsg = "获取用印合同信息失败,Err:" + err.Error()
  712. return
  713. }
  714. contractCode = sealInfo.Code
  715. sealId = sealInfo.SealId
  716. }
  717. }
  718. //当前是否存在待审批的单子
  719. nowCompanyApprovalInfo, err := company.GetCompanyApprovalById(req.CompanyId, productId)
  720. if (err == nil || nowCompanyApprovalInfo != nil) || err != nil && err.Error() != utils.ErrNoRow() {
  721. br.Msg = "此客户已存在待审批的申请单,请在申请单审批完成后重新提交"
  722. br.ErrMsg = "此客户已存在待审批的申请单,请在申请单审批完成后重新提交"
  723. return
  724. }
  725. //当前待审批的合同,有且只能存在一条
  726. //count, err := company.GetCompanyContractApproveCount(req.CompanyId, productId)
  727. //if err != nil {
  728. // br.Msg = "获取信息失败"
  729. // br.ErrMsg = "判断是否已存在待审批合同失败,Err:" + err.Error()
  730. // return
  731. //}
  732. //if count > 0 {
  733. // br.Msg = "此客户已提交过申请,请在客户审批列表重新提交"
  734. // return
  735. //}
  736. contract := new(company.CompanyContract)
  737. contract.CompanyId = req.CompanyId
  738. contract.ProductId = productId
  739. contract.ContractType = contractType
  740. contract.RaiContractType = raicontractType
  741. contract.ProductName = companyProduct.ProductName
  742. contract.CompanyProductId = companyProduct.CompanyProductId
  743. contract.ContractCode = contractCode
  744. contract.StartDate = req.StartDate
  745. contract.EndDate = req.EndDate
  746. contract.Money = req.Money
  747. contract.PayMethod = req.PayMethod
  748. contract.PayChannel = req.PayChannel
  749. contract.ImgUrl = req.ImgUrl
  750. contract.Status = 0
  751. contract.Source = "上传附件"
  752. contract.PackageType = req.PackageType
  753. contract.RaiPackageType = req.RaiPackageType
  754. contract.CreateTime = time.Now()
  755. contract.ModifyTime = time.Now()
  756. contract.SellerIdInit = companyProduct.SellerId
  757. contract.SellerNameInit = companyProduct.SellerName
  758. contract.ShareSellerInit = companyProduct.ShareSeller
  759. contract.ShareSellerIdInit = companyProduct.ShareSellerId
  760. contract.SellerIdLast = companyProduct.SellerId
  761. contract.SellerNameLast = companyProduct.SellerName
  762. contract.ShareSellerLast = companyProduct.ShareSeller
  763. contract.ShareSellerIdLast = companyProduct.ShareSellerId
  764. newId, err := company.AddCompanyContract(contract)
  765. if err != nil {
  766. br.Msg = "新增合同失败"
  767. br.ErrMsg = "新增合同失败,Err:" + err.Error()
  768. return
  769. }
  770. //如果用印ID > 0 建立合同ID,与用印申请表的关系
  771. if sealId > 0 {
  772. //用印作废
  773. err = seal.UpdateSealCompanyContractId(int(newId), sealId)
  774. if err != nil {
  775. br.Msg = "操作失败"
  776. br.ErrMsg = "添加用印与合同的关系失败,Err:" + err.Error()
  777. return
  778. }
  779. }
  780. permissionArr := strings.Split(req.PermissionIds, ",")
  781. permissionMap := make(map[int]int) //权限id的map集合,用来避免重复添加权限校验
  782. permissionItems := make([]*company.CompanyContractPermission, 0)
  783. permissionLogs := make([]*company.CompanyContractPermissionLog, 0)
  784. for _, v := range permissionArr {
  785. item := new(company.CompanyContractPermission)
  786. item.CompanyId = req.CompanyId
  787. item.CompanyContractId = int(newId)
  788. chartPermissionId, tmpErr := strconv.Atoi(v)
  789. if tmpErr != nil {
  790. err = tmpErr
  791. return
  792. }
  793. //权限id判断,避免重复添加权限
  794. if _, ok := permissionMap[chartPermissionId]; ok {
  795. continue
  796. }
  797. permissionMap[chartPermissionId] = chartPermissionId
  798. item.ChartPermissionId = chartPermissionId
  799. if _, ok := mapPermissionIdUpgrade[chartPermissionId]; ok {
  800. item.IsUpgrade = 1
  801. }
  802. if chartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN {
  803. item.ExpensiveYx = expensiveYx
  804. }
  805. //如果是研选扣点包,这个行业权限的ID;那么就对点数进行赋值。目前就研选扣点包 这一个行业有点数,先不用数组类型处理。v:查研观向13.2
  806. if chartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
  807. if req.Points <= 0 {
  808. br.Msg = "点数不能为空"
  809. return
  810. }
  811. item.Points = req.Points
  812. }
  813. item.PermissionName = permissionNameMap[chartPermissionId]
  814. item.StartDate = req.StartDate
  815. item.EndDate = req.EndDate
  816. item.ModifyTime = time.Now()
  817. item.CreateTime = time.Now()
  818. permissionItems = append(permissionItems, item)
  819. //新增日志
  820. itemLog := new(company.CompanyContractPermissionLog)
  821. itemLog.CompanyId = req.CompanyId
  822. itemLog.CompanyContractId = int(newId)
  823. itemLog.ChartPermissionId = v
  824. itemLog.StartDate = req.StartDate
  825. itemLog.EndDate = req.EndDate
  826. itemLog.ModifyTime = time.Now()
  827. itemLog.CreateTime = time.Now()
  828. itemLog.LogCreateTime = time.Now()
  829. permissionLogs = append(permissionLogs, itemLog)
  830. }
  831. // 批量新增company_contract_permission和log
  832. if e := company.MultiAddCompanyContractPermission(permissionItems); e != nil {
  833. br.Msg = "新增合同失败"
  834. br.ErrMsg = "批量新增合同权限失败, MultiAddCompanyContractPermission Err:" + e.Error()
  835. return
  836. }
  837. go company.MultiAddCompanyContractPermissionLog(permissionLogs)
  838. applyMethod := 1
  839. approval := new(company.CompanyApproval)
  840. approval.CompanyId = req.CompanyId
  841. approval.ProductId = productId
  842. approval.ApproveStatus = "待审批"
  843. approval.ApproveUserId = 0
  844. approval.ApproveRoleTypeCode = approveRoleTypeCode
  845. approval.ApproveRealName = ""
  846. approval.ApproveTime = time.Now()
  847. approval.ApproveContent = "试用转正式"
  848. approval.ApplyMethod = applyMethod
  849. approval.ApplyReasons = ""
  850. approval.ApplyUserId = sysUser.AdminId
  851. approval.ApplyRealName = sysUser.RealName
  852. approval.CreateTime = time.Now()
  853. approval.ModifyTime = time.Now()
  854. approval.CompanyContractId = int(newId)
  855. approval.IsFormal = companyProduct.IsFormal
  856. companyApprovalId, err = company.AddCompanyApproval(approval)
  857. if err != nil {
  858. br.Msg = "申请失败"
  859. br.ErrMsg = "申请失败,Err:" + err.Error()
  860. return
  861. }
  862. } else {
  863. // 重新申请
  864. recode, err := company.GetCompanyApprovalByApprovalId(req.CompanyApprovalId)
  865. if err != nil {
  866. br.Msg = "申请失败"
  867. br.ErrMsg = "获取审批记录失败,Err:" + err.Error()
  868. return
  869. }
  870. err = company.ModifyCompanyTurnPositive(&req, productId, recode.CompanyContractId, companyProduct.IsFormal, mapPermissionIdUpgrade, expensiveYx)
  871. if err != nil {
  872. br.Msg = "申请失败"
  873. br.ErrMsg = "申请失败,Err:" + err.Error()
  874. return
  875. }
  876. companyApprovalId = req.CompanyApprovalId
  877. }
  878. //新增操作记录
  879. {
  880. remark := "申请转正"
  881. operation := "turn_positive"
  882. services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, approveUserId, companyProduct.CompanyName,
  883. companyProduct.ProductName, sysUser.RealName, remark, operation, approveContent, approveUserRealName, "", companyProduct.Status)
  884. }
  885. //新增客户产品日志
  886. {
  887. logType := "modify"
  888. go services.AddCompanyProductLog(companyProduct, this.SysUser.AdminId, logType)
  889. }
  890. //新增申请单
  891. err = company_apply.Apply(companyApprovalId, "试用转正式申请单")
  892. if err != nil {
  893. br.Msg = "发起审批异常,请联系管理员"
  894. br.ErrMsg = "客户审批发起异常,Err:" + err.Error()
  895. return
  896. }
  897. br.Ret = 200
  898. br.Success = true
  899. br.Msg = "申请成功"
  900. }
  901. // ApplyBySystemContract
  902. // @Title 通过系统合同申请转正/服务更新
  903. // @Description 通过系统合同申请转正/服务更新接口
  904. // @Param request body company.CompanyApplyBySystemContractReq true "type json string"
  905. // @Success Ret=200 申请成功
  906. // @router /apply/systemContract [post]
  907. func (this *CompanyApplyController) ApplyBySystemContract() {
  908. br := new(models.BaseResponse).Init()
  909. defer func() {
  910. this.Data["json"] = br
  911. this.ServeJSON()
  912. }()
  913. sysUser := this.SysUser
  914. if sysUser == nil {
  915. br.Msg = "请登录"
  916. br.ErrMsg = "请登录,SysUser Is Empty"
  917. br.Ret = 408
  918. return
  919. }
  920. var req company.CompanyApplyBySystemContractReq
  921. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  922. if err != nil {
  923. br.Msg = "参数解析异常!"
  924. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  925. return
  926. }
  927. if req.CompanyId <= 0 {
  928. br.Msg = "参数错误"
  929. br.ErrMsg = "参数错误,客户id小于等于0"
  930. return
  931. }
  932. //获取客户信息
  933. companyInfo, err := company.GetCompanyById(req.CompanyId)
  934. if err != nil {
  935. br.Msg = "获取客户信息失败"
  936. br.ErrMsg = "获取客户信息失败"
  937. return
  938. }
  939. roleTypeCode := sysUser.RoleTypeCode
  940. productId := services.GetProductId(roleTypeCode)
  941. if productId == 0 {
  942. br.Msg = "参数错误"
  943. br.ErrMsg = "客户类型为:" + roleTypeCode
  944. return
  945. }
  946. //添加缓存,避免多次点击提交
  947. {
  948. deleteCache := true
  949. cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
  950. defer func() {
  951. if deleteCache {
  952. utils.Rc.Delete(cacheKey)
  953. }
  954. }()
  955. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  956. deleteCache = false
  957. br.Msg = "系统处理中,请勿重复操作"
  958. br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  959. return
  960. }
  961. }
  962. companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
  963. if err != nil {
  964. br.Msg = "获取客户信息失败"
  965. br.ErrMsg = "获取客户信息失败"
  966. return
  967. }
  968. if companyProduct == nil {
  969. br.Msg = "获取客户信息失败"
  970. br.ErrMsg = "客户不存在,不可提交审批:companyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
  971. return
  972. }
  973. if companyProduct.SellerId != sysUser.AdminId && companyProduct.ShareSellerId != sysUser.AdminId {
  974. br.Msg = "客户信息异常"
  975. br.ErrMsg = "客户信息异常,不是当前所属销售,客户所属销售:" + companyProduct.SellerName + ";当前销售:" + sysUser.AdminName
  976. return
  977. }
  978. if companyProduct.ApproveStatus == "待审批" {
  979. br.Msg = "存在待审批订单,不可重复提交"
  980. return
  981. }
  982. var approveUserId int
  983. var approveUserRealName string
  984. approveRoleTypeCode := ""
  985. if productId == 1 {
  986. approveRoleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
  987. } else {
  988. approveRoleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
  989. }
  990. approveUser, err := company.GetApproveUser(approveRoleTypeCode)
  991. if err != nil && err.Error() != utils.ErrNoRow() {
  992. br.Msg = "获取审批信息失败"
  993. br.ErrMsg = "获取审批人信息失败,Err:" + err.Error()
  994. return
  995. }
  996. if approveUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  997. br.Msg = "审批用户不存在"
  998. br.ErrMsg = "审批用户不存在"
  999. return
  1000. }
  1001. //获取合同详情
  1002. contractDetail, err := contractService.GetContractDetail(req.ContractId)
  1003. if err != nil {
  1004. br.Msg = "查询合同异常"
  1005. br.ErrMsg = fmt.Sprint("查询合同异常,合同id:", req.ContractId, ";Err:", err.Error())
  1006. return
  1007. }
  1008. if contractDetail.ContractBusinessType != "业务合同" {
  1009. br.Msg = "合同类型异常"
  1010. br.ErrMsg = "合同类型异常,当前合同类型为:" + contractDetail.ContractBusinessType
  1011. return
  1012. }
  1013. //判断产品类型是否一致
  1014. if productId != contractDetail.ProductId {
  1015. br.Msg = "合同产品类型异常"
  1016. br.ErrMsg = fmt.Sprint("合同产品类型异常,合同产品类型Id:", contractDetail.ProductId)
  1017. return
  1018. }
  1019. //判断销售归属
  1020. if contractDetail.SellerId != sysUser.AdminId {
  1021. br.Msg = "合同异常"
  1022. br.ErrMsg = fmt.Sprint("合同归属人异常,合同申请人Id:", contractDetail.SellerId, ";当前账号:", sysUser.AdminId)
  1023. return
  1024. }
  1025. //判断合同是否该客户的(组织社会信用代码是否一致)
  1026. if contractDetail.CreditCode != companyInfo.CreditCode {
  1027. br.Msg = "合同异常,请选择社会信用码一致的合同"
  1028. br.ErrMsg = fmt.Sprint("该合同组织社会信用码与客户不符,合同的社会信用码:", contractDetail.CreditCode, ";客户的社会信用码:", companyInfo.CreditCode)
  1029. return
  1030. }
  1031. if contractDetail.Status != "已签回" {
  1032. br.Msg = "合同状态异常"
  1033. br.ErrMsg = fmt.Sprint("合同状态异常,合同状态:", contractDetail.Status)
  1034. return
  1035. }
  1036. //当前客户需要的合同状态
  1037. needContractType, err := services.GetApplyTurnContractType(req.CompanyId, productId)
  1038. if err != nil {
  1039. br.Msg = "获取失败"
  1040. br.ErrMsg = "获取合同类型失败,Err:" + err.Error()
  1041. return
  1042. }
  1043. //判断 系统需要的合同类型 与 实际提交的合同类型是否一致,如果不一致,那么不支持
  1044. if needContractType != "新签合同" && contractDetail.ContractType == "新签合同" {
  1045. br.Msg = "申请失败,该客户曾经开通过正式服务,不允许签署新签合同"
  1046. br.ErrMsg = "申请失败,该客户曾经开通过正式服务,不允许签署新签合同"
  1047. return
  1048. }
  1049. if contractDetail.ContractType == "新签合同" {
  1050. if companyProduct.Status != utils.COMPANY_STATUS_TRY_OUT {
  1051. br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行转正申请"
  1052. br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行转正申请"
  1053. return
  1054. }
  1055. } else {
  1056. if companyProduct.Status != utils.COMPANY_STATUS_FORMAL && companyProduct.Status != utils.COMPANY_STATUS_TRY_OUT {
  1057. br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行服务更新申请"
  1058. br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行服务更新申请"
  1059. return
  1060. }
  1061. }
  1062. //合同类型
  1063. contractType := contractDetail.ContractType
  1064. var hasContract bool //是否已经存在合同,如果存在,那么就不能是新签合同
  1065. var condition string
  1066. var pars []interface{}
  1067. condition += " AND product_id = ?"
  1068. pars = append(pars, productId)
  1069. condition += " AND status = ?"
  1070. pars = append(pars, 1)
  1071. list, err := company.GetCompanyContractListByCompanyId(req.CompanyId, condition, pars)
  1072. if err != nil {
  1073. br.Msg = "获取合同信息失败"
  1074. br.ErrMsg = "获取合同信息失败,Err:" + err.Error()
  1075. return
  1076. }
  1077. if len(list) > 0 {
  1078. hasContract = true
  1079. }
  1080. //查询是否存在正式转试用的记录,如果存在,那么合同类型是续约合同
  1081. total, err := company.GetCompanyProductOperationRecordCount(companyProduct.CompanyId, companyProduct.ProductId)
  1082. if err != nil {
  1083. br.Msg = "查询用户信息异常"
  1084. br.ErrMsg = "查询用户变更记录异常"
  1085. return
  1086. }
  1087. if total > 0 {
  1088. hasContract = true
  1089. }
  1090. if hasContract && contractType == "新签合同" {
  1091. br.Msg = "客户已存在老合同,不允许签署新签合同"
  1092. br.ErrMsg = "客户已存在老合同,不允许签署新签合同"
  1093. return
  1094. }
  1095. //合同类型 end
  1096. //获取权限id列表
  1097. permissionMap, err := contractService.GetServicePermissionMap(contractDetail.Service)
  1098. permissionArr := make([]int, 0)
  1099. for _, permissionId := range permissionMap {
  1100. permissionArr = append(permissionArr, permissionId)
  1101. }
  1102. //对权限排序
  1103. sort.Ints(permissionArr)
  1104. // CRM8.8-权益主客观此处拆分成了三级套餐, 根据contract_service写权限
  1105. // CRM8.8-如果是权益客户那么主观客观权限一起带上(暂弃)
  1106. //if companyProduct.ProductId == 2 {
  1107. // var permissionIds string
  1108. // var permissionNames string
  1109. // for _, v := range permissionArr {
  1110. // permissionIds += strconv.Itoa(v) + ","
  1111. // }
  1112. // permissionIds = strings.TrimRight(permissionIds, ",")
  1113. // listSandO, err := company.GetPermissionLookItemsSandO(permissionIds)
  1114. // if err != nil {
  1115. // br.Msg = "获取现有的合同权限信息失败"
  1116. // br.ErrMsg = "获取现有的合同权限ID信息失败,Err:" + err.Error()
  1117. // return
  1118. // }
  1119. // for _, v := range listSandO {
  1120. // permissionNames += "'" + v.PermissionName + "'" + ","
  1121. // }
  1122. // permissionNames = strings.TrimRight(permissionNames, ",")
  1123. // listPermissionByname, err := company.GetPermissionLookItemsSandOByName(permissionNames)
  1124. // if err != nil {
  1125. // br.Msg = "获取现有的合同权限信息失败"
  1126. // br.ErrMsg = "通过名称获取主观客观权限ID信息失败,Err:" + err.Error()
  1127. // return
  1128. // }
  1129. // permissionArr = make([]int, 0)
  1130. // for _, v := range listPermissionByname {
  1131. // permissionArr = append(permissionArr, v.ChartPermissionId)
  1132. // }
  1133. // //对权限排序
  1134. // sort.Ints(permissionArr)
  1135. //}
  1136. //当前已经存在的权限
  1137. currPermissionMap, err := services.GetLastContractPermissionList(companyProduct.CompanyId, companyProduct.ProductId)
  1138. if err != nil {
  1139. br.Msg = "获取现有的合同权限信息失败"
  1140. br.ErrMsg = "获取现有的合同权限信息失败,Err:" + err.Error()
  1141. return
  1142. }
  1143. //权限内日期判断校验
  1144. for _, permissionId := range permissionArr {
  1145. //研选服务不做校验
  1146. if permissionId == 31 {
  1147. continue
  1148. }
  1149. if currPermission, ok := currPermissionMap[permissionId]; ok {
  1150. currStartDate, _ := time.Parse(utils.FormatDate, currPermission.StartDate)
  1151. currEndDate, _ := time.Parse(utils.FormatDate, currPermission.EndDate)
  1152. if !(contractDetail.StartDate.After(currEndDate) || currStartDate.After(contractDetail.EndDate)) {
  1153. br.Msg = currPermission.PermissionName + "合同日期存在重复,不允许申请"
  1154. br.ErrMsg = "权限合同日期存在重复,不允许申请"
  1155. return
  1156. }
  1157. }
  1158. }
  1159. //获取客户产品套餐类型(大套餐、小套餐、无)
  1160. packageType, err := contractService.GetServicePackageType(contractDetail.Service)
  1161. raiPackageType := contractService.GetRaiServicePackageType(contractDetail.Service)
  1162. recordApproveContent := `试用转正式`
  1163. applyMethod := 1
  1164. approveContent := "试用转正式"
  1165. operationRemark := `申请转正`
  1166. operation := "turn_positive"
  1167. messageContent := "试用转正式申请单"
  1168. //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议
  1169. if contractType != "新签合同" {
  1170. if contractType == "补充协议" {
  1171. applyMethod = 6 //补充协议
  1172. recordApproveContent = `正式客户新增补充协议`
  1173. approveContent = `新增补充协议`
  1174. operationRemark = `新增补充协议`
  1175. operation = "add_agreement"
  1176. messageContent = "补充协议申请单"
  1177. } else {
  1178. applyMethod = 5 //续约服务
  1179. recordApproveContent = `正式客户申请续约`
  1180. approveContent = `申请续约`
  1181. operationRemark = `申请续约`
  1182. operation = "service_update"
  1183. messageContent = "续约申请单"
  1184. }
  1185. }
  1186. var raicontractType string
  1187. if productId == 2 {
  1188. //权益的新签合同重新定义,如果合同起始时间在新签合同起始日期 1年之内的,仍为新签合同。
  1189. raicontractType = contractType
  1190. if raicontractType != "新签合同" && companyProduct.Status != utils.COMPANY_STATUS_FOREVER {
  1191. //startDateTime := contractDetail.StartDate.AddDate(-1, 0, 0)
  1192. totalLastYear, err := contractService.GetCompanyContractCountRaiByLastYear(req.CompanyId, contractDetail.StartDate.Format(utils.FormatDate))
  1193. if err != nil {
  1194. br.Msg = "获取合同信息失败"
  1195. br.ErrMsg = "获取合同信息失败,Err:" + err.Error()
  1196. return
  1197. }
  1198. if totalLastYear > 0 {
  1199. raicontractType = "新签合同"
  1200. }
  1201. }
  1202. }
  1203. var companyApprovalId int
  1204. if req.CompanyApprovalId <= 0 {
  1205. //当前是否存在待审批的单子
  1206. nowCompanyApprovalInfo, err := company.GetCompanyApprovalById(req.CompanyId, productId)
  1207. if (err == nil || nowCompanyApprovalInfo != nil) || err != nil && err.Error() != utils.ErrNoRow() {
  1208. br.Msg = "此客户已存在待审批的申请单,请在申请单审批完成后重新提交"
  1209. br.ErrMsg = "此客户已存在待审批的申请单,请在申请单审批完成后重新提交"
  1210. return
  1211. }
  1212. ////当前待审批的合同,有且只能存在一条
  1213. //count, err := company.GetCompanyContractApproveCount(req.CompanyId, productId)
  1214. //if err != nil {
  1215. // br.Msg = "获取信息失败"
  1216. // br.ErrMsg = "判断是否已存在待审批合同失败,Err:" + err.Error()
  1217. // return
  1218. //}
  1219. //if count > 0 {
  1220. // br.Msg = "此客户已提交过申请,请在客户审批列表重新提交"
  1221. // return
  1222. //}
  1223. companyContract := &company.CompanyContract{
  1224. CompanyId: req.CompanyId,
  1225. ProductId: contractDetail.ProductId,
  1226. ContractType: contractType,
  1227. RaiContractType: raicontractType,
  1228. ProductName: companyProduct.ProductName,
  1229. CompanyProductId: companyProduct.CompanyProductId,
  1230. ContractCode: contractDetail.ContractCode,
  1231. StartDate: contractDetail.StartDate.Format(utils.FormatDate),
  1232. EndDate: contractDetail.EndDate.Format(utils.FormatDate),
  1233. Money: contractDetail.Price,
  1234. PayMethod: contractDetail.PayRemark,
  1235. PayChannel: contractDetail.PayChannel,
  1236. ImgUrl: contractDetail.CheckBackFileUrl,
  1237. Status: 0,
  1238. Source: "系统合同",
  1239. PackageType: packageType,
  1240. RaiPackageType: raiPackageType,
  1241. CreateTime: time.Now(),
  1242. ModifyTime: time.Now(),
  1243. SellerIdInit: companyProduct.SellerId,
  1244. SellerNameInit: companyProduct.SellerName,
  1245. ShareSellerInit: companyProduct.ShareSeller,
  1246. ShareSellerIdInit: companyProduct.ShareSellerId,
  1247. SellerIdLast: companyProduct.SellerId,
  1248. SellerNameLast: companyProduct.SellerName,
  1249. ShareSellerLast: companyProduct.ShareSeller,
  1250. ShareSellerIdLast: companyProduct.ShareSellerId,
  1251. }
  1252. newId, err := company.AddCompanyContract(companyContract)
  1253. if err != nil {
  1254. br.Msg = "新增合同失败"
  1255. br.ErrMsg = "新增合同失败,Err:" + err.Error()
  1256. return
  1257. }
  1258. upgradeMap := make(map[int]int)
  1259. if utils.RunMode == "release" {
  1260. upgradeMap = map[int]int{22: 39, 21: 38, 20: 37, 19: 36}
  1261. } else {
  1262. upgradeMap = map[int]int{22: 37, 21: 36, 20: 35, 19: 34}
  1263. }
  1264. for _, chartPermissionId := range permissionArr {
  1265. if chartPermissionId < utils.PERMISSION_ID_UPGRADE {
  1266. item := new(company.CompanyContractPermission)
  1267. item.CompanyId = req.CompanyId
  1268. item.CompanyContractId = int(newId)
  1269. item.ChartPermissionId = chartPermissionId
  1270. item.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
  1271. item.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
  1272. item.ModifyTime = time.Now()
  1273. item.CreateTime = time.Now()
  1274. err = company.AddCompanyContractPermission(item)
  1275. if err != nil {
  1276. br.Msg = "新增合同失败"
  1277. br.ErrMsg = "新增合同权限失败,Err:" + err.Error()
  1278. return
  1279. }
  1280. //新增日志
  1281. {
  1282. itemLog := new(company.CompanyContractPermissionLog)
  1283. itemLog.CompanyId = req.CompanyId
  1284. itemLog.CompanyContractId = int(newId)
  1285. itemLog.ChartPermissionId = strconv.Itoa(chartPermissionId)
  1286. itemLog.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
  1287. itemLog.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
  1288. itemLog.ModifyTime = time.Now()
  1289. itemLog.CreateTime = time.Now()
  1290. itemLog.LogCreateTime = time.Now()
  1291. go company.AddCompanyContractPermissionLog(itemLog)
  1292. }
  1293. } else {
  1294. // 升级套餐,手动补充主客观套餐
  1295. chartPermissionId -= utils.PERMISSION_ID_UPGRADE
  1296. item := new(company.CompanyContractPermission)
  1297. item.CompanyId = req.CompanyId
  1298. item.CompanyContractId = int(newId)
  1299. item.ChartPermissionId = chartPermissionId
  1300. item.IsUpgrade = 1
  1301. item.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
  1302. item.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
  1303. item.ModifyTime = time.Now()
  1304. item.CreateTime = time.Now()
  1305. err = company.AddCompanyContractPermission(item)
  1306. if err != nil {
  1307. br.Msg = "新增合同失败"
  1308. br.ErrMsg = "新增合同权限失败,Err:" + err.Error()
  1309. return
  1310. }
  1311. //新增日志
  1312. {
  1313. itemLog := new(company.CompanyContractPermissionLog)
  1314. itemLog.CompanyId = req.CompanyId
  1315. itemLog.CompanyContractId = int(newId)
  1316. itemLog.ChartPermissionId = strconv.Itoa(chartPermissionId)
  1317. itemLog.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
  1318. itemLog.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
  1319. itemLog.ModifyTime = time.Now()
  1320. itemLog.CreateTime = time.Now()
  1321. itemLog.LogCreateTime = time.Now()
  1322. go company.AddCompanyContractPermissionLog(itemLog)
  1323. }
  1324. // 客观
  1325. upgradeChartPermissionId := upgradeMap[chartPermissionId]
  1326. upItem := new(company.CompanyContractPermission)
  1327. upItem.CompanyId = req.CompanyId
  1328. upItem.CompanyContractId = int(newId)
  1329. upItem.ChartPermissionId = upgradeChartPermissionId
  1330. upItem.IsUpgrade = 1
  1331. upItem.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
  1332. upItem.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
  1333. upItem.ModifyTime = time.Now()
  1334. upItem.CreateTime = time.Now()
  1335. err = company.AddCompanyContractPermission(upItem)
  1336. if err != nil {
  1337. br.Msg = "新增合同失败"
  1338. br.ErrMsg = "新增合同权限失败,Err:" + err.Error()
  1339. return
  1340. }
  1341. //新增日志
  1342. {
  1343. itemLog := new(company.CompanyContractPermissionLog)
  1344. itemLog.CompanyId = req.CompanyId
  1345. itemLog.CompanyContractId = int(newId)
  1346. itemLog.ChartPermissionId = strconv.Itoa(upgradeChartPermissionId)
  1347. itemLog.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
  1348. itemLog.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
  1349. itemLog.ModifyTime = time.Now()
  1350. itemLog.CreateTime = time.Now()
  1351. itemLog.LogCreateTime = time.Now()
  1352. go company.AddCompanyContractPermissionLog(itemLog)
  1353. }
  1354. }
  1355. }
  1356. approval := new(company.CompanyApproval)
  1357. approval.CompanyId = req.CompanyId
  1358. approval.ProductId = productId
  1359. approval.ApproveStatus = "待审批"
  1360. approval.ApproveUserId = 0
  1361. approval.ApproveRoleTypeCode = approveRoleTypeCode
  1362. approval.ApproveRealName = ""
  1363. approval.ApproveTime = time.Now()
  1364. approval.ApproveContent = approveContent
  1365. approval.ApplyMethod = applyMethod
  1366. approval.ApplyReasons = ""
  1367. approval.ApplyUserId = sysUser.AdminId
  1368. approval.ApplyRealName = sysUser.RealName
  1369. approval.CreateTime = time.Now()
  1370. approval.ModifyTime = time.Now()
  1371. approval.CompanyContractId = int(newId)
  1372. approval.IsFormal = companyProduct.IsFormal
  1373. companyApprovalId, err = company.AddCompanyApproval(approval)
  1374. if err != nil {
  1375. br.Msg = "申请失败"
  1376. br.ErrMsg = "申请失败,Err:" + err.Error()
  1377. return
  1378. }
  1379. } else {
  1380. recode, err := company.GetCompanyApprovalByApprovalId(req.CompanyApprovalId)
  1381. if err != nil {
  1382. br.Msg = "申请失败"
  1383. br.ErrMsg = "获取审批记录失败,Err:" + err.Error()
  1384. return
  1385. }
  1386. err = company.ModifyCompanyTurnPositiveBySystemContract(contractDetail, permissionArr, req.CompanyId, productId, req.CompanyApprovalId, recode.CompanyContractId, companyProduct.IsFormal, packageType, raiPackageType)
  1387. if err != nil {
  1388. br.Msg = "申请失败"
  1389. br.ErrMsg = "申请失败,Err:" + err.Error()
  1390. return
  1391. }
  1392. companyApprovalId = req.CompanyApprovalId
  1393. }
  1394. //新增操作记录
  1395. {
  1396. services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, approveUserId, companyProduct.CompanyName,
  1397. companyProduct.ProductName, sysUser.RealName, operationRemark, operation, recordApproveContent, approveUserRealName, "", companyProduct.Status)
  1398. }
  1399. //新增客户产品日志
  1400. {
  1401. logType := "modify"
  1402. go services.AddCompanyProductLog(companyProduct, this.SysUser.AdminId, logType)
  1403. }
  1404. err = company_apply.Apply(companyApprovalId, messageContent)
  1405. if err != nil {
  1406. br.Msg = "发起审批异常,请联系管理员"
  1407. br.ErrMsg = "客户审批发起异常,Err:" + err.Error()
  1408. return
  1409. }
  1410. //待办通知
  1411. //{
  1412. // for _, v := range approveUser {
  1413. // approveUserId = v.AdminId
  1414. // approveUserRealName = v.RealName
  1415. // mobile := v.Mobile
  1416. // err = services.AddCompanyApprovalMessage(sysUser.AdminId, approveUserId, req.CompanyId, companyApprovalId, 1, 1, 1, companyProduct.CompanyName, messageContent, messageContent, mobile)
  1417. // if err != nil {
  1418. // br.Msg = "申请失败"
  1419. // br.ErrMsg = "新增待办通知失败,Err:" + err.Error()
  1420. // return
  1421. // }
  1422. // }
  1423. //}
  1424. br.Ret = 200
  1425. br.Success = true
  1426. br.Msg = "申请成功"
  1427. }
  1428. // ApplyThaw
  1429. // @Title 申请解冻
  1430. // @Description 申请解冻接口
  1431. // @Param request body company.CompanyApplyThawReq true "type json string"
  1432. // @Success Ret=200 申请成功
  1433. // @router /apply/thaw [post]
  1434. func (this *CompanyApplyController) ApplyThaw() {
  1435. br := new(models.BaseResponse).Init()
  1436. defer func() {
  1437. this.Data["json"] = br
  1438. this.ServeJSON()
  1439. }()
  1440. sysUser := this.SysUser
  1441. if sysUser == nil {
  1442. br.Msg = "请登录"
  1443. br.ErrMsg = "请登录,SysUser Is Empty"
  1444. br.Ret = 408
  1445. return
  1446. }
  1447. var req company.CompanyApplyThawReq
  1448. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1449. if err != nil {
  1450. br.Msg = "参数解析异常!"
  1451. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1452. return
  1453. }
  1454. if req.CompanyId <= 0 {
  1455. br.Msg = "参数错误"
  1456. br.ErrMsg = "参数错误,客户id小于等于0"
  1457. return
  1458. }
  1459. roleTypeCode := sysUser.RoleTypeCode
  1460. productId := services.GetProductId(roleTypeCode)
  1461. if productId == 0 {
  1462. br.Msg = "参数错误"
  1463. br.ErrMsg = "客户类型为:" + roleTypeCode
  1464. return
  1465. }
  1466. if productId == utils.COMPANY_PRODUCT_FICC_ID && req.PermissionIds == "" {
  1467. br.Msg = "请勾选需要解冻的品种"
  1468. br.ErrMsg = "请勾选需要解冻的品种"
  1469. return
  1470. }
  1471. // 查询当前销售是否有重新申请的权限
  1472. receiveEnabled, _ := services.CheckCompanyReceiveButton(sysUser.AdminId)
  1473. if !receiveEnabled {
  1474. br.Msg = "无权申请"
  1475. br.ErrMsg = "无权申请"
  1476. return
  1477. }
  1478. //添加缓存,避免多次点击提交
  1479. {
  1480. deleteCache := true
  1481. cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
  1482. defer func() {
  1483. if deleteCache {
  1484. utils.Rc.Delete(cacheKey)
  1485. }
  1486. }()
  1487. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1488. deleteCache = false
  1489. br.Msg = "系统处理中,请勿重复操作"
  1490. br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  1491. return
  1492. }
  1493. }
  1494. var companyApprovalCount int //某一客户申请的次数
  1495. approveRoleTypeCode := ""
  1496. if productId == 1 {
  1497. approveRoleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
  1498. } else {
  1499. approveRoleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
  1500. companyApprovalCount, err = company.GetCompanyApprovalCount(req.CompanyId, productId, 2)
  1501. if err != nil {
  1502. br.Msg = "获取客户信息失败"
  1503. br.ErrMsg = "获取客户申请的次数失败,Err:" + err.Error()
  1504. return
  1505. }
  1506. permissionArr := strings.Split(req.PermissionIds, ",")
  1507. //如果所选行业不属于研选订阅跟研选扣点包,那就是非研选试用客户
  1508. var isNoResearch bool
  1509. for _, v := range permissionArr {
  1510. if v != strconv.Itoa(utils.CHART_PERMISSION_ID_YANXUAN) && v != strconv.Itoa(utils.YAN_XUAN_KOU_DIAN_BAO_ID) {
  1511. isNoResearch = true
  1512. continue
  1513. }
  1514. }
  1515. if isNoResearch {
  1516. tryOutCountRai, err := company.GetRaiCompanyTryOutCountNoResearch(sysUser.AdminId)
  1517. if err != nil {
  1518. br.Msg = "判断试用客户量失败"
  1519. br.ErrMsg = "判断试用客户量失败,Err:" + err.Error()
  1520. return
  1521. }
  1522. if tryOutCountRai >= utils.COMPANY_STATUS_TRY_OUT_COUNT_RAI {
  1523. br.Msg = "非研选试用客户最多40家"
  1524. return
  1525. }
  1526. }
  1527. }
  1528. companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
  1529. if err != nil {
  1530. br.Msg = "获取客户信息失败"
  1531. br.ErrMsg = "获取客户信息失败"
  1532. return
  1533. }
  1534. if companyProduct == nil {
  1535. br.Msg = "获取客户信息失败"
  1536. br.ErrMsg = "客户不存在,不可提交审批:companyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
  1537. return
  1538. }
  1539. if companyProduct.Status != utils.COMPANY_STATUS_FREEZE {
  1540. br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行解冻申请"
  1541. br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行解冻申请"
  1542. return
  1543. }
  1544. if companyProduct.ApproveStatus == utils.COMPANY_APPROVE_STATUS {
  1545. br.Msg = "存在待审批订单,不可重复提交"
  1546. return
  1547. }
  1548. //客户归属判断
  1549. if companyProduct.SellerId != sysUser.AdminId && companyProduct.ShareSellerId != sysUser.AdminId {
  1550. br.Msg = "客户信息异常"
  1551. br.ErrMsg = "客户信息异常,不是当前所属销售,客户所属销售:" + companyProduct.SellerName + ";当前销售:" + sysUser.AdminName
  1552. return
  1553. }
  1554. // CRM8.8(仅针对权益客户)-此处同申请试用的逻辑
  1555. if companyProduct.ProductId == utils.COMPANY_PRODUCT_RAI_ID {
  1556. reportPermissions, e := company.GetCompanyReportPermission(req.CompanyId, utils.COMPANY_PRODUCT_RAI_ID)
  1557. if e != nil {
  1558. br.Msg = "客户权限信息异常"
  1559. br.ErrMsg = "客户权限信息异常, Err: " + e.Error()
  1560. return
  1561. }
  1562. reportPermissionArr := make([]string, 0)
  1563. for i := range reportPermissions {
  1564. reportPermissionArr = append(reportPermissionArr, strconv.Itoa(reportPermissions[i].ChartPermissionId))
  1565. }
  1566. if len(reportPermissionArr) > 0 {
  1567. req.PermissionIds = strings.Join(reportPermissionArr, ",")
  1568. }
  1569. }
  1570. approveUser, err := company.GetApproveUser(approveRoleTypeCode)
  1571. if err != nil && err.Error() != utils.ErrNoRow() {
  1572. br.Msg = "获取审批信息失败"
  1573. br.ErrMsg = "获取审批人信息失败,Err:" + err.Error()
  1574. return
  1575. }
  1576. if approveUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  1577. br.Msg = "审批用户不存在"
  1578. br.ErrMsg = "审批用户不存在"
  1579. return
  1580. }
  1581. //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议
  1582. approveContent := `冻结转试用`
  1583. var companyApprovalId int
  1584. if req.CompanyApprovalId <= 0 {
  1585. applyMethod := 2
  1586. approval := new(company.CompanyApproval)
  1587. approval.CompanyId = req.CompanyId
  1588. approval.ProductId = productId
  1589. approval.ApproveStatus = "待审批"
  1590. approval.ApproveUserId = 0
  1591. approval.ApproveRoleTypeCode = approveRoleTypeCode
  1592. approval.ApproveRealName = ""
  1593. approval.ApproveTime = time.Now()
  1594. approval.ApproveContent = approveContent
  1595. approval.ApplyMethod = applyMethod
  1596. approval.ApplyReasons = req.ApplyRemark
  1597. approval.ApplyUserId = sysUser.AdminId
  1598. approval.ApplyRealName = sysUser.RealName
  1599. approval.CreateTime = time.Now()
  1600. approval.ModifyTime = time.Now()
  1601. companyApprovalId, err = company.AddCompanyApproval(approval)
  1602. if err != nil {
  1603. br.Msg = "申请失败"
  1604. br.ErrMsg = "申请失败,Err:" + err.Error()
  1605. return
  1606. }
  1607. } else {
  1608. err = company.ModifyCompanyApproval(req.CompanyId, productId, req.CompanyApprovalId, approveContent, req.ApplyRemark)
  1609. if err != nil {
  1610. br.Msg = "申请失败"
  1611. br.ErrMsg = "申请失败,Err:" + err.Error()
  1612. return
  1613. }
  1614. companyApprovalId = req.CompanyApprovalId
  1615. //先删除之前申请的权限
  1616. _ = company.DeleteCompanyDelayPermission(req.CompanyApprovalId)
  1617. }
  1618. //处理权限
  1619. {
  1620. if req.PermissionIds != "" {
  1621. // CRM8.8(仅针对权益客户)-此处不再去查询主观对应的客观, 而是根据客户之前拥有的权限来操作
  1622. //permissionStr, err := company.GetPermissionIdById(req.PermissionIds)
  1623. //if err != nil {
  1624. // br.Msg = "获取信息失败"
  1625. // br.ErrMsg = "获取主观客观权限信息失败,Err:" + err.Error()
  1626. // return
  1627. //}
  1628. //req.PermissionIds = permissionStr
  1629. permissionArr := strings.Split(req.PermissionIds, ",")
  1630. for _, v := range permissionArr {
  1631. //fmt.Println(k, v)
  1632. //新增
  1633. if v != "" {
  1634. permissionId, err := strconv.Atoi(v)
  1635. if err != nil {
  1636. br.Msg = "申请失败"
  1637. br.ErrMsg = "权限格式转换失败,Err:" + err.Error()
  1638. return
  1639. }
  1640. delayItem := new(company.CompanyDelayPermission)
  1641. delayItem.CompanyId = req.CompanyId
  1642. delayItem.CompanyApplyId = companyApprovalId
  1643. delayItem.ChartPermissionId = permissionId
  1644. delayItem.StartDate = time.Now().Format(utils.FormatDate)
  1645. if productId == utils.COMPANY_PRODUCT_RAI_ID { // 权益客户试用期改为1个月。 需求池 1035
  1646. delayItem.EndDate = time.Now().AddDate(0, 1, 0).Format(utils.FormatDate)
  1647. } else {
  1648. delayItem.EndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  1649. }
  1650. delayItem.CreateTime = time.Now()
  1651. delayItem.ModifyTime = time.Now()
  1652. err = company.AddCompanyDelayPermission(delayItem)
  1653. if err != nil {
  1654. br.Msg = "申请失败"
  1655. br.ErrMsg = "新增冻结转试用审批权限失败,Err:" + err.Error()
  1656. return
  1657. }
  1658. }
  1659. }
  1660. }
  1661. }
  1662. //新增操作记录
  1663. {
  1664. remark := "申请解冻"
  1665. operation := "thaw"
  1666. services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, 0, companyProduct.CompanyName,
  1667. companyProduct.ProductName, sysUser.RealName, remark, operation, approveContent, "", "", companyProduct.Status)
  1668. }
  1669. //新增客户产品日志
  1670. {
  1671. logType := "modify"
  1672. go services.AddCompanyProductLog(companyProduct, this.SysUser.AdminId, logType)
  1673. }
  1674. err = company_apply.Apply(companyApprovalId, "冻结转试用申请单")
  1675. if err != nil {
  1676. br.Msg = "发起审批异常,请联系管理员"
  1677. br.ErrMsg = "客户审批发起异常,Err:" + err.Error()
  1678. return
  1679. }
  1680. if companyApprovalCount == 0 && productId == 2 {
  1681. //获取王芳信息,自动审批使用
  1682. approvalUserInfo, err := system.CheckSysUserByName("王芳", utils.ROLE_TYPE_CODE_RAI_ADMIN)
  1683. if err != nil {
  1684. br.Msg = "权益客户自动审批失败,请联系管理员"
  1685. br.ErrMsg = "审批失败,Err:" + err.Error()
  1686. return
  1687. }
  1688. //数据校验(校验是否具有审批权限)
  1689. _, _, approvalRecord, err := company_apply.CheckApproveAuth(req.CompanyId, productId, approvalUserInfo)
  1690. if err != nil {
  1691. br.Msg = "权益客户自动审批失败,请联系管理员"
  1692. br.ErrMsg = "校验是否具有审批权限失败,Err:" + err.Error()
  1693. return
  1694. }
  1695. err = company_apply.Approved(approvalRecord, approvalUserInfo, "权益客首次申请解冻自动审批通过")
  1696. if err != nil {
  1697. br.Msg = "权益客户自动审批失败,请联系管理员!"
  1698. br.ErrMsg = "审核失败,Err:" + err.Error()
  1699. return
  1700. }
  1701. }
  1702. br.Ret = 200
  1703. br.Success = true
  1704. br.Msg = "申请成功"
  1705. }
  1706. // ApplyDelay
  1707. // @Title 申请延期
  1708. // @Description 申请延期接口
  1709. // @Param request body company.CompanyApplyDelayReq true "type json string"
  1710. // @Success Ret=200 申请成功
  1711. // @router /apply/delay [post]
  1712. func (this *CompanyApplyController) ApplyDelay() {
  1713. br := new(models.BaseResponse).Init()
  1714. defer func() {
  1715. this.Data["json"] = br
  1716. this.ServeJSON()
  1717. }()
  1718. sysUser := this.SysUser
  1719. if sysUser == nil {
  1720. br.Msg = "请登录"
  1721. br.ErrMsg = "请登录,SysUser Is Empty"
  1722. br.Ret = 408
  1723. return
  1724. }
  1725. var req company.CompanyApplyDelayReq
  1726. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1727. if err != nil {
  1728. br.Msg = "参数解析异常!"
  1729. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1730. return
  1731. }
  1732. if req.CompanyId <= 0 {
  1733. br.Msg = "参数错误"
  1734. br.ErrMsg = "参数错误,客户id小于等于0"
  1735. return
  1736. }
  1737. roleTypeCode := sysUser.RoleTypeCode
  1738. productId := services.GetProductId(roleTypeCode)
  1739. if productId == 0 {
  1740. br.Msg = "参数错误"
  1741. br.ErrMsg = "客户类型为:" + roleTypeCode
  1742. return
  1743. }
  1744. if productId == utils.COMPANY_PRODUCT_FICC_ID && req.PermissionIds == "" {
  1745. br.Msg = "请勾选需要延期的品种"
  1746. br.ErrMsg = "请勾选需要延期的品种"
  1747. return
  1748. }
  1749. //添加缓存,避免多次点击提交
  1750. {
  1751. deleteCache := true
  1752. cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
  1753. defer func() {
  1754. if deleteCache {
  1755. utils.Rc.Delete(cacheKey)
  1756. }
  1757. }()
  1758. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1759. deleteCache = false
  1760. br.Msg = "系统处理中,请勿重复操作"
  1761. br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  1762. return
  1763. }
  1764. }
  1765. approveRoleTypeCode := ""
  1766. if productId == 1 {
  1767. approveRoleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
  1768. } else {
  1769. approveRoleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
  1770. }
  1771. companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
  1772. if err != nil {
  1773. br.Msg = "获取客户信息失败"
  1774. br.ErrMsg = "获取客户信息失败"
  1775. return
  1776. }
  1777. if companyProduct == nil {
  1778. br.Msg = "获取客户信息失败"
  1779. br.ErrMsg = "客户不存在,不可提交审批:companyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
  1780. return
  1781. }
  1782. if companyProduct.Status != utils.COMPANY_STATUS_TRY_OUT {
  1783. br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行延期申请"
  1784. br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行延期申请"
  1785. return
  1786. }
  1787. if companyProduct.ApproveStatus == "待审批" {
  1788. br.Msg = "存在待审批订单,不可重复提交"
  1789. return
  1790. }
  1791. //客户归属判断
  1792. if companyProduct.SellerId != sysUser.AdminId && companyProduct.ShareSellerId != sysUser.AdminId {
  1793. br.Msg = "客户信息异常"
  1794. br.ErrMsg = "客户信息异常,不是当前所属销售,客户所属销售:" + companyProduct.SellerName + ";当前销售:" + sysUser.AdminName
  1795. return
  1796. }
  1797. // CRM8.8(仅针对权益客户)-申请延期的权限不再进行选择, 而是自动将所有的试用权限延期
  1798. if companyProduct.ProductId == utils.COMPANY_PRODUCT_RAI_ID {
  1799. reportPermissions, e := company.GetCompanyReportPermission(req.CompanyId, utils.COMPANY_PRODUCT_RAI_ID)
  1800. if e != nil {
  1801. br.Msg = "客户权限信息异常"
  1802. br.ErrMsg = "客户权限信息异常, Err: " + e.Error()
  1803. return
  1804. }
  1805. reportPermissionArr := make([]string, 0)
  1806. for i := range reportPermissions {
  1807. reportPermissionArr = append(reportPermissionArr, strconv.Itoa(reportPermissions[i].ChartPermissionId))
  1808. }
  1809. if len(reportPermissionArr) > 0 {
  1810. req.PermissionIds = strings.Join(reportPermissionArr, ",")
  1811. }
  1812. }
  1813. approveUser, err := company.GetApproveUser(approveRoleTypeCode)
  1814. if err != nil && err.Error() != utils.ErrNoRow() {
  1815. br.Msg = "获取审批信息失败"
  1816. br.ErrMsg = "获取审批人信息失败,Err:" + err.Error()
  1817. return
  1818. }
  1819. if approveUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  1820. br.Msg = "审批用户不存在"
  1821. br.ErrMsg = "审批用户不存在"
  1822. return
  1823. }
  1824. //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议
  1825. approveContent := `试用延期`
  1826. var companyApprovalId int
  1827. utils.FileLog.Info("试用延期申请:%d", req.CompanyApprovalId)
  1828. if req.CompanyApprovalId > 0 {
  1829. utils.FileLog.Info("试用延期申请:%d", req.CompanyApprovalId)
  1830. err = company.ModifyCompanyApproval(req.CompanyId, productId, req.CompanyApprovalId, approveContent, req.ApplyRemark)
  1831. if err != nil {
  1832. br.Msg = "申请失败"
  1833. br.ErrMsg = "申请失败,Err:" + err.Error()
  1834. return
  1835. }
  1836. companyApprovalId = req.CompanyApprovalId
  1837. } else {
  1838. applyMethod := 3
  1839. approval := new(company.CompanyApproval)
  1840. approval.CompanyId = req.CompanyId
  1841. approval.ProductId = productId
  1842. approval.ApproveStatus = "待审批"
  1843. approval.ApproveUserId = 0
  1844. approval.ApproveRoleTypeCode = approveRoleTypeCode
  1845. approval.ApproveRealName = ""
  1846. approval.ApproveTime = time.Now()
  1847. approval.ApproveContent = "试用延期"
  1848. approval.ApplyMethod = applyMethod
  1849. approval.ApplyReasons = req.ApplyRemark
  1850. approval.ApplyUserId = sysUser.AdminId
  1851. approval.ApplyRealName = sysUser.RealName
  1852. approval.CreateTime = time.Now()
  1853. approval.ModifyTime = time.Now()
  1854. companyApprovalId, err = company.AddCompanyApproval(approval)
  1855. if err != nil {
  1856. br.Msg = "申请失败"
  1857. br.ErrMsg = "申请失败,Err:" + err.Error()
  1858. return
  1859. }
  1860. }
  1861. //处理权限
  1862. {
  1863. if req.PermissionIds != "" {
  1864. // CRM8.8(仅针对权益客户)-此处不再去查询主观对应的客观, 而是根据客户之前拥有的权限来操作
  1865. //permissionStr, err := company.GetPermissionIdById(req.PermissionIds)
  1866. //if err != nil {
  1867. // br.Msg = "获取信息失败"
  1868. // br.ErrMsg = "获取主观客观权限信息失败,Err:" + err.Error()
  1869. // return
  1870. //}
  1871. //req.PermissionIds = permissionStr
  1872. permissionArr := strings.Split(req.PermissionIds, ",")
  1873. //如果是编辑的话,删除原先相关的所有品种权限
  1874. if req.CompanyApprovalId > 0 {
  1875. err = company.DeleteCompanyDelayPermission(req.CompanyApprovalId)
  1876. if err != nil {
  1877. br.Msg = "申请失败"
  1878. br.ErrMsg = "删除之前的申请记录失败,Err:" + err.Error()
  1879. return
  1880. }
  1881. }
  1882. for k, v := range permissionArr {
  1883. fmt.Println(k, v)
  1884. permissionId, _ := strconv.Atoi(v)
  1885. //companyId, companyApplyId, chartPermissionId int
  1886. count, err := company.GetDelayPermissionCount(req.CompanyId, companyApprovalId, permissionId)
  1887. if err != nil && err.Error() != utils.ErrNoRow() {
  1888. br.Msg = "申请失败"
  1889. br.ErrMsg = "判断权限是否存在失败,Err:" + err.Error()
  1890. return
  1891. }
  1892. if count > 0 { //修改
  1893. err = company.ModifyCompanyDelayPermission(req.CompanyId, companyApprovalId, permissionId)
  1894. if err != nil {
  1895. br.Msg = "申请失败"
  1896. br.ErrMsg = "修改权限失败,Err:" + err.Error()
  1897. return
  1898. }
  1899. } else { //新增
  1900. if v != "" {
  1901. permissionId, err := strconv.Atoi(v)
  1902. if err != nil {
  1903. br.Msg = "申请失败"
  1904. br.ErrMsg = "权限格式转换失败,Err:" + err.Error()
  1905. return
  1906. }
  1907. delayItem := new(company.CompanyDelayPermission)
  1908. delayItem.CompanyId = req.CompanyId
  1909. delayItem.CompanyApplyId = companyApprovalId
  1910. delayItem.ChartPermissionId = permissionId
  1911. delayItem.StartDate = time.Now().Format(utils.FormatDate)
  1912. delayItem.EndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  1913. delayItem.CreateTime = time.Now()
  1914. delayItem.ModifyTime = time.Now()
  1915. err = company.AddCompanyDelayPermission(delayItem)
  1916. if err != nil {
  1917. br.Msg = "申请失败"
  1918. br.ErrMsg = "新增延期审批权限失败,Err:" + err.Error()
  1919. return
  1920. }
  1921. }
  1922. }
  1923. }
  1924. }
  1925. }
  1926. //新增操作记录
  1927. {
  1928. remark := "申请延期"
  1929. operation := "delay"
  1930. go services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, 0, companyProduct.CompanyName,
  1931. companyProduct.ProductName, sysUser.RealName, remark, operation, approveContent, "", "", companyProduct.Status)
  1932. }
  1933. //新增客户产品日志
  1934. {
  1935. logType := "modify"
  1936. go services.AddCompanyProductLog(companyProduct, this.SysUser.AdminId, logType)
  1937. }
  1938. err = company_apply.Apply(companyApprovalId, "试用延期申请单")
  1939. if err != nil {
  1940. br.Msg = "发起审批异常,请联系管理员"
  1941. br.ErrMsg = "客户审批发起异常,Err:" + err.Error()
  1942. return
  1943. }
  1944. br.Ret = 200
  1945. br.Success = true
  1946. br.Msg = "申请成功"
  1947. }
  1948. // ApplyReceive
  1949. // @Title 申请领取
  1950. // @Description 申请领取接口
  1951. // @Param request body company.CompanyApplyReceiveReq true "type json string"
  1952. // @Success Ret=200 申请成功
  1953. // @router /apply/receive [post]
  1954. func (this *CompanyApplyController) ApplyReceive() {
  1955. br := new(models.BaseResponse).Init()
  1956. defer func() {
  1957. this.Data["json"] = br
  1958. this.ServeJSON()
  1959. }()
  1960. sysUser := this.SysUser
  1961. if sysUser == nil {
  1962. br.Msg = "请登录"
  1963. br.ErrMsg = "请登录,SysUser Is Empty"
  1964. br.Ret = 408
  1965. return
  1966. }
  1967. var req company.CompanyApplyReceiveReq
  1968. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1969. if err != nil {
  1970. br.Msg = "参数解析异常!"
  1971. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1972. return
  1973. }
  1974. if req.CompanyId <= 0 {
  1975. br.Msg = "参数错误"
  1976. br.ErrMsg = "参数错误,客户id小于等于0"
  1977. return
  1978. }
  1979. // 查询当前销售是否有领取客户的权限
  1980. receiveEnabled, _ := services.CheckCompanyReceiveButton(sysUser.AdminId)
  1981. if !receiveEnabled {
  1982. br.Msg = "领取失败,您目前没有领取客户的权限"
  1983. br.ErrMsg = "领取失败,您目前没有领取客户的权限"
  1984. return
  1985. }
  1986. roleTypeCode := sysUser.RoleTypeCode
  1987. productId := services.GetProductId(roleTypeCode)
  1988. if productId == 0 {
  1989. br.Msg = "参数错误"
  1990. br.ErrMsg = "客户类型为:" + roleTypeCode
  1991. return
  1992. }
  1993. //如果是权益客户,在选择非研选行业试用时,需要判断当前有多少非研选的试用客户(试用状态,且行业是非研选)
  1994. if productId == utils.COMPANY_PRODUCT_RAI_ID {
  1995. permissionArr := strings.Split(req.PermissionIds, ",")
  1996. //如果所选行业不属于研选订阅跟研选扣点包,那就是非研选试用客户
  1997. var isNoResearch bool
  1998. for _, v := range permissionArr {
  1999. if v != strconv.Itoa(utils.CHART_PERMISSION_ID_YANXUAN) && v != strconv.Itoa(utils.YAN_XUAN_KOU_DIAN_BAO_ID) {
  2000. isNoResearch = true
  2001. continue
  2002. }
  2003. }
  2004. if isNoResearch {
  2005. tryOutCountRai, err := company.GetRaiCompanyTryOutCountNoResearch(sysUser.AdminId)
  2006. if err != nil {
  2007. br.Msg = "判断试用客户量失败"
  2008. br.ErrMsg = "判断试用客户量失败,Err:" + err.Error()
  2009. return
  2010. }
  2011. if tryOutCountRai >= utils.COMPANY_STATUS_TRY_OUT_COUNT_RAI {
  2012. br.Msg = "非研选试用客户最多40家"
  2013. return
  2014. }
  2015. }
  2016. }
  2017. //添加缓存,避免多次点击提交
  2018. {
  2019. deleteCache := true
  2020. cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
  2021. defer func() {
  2022. if deleteCache {
  2023. utils.Rc.Delete(cacheKey)
  2024. }
  2025. }()
  2026. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  2027. deleteCache = false
  2028. br.Msg = "系统处理中,请勿重复操作"
  2029. br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  2030. return
  2031. }
  2032. }
  2033. approveRoleTypeCode := ""
  2034. if productId == 1 {
  2035. approveRoleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
  2036. } else {
  2037. approveRoleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
  2038. }
  2039. companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
  2040. if err != nil {
  2041. br.Msg = "获取客户信息失败"
  2042. br.ErrMsg = "获取客户信息失败"
  2043. return
  2044. }
  2045. if companyProduct == nil {
  2046. br.Msg = "获取客户信息失败"
  2047. br.ErrMsg = "客户不存在,不可提交审批:companyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
  2048. return
  2049. }
  2050. if companyProduct.Status != utils.COMPANY_STATUS_LOSE {
  2051. br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行延期申请"
  2052. br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行延期申请"
  2053. return
  2054. }
  2055. if companyProduct.ApproveStatus == "待审批" {
  2056. br.Msg = "存在待审批订单,不可重复提交"
  2057. return
  2058. }
  2059. approveUser, err := company.GetApproveUser(approveRoleTypeCode)
  2060. if err != nil && err.Error() != utils.ErrNoRow() {
  2061. br.Msg = "获取审批信息失败"
  2062. br.ErrMsg = "获取审批人信息失败,Err:" + err.Error()
  2063. return
  2064. }
  2065. if approveUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  2066. br.Msg = "审批用户不存在"
  2067. br.ErrMsg = "审批用户不存在"
  2068. return
  2069. }
  2070. approveContent := `原销售申领客户`
  2071. applyMethod := 4
  2072. var companyApprovalId int
  2073. //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议
  2074. if req.CompanyApprovalId <= 0 {
  2075. approval := new(company.CompanyApproval)
  2076. approval.CompanyId = req.CompanyId
  2077. approval.ProductId = productId
  2078. approval.ApproveStatus = "待审批"
  2079. approval.ApproveUserId = 0
  2080. approval.ApproveRoleTypeCode = approveRoleTypeCode
  2081. approval.ApproveRealName = ""
  2082. approval.ApproveTime = time.Now()
  2083. approval.ApproveContent = approveContent
  2084. approval.ApplyMethod = applyMethod
  2085. approval.ApplyReasons = req.ApplyRemark
  2086. approval.ApplyUserId = sysUser.AdminId
  2087. approval.ApplyRealName = sysUser.RealName
  2088. approval.CreateTime = time.Now()
  2089. approval.ModifyTime = time.Now()
  2090. companyApprovalId, err = company.AddCompanyApproval(approval)
  2091. if err != nil {
  2092. br.Msg = "申请失败"
  2093. br.ErrMsg = "申请失败,Err:" + err.Error()
  2094. return
  2095. }
  2096. } else {
  2097. err = company.ModifyCompanyApproval(req.CompanyId, productId, req.CompanyApprovalId, approveContent, req.ApplyRemark)
  2098. if err != nil {
  2099. br.Msg = "申请失败"
  2100. br.ErrMsg = "申请失败,Err:" + err.Error()
  2101. return
  2102. }
  2103. companyApprovalId = req.CompanyApprovalId
  2104. //先删除之前申请的权限
  2105. _ = company.DeleteCompanyDelayPermission(req.CompanyApprovalId)
  2106. }
  2107. //处理权限
  2108. {
  2109. if req.PermissionIds != "" {
  2110. permissionStr, err := company.GetPermissionIdById(req.PermissionIds)
  2111. if err != nil {
  2112. br.Msg = "获取信息失败"
  2113. br.ErrMsg = "获取主观客观权限信息失败,Err:" + err.Error()
  2114. return
  2115. }
  2116. req.PermissionIds = permissionStr
  2117. permissionArr := strings.Split(req.PermissionIds, ",")
  2118. for _, v := range permissionArr {
  2119. if v != "" {
  2120. permissionId, err := strconv.Atoi(v)
  2121. if err != nil {
  2122. br.Msg = "申请失败"
  2123. br.ErrMsg = "权限格式转换失败,Err:" + err.Error()
  2124. return
  2125. }
  2126. delayItem := new(company.CompanyDelayPermission)
  2127. delayItem.CompanyId = req.CompanyId
  2128. delayItem.CompanyApplyId = companyApprovalId
  2129. delayItem.ChartPermissionId = permissionId
  2130. delayItem.StartDate = time.Now().Format(utils.FormatDate)
  2131. if productId == utils.COMPANY_PRODUCT_RAI_ID { // 权益客户试用期改为1个月。 需求池 1035
  2132. delayItem.EndDate = time.Now().AddDate(0, 1, 0).Format(utils.FormatDate)
  2133. } else {
  2134. delayItem.EndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  2135. }
  2136. delayItem.CreateTime = time.Now()
  2137. delayItem.ModifyTime = time.Now()
  2138. err = company.AddCompanyDelayPermission(delayItem)
  2139. if err != nil {
  2140. br.Msg = "申请失败"
  2141. br.ErrMsg = "新增领取客户审批权限失败,Err:" + err.Error()
  2142. return
  2143. }
  2144. }
  2145. }
  2146. }
  2147. }
  2148. //新增操作记录
  2149. {
  2150. remark := "申请领取"
  2151. operation := "apply_receive"
  2152. services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, 0, companyProduct.CompanyName,
  2153. companyProduct.ProductName, sysUser.RealName, remark, operation, approveContent, "", "", companyProduct.Status)
  2154. }
  2155. //新增客户产品日志
  2156. {
  2157. logType := "modify"
  2158. go services.AddCompanyProductLog(companyProduct, this.SysUser.AdminId, logType)
  2159. }
  2160. err = company_apply.Apply(companyApprovalId, "申请领取申请单")
  2161. if err != nil {
  2162. br.Msg = "发起审批异常,请联系管理员"
  2163. br.ErrMsg = "客户审批发起异常,Err:" + err.Error()
  2164. return
  2165. }
  2166. br.Ret = 200
  2167. br.Success = true
  2168. br.Msg = "申请成功"
  2169. }
  2170. // ApplyRevoke
  2171. // @Title 申请撤回
  2172. // @Description 申请撤回接口
  2173. // @Param request body company.CompanyApplyRevokeReq true "type json string"
  2174. // @Success Ret=200 成功
  2175. // @router /apply/revoke [post]
  2176. func (this *CompanyApprovalController) ApplyRevoke() {
  2177. br := new(models.BaseResponse).Init()
  2178. defer func() {
  2179. this.Data["json"] = br
  2180. this.ServeJSON()
  2181. }()
  2182. sysUser := this.SysUser
  2183. if sysUser == nil {
  2184. br.Msg = "请登录"
  2185. br.ErrMsg = "请登录,SysUser Is Empty"
  2186. br.Ret = 408
  2187. return
  2188. }
  2189. var req company.CompanyApplyRevokeReq
  2190. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2191. if err != nil {
  2192. br.Msg = "参数解析异常!"
  2193. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2194. return
  2195. }
  2196. if req.CompanyId <= 0 {
  2197. br.Msg = "参数错误"
  2198. br.ErrMsg = "客户Id小于等于0:"
  2199. return
  2200. }
  2201. roleTypeCode := sysUser.RoleTypeCode
  2202. productId := services.GetProductId(roleTypeCode)
  2203. if productId == 0 {
  2204. br.Msg = "参数错误"
  2205. br.ErrMsg = "客户类型为:" + roleTypeCode
  2206. return
  2207. }
  2208. //添加缓存,避免多次点击提交
  2209. {
  2210. deleteCache := true
  2211. cacheKey := "CACHE_COMPANY_APPLY_" + strconv.Itoa(req.CompanyId) + "_" + strconv.Itoa(productId)
  2212. defer func() {
  2213. if deleteCache {
  2214. utils.Rc.Delete(cacheKey)
  2215. }
  2216. }()
  2217. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  2218. deleteCache = false
  2219. br.Msg = "系统处理中,请勿重复操作"
  2220. br.ErrMsg = "系统处理中,请勿重复操作" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  2221. return
  2222. }
  2223. }
  2224. companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, productId)
  2225. if err != nil {
  2226. br.Msg = "获取客户信息失败"
  2227. br.ErrMsg = "获取客户信息失败"
  2228. return
  2229. }
  2230. if companyProduct == nil {
  2231. br.Msg = "获取客户信息失败"
  2232. br.ErrMsg = "客户不存在,不可提交审批:companyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
  2233. return
  2234. }
  2235. approvalItem, err := company.GetCompanyApprovalById(req.CompanyId, productId)
  2236. if err != nil {
  2237. if err.Error() == utils.ErrNoRow() {
  2238. br.Msg = "待审批信息不存在"
  2239. br.ErrMsg = "待审批信息不存在,CompanyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
  2240. return
  2241. }
  2242. br.Msg = "获取信息失败"
  2243. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  2244. return
  2245. }
  2246. if approvalItem == nil {
  2247. br.Msg = "待审批信息不存在"
  2248. br.ErrMsg = "待审批信息不存在,CompanyId:" + strconv.Itoa(req.CompanyId) + ";productId:" + strconv.Itoa(productId)
  2249. return
  2250. }
  2251. if approvalItem.ApproveStatus != "待审批" {
  2252. br.Msg = "客户状态为:" + companyProduct.Status + ";不可进行撤回操作"
  2253. br.ErrMsg = "客户状态为:" + companyProduct.Status + ";不可进行撤回操作"
  2254. return
  2255. }
  2256. //撤回审批单
  2257. err = company_apply.CancelApply(approvalItem, sysUser.AdminId)
  2258. if err != nil {
  2259. br.Msg = "撤回失败"
  2260. br.ErrMsg = "撤回失败,Err:" + err.Error()
  2261. return
  2262. }
  2263. //真正撤回操作
  2264. err = company.ApplyRevoke(approvalItem.CompanyApprovalId, approvalItem.CompanyId, approvalItem.ProductId)
  2265. if err != nil {
  2266. br.Msg = "撤回失败"
  2267. br.ErrMsg = "撤回失败,Err:" + err.Error()
  2268. return
  2269. }
  2270. //新增操作记录
  2271. {
  2272. remark := "撤回"
  2273. operation := "apply_revoke"
  2274. services.AddCompanyOperationRecord(req.CompanyId, companyProduct.SellerId, sysUser.AdminId, productId, 0, companyProduct.CompanyName,
  2275. companyProduct.ProductName, sysUser.RealName, remark, operation, "", "", "", companyProduct.Status)
  2276. }
  2277. br.Ret = 200
  2278. br.Success = true
  2279. br.Msg = "撤回成功"
  2280. }