jury.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. package http
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "math"
  8. "sort"
  9. "strconv"
  10. "time"
  11. "go-common/app/admin/main/credit/model"
  12. "go-common/app/admin/main/credit/model/blocked"
  13. "go-common/library/ecode"
  14. "go-common/library/log"
  15. bm "go-common/library/net/http/blademaster"
  16. "go-common/library/net/metadata"
  17. xtime "go-common/library/time"
  18. "go-common/library/xstr"
  19. "github.com/jinzhu/gorm"
  20. )
  21. func users(c *bm.Context) {
  22. v := new(blocked.ArgJurySearch)
  23. if err := c.Bind(v); err != nil {
  24. return
  25. }
  26. list, pager, err := creSvc.Jurys(c, v)
  27. if err != nil {
  28. log.Error("creSvc.Jurys error(%v)", err)
  29. httpCode(c, err)
  30. return
  31. }
  32. if len(list) == 0 {
  33. httpData(c, nil, pager)
  34. return
  35. }
  36. httpData(c, list, pager)
  37. }
  38. func userAdd(c *bm.Context) {
  39. var err error
  40. v := new(blocked.ArgAddJurys)
  41. if err = c.Bind(v); err != nil {
  42. return
  43. }
  44. if err = creSvc.AddJury(c, v); err != nil {
  45. httpCode(c, err)
  46. return
  47. }
  48. log.Info("business_userAdd:%+v", v)
  49. httpCode(c, nil)
  50. }
  51. // userByID get user by id.
  52. func userByID(c *bm.Context) {
  53. var err error
  54. v := new(struct {
  55. MID int64 `form:"mid" validate:"required"`
  56. })
  57. if err = c.Bind(v); err != nil {
  58. return
  59. }
  60. item := &blocked.Jury{}
  61. if err = creSvc.ReadDB.Where("mid = ?", v.MID).Find(item).Error; err != nil {
  62. if err != ecode.NothingFound {
  63. log.Error("creSvc.userByID error(%v)", err)
  64. httpCode(c, err)
  65. return
  66. }
  67. httpData(c, nil, nil)
  68. return
  69. }
  70. if item != nil {
  71. if item.VoteTotal > 0 {
  72. item.VoteRadio = strconv.FormatFloat(float64(item.VoteRight)/float64(item.VoteTotal)*100, 'f', 2, 64)
  73. } else {
  74. item.VoteRadio = "-1"
  75. }
  76. item.StatusDesc = blocked.JuryerStatus[item.Status]
  77. item.BlackDesc = blocked.JuryerStyle[item.Black]
  78. }
  79. httpData(c, item, nil)
  80. }
  81. func upUserStatus(c *bm.Context) {
  82. v := new(struct {
  83. MIDS []int64 `form:"mids,split" validate:"min=1,max=100"`
  84. Status int8 `form:"status" validate:"min=1,max=2" default:"2"`
  85. OID int64 `form:"op_id" validate:"required"`
  86. })
  87. if err := c.Bind(v); err != nil {
  88. return
  89. }
  90. items := []*blocked.Jury{}
  91. if err := creSvc.ReadDB.Where("mid IN (?)", v.MIDS).Find(&items).Error; err != nil {
  92. log.Error("creSvc.juryByMIDs(%s) error(%v)", xstr.JoinInts(v.MIDS), err)
  93. httpCode(c, err)
  94. return
  95. }
  96. now := time.Now()
  97. if err := creSvc.DB.Model(&blocked.Jury{}).Where("mid IN(?)", v.MIDS).Updates(map[string]interface{}{"status": v.Status, "expired": now, "oper_id": v.OID}).Error; err != nil {
  98. httpCode(c, err)
  99. return
  100. }
  101. log.Info("business_upUserStatus:%+v", v)
  102. mids := make(map[int64]*blocked.Jury, len(v.MIDS))
  103. for _, v := range items {
  104. mids[v.UID] = v
  105. }
  106. var multiple []interface{}
  107. for _, mid := range v.MIDS {
  108. single := map[string]interface{}{
  109. "id": mids[mid].ID,
  110. "oper_id": v.OID,
  111. "status": v.Status,
  112. "expired": now.Format(model.TimeFormatSec),
  113. "black": mids[mid].Black,
  114. }
  115. multiple = append(multiple, single)
  116. }
  117. creSvc.AddNotify(func() {
  118. creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedJury, blocked.TableBlockedJury, multiple)
  119. })
  120. httpCode(c, nil)
  121. }
  122. func blackWhite(c *bm.Context) {
  123. var err error
  124. v := new(struct {
  125. MIDS []int64 `form:"mids,split" validate:"min=1,max=100"`
  126. Status int8 `form:"status" validate:"min=1,max=2"`
  127. Black int8 `form:"black"`
  128. ReMark string `form:"remark"`
  129. OID int64 `form:"op_id" validate:"required"`
  130. })
  131. if err = c.Bind(v); err != nil {
  132. return
  133. }
  134. items := []*blocked.Jury{}
  135. if err = creSvc.ReadDB.Where("mid IN (?)", v.MIDS).Find(&items).Error; err != nil {
  136. log.Error("creSvc.juryByMIDs(%s) error(%v)", xstr.JoinInts(v.MIDS), err)
  137. httpCode(c, err)
  138. return
  139. }
  140. mids := make(map[int64]*blocked.Jury, len(v.MIDS))
  141. for _, v := range items {
  142. mids[v.UID] = v
  143. }
  144. now := time.Now()
  145. var multiple []interface{}
  146. switch {
  147. case v.Black == blocked.JuryBlack && v.Status == blocked.JuryStatusOn:
  148. err = creSvc.DB.Model(&blocked.Jury{}).Where("mid IN(?)", v.MIDS).Updates(
  149. map[string]interface{}{
  150. "status": blocked.JuryStatusDown,
  151. "black": blocked.JuryBlack,
  152. "oper_id": v.OID,
  153. "expired": time.Now().Unix(),
  154. "remark": v.ReMark,
  155. }).Error
  156. for _, mid := range v.MIDS {
  157. single := map[string]interface{}{
  158. "id": mids[mid].ID,
  159. "oper_id": v.OID,
  160. "status": v.Status,
  161. "expired": now.Format(model.TimeFormatSec),
  162. "black": blocked.JuryBlack,
  163. }
  164. multiple = append(multiple, single)
  165. }
  166. case v.Black == blocked.JuryWhite && v.Status == blocked.JuryStatusOn:
  167. err = creSvc.DB.Model(&blocked.Jury{}).Where("mid IN(?)", v.MIDS).Updates(
  168. map[string]interface{}{
  169. "black": blocked.JuryWhite,
  170. "oper_id": v.OID,
  171. "remark": v.ReMark,
  172. }).Error
  173. for _, mid := range v.MIDS {
  174. single := map[string]interface{}{
  175. "id": mids[mid].ID,
  176. "oper_id": v.OID,
  177. "black": blocked.JuryWhite,
  178. }
  179. multiple = append(multiple, single)
  180. }
  181. case (v.Black == blocked.JuryBlack && v.Status == blocked.JuryStatusDown) || (v.Black == blocked.JuryWhite && v.Status == blocked.JuryStatusDown):
  182. err = creSvc.DB.Model(&blocked.Jury{}).Where("mid IN(?)", v.MIDS).Updates(
  183. map[string]interface{}{
  184. "black": blocked.JuryNormal,
  185. "oper_id": v.OID,
  186. "remark": v.ReMark,
  187. }).Error
  188. for _, mid := range v.MIDS {
  189. single := map[string]interface{}{
  190. "id": mids[mid].ID,
  191. "oper_id": v.OID,
  192. "black": blocked.JuryNormal,
  193. }
  194. multiple = append(multiple, single)
  195. }
  196. }
  197. if err != nil {
  198. log.Error("blackWhite creSvc.DB error(%v)", err)
  199. httpCode(c, err)
  200. return
  201. }
  202. log.Info("business_blackWhite:%+v", v)
  203. creSvc.AddNotify(func() {
  204. creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedJury, blocked.TableBlockedJury, multiple)
  205. })
  206. httpCode(c, nil)
  207. }
  208. func usersEx(c *bm.Context) {
  209. v := new(blocked.ArgJurySearch)
  210. if err := c.Bind(v); err != nil {
  211. return
  212. }
  213. list, err := creSvc.JurysEx(c, v)
  214. if err != nil {
  215. log.Error("creSvc.InfosEx error(%v)", err)
  216. httpCode(c, err)
  217. return
  218. }
  219. re, err := blocked.DealJury(list)
  220. if err != nil {
  221. log.Error("blocked.DealInfo error(%v)", err)
  222. httpCode(c, err)
  223. return
  224. }
  225. outBuf(c, creSvc.FormatCSV(re), fmt.Sprintf("%s-%s", time.Now().Format(model.TimeFormatDay), "juryer"))
  226. }
  227. func cases(c *bm.Context) {
  228. v := new(blocked.ArgCaseSearch)
  229. if err := c.Bind(v); err != nil {
  230. return
  231. }
  232. list, pager, err := creSvc.Cases(c, v)
  233. if err != nil {
  234. log.Error("creSvc.Publishs(%+v) error(%v)", v, err)
  235. httpCode(c, err)
  236. return
  237. }
  238. if len(list) == 0 {
  239. httpData(c, nil, pager)
  240. return
  241. }
  242. httpData(c, list, pager)
  243. }
  244. // caseByID get case by id.
  245. func caseByID(c *bm.Context) {
  246. var err error
  247. v := new(struct {
  248. ID int64 `form:"id" validate:"required"`
  249. })
  250. if err = c.Bind(v); err != nil {
  251. return
  252. }
  253. item := &blocked.Case{}
  254. if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
  255. if err != ecode.NothingFound {
  256. log.Error("creSvc.caseByID error(%v)", err)
  257. httpCode(c, err)
  258. return
  259. }
  260. httpData(c, nil, nil)
  261. return
  262. }
  263. if item != nil {
  264. item.CaseTypeDesc = blocked.CaseTypeDesc[item.CaseType]
  265. item.StatusDesc = blocked.StatusDesc[item.Status]
  266. item.OriginTypeDesc = blocked.OriginTypeDesc[item.OriginType]
  267. item.ReasonTypeDesc = blocked.ReasonTypeDesc(item.ReasonType)
  268. item.PunishDesc = blocked.PunishDesc[item.PunishResult]
  269. item.VoteTotal = blocked.VoteTotal(item.VoteRule, item.VoteBreak, item.VoteDelete)
  270. if item.VoteRule > 0 || item.VoteBreak > 0 || item.VoteDelete > 0 {
  271. item.BlockedPercent = blocked.BreakPercent(item.VoteRule, item.VoteBreak, item.VoteDelete)
  272. item.DeletePercent = blocked.DeletePercent(item.VoteRule, item.VoteBreak, item.VoteDelete)
  273. item.RulePercent = blocked.RulePercent(item.VoteRule, item.VoteBreak, item.VoteDelete)
  274. }
  275. }
  276. httpData(c, item, nil)
  277. }
  278. // reasons get all case reason.
  279. func reasons(c *bm.Context) {
  280. cr, err := creSvc.CaseReason(c)
  281. if err != nil {
  282. log.Error("creSvc.CaseReason error(%v)", err)
  283. httpCode(c, err)
  284. return
  285. }
  286. httpData(c, cr, nil)
  287. }
  288. func addCase(c *bm.Context) {
  289. var err error
  290. v := new(blocked.ArgCase)
  291. if err = c.Bind(v); err != nil {
  292. return
  293. }
  294. defTime, _ := time.ParseInLocation(model.TimeFormatSec, blocked.DefaultTime, time.Local)
  295. if err = creSvc.DB.Create(
  296. &blocked.Case{
  297. MID: v.UID,
  298. OriginType: v.Otype,
  299. ReasonType: v.ReasonType,
  300. PunishResult: v.PunishResult,
  301. BlockedDays: v.BlockedDays,
  302. OriginTitle: v.OriginTitle,
  303. OriginContent: v.OriginContent,
  304. OriginURL: v.OriginURL,
  305. OPID: v.OID,
  306. RelationID: v.RelationID,
  307. Status: blocked.CaseStatusGrantStop,
  308. StartTime: xtime.Time(defTime.Unix()),
  309. EndTime: xtime.Time(defTime.Unix()),
  310. }).Error; err != nil {
  311. log.Error("creSvc.addCase(%+v) error(%v)", v, err)
  312. httpCode(c, err)
  313. return
  314. }
  315. log.Info("business_addCase:%+v", v)
  316. httpCode(c, nil)
  317. }
  318. func upCase(c *bm.Context) {
  319. var err error
  320. v := new(blocked.ArgCase)
  321. if err = c.Bind(v); err != nil {
  322. return
  323. }
  324. item := &blocked.Case{}
  325. if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
  326. log.Error("creSvc.caseByID(%d) error(%v)", v.ID, err)
  327. httpCode(c, err)
  328. return
  329. }
  330. if err = creSvc.DB.Model(blocked.Case{}).Where("id = ?", v.ID).Updates(
  331. map[string]interface{}{
  332. "mid": v.UID,
  333. "origin_type": v.Otype,
  334. "reason_type": v.ReasonType,
  335. "punish_result": v.PunishResult,
  336. "blocked_days": v.BlockedDays,
  337. "origin_title": v.OriginTitle,
  338. "origin_content": v.OriginContent,
  339. "origin_url": v.OriginURL,
  340. "relation_id": v.RelationID,
  341. "oper_id": v.OID,
  342. }).Error; err != nil {
  343. log.Error("creSvc.upCase(%+v) error(%v)", v, err)
  344. httpCode(c, err)
  345. return
  346. }
  347. log.Info("business_upCase:%+v", v)
  348. var multiple []interface{}
  349. single := map[string]interface{}{
  350. "id": v.ID,
  351. "oper_id": v.OID,
  352. "origin_type": v.Otype,
  353. "mid": v.UID,
  354. "case_type": item.CaseType,
  355. }
  356. multiple = append(multiple, single)
  357. creSvc.AddNotify(func() {
  358. creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedCase, blocked.TableBlockedCase, multiple)
  359. })
  360. httpCode(c, nil)
  361. }
  362. func addCaseVote(c *bm.Context) {
  363. var err error
  364. v := new(struct {
  365. ID int64 `form:"id" validate:"required"`
  366. OPID int64 `form:"op_id" validate:"required"`
  367. VoteRule int64 `form:"vote_rule" default:"0"`
  368. VoteBreak int64 `form:"vote_break" default:"0"`
  369. VoteDelete int64 `form:"vote_delete" default:"0"`
  370. })
  371. if err = c.Bind(v); err != nil {
  372. return
  373. }
  374. item := &blocked.Case{}
  375. if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
  376. log.Error("creSvc.caseByID(%d) error(%v)", v.ID, err)
  377. httpCode(c, err)
  378. return
  379. }
  380. if (v.VoteBreak < 0 && (float64(item.VoteBreak)-math.Abs(float64(v.VoteBreak))) < 0) ||
  381. (v.VoteRule < 0 && (float64(item.VoteRule)-math.Abs(float64(v.VoteRule))) < 0) ||
  382. (v.VoteDelete < 0 && (float64(item.VoteDelete)-math.Abs(float64(v.VoteDelete))) < 0) {
  383. httpCode(c, ecode.RequestErr)
  384. return
  385. }
  386. if err = creSvc.DB.Model(&blocked.Case{}).Where("id = ?", v.ID).UpdateColumns(
  387. map[string]interface{}{
  388. "vote_rule": gorm.Expr("vote_rule + ?", v.VoteRule),
  389. "vote_break": gorm.Expr("vote_break + ?", v.VoteBreak),
  390. "vote_delete": gorm.Expr("vote_delete + ?", v.VoteDelete),
  391. "oper_id": v.OPID,
  392. }).Error; err != nil {
  393. log.Error("creSvc.addCaseVote(%+v) error(%v)", v, err)
  394. httpCode(c, err)
  395. return
  396. }
  397. log.Info("business_addCaseVote:%+v", v)
  398. httpCode(c, nil)
  399. }
  400. func upCaseStatus(c *bm.Context) {
  401. var err error
  402. v := new(blocked.ArgUpStatus)
  403. if err = c.Bind(v); err != nil {
  404. return
  405. }
  406. if err = creSvc.UpCaseStatus(c, v); err != nil {
  407. log.Error("creSvc.UpCaseStatus(%+v) error(%v)", v, err)
  408. httpCode(c, err)
  409. return
  410. }
  411. log.Info("business_upCaseStatus:%+v", v)
  412. httpCode(c, nil)
  413. }
  414. func addCaseType(c *bm.Context) {
  415. var err error
  416. v := new(struct {
  417. IDS []int64 `form:"ids,split" validate:"min=1,max=200"`
  418. Type int8 `form:"type" validate:"min=0,max=1"`
  419. OID int64 `form:"op_id" validate:"required"`
  420. })
  421. if err = c.Bind(v); err != nil {
  422. return
  423. }
  424. if err = creSvc.DB.Model(blocked.Case{}).Where("id IN(?)", v.IDS).Updates(
  425. map[string]interface{}{
  426. "case_type": v.Type,
  427. "oper_id": v.OID,
  428. }).Error; err != nil {
  429. log.Error("creSvc.addCaseType(%+v) error(%v)", v, err)
  430. httpCode(c, err)
  431. return
  432. }
  433. log.Info("business_addCaseType:%+v", v)
  434. var multiple []interface{}
  435. for _, id := range v.IDS {
  436. single := map[string]interface{}{
  437. "id": id,
  438. "oper_id": v.OID,
  439. "case_type": v.Type,
  440. }
  441. multiple = append(multiple, single)
  442. }
  443. creSvc.AddNotify(func() {
  444. creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedCase, blocked.TableBlockedCase, multiple)
  445. })
  446. httpCode(c, nil)
  447. }
  448. func opinions(c *bm.Context) {
  449. v := new(blocked.ArgOpinionSearch)
  450. if err := c.Bind(v); err != nil {
  451. return
  452. }
  453. list, pager, err := creSvc.Opinions(c, v)
  454. if err != nil {
  455. log.Error("creSvc.Opinions(%+v) error(%v)", v, err)
  456. httpCode(c, err)
  457. return
  458. }
  459. if len(list) == 0 {
  460. httpData(c, nil, pager)
  461. return
  462. }
  463. httpData(c, list, pager)
  464. }
  465. // opinionByID get opinion by id.
  466. func opinionByID(c *bm.Context) {
  467. var err error
  468. v := new(struct {
  469. ID int64 `form:"id" validate:"required"`
  470. })
  471. if err = c.Bind(v); err != nil {
  472. return
  473. }
  474. item := &blocked.Opinion{}
  475. if err = creSvc.ReadDB.Where("id = ?", v.ID).Find(item).Error; err != nil {
  476. if err != ecode.NothingFound {
  477. log.Error("creSvc.opinionByID error(%v)", err)
  478. httpCode(c, err)
  479. return
  480. }
  481. httpData(c, nil, nil)
  482. return
  483. }
  484. if item != nil {
  485. item.AttrDesc = blocked.AttrDesc[item.Attr]
  486. item.VoteDesc = blocked.VoteDesc[item.Vote]
  487. item.VoteStateDesc = blocked.VoteStateDesc[item.State]
  488. }
  489. httpData(c, item, nil)
  490. }
  491. func delOpinions(c *bm.Context) {
  492. var err error
  493. v := new(struct {
  494. IDS []int64 `form:"ids,split" validate:"min=1,max=20"`
  495. OID int64 `form:"op_id" validate:"required"`
  496. Status int8 `form:"status" validate:"min=0,max=1" default:"1"`
  497. Send int8 `form:"send" validate:"min=0,max=1"`
  498. })
  499. if err = c.Bind(v); err != nil {
  500. return
  501. }
  502. v.IDS = model.ArrayUnique(v.IDS)
  503. if len(v.IDS) == 0 {
  504. return
  505. }
  506. if err = creSvc.DB.Model(&blocked.Opinion{}).Where("id IN (?)", v.IDS).Updates(map[string]interface{}{"state": v.Status, "oper_id": v.OID}).Error; err != nil {
  507. log.Error("creSvc.delOpinions(%+v) error(%v)", v, err)
  508. httpCode(c, err)
  509. return
  510. }
  511. log.Info("business_delOpinions:%+v", v)
  512. var multiple []interface{}
  513. for _, id := range v.IDS {
  514. single := map[string]interface{}{
  515. "id": id,
  516. "oper_id": v.OID,
  517. "state": v.Status,
  518. }
  519. multiple = append(multiple, single)
  520. }
  521. creSvc.AddNotify(func() {
  522. creSvc.Search.SearchUpdate(context.TODO(), blocked.BusinessBlockedOpinion, blocked.TableBlockedOpinion, multiple)
  523. })
  524. if v.Send == blocked.NeedSendMsg {
  525. items := []*blocked.Opinion{}
  526. if err = creSvc.ReadDB.Where("id IN (?)", v.IDS).Find(&items).Error; err != nil {
  527. log.Error("creSvc.opinionByIDs(%s) error(%v)", xstr.JoinInts(v.IDS), err)
  528. err = nil
  529. return
  530. }
  531. for _, v := range items {
  532. msg := &blocked.SysMsg{
  533. Type: blocked.MsgTypeDelOpinion,
  534. MID: v.MID,
  535. CID: v.CID,
  536. CaseContent: v.Content,
  537. RemoteIP: metadata.String(c, metadata.RemoteIP),
  538. }
  539. creSvc.MsgCh <- msg
  540. }
  541. }
  542. httpCode(c, nil)
  543. }
  544. func kpis(c *bm.Context) {
  545. var (
  546. err error
  547. start, end time.Time
  548. order = "id"
  549. sort = "DESC"
  550. )
  551. v := new(blocked.ArgKpiSearch)
  552. if err = c.Bind(v); err != nil {
  553. return
  554. }
  555. if v.Start == "" {
  556. if start, err = time.ParseInLocation(model.TimeFormatDay, time.Now().AddDate(0, -1, 0).Format(model.TimeFormatDay), time.Local); err != nil {
  557. log.Error("time.ParseInLocation(%s) error(%v)", time.Now().AddDate(0, -1, 0).Format(model.TimeFormatDay), err)
  558. }
  559. } else {
  560. if start, err = time.ParseInLocation(model.TimeFormatSec, v.Start, time.Local); err != nil {
  561. log.Error("time.ParseInLocation(%s) error(%v)", v.Start, err)
  562. }
  563. }
  564. if v.End == "" {
  565. if end, err = time.ParseInLocation(model.TimeFormatDay, time.Now().Format(model.TimeFormatDay), time.Local); err != nil {
  566. log.Error("time.ParseInLocation(%s) error(%v)", time.Now().Format(model.TimeFormatDay), err)
  567. }
  568. } else {
  569. if end, err = time.ParseInLocation(model.TimeFormatSec, v.End, time.Local); err != nil {
  570. log.Error("time.ParseInLocation(%s) error(%v)", v.End, err)
  571. }
  572. }
  573. count := int(0)
  574. items := []*blocked.KPI{}
  575. pager := &blocked.Pager{
  576. Total: count,
  577. PN: v.PN,
  578. PS: v.PS,
  579. Order: order,
  580. Sort: sort,
  581. }
  582. if v.UID > 0 {
  583. err = creSvc.ReadDB.Where("mid=? and day BETWEEN ? AND ? ", v.UID, start, end).Offset((v.PN - 1) * v.PS).Limit(v.PS).Order(fmt.Sprintf("%s %s", order, sort)).Find(&items).Error
  584. creSvc.ReadDB.Model(&blocked.KPI{}).Where("mid=? and day BETWEEN ? AND ? ", v.UID, start, end).Count(&count)
  585. } else {
  586. err = creSvc.ReadDB.Where("day BETWEEN ? AND ? ", start, end).Offset((v.PN - 1) * v.PS).Limit(v.PS).Order(fmt.Sprintf("%s %s", order, sort)).Find(&items).Error
  587. creSvc.ReadDB.Model(&blocked.KPI{}).Where("day BETWEEN ? AND ? ", start, end).Count(&count)
  588. }
  589. if err != nil {
  590. if err != ecode.NothingFound {
  591. log.Error("creSvc.kpis error(%v)", err)
  592. httpCode(c, err)
  593. return
  594. }
  595. httpData(c, nil, pager)
  596. return
  597. }
  598. pager.Total = count
  599. httpData(c, items, pager)
  600. }
  601. func kpiPoints(c *bm.Context) {
  602. v := new(blocked.ArgKpiPointSearch)
  603. if err := c.Bind(v); err != nil {
  604. return
  605. }
  606. list, pager, err := creSvc.KPIPoint(c, v)
  607. if err != nil {
  608. log.Error("creSvc.KPIPoint(%+v) error(%v)", v, err)
  609. httpCode(c, err)
  610. return
  611. }
  612. if len(list) == 0 {
  613. httpData(c, nil, pager)
  614. return
  615. }
  616. httpData(c, list, pager)
  617. }
  618. func kpisEx(c *bm.Context) {
  619. var (
  620. err error
  621. start, end time.Time
  622. items, list []*blocked.KPI
  623. )
  624. v := new(blocked.ArgKpiSearch)
  625. if err = c.Bind(v); err != nil {
  626. return
  627. }
  628. if v.Start == "" {
  629. if start, err = time.ParseInLocation(model.TimeFormatDay, time.Now().AddDate(0, -1, 0).Format(model.TimeFormatDay), time.Local); err != nil {
  630. log.Error("time.ParseInLocation(%s) error(%v)", time.Now().AddDate(0, -1, 0).Format(model.TimeFormatDay), err)
  631. }
  632. } else {
  633. if start, err = time.ParseInLocation(model.TimeFormatSec, v.Start, time.Local); err != nil {
  634. log.Error("time.ParseInLocation(%s) error(%v)", v.Start, err)
  635. }
  636. }
  637. if v.End == "" {
  638. if end, err = time.ParseInLocation(model.TimeFormatDay, time.Now().Format(model.TimeFormatDay), time.Local); err != nil {
  639. log.Error("time.ParseInLocation(%s) error(%v)", time.Now().Format(model.TimeFormatDay), err)
  640. }
  641. } else {
  642. if end, err = time.ParseInLocation(model.TimeFormatSec, v.End, time.Local); err != nil {
  643. log.Error("time.ParseInLocation(%s) error(%v)", v.End, err)
  644. }
  645. }
  646. ps, pn, loop, count := int64(500), int64(1), int64(0), int64(0)
  647. if v.UID > 0 {
  648. err = creSvc.ReadDB.Model(&blocked.KPI{}).Where("mid=? and day BETWEEN ? AND ?", v.UID, start, end).Count(&count).Error
  649. } else {
  650. err = creSvc.ReadDB.Model(&blocked.KPI{}).Where("day BETWEEN ? AND ?", start, end).Count(&count).Error
  651. }
  652. if err != nil {
  653. log.Error("creSvc.kpis count error(%v)", err)
  654. httpCode(c, err)
  655. return
  656. }
  657. if count < 1 {
  658. return
  659. }
  660. items = make([]*blocked.KPI, 0, count)
  661. list = make([]*blocked.KPI, 0, count)
  662. loop = (count / ps) + 1
  663. for pn = 1; pn <= loop; pn++ {
  664. if v.UID > 0 {
  665. err = creSvc.ReadDB.Where("mid=? and day BETWEEN ? AND ?", v.UID, start, end).Offset((pn - 1) * ps).Limit(ps).Order("id DESC").Find(&items).Error
  666. } else {
  667. err = creSvc.ReadDB.Where("day BETWEEN ? AND ?", start, end).Offset((pn - 1) * ps).Limit(ps).Order("id DESC").Find(&items).Error
  668. }
  669. if err != nil {
  670. log.Error("creSvc.kpis error(%v)", err)
  671. httpCode(c, err)
  672. return
  673. }
  674. list = append(list, items...)
  675. }
  676. sort.Slice(list, func(i int, j int) bool {
  677. return list[i].ID < list[j].ID
  678. })
  679. re, err := blocked.DealKPI(list)
  680. if err != nil {
  681. httpCode(c, err)
  682. return
  683. }
  684. outBuf(c, creSvc.FormatCSV(re), fmt.Sprintf("%s-%s", time.Now().Format(model.TimeFormatDay), "kpi"))
  685. }
  686. func caseConf(c *bm.Context) {
  687. cc, err := creSvc.CaseConf(c)
  688. if err != nil {
  689. log.Error("caseConfig error(%v)", err)
  690. httpCode(c, err)
  691. return
  692. }
  693. httpData(c, cc, nil)
  694. }
  695. func setCaseConf(c *bm.Context) {
  696. var err error
  697. v := new(blocked.ArgCaseConf)
  698. if err = c.Bind(v); err != nil {
  699. return
  700. }
  701. if err = creSvc.SetCaseConf(c, v); err != nil {
  702. log.Error("creSvc.SetCaseConf(%+v) error(%v)", v, err)
  703. httpCode(c, err)
  704. return
  705. }
  706. log.Info("business_setCaseConf:%+v", v)
  707. httpCode(c, nil)
  708. }
  709. func autoCaseConfig(c *bm.Context) {
  710. var err error
  711. v := new(struct {
  712. Platform int8 `form:"platform"`
  713. })
  714. if err = c.Bind(v); err != nil {
  715. return
  716. }
  717. item := &blocked.AutoCase{}
  718. if err = creSvc.ReadDB.Where("platform=?", v.Platform).Find(&item).Error; err != nil {
  719. if err != ecode.NothingFound {
  720. log.Error("http.autoCaseConfig error(%v)", err)
  721. httpCode(c, err)
  722. return
  723. }
  724. httpData(c, nil, nil)
  725. return
  726. }
  727. if item.ReasonStr != "" {
  728. if item.Reasons, err = xstr.SplitInts(item.ReasonStr); err != nil {
  729. log.Error("xstr.SplitInts(%s) err(%v)", item.ReasonStr, err)
  730. httpCode(c, err)
  731. return
  732. }
  733. }
  734. httpData(c, item, nil)
  735. }
  736. func setAutoCaseConfig(c *bm.Context) {
  737. var err error
  738. v := new(blocked.ArgAutoCaseConf)
  739. if err = c.Bind(v); err != nil {
  740. return
  741. }
  742. item := &blocked.AutoCase{}
  743. creSvc.ReadDB.Where("id = ?", v.ID).Find(item)
  744. var autoCase *blocked.AutoCase
  745. if item != nil && item.ID != 0 {
  746. autoCase = &blocked.AutoCase{
  747. ID: item.ID,
  748. Platform: item.Platform,
  749. Likes: v.Likes,
  750. ReasonStr: xstr.JoinInts(v.Reasons),
  751. ReportScore: v.ReportScore,
  752. OPID: v.OID,
  753. CTime: item.CTime,
  754. }
  755. } else {
  756. autoCase = &blocked.AutoCase{
  757. Platform: v.Platform,
  758. Likes: v.Likes,
  759. ReasonStr: xstr.JoinInts(v.Reasons),
  760. ReportScore: v.ReportScore,
  761. OPID: v.OID,
  762. }
  763. }
  764. if err = creSvc.DB.Save(&autoCase).Error; err != nil {
  765. log.Error("http.setAutoCaseConfig error(%v)", err)
  766. httpCode(c, err)
  767. return
  768. }
  769. log.Info("business_setAutoCaseConfig:%+v", v)
  770. httpCode(c, nil)
  771. }
  772. func votenumConf(c *bm.Context) {
  773. content, err := creSvc.VotenumConf(c)
  774. if err != nil {
  775. log.Error("caseConfig error(%v)", err)
  776. httpCode(c, err)
  777. return
  778. }
  779. vc := blocked.VoteNum{}
  780. if err = json.Unmarshal([]byte(content), &vc); err != nil {
  781. log.Error("votenumConf_Unmarshal err:%+v", err)
  782. httpCode(c, err)
  783. return
  784. }
  785. httpData(c, vc, nil)
  786. }
  787. func setVotenumConf(c *bm.Context) {
  788. var err error
  789. v := new(blocked.ArgVoteNum)
  790. if err = c.Bind(v); err != nil {
  791. return
  792. }
  793. if err = creSvc.SetVotenumConf(c, v); err != nil {
  794. log.Error("creSvc.setVotenumConf(%+v) error(%v)", v, err)
  795. httpCode(c, err)
  796. return
  797. }
  798. log.Info("business_SetVotenumConf:%+v", v)
  799. httpCode(c, nil)
  800. }
  801. func webHook(c *bm.Context) {
  802. body, err := ioutil.ReadAll(c.Request.Body)
  803. if err != nil {
  804. log.Error("read body fail error(%v)", err)
  805. httpCode(c, ecode.RequestErr)
  806. return
  807. }
  808. if err = creSvc.WebHook(c, body); err != nil {
  809. log.Error("creSvc.WebHook error(%v)", err)
  810. httpCode(c, err)
  811. return
  812. }
  813. httpCode(c, nil)
  814. }