ecode.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. package http
  2. import (
  3. "encoding/json"
  4. "go-common/app/admin/main/apm/model/ecode"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. )
  9. func ecodeList(c *bm.Context) {
  10. v := new(struct {
  11. Code string `form:"code"`
  12. Pn int `form:"pn" default:"1"`
  13. Ps int `form:"ps" default:"20"`
  14. })
  15. var err error
  16. if err = c.Bind(v); err != nil {
  17. return
  18. }
  19. var (
  20. cs []*codes.Codes
  21. count int
  22. lk = "%" + v.Code + "%"
  23. )
  24. if v.Code != "" {
  25. err = apmSvc.DB.Where("CAST(code AS CHAR) LIKE ? OR message LIKE ?", lk, lk).Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&cs).Error
  26. } else {
  27. err = apmSvc.DB.Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&cs).Error
  28. }
  29. if err != nil {
  30. log.Error("apmSvc.EcodeList error(%v)", err)
  31. c.JSON(nil, err)
  32. return
  33. }
  34. if v.Code != "" {
  35. err = apmSvc.DB.Where("CAST(code AS CHAR) LIKE ? OR message LIKE ?", lk, lk).Model(&codes.Codes{}).Count(&count).Error
  36. } else {
  37. err = apmSvc.DB.Model(&codes.Codes{}).Count(&count).Error
  38. }
  39. if err != nil {
  40. log.Error("apmSvc.EcodeList count error(%v)", err)
  41. c.JSON(nil, err)
  42. return
  43. }
  44. data := &Paper{
  45. Pn: v.Pn,
  46. Ps: v.Ps,
  47. Items: cs,
  48. Total: count,
  49. }
  50. c.JSON(data, nil)
  51. }
  52. func ecodeAdd(c *bm.Context) {
  53. v := new(struct {
  54. Code int32 `form:"code" validate:"required"`
  55. Message string `form:"message" validate:"required"`
  56. HantMessage string `form:"hant_message"`
  57. Level int8 `form:"level" default:"2"`
  58. })
  59. var err error
  60. if err = c.Bind(v); err != nil {
  61. return
  62. }
  63. cnt := 0
  64. if err = apmSvc.DB.Model(&codes.Codes{}).Where("code=?", v.Code).Count(&cnt).Error; err != nil {
  65. log.Error("apmSvc.EcodeAdd count error(%v)", err)
  66. c.JSON(nil, ecode.RequestErr)
  67. return
  68. }
  69. username := name(c)
  70. code := &codes.Codes{
  71. Code: v.Code,
  72. Message: v.Message,
  73. HantMessage: v.HantMessage,
  74. Level: v.Level,
  75. Operator: username,
  76. }
  77. if err = apmSvc.DB.Create(&code).Error; err != nil {
  78. log.Error("apmSvc.EcodeAdd create error(%v)", err)
  79. c.JSON(nil, err)
  80. return
  81. }
  82. apmSvc.SendLog(*c, username, 0, 1, code.ID, "apmSvc.EcodeAdd", code)
  83. c.JSON(nil, err)
  84. }
  85. func ecodeEdit(c *bm.Context) {
  86. v := new(struct {
  87. ID int64 `form:"id" validate:"required"`
  88. Code int32 `form:"code" validate:"required"`
  89. Message string `form:"message" validate:"required"`
  90. HantMessage string `form:"hant_message"`
  91. Level int8 `form:"level" default:"2"`
  92. })
  93. var err error
  94. if err = c.Bind(v); err != nil {
  95. return
  96. }
  97. code := &codes.Codes{}
  98. if err = apmSvc.DB.Where("id = ?", v.ID).Find(code).Error; err != nil {
  99. log.Error("apmSvc.EcodeEdit find(%d) error(%v)", v.ID, err)
  100. c.JSON(nil, err)
  101. return
  102. }
  103. username := name(c)
  104. ups := map[string]interface{}{
  105. "code": v.Code,
  106. "message": v.Message,
  107. "operator": username,
  108. "hant_message": v.HantMessage,
  109. "level": v.Level,
  110. }
  111. if err = apmSvc.DB.Model(&codes.Codes{}).Where("id = ?", v.ID).Updates(ups).Error; err != nil {
  112. log.Error("apmSvc.EcodeEdit updates error(%v)", err)
  113. c.JSON(nil, err)
  114. return
  115. }
  116. sqlLog := &map[string]interface{}{
  117. "SQLType": "update",
  118. "Where": "id = ?",
  119. "Value1": v.ID,
  120. "Update": ups,
  121. "Old": code,
  122. }
  123. apmSvc.SendLog(*c, username, 0, 2, v.ID, "apmSvc.EcodeEdit", sqlLog)
  124. c.JSON(nil, err)
  125. }
  126. func ecodeDelete(c *bm.Context) {
  127. v := new(struct {
  128. ID int64 `form:"id" validate:"required"`
  129. })
  130. var err error
  131. if err = c.Bind(v); err != nil {
  132. return
  133. }
  134. code := &codes.Codes{}
  135. if err = apmSvc.DB.Where("id = ?", v.ID).Find(code).Error; err != nil {
  136. log.Error("apmSvc.EcodeDelete find(%d) error(%v)", v.ID, err)
  137. c.JSON(nil, err)
  138. return
  139. }
  140. if err = apmSvc.DB.Delete(code).Error; err != nil {
  141. log.Error("apmSvc.EcodeDelete delete(%d) error(%v)", v.ID, err)
  142. c.JSON(nil, err)
  143. return
  144. }
  145. username := name(c)
  146. apmSvc.SendLog(*c, username, 0, 3, code.ID, "apmSvc.EcodeDelete", code)
  147. c.JSON(nil, err)
  148. }
  149. func getEcodes(c *bm.Context) {
  150. v := new(struct {
  151. Interval1 int `form:"interval1" default:"0"`
  152. Interval2 int `form:"interval2" default:"0"`
  153. })
  154. var err error
  155. if err = c.Bind(v); err != nil {
  156. return
  157. }
  158. codes := []*codes.Codes{}
  159. if err = apmSvc.DB.Where("code >= ? and code <= ?", v.Interval1, v.Interval2).Find(&codes).Error; err != nil {
  160. log.Error("getEcodes error(%v)", err)
  161. c.JSON(nil, err)
  162. return
  163. }
  164. c.JSON(codes, nil)
  165. }
  166. func syncEcodes(c *bm.Context) {
  167. res := map[string]interface{}{}
  168. v := new(struct {
  169. Data string `form:"data"`
  170. })
  171. var err error
  172. if err = c.Bind(v); err != nil {
  173. return
  174. }
  175. username := name(c)
  176. var Codes []*codes.Codes
  177. err = json.Unmarshal([]byte(v.Data), &Codes)
  178. if err != nil {
  179. res["message"] = "解析失败"
  180. c.JSONMap(res, err)
  181. return
  182. }
  183. var in []int32
  184. for _, val := range Codes {
  185. in = append(in, val.Code)
  186. }
  187. oldCodes := []*codes.Codes{}
  188. if err = apmSvc.DB.Where("code in (?)", in).Find(&oldCodes).Error; err != nil {
  189. log.Error("getEcodes error(%v)", err)
  190. c.JSON(nil, err)
  191. return
  192. }
  193. tmp := make(map[int32]struct{})
  194. for _, val := range oldCodes {
  195. tmp[val.Code] = struct{}{}
  196. }
  197. var code *codes.Codes
  198. tx := apmSvc.DB.Begin()
  199. for _, val := range Codes {
  200. code = &codes.Codes{}
  201. code.Code = val.Code
  202. code.Message = val.Message
  203. code.HantMessage = val.HantMessage
  204. code.Level = val.Level
  205. code.Operator = username
  206. if _, ok := tmp[val.Code]; ok {
  207. //update
  208. if err = tx.Model(codes.Codes{}).Where("code = ?", val.Code).Updates(code).Error; err != nil {
  209. tx.Rollback()
  210. c.JSON(nil, err)
  211. return
  212. }
  213. } else {
  214. //create
  215. if err = tx.Create(code).Error; err != nil {
  216. tx.Rollback()
  217. c.JSON(nil, err)
  218. return
  219. }
  220. }
  221. }
  222. tx.Commit()
  223. c.JSON(nil, err)
  224. }
  225. func getProdEcodes(c *bm.Context) {
  226. v := new(struct {
  227. Interval1 string `form:"interval1" default:"0"`
  228. Interval2 string `form:"interval2" default:"0"`
  229. })
  230. var err error
  231. if err = c.Bind(v); err != nil {
  232. return
  233. }
  234. Data, err := apmSvc.GetCodes(c, v.Interval1, v.Interval2)
  235. if err != nil {
  236. c.JSON(nil, err)
  237. return
  238. }
  239. c.JSON(Data, nil)
  240. }
  241. func codeLangsList(c *bm.Context) {
  242. v := new(struct {
  243. Code string `form:"code"`
  244. Pn int `form:"pn" default:"1"`
  245. Ps int `form:"ps" default:"20"`
  246. })
  247. var err error
  248. if err = c.Bind(v); err != nil {
  249. return
  250. }
  251. var (
  252. cs []*codes.Codes
  253. count int
  254. lk = "%" + v.Code + "%"
  255. in []int64
  256. ts []*codes.CodeMsg
  257. res []*codes.NewCodes
  258. )
  259. if v.Code != "" {
  260. err = apmSvc.DB.Where("CAST(code AS CHAR) LIKE ? OR message LIKE ?", lk, lk).Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&cs).Error
  261. } else {
  262. err = apmSvc.DB.Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&cs).Error
  263. }
  264. if err != nil {
  265. log.Error("apmSvc.EcodeList error(%v)", err)
  266. c.JSON(nil, err)
  267. return
  268. }
  269. for _, val := range cs {
  270. in = append(in, val.ID)
  271. }
  272. err = apmSvc.DB.Where("code_id in(?)", in).Find(&ts).Error
  273. if err != nil {
  274. log.Error("apmSvc.EcodeList error(%v)", err)
  275. c.JSON(nil, err)
  276. return
  277. }
  278. for _, val := range cs {
  279. list := make([]*codes.CodeMsg, 0)
  280. for _, vv := range ts {
  281. if vv.CodeID == val.ID {
  282. list = append(list, vv)
  283. }
  284. }
  285. res = append(res, &codes.NewCodes{Codes: val, List: list})
  286. }
  287. if v.Code != "" {
  288. err = apmSvc.DB.Where("CAST(code AS CHAR) LIKE ? OR message LIKE ?", lk, lk).Model(&codes.Codes{}).Count(&count).Error
  289. } else {
  290. err = apmSvc.DB.Model(&codes.Codes{}).Count(&count).Error
  291. }
  292. if err != nil {
  293. log.Error("apmSvc.EcodeList count error(%v)", err)
  294. c.JSON(nil, err)
  295. return
  296. }
  297. data := &Paper{
  298. Pn: v.Pn,
  299. Ps: v.Ps,
  300. Items: res,
  301. Total: count,
  302. }
  303. c.JSON(data, nil)
  304. }
  305. func ecodeLangsAdd(c *bm.Context) {
  306. res := map[string]interface{}{}
  307. v := new(struct {
  308. Data string `form:"data" validate:"required"`
  309. })
  310. var err error
  311. if err = c.Bind(v); err != nil {
  312. return
  313. }
  314. var Codes []*codes.CodeMsg
  315. err = json.Unmarshal([]byte(v.Data), &Codes)
  316. if err != nil {
  317. res["message"] = "解析失败"
  318. c.JSONMap(res, err)
  319. return
  320. }
  321. username := name(c)
  322. for _, val := range Codes {
  323. if !(val.CodeID > 0 && len(val.Locale) > 0 && len(val.Msg) > 0) {
  324. err = ecode.RequestErr
  325. res["message"] = "数据有误"
  326. c.JSONMap(res, err)
  327. return
  328. }
  329. code := &codes.CodeMsg{
  330. CodeID: val.CodeID,
  331. Locale: val.Locale,
  332. Msg: val.Msg,
  333. Operator: username,
  334. }
  335. if err = apmSvc.DB.Create(&code).Error; err != nil {
  336. log.Error("apmSvc.ecodeLangsAdd create error(%v)", err)
  337. c.JSON(nil, err)
  338. return
  339. }
  340. apmSvc.SendLog(*c, username, 0, 1, code.ID, "apmSvc.ecodeLangsAdd", code)
  341. }
  342. c.JSON(nil, err)
  343. }
  344. func ecodeLangsEdit(c *bm.Context) {
  345. res := map[string]interface{}{}
  346. v := new(struct {
  347. Data string `form:"data" validate:"required"`
  348. })
  349. var err error
  350. if err = c.Bind(v); err != nil {
  351. return
  352. }
  353. var Codes []*codes.CodeMsg
  354. err = json.Unmarshal([]byte(v.Data), &Codes)
  355. if err != nil {
  356. res["message"] = "解析失败"
  357. c.JSONMap(res, err)
  358. return
  359. }
  360. username := name(c)
  361. for _, val := range Codes {
  362. code := &codes.CodeMsg{}
  363. if err = apmSvc.DB.Where("id = ?", val.ID).First(code).Error; err != nil {
  364. log.Error("apmSvc.ecodeLangsEdit find(%d) error(%v)", val.ID, err)
  365. c.JSON(nil, err)
  366. return
  367. }
  368. ups := map[string]interface{}{
  369. "operator": username,
  370. "msg": val.Msg,
  371. "locale": val.Locale,
  372. }
  373. if err = apmSvc.DB.Model(&codes.CodeMsg{}).Where("id = ?", val.ID).Updates(ups).Error; err != nil {
  374. log.Error("apmSvc.ecodeLangsEdit updates error(%v)", err)
  375. c.JSON(nil, err)
  376. return
  377. }
  378. sqlLog := &map[string]interface{}{
  379. "SQLType": "update",
  380. "Where": "id = ?",
  381. "Value1": val.ID,
  382. "Update": ups,
  383. "Old": code,
  384. }
  385. apmSvc.SendLog(*c, username, 0, 2, val.ID, "apmSvc.ecodeLangsEdit", sqlLog)
  386. }
  387. c.JSON(nil, err)
  388. }
  389. func ecodeLangsDelete(c *bm.Context) {
  390. v := new(struct {
  391. CID int64 `form:"cid" validate:"required"`
  392. })
  393. var err error
  394. if err = c.Bind(v); err != nil {
  395. return
  396. }
  397. code := &codes.CodeMsg{}
  398. if err = apmSvc.DB.Where("id = ?", v.CID).First(code).Error; err != nil {
  399. log.Error("apmSvc.ecodeLangsDelete find(%d) error(%v)", v.CID, err)
  400. c.JSON(nil, err)
  401. return
  402. }
  403. if err = apmSvc.DB.Delete(code).Error; err != nil {
  404. log.Error("apmSvc.ecodeLangsDelete delete(%d) error(%v)", v.CID, err)
  405. c.JSON(nil, err)
  406. return
  407. }
  408. username := name(c)
  409. apmSvc.SendLog(*c, username, 0, 3, code.ID, "apmSvc.ecodeLangsDelete", code)
  410. c.JSON(nil, err)
  411. }
  412. func ecodeLangsSave(c *bm.Context) {
  413. res := map[string]interface{}{}
  414. v := new(struct {
  415. Data string `form:"data" validate:"required"`
  416. })
  417. var err error
  418. if err = c.Bind(v); err != nil {
  419. return
  420. }
  421. var Codes []*codes.CodeMsg
  422. err = json.Unmarshal([]byte(v.Data), &Codes)
  423. if err != nil {
  424. res["message"] = "解析失败"
  425. c.JSONMap(res, err)
  426. return
  427. }
  428. username := name(c)
  429. for _, val := range Codes {
  430. code := &codes.CodeMsg{}
  431. if val.ID == 0 {
  432. if !(val.CodeID > 0 && len(val.Locale) > 0 && len(val.Msg) > 0) {
  433. err = ecode.RequestErr
  434. res["message"] = "数据有误"
  435. c.JSONMap(res, err)
  436. return
  437. }
  438. code = &codes.CodeMsg{
  439. CodeID: val.CodeID,
  440. Locale: val.Locale,
  441. Msg: val.Msg,
  442. Operator: username,
  443. }
  444. if err = apmSvc.DB.Create(&code).Error; err != nil {
  445. log.Error("apmSvc.ecodeLangsSvae create error(%v)", err)
  446. c.JSON(nil, err)
  447. return
  448. }
  449. apmSvc.SendLog(*c, username, 0, 1, code.ID, "apmSvc.ecodeLangsAdd", code)
  450. } else {
  451. if err = apmSvc.DB.Where("id = ?", val.ID).First(code).Error; err != nil {
  452. log.Error("apmSvc.ecodeLangsSvae find(%d) error(%v)", val.ID, err)
  453. c.JSON(nil, err)
  454. return
  455. }
  456. ups := map[string]interface{}{
  457. "operator": username,
  458. "msg": val.Msg,
  459. "locale": val.Locale,
  460. }
  461. if err = apmSvc.DB.Model(&codes.CodeMsg{}).Where("id = ?", val.ID).Updates(ups).Error; err != nil {
  462. log.Error("apmSvc.ecodeLangsSave updates error(%v)", err)
  463. c.JSON(nil, err)
  464. return
  465. }
  466. sqlLog := &map[string]interface{}{
  467. "SQLType": "update",
  468. "Where": "id = ?",
  469. "Value1": val.ID,
  470. "Update": ups,
  471. "Old": code,
  472. }
  473. apmSvc.SendLog(*c, username, 0, 2, val.ID, "apmSvc.ecodeLangsSave", sqlLog)
  474. }
  475. }
  476. c.JSON(nil, err)
  477. }