company_apply_v2.go 75 KB

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