company_apply_v2.go 77 KB

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