blocked.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. package http
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/main/credit/model"
  7. "go-common/app/admin/main/credit/model/blocked"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. xtime "go-common/library/time"
  12. "go-common/library/xstr"
  13. )
  14. // infos get info list.
  15. func infos(c *bm.Context) {
  16. v := new(blocked.ArgBlockedSearch)
  17. err := c.Bind(v)
  18. if err != nil {
  19. return
  20. }
  21. info, pager, err := creSvc.Infos(c, v)
  22. if err != nil {
  23. log.Error("creSvc.Infos(%+v) error(%v)", v, err)
  24. httpCode(c, err)
  25. return
  26. }
  27. if len(info) == 0 {
  28. httpData(c, nil, pager)
  29. return
  30. }
  31. httpData(c, info, pager)
  32. }
  33. // infoByID get info by id.
  34. func infoByID(c *bm.Context) {
  35. var err error
  36. v := new(struct {
  37. ID int64 `form:"id" validate:"required"`
  38. })
  39. if err = c.Bind(v); err != nil {
  40. return
  41. }
  42. item := &blocked.Info{}
  43. if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
  44. if err != ecode.NothingFound {
  45. log.Error("creSvc.infoByID error(%v)", err)
  46. httpCode(c, err)
  47. return
  48. }
  49. httpData(c, nil, nil)
  50. return
  51. }
  52. if item != nil {
  53. info, err := creSvc.UserInfo(c, item.UID)
  54. if err != nil {
  55. log.Error("creSvc.UserInfo error(%v)", err)
  56. err = nil
  57. }
  58. if info != nil {
  59. item.UName = info.Info.Name
  60. }
  61. item.ReasonTypeDesc = blocked.ReasonTypeDesc(item.ReasonType)
  62. item.BlockedDaysDesc = blocked.BDaysDesc(item.BlockedDays, item.MoralNum, item.PunishType, item.BlockedForever)
  63. item.PublishStatusDesc = blocked.PStatusDesc[item.PublishStatus]
  64. item.OriginTypeDesc = blocked.OriginTypeDesc[item.OriginType]
  65. item.BlockedTypeDesc = blocked.BTypeDesc[item.BlockedType]
  66. }
  67. httpData(c, item, nil)
  68. }
  69. // upInfo update info.
  70. func upInfo(c *bm.Context) {
  71. var err error
  72. v := new(blocked.ArgUpInfo)
  73. if err = c.Bind(v); err != nil {
  74. return
  75. }
  76. item := &blocked.Info{}
  77. if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
  78. log.Error("http.blcokedInfoByID(%d) error(%v)", v.ID, err)
  79. httpCode(c, err)
  80. return
  81. }
  82. var data map[string]interface{}
  83. switch {
  84. case item.PublishStatus == blocked.StatusClose && v.Status == blocked.StatusOpen:
  85. data = map[string]interface{}{
  86. "origin_content_modify": v.Content,
  87. "publish_status": v.Status,
  88. "oper_id": v.OID,
  89. "publish_time": xtime.Time(time.Now().Unix()),
  90. }
  91. default:
  92. data = map[string]interface{}{
  93. "origin_content_modify": v.Content,
  94. "publish_status": v.Status,
  95. "oper_id": v.OID,
  96. }
  97. }
  98. if err = creSvc.DB.Model(&blocked.Info{}).Where("id = ?", v.ID).Updates(data).Error; err != nil {
  99. log.Error("s.dao.UpInfo(%v) error(%v)", v, err)
  100. httpCode(c, err)
  101. return
  102. }
  103. log.Info("business_upInfo:%+v", v)
  104. var multiple []interface{}
  105. single := map[string]interface{}{
  106. "id": item.ID,
  107. "oper_id": v.OID,
  108. "status": item.Status,
  109. "publish_status": v.Status,
  110. }
  111. multiple = append(multiple, single)
  112. creSvc.AddNotify(func() {
  113. creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedInfo, blocked.TableBlockedInfo, multiple)
  114. })
  115. httpCode(c, nil)
  116. }
  117. func delInfo(c *bm.Context) {
  118. var err error
  119. v := new(struct {
  120. IDS []int64 `form:"ids,split" validate:"min=1,max=100"`
  121. OID int64 `form:"op_id" validate:"required"`
  122. Status int8 `form:"status" default:"1"`
  123. })
  124. if err = c.Bind(v); err != nil {
  125. return
  126. }
  127. items := []*blocked.Info{}
  128. if err = creSvc.ReadDB.Where("id IN (?)", v.IDS).Find(&items).Error; err != nil {
  129. log.Error("http.blcokedInfoByIDs(%s) error(%v)", xstr.JoinInts(v.IDS), err)
  130. httpCode(c, err)
  131. return
  132. }
  133. if err = creSvc.DB.Model(&blocked.Info{}).Where("id IN (?)", v.IDS).Updates(map[string]interface{}{"status": v.Status, "oper_id": v.OID}).Error; err != nil {
  134. log.Error("s.dao.delInfo(%v) error(%v)", v, err)
  135. httpCode(c, err)
  136. return
  137. }
  138. log.Info("business_delInfo:%+v", v)
  139. var multiple []interface{}
  140. for _, item := range items {
  141. single := map[string]interface{}{
  142. "id": item.ID,
  143. "oper_id": v.OID,
  144. "status": v.Status,
  145. "publish_status": item.PublishStatus,
  146. }
  147. multiple = append(multiple, single)
  148. }
  149. creSvc.AddNotify(func() {
  150. creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedInfo, blocked.TableBlockedInfo, multiple)
  151. })
  152. httpCode(c, nil)
  153. }
  154. // upInfoStatus update info publish_status.
  155. func upInfoStatus(c *bm.Context) {
  156. var err error
  157. v := new(blocked.ArgUpStatus)
  158. if err = c.Bind(v); err != nil {
  159. return
  160. }
  161. items := []*blocked.Info{}
  162. if err = creSvc.ReadDB.Where("id IN (?)", v.IDS).Find(&items).Error; err != nil {
  163. log.Error("http.blcokedInfoByIDs(%s) error(%v)", xstr.JoinInts(v.IDS), err)
  164. httpCode(c, err)
  165. return
  166. }
  167. if err = creSvc.DB.Model(&blocked.Info{}).Where("id IN (?)", v.IDS).Updates(
  168. map[string]interface{}{
  169. "oper_id": v.OID,
  170. "publish_status": v.Status,
  171. "publish_time": xtime.Time(time.Now().Unix()),
  172. }).Error; err != nil {
  173. log.Error("creSvc.upInfoStatus(%v) error(%v)", v, err)
  174. httpCode(c, err)
  175. return
  176. }
  177. log.Info("business_upInfoStatus:%+v", v)
  178. var multiple []interface{}
  179. for _, item := range items {
  180. single := map[string]interface{}{
  181. "id": item.ID,
  182. "oper_id": v.OID,
  183. "status": item.Status,
  184. "publish_status": v.Status,
  185. }
  186. multiple = append(multiple, single)
  187. }
  188. creSvc.AddNotify(func() {
  189. creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedInfo, blocked.TableBlockedInfo, multiple)
  190. })
  191. httpCode(c, nil)
  192. }
  193. // infosEx export info data.
  194. func infosEx(c *bm.Context) {
  195. v := new(blocked.ArgBlockedSearch)
  196. if err := c.Bind(v); err != nil {
  197. return
  198. }
  199. list, err := creSvc.InfosEx(c, v)
  200. if err != nil {
  201. log.Error("creSvc.InfosEx error(%v)", err)
  202. httpCode(c, err)
  203. return
  204. }
  205. re, err := blocked.DealInfo(list)
  206. if err != nil {
  207. log.Error("blocked.DealInfo error(%v)", err)
  208. httpCode(c, err)
  209. return
  210. }
  211. outBuf(c, creSvc.FormatCSV(re), fmt.Sprintf("%s-%s", time.Now().Format(model.TimeFormatDay), "blocked-info"))
  212. }
  213. func publishs(c *bm.Context) {
  214. v := new(blocked.ArgPublishSearch)
  215. if err := c.Bind(v); err != nil {
  216. return
  217. }
  218. publish, pager, err := creSvc.Publishs(c, v)
  219. if err != nil {
  220. log.Error("creSvc.Publishs(%+v) error(%v)", v, err)
  221. httpCode(c, err)
  222. return
  223. }
  224. if len(publish) == 0 {
  225. httpData(c, nil, pager)
  226. return
  227. }
  228. httpData(c, publish, pager)
  229. }
  230. // publishByID get publish by id.
  231. func publishByID(c *bm.Context) {
  232. var err error
  233. v := new(struct {
  234. ID int64 `form:"id" validate:"required"`
  235. })
  236. if err = c.Bind(v); err != nil {
  237. return
  238. }
  239. item := &blocked.Publish{}
  240. if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
  241. if err != ecode.NothingFound {
  242. log.Error("creSvc.publishByID error(%v)", err)
  243. httpCode(c, err)
  244. return
  245. }
  246. httpData(c, nil, nil)
  247. return
  248. }
  249. if item != nil {
  250. item.PublishTypeDesc = blocked.PTypeDesc[item.Type]
  251. item.PublishStatusDesc = blocked.PStatusDesc[item.PublishStatus]
  252. item.StickStatusDesc = blocked.SStatusDesc[item.StickStatus]
  253. }
  254. httpData(c, item, nil)
  255. }
  256. func addPublish(c *bm.Context) {
  257. var err error
  258. v := new(blocked.ArgPublish)
  259. if err = c.Bind(v); err != nil {
  260. return
  261. }
  262. publish := &blocked.Publish{
  263. Title: v.Title,
  264. SubTitle: v.SubTitle,
  265. PublishStatus: v.PublishStatus,
  266. StickStatus: v.StickStatus,
  267. Content: v.Content,
  268. URL: v.URL,
  269. Type: v.PType,
  270. OPID: v.OID,
  271. ShowTime: xtime.Time(time.Now().Unix()),
  272. }
  273. if v.ShowTime != "" {
  274. var tm time.Time
  275. tm, err = time.ParseInLocation(model.TimeFormatSec, v.ShowTime, time.Local)
  276. if err == nil {
  277. publish.ShowTime = xtime.Time(tm.Unix())
  278. }
  279. }
  280. if err = creSvc.DB.Create(publish).Error; err != nil {
  281. log.Error("creSvc.AddPublish(%+v) error(%v)", v, err)
  282. httpCode(c, err)
  283. return
  284. }
  285. log.Info("business_addPublish:%+v", v)
  286. httpCode(c, nil)
  287. }
  288. func upPublish(c *bm.Context) {
  289. var err error
  290. v := new(blocked.ArgPublish)
  291. if err = c.Bind(v); err != nil {
  292. return
  293. }
  294. item := &blocked.Publish{}
  295. if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
  296. log.Error("http.publishByID(%d) error(%v)", v.ID, err)
  297. httpCode(c, err)
  298. return
  299. }
  300. var showTime xtime.Time
  301. tm, err := time.ParseInLocation(model.TimeFormatSec, v.ShowTime, time.Local)
  302. if err == nil {
  303. showTime = xtime.Time(tm.Unix())
  304. }
  305. if err = creSvc.DB.Model(&blocked.Publish{}).Where("id = ?", v.ID).
  306. Updates(
  307. map[string]interface{}{
  308. "title": v.Title,
  309. "sub_title": v.SubTitle,
  310. "publish_status": v.PublishStatus,
  311. "stick_status": v.StickStatus,
  312. "content": v.Content,
  313. "url": v.URL,
  314. "ptype": v.PType,
  315. "show_time": showTime,
  316. "oper_id": v.OID,
  317. }).Error; err != nil {
  318. log.Error("creSvc.UpPublish(%+v) error(%v)", v, err)
  319. httpCode(c, err)
  320. return
  321. }
  322. log.Info("business_upPublish:%+v", v)
  323. var multiple []interface{}
  324. single := map[string]interface{}{
  325. "id": v.ID,
  326. "title": v.Title,
  327. "sub_title": v.SubTitle,
  328. "oper_id": v.OID,
  329. "ptype": v.PType,
  330. "show_time": showTime.Time().Format(model.TimeFormatSec),
  331. "status": item.Status,
  332. }
  333. multiple = append(multiple, single)
  334. creSvc.AddNotify(func() {
  335. creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedPublish, blocked.TableBlockedPublish, multiple)
  336. })
  337. httpCode(c, nil)
  338. }
  339. func delPublish(c *bm.Context) {
  340. var err error
  341. v := new(struct {
  342. IDS []int64 `form:"ids,split" validate:"required"`
  343. Status int8 `form:"status" default:"1"`
  344. OID int64 `form:"op_id" validate:"required"`
  345. })
  346. if err = c.Bind(v); err != nil {
  347. return
  348. }
  349. if err = creSvc.DB.Model(&blocked.Publish{}).Where(v.IDS).Updates(map[string]interface{}{"status": v.Status, "oper_id": v.OID}).Error; err != nil {
  350. httpCode(c, err)
  351. return
  352. }
  353. log.Info("business_delPublish:%+v", v)
  354. var multiple []interface{}
  355. for _, id := range v.IDS {
  356. single := map[string]interface{}{
  357. "id": id,
  358. "oper_id": v.OID,
  359. "status": v.Status,
  360. }
  361. multiple = append(multiple, single)
  362. }
  363. creSvc.AddNotify(func() {
  364. creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedPublish, blocked.TableBlockedPublish, multiple)
  365. })
  366. httpCode(c, nil)
  367. }
  368. func notices(c *bm.Context) {
  369. var (
  370. err error
  371. order = "id"
  372. sort = "DESC"
  373. )
  374. v := new(struct {
  375. PN int `form:"pn" default:"1"`
  376. PS int `form:"ps" default:"20"`
  377. })
  378. if err = c.Bind(v); err != nil {
  379. return
  380. }
  381. count := int(0)
  382. items := []*blocked.Notice{}
  383. pager := &blocked.Pager{
  384. Total: count,
  385. PN: v.PN,
  386. PS: v.PS,
  387. Order: order,
  388. Sort: sort,
  389. }
  390. if err = creSvc.ReadDB.Offset((v.PN - 1) * v.PS).Limit(v.PS).Order(fmt.Sprintf("%s %s", order, sort)).Find(&items).Error; err != nil {
  391. if err != ecode.NothingFound {
  392. log.Error("creSvc.notices error(%v)", err)
  393. httpData(c, nil, nil)
  394. return
  395. }
  396. httpData(c, nil, pager)
  397. return
  398. }
  399. if err = creSvc.ReadDB.Model(&blocked.Notice{}).Count(&count).Error; err != nil {
  400. if err != ecode.NothingFound {
  401. log.Error("creSvc.notices count error(%v)", err)
  402. httpData(c, nil, nil)
  403. return
  404. }
  405. httpData(c, nil, pager)
  406. return
  407. }
  408. pager.Total = count
  409. for _, v := range items {
  410. v.StatusDesc = blocked.NoticeStateDesc[v.Status]
  411. v.OPName = creSvc.Managers[v.OperID]
  412. }
  413. httpData(c, items, pager)
  414. }
  415. func addNotice(c *bm.Context) {
  416. var err error
  417. v := new(struct {
  418. Content string `form:"content" validate:"required"`
  419. URL string `form:"url" validate:"required"`
  420. OID int64 `form:"op_id" validate:"required"`
  421. })
  422. if err = c.Bind(v); err != nil {
  423. return
  424. }
  425. if err = creSvc.DB.Model(&blocked.Notice{}).Create(&blocked.Notice{Content: v.Content, URL: v.URL, OperID: v.OID}).Error; err != nil {
  426. log.Error("creSvc.addNotice(%v) error(%v)", v, err)
  427. httpCode(c, err)
  428. return
  429. }
  430. log.Info("business_addNotice:%+v", v)
  431. httpCode(c, nil)
  432. }
  433. func upNoticeStatus(c *bm.Context) {
  434. var err error
  435. v := new(struct {
  436. ID int `form:"id" validate:"required"`
  437. Status int8 `form:"status" validate:"min=0,max=1"`
  438. OID int `form:"op_id" validate:"required"`
  439. })
  440. if err = c.Bind(v); err != nil {
  441. return
  442. }
  443. if err = creSvc.DB.Model(&blocked.Notice{}).Where("id=?", v.ID).Updates(map[string]interface{}{"status": v.Status, "oper_id": v.OID}).Error; err != nil {
  444. log.Error("creSvc.upNoticeStatus(%v) error(%v)", v, err)
  445. httpCode(c, err)
  446. return
  447. }
  448. log.Info("business_upNoticeStatus:%+v", v)
  449. httpCode(c, nil)
  450. }