company_apply_v2.go 78 KB

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