sign.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package mcndao
  2. import (
  3. "time"
  4. adminmodel "go-common/app/admin/main/mcn/model"
  5. "go-common/app/interface/main/mcn/model"
  6. "go-common/app/interface/main/mcn/model/mcnmodel"
  7. "go-common/app/interface/main/mcn/tool/validate"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "github.com/jinzhu/gorm"
  11. "github.com/pkg/errors"
  12. )
  13. var (
  14. signNotInStates = []model.MCNSignState{model.MCNSignStateOnDelete, model.MCNSignStateOnPreOpen, model.MCNSignStateOnExpire, model.MCNSignStateOnClear}
  15. // 从高到低,先找到的返回
  16. // 状态优先级:
  17. // 0: 封禁
  18. // 1: 签约
  19. // 2:
  20. statePriority = []model.MCNSignState{
  21. model.MCNSignStateOnBlock,
  22. model.MCNSignStateOnSign,
  23. model.MCNSignStateOnReject,
  24. model.MCNSignStateOnReview,
  25. model.MCNSignStateNoApply,
  26. }
  27. // mcnSign = mcnmodel.McnSign{}
  28. // mcnUp = mcnmodel.McnUp{}
  29. // UpPermissionApplyCannotApplyStates 在这此状态下,不能再申请改变Up主
  30. UpPermissionApplyCannotApplyStates = []adminmodel.MCNUPPermissionState{
  31. adminmodel.MCNUPPermissionStateNoAuthorize, // 待Up主同意
  32. adminmodel.MCNUPPermissionStateReview, // 待审核
  33. }
  34. // UpSignedStates up signed state
  35. UpSignedStates = []model.MCNUPState{
  36. model.MCNUPStateOnSign,
  37. model.MCNUPStateOnPreOpen,
  38. }
  39. )
  40. // GetMcnSignState .
  41. // mcnList, it's all mcn sign found with the state
  42. // state, the mcn's state of qualified, if multiple state found, will be return in priority
  43. func (d *Dao) GetMcnSignState(fields string, mcnMid int64) (mcn *mcnmodel.McnSign, state model.MCNSignState, err error) {
  44. var mcnList []*mcnmodel.McnSign
  45. if err = d.mcndb.Select(fields).Where("mcn_mid=? and state not in(?)", mcnMid, signNotInStates).Find(&mcnList).Error; err != nil {
  46. err = errors.WithStack(err)
  47. return
  48. }
  49. if len(mcnList) == 0 {
  50. log.Warn("mcn not exist, mcn id=%d", mcnMid)
  51. err = ecode.NothingFound
  52. return
  53. }
  54. var stateMap = make(map[model.MCNSignState]*mcnmodel.McnSign)
  55. for _, v := range mcnList {
  56. stateMap[model.MCNSignState(v.State)] = v
  57. }
  58. for _, v := range statePriority {
  59. if mcnValue, ok := stateMap[v]; ok {
  60. state = v
  61. mcn = mcnValue
  62. break
  63. }
  64. }
  65. return
  66. }
  67. // GetUpBind .
  68. func (d *Dao) GetUpBind(query interface{}, args ...interface{}) (upList []*mcnmodel.McnUp, err error) {
  69. if err = d.mcndb.Select("*").Where(query, args...).Find(&upList).Error; err != nil {
  70. if err == gorm.ErrRecordNotFound {
  71. err = nil
  72. } else {
  73. log.Error("fail to get bind up from db, err=%s", err)
  74. return
  75. }
  76. }
  77. return
  78. }
  79. // BindUp .
  80. func (d *Dao) BindUp(up *mcnmodel.McnUp, sign *mcnmodel.McnSign, arg *mcnmodel.McnBindUpApplyReq) (result *mcnmodel.McnUp, affectedRow int64, err error) {
  81. if arg == nil || sign == nil {
  82. return nil, 0, ecode.ServerErr
  83. }
  84. var db *gorm.DB
  85. if up == nil {
  86. up = &mcnmodel.McnUp{
  87. SignID: sign.ID,
  88. }
  89. }
  90. arg.CopyTo(up)
  91. // 如果绑定自己,那么直接接受
  92. if sign.McnMid == arg.UpMid {
  93. up.State = model.MCNUPStateOnSign
  94. // 签约周期为MCN的签约周期
  95. if up.BeginDate == 0 {
  96. up.BeginDate = sign.BeginDate
  97. }
  98. if up.EndDate == 0 {
  99. up.EndDate = sign.EndDate
  100. }
  101. var (
  102. now = time.Now()
  103. date = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
  104. )
  105. if up.BeginDate.Time().After(date) {
  106. up.State = model.MCNUPStateOnPreOpen
  107. }
  108. } else {
  109. // 清除状态为未授权
  110. up.State = model.MCNUPStateNoAuthorize
  111. if !validate.RegHTTPCheck.MatchString(arg.ContractLink) || !validate.RegHTTPCheck.MatchString(arg.UpAuthLink) {
  112. log.Error("contract link or up auth link is not http, arg=%v", arg)
  113. err = ecode.RequestErr
  114. return
  115. }
  116. }
  117. // 判断开始时间与结束时间
  118. if up.BeginDate == 0 || up.EndDate < up.BeginDate {
  119. log.Error("begin date is after end date, arg=%v", arg)
  120. err = ecode.MCNUpBindUpDateError
  121. return
  122. }
  123. db = d.mcndb.Save(up)
  124. affectedRow, err = db.RowsAffected, db.Error
  125. if err != nil {
  126. log.Error("save bind up info fail, err=%s, sign=%v", err, sign)
  127. err = ecode.ServerErr
  128. }
  129. result = up
  130. return
  131. }
  132. // UpdateBindUp .
  133. func (d *Dao) UpdateBindUp(values map[string]interface{}, query interface{}, args ...interface{}) (affectedRow int64, err error) {
  134. var db = d.mcndb.Table(mcnmodel.TableNameMcnUp).Where(query, args...).Updates(values)
  135. affectedRow, err = db.RowsAffected, db.Error
  136. if err != nil {
  137. log.Error("fail to update bind up, err=%s", err)
  138. }
  139. return
  140. }
  141. //UpConfirm up confrim
  142. func (d *Dao) UpConfirm(arg *mcnmodel.McnUpConfirmReq, state model.MCNUPState) (err error) {
  143. var tx = d.mcndb.Begin()
  144. defer func() {
  145. if r := recover(); r != nil || err != nil {
  146. tx.Rollback()
  147. }
  148. }()
  149. var changeMap = map[string]interface{}{
  150. "state": state,
  151. "state_change_time": time.Now(),
  152. }
  153. if arg.Choice {
  154. changeMap["confirm_time"] = time.Now()
  155. }
  156. err = tx.Table(mcnmodel.TableNameMcnUp).
  157. Where("id=? and up_mid=? and state=?", arg.BindID, arg.UpMid, model.MCNUPStateNoAuthorize).
  158. Updates(changeMap).Error
  159. if err != nil {
  160. log.Error("fail to update db, err=%s", err)
  161. return
  162. }
  163. // 表示同意
  164. if arg.Choice {
  165. // 驳回其他的绑定请求
  166. err = tx.Table(mcnmodel.TableNameMcnUp).
  167. Where("id !=? and up_mid=? and state=?", arg.BindID, arg.UpMid, model.MCNUPStateNoAuthorize).
  168. Updates(map[string]interface{}{
  169. "state": model.MCNUPStateOnRefuse,
  170. "state_change_time": time.Now(),
  171. }).Error
  172. if err != nil {
  173. log.Error("fail to update db, err=%s", err)
  174. return
  175. }
  176. }
  177. return tx.Commit().Error
  178. }
  179. // GetBindInfo .
  180. func (d *Dao) GetBindInfo(arg *mcnmodel.McnUpGetBindReq) (res *mcnmodel.McnGetBindReply, err error) {
  181. var result mcnmodel.McnGetBindReply
  182. err = d.mcndb.Raw(`select s.company_name, s.mcn_mid, u.up_auth_link, u.id as bind_id, u.permission as new_permission
  183. from mcn_up as u inner join mcn_sign as s
  184. on s.id = u.sign_id
  185. where u.id = ? and u.up_mid=? and u.state = 0;`, arg.BindID, arg.UpMid).Find(&result).Error
  186. res = &result
  187. return
  188. }
  189. //GetMcnOldInfo 获取冷却中的信息
  190. func (d *Dao) GetMcnOldInfo(mcnMid int64) (res *mcnmodel.McnSign, err error) {
  191. res = new(mcnmodel.McnSign)
  192. err = d.mcndb.Where("mcn_mid=? and state=?", mcnMid, model.MCNSignStateOnCooling).Order("id desc").Limit(1).Find(res).Error
  193. if err != nil && err != gorm.ErrRecordNotFound {
  194. log.Error("fail to get db, err=%s", err)
  195. return
  196. }
  197. return
  198. }
  199. //GetUpPermissionApply 从permission apply表中读取数据
  200. func (d *Dao) GetUpPermissionApply(fields string, query interface{}, args ...interface{}) (res []*mcnmodel.McnUpPermissionApply, err error) {
  201. err = d.mcndb.Select(fields).Where(query, args...).Find(&res).Error
  202. if err != nil {
  203. log.Error("fail to get db, err=%v", err)
  204. return
  205. }
  206. return
  207. }
  208. // GetUpPermissionBindInfo .
  209. func (d *Dao) GetUpPermissionBindInfo(arg *mcnmodel.McnUpGetBindReq) (res *mcnmodel.McnGetBindReply, err error) {
  210. var result mcnmodel.McnGetBindReply
  211. err = d.mcndb.Raw(`select s.company_name, u.mcn_mid, u.up_auth_link, u.id as bind_id, u.old_permission, u.new_permission
  212. from mcn_up_permission_apply as u inner join mcn_sign as s
  213. on s.id = u.sign_id
  214. where u.id = ? and u.up_mid=? and u.state = 0;`, arg.BindID, arg.UpMid).Find(&result).Error
  215. res = &result
  216. return
  217. }
  218. //UpPermissionConfirm up confrim
  219. func (d *Dao) UpPermissionConfirm(arg *mcnmodel.McnUpConfirmReq, state adminmodel.MCNUPPermissionState) (err error) {
  220. var db = d.mcndb
  221. var changeMap = map[string]interface{}{
  222. "state": state,
  223. }
  224. err = db.Table(mcnmodel.TableMcnUpPermissionApply).
  225. Where("id=? and up_mid=? and state=?", arg.BindID, arg.UpMid, adminmodel.MCNUPPermissionStateNoAuthorize).
  226. Updates(changeMap).Error
  227. if err != nil {
  228. log.Error("fail to update db, err=%s", err)
  229. return
  230. }
  231. return
  232. }