mcn.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. adminmodel "go-common/app/admin/main/mcn/model"
  6. "go-common/app/interface/main/mcn/dao/cache"
  7. "go-common/app/interface/main/mcn/dao/global"
  8. "go-common/app/interface/main/mcn/model"
  9. "go-common/app/interface/main/mcn/model/mcnmodel"
  10. accgrpc "go-common/app/service/main/account/api"
  11. memgrpc "go-common/app/service/main/member/api"
  12. memmdl "go-common/app/service/main/member/model"
  13. "go-common/app/service/main/member/model/block"
  14. "go-common/library/ecode"
  15. "go-common/library/log"
  16. "go-common/library/net/metadata"
  17. "go-common/app/interface/main/mcn/conf"
  18. "go-common/app/interface/main/mcn/dao/mcndao"
  19. "strings"
  20. "github.com/jinzhu/gorm"
  21. )
  22. // getMcnWithState
  23. // if state is nil, state is not checked
  24. func (s *Service) getMcnWithState(c context.Context, mcnmid int64, state ...model.MCNSignState) (mcnSign *mcnmodel.McnSign, err error) {
  25. mcnSign, err = s.mcndao.McnSign(c, mcnmid)
  26. if err != nil {
  27. if err != ecode.NothingFound {
  28. log.Error("error get state, err=%s", err)
  29. }
  30. return
  31. }
  32. if mcnSign == nil {
  33. err = ecode.NothingFound
  34. return
  35. }
  36. var ok = false
  37. if state == nil {
  38. ok = true
  39. } else {
  40. for _, s := range state {
  41. if mcnSign.State == s {
  42. ok = true
  43. break
  44. }
  45. }
  46. }
  47. if !ok {
  48. log.Info("mcnmid=%d, mcn is in %d, should in (%v)", mcnmid, mcnSign.State, state)
  49. err = ecode.MCNNotAllowed
  50. return
  51. }
  52. return
  53. }
  54. func (s *Service) checkPermission(c context.Context, mcnMid, upMid int64, permissions ...adminmodel.AttrBasePermit) (res bool) {
  55. var permLen = len(permissions)
  56. if permLen == 0 {
  57. return
  58. } else if permLen == 1 {
  59. // 基础权限直接放过
  60. if permissions[0] == adminmodel.AttrBasePermitBit {
  61. return true
  62. }
  63. }
  64. mcnSign, err := s.getMcnWithState(c, mcnMid, model.MCNSignStateOnSign)
  65. if err != nil {
  66. log.Error("get mcn fail, mcnmid=%d, err=%v", mcnMid, err)
  67. return
  68. }
  69. permForUp, err := s.mcndao.UpPermission(c, mcnSign.ID, upMid)
  70. if err != nil || permForUp == nil {
  71. log.Error("get up permission fail, signID=%d, upmid=%d, err=%v or up not found", mcnSign.ID, upMid, err)
  72. return
  73. }
  74. // 比较mcn与up的权限
  75. var wantPermission uint32
  76. for _, v := range permissions {
  77. wantPermission = wantPermission | (1 << v)
  78. }
  79. var resultPermission = wantPermission & mcnSign.Permission & permForUp.Permission
  80. if resultPermission != wantPermission {
  81. log.Warn("mcn doesnt have permission, mcn perm=0x%x, up perm=0x%x, want=0x%x, lack=0x%x", mcnSign.Permission, permForUp.Permission, wantPermission, resultPermission^wantPermission)
  82. return
  83. }
  84. res = true
  85. return
  86. }
  87. //McnGetState mcn state
  88. func (s *Service) McnGetState(c context.Context, arg *mcnmodel.GetStateReq) (res *mcnmodel.McnGetStateReply, err error) {
  89. mcnSign, err := s.getMcnWithState(c, arg.McnMid)
  90. if err != nil {
  91. if err != ecode.NothingFound {
  92. log.Error("error get state, err=%s", err)
  93. }
  94. return
  95. }
  96. res = new(mcnmodel.McnGetStateReply)
  97. res.State = int8(mcnSign.State)
  98. if mcnSign.State == model.MCNSignStateOnReject {
  99. res.RejectReason = mcnSign.RejectReason
  100. }
  101. log.Info("mcn_state=%d, mcn_id=%d", res.State, arg.McnMid)
  102. return
  103. }
  104. //McnExist .
  105. func (s *Service) McnExist(c context.Context, arg *mcnmodel.GetStateReq) (res *mcnmodel.McnExistReply, err error) {
  106. res = new(mcnmodel.McnExistReply)
  107. _, err = s.getMcnWithState(c, arg.McnMid)
  108. if err == ecode.NothingFound {
  109. res.Exist = 0
  110. return
  111. } else if err != nil {
  112. log.Error("error get state, err=%s", err)
  113. return
  114. }
  115. res.Exist = 1
  116. return
  117. }
  118. // McnBaseInfo .
  119. func (s *Service) McnBaseInfo(c context.Context, arg *mcnmodel.GetStateReq) (res *mcnmodel.McnBaseInfoReply, err error) {
  120. res = new(mcnmodel.McnBaseInfoReply)
  121. mcnSign, err := s.getMcnWithState(c, arg.McnMid)
  122. if err != nil {
  123. if err != ecode.NothingFound {
  124. log.Error("error get state, err=%s", err)
  125. }
  126. return
  127. }
  128. res.CopyFromMcnInfo(mcnSign)
  129. return
  130. }
  131. //McnApply .
  132. func (s *Service) McnApply(c context.Context, arg *mcnmodel.McnApplyReq) (res *mcnmodel.CommonReply, err error) {
  133. mcnSign, err := s.getMcnWithState(c, arg.McnMid, model.MCNSignStateOnReject, model.MCNSignStateNoApply)
  134. if err != nil {
  135. if err != ecode.NothingFound {
  136. log.Error("error get state, err=%s", err)
  137. }
  138. return
  139. }
  140. var sign mcnmodel.McnSign
  141. if err = s.uniqueChecker.CheckIsUniqe(arg); err != nil {
  142. log.Info("check unique fail, err=%s, arg=%v", err, arg)
  143. return
  144. }
  145. arg.CopyTo(&sign)
  146. sign.ID = mcnSign.ID
  147. sign.State = model.MCNSignStateOnReview
  148. var db = s.mcndao.GetMcnDB()
  149. if err = db.Table(sign.TableName()).Where("id=?", sign.ID).Updates(map[string]interface{}{
  150. "company_name": sign.CompanyName,
  151. "company_license_id": sign.CompanyLicenseID,
  152. "contact_name": sign.ContactName,
  153. "contact_title": sign.ContactTitle,
  154. "contact_idcard": sign.ContactIdcard,
  155. "contact_phone": sign.ContactPhone,
  156. "company_license_link": sign.CompanyLicenseLink,
  157. "contract_link": sign.ContractLink,
  158. "state": sign.State,
  159. }).Error; err != nil {
  160. log.Error("save mcn fail, mcn mid=%d, row id=%d", sign.McnMid, sign.ID)
  161. err = ecode.ServerErr
  162. return
  163. }
  164. s.mcndao.DelCacheMcnSign(c, arg.McnMid)
  165. s.worker.Add(func() {
  166. s.loadMcnUniqueCache()
  167. })
  168. return
  169. }
  170. //McnBindUpApply .
  171. func (s *Service) McnBindUpApply(c context.Context, arg *mcnmodel.McnBindUpApplyReq) (res *mcnmodel.McnBindUpApplyReply, err error) {
  172. if arg.BeginDate > arg.EndDate {
  173. err = ecode.MCNUpBindUpSTimeLtETime
  174. return
  175. }
  176. // 查询mcn状态
  177. mcnSign, err := s.getMcnWithState(c, arg.McnMid, model.MCNSignStateOnSign)
  178. if err != nil {
  179. if err != ecode.NothingFound {
  180. log.Error("error get state, err=%s", err)
  181. }
  182. return
  183. }
  184. // 0.检查是否封禁
  185. var blockArg = memgrpc.MemberMidReq{Mid: arg.UpMid, RemoteIP: metadata.String(c, metadata.RemoteIP)}
  186. var blockInfo, e = global.GetMemGRPC().BlockInfo(c, &blockArg)
  187. if e == nil {
  188. if blockInfo.BlockStatus > int32(block.BlockStatusFalse) {
  189. log.Info("up is blocked, mid=%d, blockstatus=%d", arg.UpMid, blockInfo.BlockStatus)
  190. err = ecode.MCNUpBindUpIsBlocked
  191. return
  192. }
  193. } else {
  194. log.Error("get block info error, err=%s", e)
  195. }
  196. // 1.检查是否是蓝V用户
  197. var (
  198. memberInfo *memgrpc.MemberInfoReply
  199. memberArg = memgrpc.MemberMidReq{Mid: arg.UpMid, RemoteIP: metadata.String(c, metadata.RemoteIP)}
  200. )
  201. if memberInfo, err = global.GetMemGRPC().Member(c, &memberArg); err != nil {
  202. log.Error("get member info error, err=%s", err)
  203. } else {
  204. if memberInfo.OfficialInfo != nil &&
  205. (memberInfo.OfficialInfo.Role == memmdl.OfficialRoleBusiness ||
  206. memberInfo.OfficialInfo.Role == memmdl.OfficialRoleGov ||
  207. memberInfo.OfficialInfo.Role == memmdl.OfficialRoleMedia ||
  208. memberInfo.OfficialInfo.Role == memmdl.OfficialRoleOther) {
  209. err = ecode.MCNUpBindUpIsBlueUser
  210. return
  211. }
  212. }
  213. // 2.查询当前up状态
  214. upList, err := s.mcndao.GetUpBind("up_mid=?", arg.UpMid)
  215. if err != nil {
  216. log.Error("get up bind fail, err=%s", err)
  217. err = ecode.ServerErr
  218. return
  219. }
  220. // 3.可以申请绑定的up主才能绑定
  221. var mcnUp *mcnmodel.McnUp
  222. for _, v := range upList {
  223. if !v.IsBindable() {
  224. log.Info("up is in state(%d), cannot be bind again. id=%d, upmid=%d, signid=%d, mcnSign=%d", v.State, v.ID, v.UpMid, v.SignID, v.McnMid)
  225. err = ecode.MCNUpCannotBind
  226. return
  227. }
  228. if v.IsBeingBindedWithMcn(mcnSign) {
  229. log.Info("up is being binded with mcnSign, state=%d, id=%d, signid=%d, mcnSign=%d, mcn_mid=%d", v.State, v.State, v.ID, v.SignID, v.McnMid)
  230. err = ecode.MCNUpBindUpAlreadyInProgress
  231. return
  232. }
  233. if v.SignID == mcnSign.ID {
  234. mcnUp = v
  235. }
  236. }
  237. if arg.UpType == 1 {
  238. // 站外up主需要满足条件:
  239. // 1.粉丝数≤100 或 2. 投稿数<2及90天内未投稿 (1,2并列关系,满足其一即可申请)
  240. baseInfoMap, e := s.mcndao.GetUpBaseInfo("article_count_accumulate, activity, fans_count, mid", []int64{arg.UpMid})
  241. if e == nil {
  242. var upInfo, ok = baseInfoMap[arg.UpMid]
  243. if ok && upInfo != nil {
  244. //upInfo.Activity 1高,2中,3低,4流失
  245. //高=30天内有投稿
  246. //中=31~90天内有投稿
  247. //低=91~180天内有投稿
  248. //流失=180内以上未投稿
  249. if !(upInfo.FansCount <= 100 || (upInfo.ArticleCountAccumulate < 2 && upInfo.Activity > 2)) {
  250. err = ecode.MCNUpOutSiteIsNotQualified
  251. log.Error("outsite cannot bind, up fans count(%d) > 100", upInfo.FansCount)
  252. return
  253. }
  254. } else {
  255. log.Warn("up info is not found in up base info, up=%d", arg.UpMid)
  256. }
  257. }
  258. }
  259. // 站外信息是否OK
  260. if !arg.IsSiteInfoOk() {
  261. err = ecode.MCNUpBindInvalidURL
  262. log.Warn("arg error, up is out site up, but site url is not valid, arg=%v", arg)
  263. return
  264. }
  265. // 只能设置mcn自己有的权限,如果要设置其他权限,返回错误。
  266. // 只有mcn有的权限,才可以申请up主的权限
  267. _, err = mcnShouldContainUpPermission(mcnSign.Permission, arg.GetAttrPermitVal())
  268. if err != nil {
  269. return
  270. }
  271. // 3.绑定Up主,如果已有记录,则更新记录
  272. bindup, affectedRow, err := s.mcndao.BindUp(mcnUp, mcnSign, arg)
  273. if err != nil {
  274. log.Error("fail to bind up, mcnmid=%d, upmid=%d err=%s", arg.McnMid, arg.UpMid, err)
  275. return
  276. }
  277. res = new(mcnmodel.McnBindUpApplyReply)
  278. res.BindID = bindup.ID
  279. // 4.发送站内信息
  280. if arg.UpMid != arg.McnMid {
  281. var nickname = global.GetName(c, arg.McnMid)
  282. var msg = adminmodel.ArgMsg{
  283. MSGType: adminmodel.McnUpBindAuthApply,
  284. MIDs: []int64{arg.UpMid},
  285. McnName: nickname,
  286. McnMid: arg.McnMid,
  287. CompanyName: mcnSign.CompanyName,
  288. SignUpID: bindup.ID,
  289. }
  290. s.sendMsg(&msg)
  291. }
  292. log.Info("bind up apply success, mcn=%d, upmid=%d, rowaffected=%d", arg.McnMid, arg.UpMid, affectedRow)
  293. return
  294. }
  295. //McnUpConfirm .
  296. func (s *Service) McnUpConfirm(c context.Context, arg *mcnmodel.McnUpConfirmReq) (res *mcnmodel.CommonReply, err error) {
  297. // 1.查询当前up状态
  298. upList, err := s.mcndao.GetUpBind("id=? and up_mid=? and state=?", arg.BindID, arg.UpMid, model.MCNUPStateNoAuthorize)
  299. if err != nil {
  300. log.Error("get up bind fail, err=%s", err)
  301. err = ecode.ServerErr
  302. return
  303. }
  304. // 不存在
  305. if len(upList) == 0 {
  306. log.Info("bind id not found, id=%d", arg.BindID)
  307. err = ecode.MCNNotAllowed
  308. return
  309. }
  310. var upBind = upList[0]
  311. // 查询mcn状态
  312. mcnSign, err := s.getMcnWithState(c, upBind.McnMid, model.MCNSignStateOnSign)
  313. if err != nil {
  314. if err != ecode.MCNStateInvalid {
  315. log.Error("error get state, err=%s", err)
  316. }
  317. return
  318. }
  319. if mcnSign.ID != upBind.SignID {
  320. log.Error("bind id not same with mcn signid, bind id=%d, signid=%d", upBind.ID, upBind.SignID)
  321. err = ecode.MCNUpBindInvalid
  322. return
  323. }
  324. var state = model.MCNUPStateOnRefuse
  325. if arg.Choice {
  326. state = model.MCNUPStateOnReview
  327. }
  328. // 更新状态
  329. err = s.mcndao.UpConfirm(arg, state)
  330. if err != nil {
  331. log.Error("fail to update up bind, bind id=%d, upmid=%d, err=%s", arg.BindID, arg.UpMid, err)
  332. err = ecode.ServerErr
  333. return
  334. }
  335. // 同意
  336. if arg.Choice {
  337. var mcnName = global.GetName(c, mcnSign.McnMid)
  338. var msg = adminmodel.ArgMsg{
  339. MSGType: adminmodel.McnUpBindAuthReview,
  340. MIDs: []int64{arg.UpMid},
  341. McnName: mcnName,
  342. McnMid: mcnSign.McnMid,
  343. CompanyName: mcnSign.CompanyName,
  344. }
  345. s.sendMsg(&msg)
  346. } else {
  347. var upName = global.GetName(c, arg.UpMid)
  348. var msg = adminmodel.ArgMsg{
  349. MSGType: adminmodel.McnUpBindAuthApplyRefuse,
  350. MIDs: []int64{mcnSign.McnMid},
  351. UpMid: arg.UpMid,
  352. UpName: upName,
  353. }
  354. s.sendMsg(&msg)
  355. }
  356. log.Info("up bind change, bind id=%d, upmid=%d, isaccept=%t", arg.BindID, arg.UpMid, arg.Choice)
  357. return
  358. }
  359. //McnUpGetBind .
  360. func (s *Service) McnUpGetBind(c context.Context, arg *mcnmodel.McnUpGetBindReq) (res *mcnmodel.McnGetBindReply, err error) {
  361. res, err = s.mcndao.GetBindInfo(arg)
  362. if err != nil {
  363. log.Error("fail to get bind info, err=%s", err)
  364. return
  365. }
  366. accInfo, err := global.GetInfo(c, int64(res.McnMid))
  367. if err == nil && accInfo != nil {
  368. res.McnName = accInfo.Name
  369. }
  370. res.Finish()
  371. res.UpAuthLink = model.BuildBfsURL(res.UpAuthLink, s.c.BFS.Key, s.c.BFS.Secret, s.c.BFS.Bucket, model.BfsEasyPath)
  372. return
  373. }
  374. //McnDataSummary .
  375. func (s *Service) McnDataSummary(c context.Context, arg *mcnmodel.McnGetDataSummaryReq) (res *mcnmodel.McnGetDataSummaryReply, err error) {
  376. mcnSign, err := s.getMcnWithState(c, arg.McnMid, model.MCNSignStateOnSign)
  377. if err != nil {
  378. if err != ecode.NothingFound {
  379. log.Error("error get state, err=%s", err)
  380. }
  381. return
  382. }
  383. var today = time.Now().Add(-12 * time.Hour)
  384. res, err = s.datadao.GetMcnSummaryCache(c, mcnSign.ID, today)
  385. if err != nil {
  386. log.Error("fail to get mcn data, sign id=%d, mcnmid=%d, err=%s", mcnSign.ID, mcnSign.McnMid, err)
  387. return
  388. }
  389. // today is not found, try yesterday
  390. if res == nil {
  391. res, err = s.mcndao.McnDataSummary(c, mcnSign.ID, today.AddDate(0, 0, -1))
  392. if err != nil {
  393. log.Error("fail to get mcn data, sign id=%d, mcnmid=%d, err=%s", mcnSign.ID, mcnSign.McnMid, err)
  394. return
  395. }
  396. }
  397. if res == nil {
  398. log.Error("fail to get mcn data, res = nil, sign id=%d", mcnSign.ID)
  399. res = new(mcnmodel.McnGetDataSummaryReply)
  400. }
  401. return
  402. }
  403. //McnDataUpList .
  404. func (s *Service) McnDataUpList(c context.Context, arg *mcnmodel.McnGetUpListReq) (res *mcnmodel.McnGetUpListReply, err error) {
  405. mcnSign, err := s.getMcnWithState(c, arg.McnMid, model.MCNSignStateOnSign)
  406. if err != nil {
  407. if err != ecode.NothingFound {
  408. log.Error("error get state, err=%s", err)
  409. }
  410. return
  411. }
  412. generateDate, err := s.mcndao.GetDataUpLatestDate(mcnmodel.DataTypeAccumulate, mcnSign.ID)
  413. if err != nil {
  414. if err == gorm.ErrRecordNotFound {
  415. err = nil
  416. log.Warn("no data list found for mcn=%d, sign id=%d", mcnSign.McnMid, mcnSign.ID)
  417. var now = time.Now()
  418. generateDate = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
  419. } else {
  420. log.Error("fail to get latest generate up date, err=%s", err)
  421. return
  422. }
  423. }
  424. // 获取数据
  425. upData, err := s.mcndao.GetAllUpData(int64(mcnSign.ID), arg.UpMid, generateDate)
  426. // 在正式数据出来之前,临时使用
  427. //upData, err := s.mcndao.GetAllUpDataTemp(int64(mcnSign.ID), arg.UpMid, time.Now())
  428. var mids []int64
  429. for _, v := range upData {
  430. mids = append(mids, v.UpMid)
  431. v.Permission = v.Permission & mcnSign.Permission
  432. }
  433. var infosReply *accgrpc.InfosReply
  434. var midtidmap map[int64]int64
  435. var accInfos map[int64]*accgrpc.Info
  436. if len(mids) > 0 {
  437. var e error
  438. infosReply, e = global.GetAccGRPC().Infos3(c, &accgrpc.MidsReq{Mids: mids})
  439. if e != nil {
  440. log.Warn("fail to get info, err=%s", e)
  441. } else {
  442. accInfos = infosReply.Infos
  443. }
  444. midtidmap, e = s.mcndao.GetActiveTid(mids)
  445. if e != nil {
  446. log.Warn("fail to get activit, err=%s", e)
  447. }
  448. }
  449. res = new(mcnmodel.McnGetUpListReply)
  450. for _, v := range upData {
  451. var info, ok = accInfos[v.UpMid]
  452. if ok {
  453. v.Name = info.Name
  454. }
  455. if v.State != int8(model.MCNUPStateOnSign) {
  456. // MCNUPStateOnSign 与 MCNUPStateOnPreOpen 状态下 不隐藏时间
  457. v.HideData(!(v.State == int8(model.MCNUPStateOnSign) ||
  458. v.State == int8(model.MCNUPStateOnPreOpen)))
  459. }
  460. tid, ok := midtidmap[v.UpMid]
  461. if ok {
  462. v.TidName = cache.GetTidName(tid)
  463. v.ActiveTid = int16(tid)
  464. }
  465. res.Result = append(res.Result, v)
  466. }
  467. res.Finish()
  468. return
  469. }
  470. //McnGetOldInfo .
  471. func (s *Service) McnGetOldInfo(c context.Context, arg *mcnmodel.McnGetMcnOldInfoReq) (res *mcnmodel.McnGetMcnOldInfoReply, err error) {
  472. mcnSign, err := s.getMcnWithState(c, arg.McnMid, model.MCNSignStateNoApply)
  473. if err != nil {
  474. if err != ecode.NothingFound {
  475. log.Error("error get state, err=%s", err)
  476. }
  477. return
  478. }
  479. info, err := s.mcndao.GetMcnOldInfo(mcnSign.McnMid)
  480. if err != nil {
  481. if err == ecode.NothingFound {
  482. err = nil
  483. } else {
  484. log.Error("fail get mcn old info err=%s", err)
  485. return
  486. }
  487. }
  488. res = new(mcnmodel.McnGetMcnOldInfoReply)
  489. res.Copy(info)
  490. return
  491. }
  492. func getUpPermitString(permission uint32) (ps []string) {
  493. for permit := range adminmodel.PermitMap {
  494. var p = adminmodel.AttrVal(permission, uint(permit))
  495. if p <= 0 {
  496. continue
  497. }
  498. ps = append(ps, permit.String())
  499. }
  500. return
  501. }
  502. // 检查up主权限,是否是mcn的子集
  503. // mcnPermission mcn自己的permission
  504. // upPermission up的permission
  505. // return finalPermission = mcnPermission &upPermission
  506. func mcnShouldContainUpPermission(mcnPermission, upPermission uint32) (finalPermission uint32, err error) {
  507. // 3.只能设置mcn自己有的权限,如果要设置其他权限,返回错误。
  508. // 只有mcn有的权限,才可以申请up主的权限
  509. finalPermission = mcnPermission & upPermission
  510. if finalPermission != upPermission {
  511. log.Error("mcn has no permission to change, mcn=0x%x, wantup=0x%x, notallowd=0x%x", mcnPermission, upPermission, finalPermission^upPermission)
  512. err = ecode.MCNChangePermissionLackPermission
  513. return
  514. }
  515. return
  516. }
  517. //McnChangePermit change up's permission
  518. func (s *Service) McnChangePermit(c context.Context, arg *mcnmodel.McnChangePermitReq) (res *mcnmodel.McnChangePermitReply, err error) {
  519. mcnSign, err := s.getMcnWithState(c, arg.McnMid, model.MCNSignStateOnSign)
  520. if err != nil {
  521. if err != ecode.NothingFound {
  522. log.Error("error get state, err=%s", err)
  523. }
  524. return
  525. }
  526. // 1.检查Up主关系,只有“已签约”,“待开启”状态的Up主可以修改
  527. // 2.查询当前up状态
  528. upList, err := s.mcndao.GetUpBind("up_mid=? and sign_id=? and state in (?)", arg.UpMid, mcnSign.ID, mcndao.UpSignedStates)
  529. if err != nil {
  530. log.Error("get up bind fail, err=%v", err)
  531. err = ecode.ServerErr
  532. return
  533. }
  534. if len(upList) == 0 {
  535. log.Error("up is not in signed state with mcn, up_mid=%d and sign_id=%d , need state in (%v)", arg.UpMid, mcnSign.ID, mcndao.UpSignedStates)
  536. err = ecode.MCNUpSignStateInvalid
  537. return
  538. }
  539. var oldUp = upList[0]
  540. var newPermission = arg.GetAttrPermitVal()
  541. if oldUp.Permission == uint32(newPermission) {
  542. log.Error("permission not changed, arg=%+v", arg)
  543. err = ecode.MCNChangePermissionSamePermission
  544. return
  545. }
  546. // 2.只能设置mcn自己有的权限,如果要设置其他权限,返回错误。
  547. // 只有mcn有的权限,才可以申请up主的权限
  548. maskedPermission, err := mcnShouldContainUpPermission(mcnSign.Permission, newPermission)
  549. if err != nil {
  550. return
  551. }
  552. // 如果是自己,则直接进行修改
  553. if arg.UpMid == mcnSign.McnMid {
  554. var _, e = s.mcndao.UpdateBindUp(map[string]interface{}{
  555. "permission": maskedPermission,
  556. }, "up_mid=? and sign_id=?", arg.UpMid, mcnSign.ID)
  557. if e != nil {
  558. err = e
  559. log.Error("fail to change up permission, err=%v, arg=%v", err, arg)
  560. return
  561. }
  562. return
  563. }
  564. // 3.检查是否有对应up主的修改请求,如果有就拒绝这次修改
  565. existedApply, _ := s.mcndao.GetUpPermissionApply("id", "sign_id=? and up_mid=? and state in (?)", mcnSign.ID, arg.UpMid, mcndao.UpPermissionApplyCannotApplyStates)
  566. if len(existedApply) > 0 {
  567. log.Error("apply already exist, id=%d, sign id=%d, mid=%d", existedApply[0].ID, existedApply[0].SignID, existedApply[0].UpMid)
  568. err = ecode.MCNChangePermissionAlreadyInProgress
  569. return
  570. }
  571. // 真的去增加permission
  572. var permissionApply = mcnmodel.McnUpPermissionApply{
  573. SignID: mcnSign.ID,
  574. McnMid: mcnSign.McnMid,
  575. UpMid: arg.UpMid,
  576. NewPermission: maskedPermission,
  577. OldPermission: oldUp.Permission,
  578. UpAuthLink: arg.UpAuthLink,
  579. }
  580. var db = s.mcndao.GetMcnDB()
  581. err = db.Create(&permissionApply).Error
  582. if err != nil {
  583. log.Error("create permission apply fail, err=%v, arg=%+v", err, arg)
  584. return
  585. }
  586. // 返回bind_id
  587. res = &mcnmodel.McnChangePermitReply{BindID: permissionApply.ID}
  588. // 4.发送站内信息
  589. if arg.UpMid != arg.McnMid {
  590. var nickname = global.GetName(c, arg.McnMid)
  591. var msg = adminmodel.ArgMsg{
  592. MSGType: adminmodel.McnApplyUpChangePermit,
  593. MIDs: []int64{arg.UpMid},
  594. McnName: nickname,
  595. McnMid: arg.McnMid,
  596. CompanyName: mcnSign.CompanyName,
  597. SignUpID: permissionApply.ID,
  598. Permission: strings.Join(getUpPermitString(maskedPermission), "、"),
  599. }
  600. s.sendMsg(&msg)
  601. }
  602. return
  603. }
  604. //McnPermitApplyGetBind get permit apply bind
  605. func (s *Service) McnPermitApplyGetBind(c context.Context, arg *mcnmodel.McnUpGetBindReq) (res *mcnmodel.McnGetBindReply, err error) {
  606. res, err = s.mcndao.GetUpPermissionBindInfo(arg)
  607. if err != nil {
  608. log.Error("fail to get bind info, err=%s", err)
  609. return
  610. }
  611. accInfo, err := global.GetInfo(c, int64(res.McnMid))
  612. if err == nil && accInfo != nil {
  613. res.McnName = accInfo.Name
  614. }
  615. res.Finish()
  616. res.UpAuthLink = model.BuildBfsURL(res.UpAuthLink, s.c.BFS.Key, s.c.BFS.Secret, s.c.BFS.Bucket, model.BfsEasyPath)
  617. return
  618. }
  619. //McnUpPermitApplyConfirm permit apply confirm
  620. func (s *Service) McnUpPermitApplyConfirm(c context.Context, arg *mcnmodel.McnUpConfirmReq) (res *mcnmodel.CommonReply, err error) {
  621. // 1.查询当前up状态
  622. upList, err := s.mcndao.GetUpPermissionApply("*", "id=? and up_mid=? and state=?", arg.BindID, arg.UpMid, adminmodel.MCNUPPermissionStateNoAuthorize)
  623. if err != nil {
  624. log.Error("get up bind fail, err=%s", err)
  625. err = ecode.ServerErr
  626. return
  627. }
  628. // 不存在
  629. if len(upList) == 0 {
  630. log.Info("bind id not found, id=%d", arg.BindID)
  631. err = ecode.MCNNotAllowed
  632. return
  633. }
  634. var upBind = upList[0]
  635. // 查询mcn状态
  636. mcnSign, err := s.getMcnWithState(c, upBind.McnMid, model.MCNSignStateOnSign)
  637. if err != nil {
  638. if err != ecode.MCNStateInvalid {
  639. log.Error("error get state, err=%s", err)
  640. }
  641. return
  642. }
  643. if mcnSign.ID != upBind.SignID {
  644. log.Error("bind id not same with mcn signid, bind id=%d, signid=%d", upBind.ID, upBind.SignID)
  645. err = ecode.MCNUpBindInvalid
  646. return
  647. }
  648. var state = adminmodel.MCNUPPermissionStateOnRefuse
  649. if arg.Choice {
  650. state = adminmodel.MCNUPPermissionStateReview
  651. }
  652. // 更新状态
  653. err = s.mcndao.UpPermissionConfirm(arg, state)
  654. if err != nil {
  655. log.Error("fail to update up bind, bind id=%d, upmid=%d, err=%s", arg.BindID, arg.UpMid, err)
  656. err = ecode.ServerErr
  657. return
  658. }
  659. // 同意
  660. if arg.Choice {
  661. // do nothing.
  662. } else {
  663. var upName = global.GetName(c, arg.UpMid)
  664. var msg = adminmodel.ArgMsg{
  665. MSGType: adminmodel.McnUpNotAgreeChangePermit,
  666. MIDs: []int64{mcnSign.McnMid},
  667. UpMid: arg.UpMid,
  668. UpName: upName,
  669. }
  670. s.sendMsg(&msg)
  671. }
  672. log.Info("up permission bind change, bind id=%d, upmid=%d, isaccept=%t", arg.BindID, arg.UpMid, arg.Choice)
  673. return
  674. }
  675. //McnPublicationPriceChange .
  676. func (s *Service) McnPublicationPriceChange(c context.Context, arg *mcnmodel.McnPublicationPriceChangeReq) (res *mcnmodel.McnPublicationPriceChangeReply, err error) {
  677. mcnSign, err := s.getMcnWithState(c, arg.McnMid, model.MCNSignStateOnSign)
  678. if err != nil {
  679. if err != ecode.NothingFound {
  680. log.Error("error get state, err=%s", err)
  681. }
  682. return
  683. }
  684. // 1.检查上次刊例价修改时间,如果时间内不能修改返回错误
  685. publicationPriceCache, e := s.mcndao.CachePublicationPrice(c, mcnSign.ID, arg.UpMid)
  686. if e != nil {
  687. log.Warn("get modify time from cache fail, arg=%+v, err=%v", arg, err)
  688. }
  689. if publicationPriceCache == nil {
  690. // 初始化为0值
  691. publicationPriceCache = &mcnmodel.PublicationPriceCache{}
  692. }
  693. var lastModifyTime = publicationPriceCache.ModifyTime // 从缓存中获取
  694. var now = time.Now()
  695. if now.Before(lastModifyTime.Add(time.Duration(conf.Conf.Other.PublicationPriceChangeLimit))) {
  696. log.Error("publication change fail, last modify time=%s, timelimit=%+v, arg=%+v", lastModifyTime, conf.Conf.Other.PublicationPriceChangeLimit, arg)
  697. err = ecode.MCNPublicationFailTimeLimit
  698. return
  699. }
  700. // 2.检查Up主关系,只有“已签约”,“待开启”状态的Up主可以修改
  701. upList, err := s.mcndao.GetUpBind("up_mid=? and sign_id=? and state in (?)", arg.UpMid, mcnSign.ID, mcndao.UpSignedStates)
  702. if err != nil {
  703. log.Error("get up bind fail, err=%v", err)
  704. err = ecode.ServerErr
  705. return
  706. }
  707. if len(upList) == 0 {
  708. log.Error("up is not in signed state with mcn, up_mid=%d and sign_id=%d , need state in (%v)", arg.UpMid, mcnSign.ID, mcndao.UpSignedStates)
  709. err = ecode.MCNUpSignStateInvalid
  710. return
  711. }
  712. var up = upList[0]
  713. // 3.修改刊例价,更新上次修改时间
  714. var db = s.mcndao.GetMcnDB()
  715. err = db.Table(mcnmodel.TableNameMcnUp).Where("id=?", up.ID).Update("publication_price", arg.Price).Error
  716. if err != nil {
  717. log.Error("change publication price fail, err=%v, arg=%+v", err, arg)
  718. return
  719. }
  720. publicationPriceCache.ModifyTime = now
  721. e = s.mcndao.AddCachePublicationPrice(c, mcnSign.ID, publicationPriceCache, arg.UpMid)
  722. if e != nil {
  723. log.Warn("fail to add cache, arg=%+v, err=%v", arg, e)
  724. }
  725. return
  726. }