company_apply_v2.go 78 KB

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