academy_occupation.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. package http
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "time"
  7. "go-common/app/admin/main/creative/model/academy"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/binding"
  12. "go-common/library/net/http/blademaster/render"
  13. "go-common/library/sync/errgroup"
  14. "go-common/library/xstr"
  15. "github.com/jinzhu/gorm"
  16. )
  17. func addOccupation(c *bm.Context) {
  18. var (
  19. err error
  20. now = time.Now().Format("2006-01-02 15:04:05")
  21. )
  22. v := new(academy.Occupation)
  23. if err = c.Bind(v); err != nil {
  24. log.Error("addOccupation c.Bind error(%v)", err)
  25. return
  26. }
  27. m := &academy.Occupation{
  28. State: academy.StateNormal,
  29. Name: v.Name,
  30. Desc: v.Desc,
  31. Logo: v.Logo,
  32. MainStep: v.MainStep,
  33. MainSoftware: v.MainSoftware,
  34. CTime: now,
  35. MTime: now,
  36. }
  37. tx := svc.DB.Begin()
  38. if err = tx.Create(m).Error; err != nil {
  39. log.Error("academy addOccupation error(%v)", err)
  40. c.JSON(nil, err)
  41. tx.Rollback()
  42. return
  43. }
  44. if err = tx.Model(&academy.Occupation{}).Where("id=?", m.ID).Updates(map[string]interface{}{
  45. "rank": m.ID,
  46. }).Error; err != nil {
  47. log.Error("academy addOccupation error(%v)", err)
  48. tx.Rollback()
  49. }
  50. tx.Commit()
  51. uid, uname := getUIDName(c)
  52. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "添加职业", TID: m.ID, OName: m.Name})
  53. c.JSON(map[string]interface{}{
  54. "id": m.ID,
  55. }, nil)
  56. }
  57. func upOccupation(c *bm.Context) {
  58. var (
  59. oc = &academy.Occupation{}
  60. err error
  61. )
  62. v := new(academy.Occupation)
  63. if err = c.Bind(v); err != nil {
  64. return
  65. }
  66. if v.ID == 0 {
  67. c.JSON(nil, ecode.RequestErr)
  68. return
  69. }
  70. if err = svc.DB.Where("id=?", v.ID).Find(oc).Error; err != nil {
  71. c.JSON(nil, err)
  72. return
  73. }
  74. if oc == nil {
  75. c.JSON(nil, ecode.NothingFound)
  76. return
  77. }
  78. if err = svc.DB.Model(&academy.Occupation{}).Where("id=?", v.ID).Updates(map[string]interface{}{
  79. "name": v.Name,
  80. "desc": v.Desc,
  81. "logo": v.Logo,
  82. "main_step": v.MainStep,
  83. "main_software": v.MainSoftware,
  84. }).Error; err != nil {
  85. log.Error("academy bindOccupation error(%v)", err)
  86. }
  87. uid, uname := getUIDName(c)
  88. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "更新职业", TID: v.ID, OName: v.Name})
  89. c.JSON(nil, err)
  90. }
  91. func bindOccupation(c *bm.Context) {
  92. var (
  93. oc = &academy.Occupation{}
  94. err error
  95. )
  96. v := new(academy.Occupation)
  97. if err = c.Bind(v); err != nil {
  98. return
  99. }
  100. if v.ID == 0 {
  101. c.JSON(nil, ecode.RequestErr)
  102. return
  103. }
  104. if err = svc.DB.Where("id=?", v.ID).Find(oc).Error; err != nil {
  105. c.JSON(nil, err)
  106. return
  107. }
  108. if oc == nil {
  109. c.JSON(nil, ecode.NothingFound)
  110. return
  111. }
  112. if err = svc.DB.Model(&academy.Occupation{}).Where("id=?", v.ID).Updates(map[string]interface{}{
  113. "state": v.State,
  114. }).Error; err != nil {
  115. log.Error("academy upOccupation error(%v)", err)
  116. }
  117. uid, uname := getUIDName(c)
  118. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "更新职业", TID: v.ID, OName: v.Name})
  119. c.JSON(nil, err)
  120. }
  121. func addSkill(c *bm.Context) {
  122. var (
  123. err error
  124. now = time.Now().Format("2006-01-02 15:04:05")
  125. )
  126. v := new(academy.Skill)
  127. if err = c.Bind(v); err != nil {
  128. log.Error("addSkill c.Bind error(%v)", err)
  129. return
  130. }
  131. m := &academy.Skill{
  132. State: academy.StateNormal,
  133. OID: v.OID,
  134. Name: v.Name,
  135. Desc: v.Desc,
  136. CTime: now,
  137. MTime: now,
  138. }
  139. if err = svc.DB.Create(m).Error; err != nil {
  140. log.Error("academy addSkill error(%v)", err)
  141. c.JSON(nil, err)
  142. return
  143. }
  144. uid, uname := getUIDName(c)
  145. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "添加步骤", TID: m.ID, OName: m.Name})
  146. c.JSON(map[string]interface{}{
  147. "id": m.ID,
  148. }, nil)
  149. }
  150. func upSkill(c *bm.Context) {
  151. var (
  152. sk = &academy.Skill{}
  153. err error
  154. )
  155. v := new(academy.Skill)
  156. if err = c.Bind(v); err != nil {
  157. return
  158. }
  159. if v.ID == 0 {
  160. c.JSON(nil, ecode.RequestErr)
  161. return
  162. }
  163. if err = svc.DB.Where("id=?", v.ID).Find(sk).Error; err != nil {
  164. c.JSON(nil, err)
  165. return
  166. }
  167. if sk == nil {
  168. c.JSON(nil, ecode.NothingFound)
  169. return
  170. }
  171. if err = svc.DB.Model(&academy.Skill{}).Where("id=?", v.ID).Updates(map[string]interface{}{
  172. "name": v.Name,
  173. "desc": v.Desc,
  174. }).Error; err != nil {
  175. log.Error("academy upSkill error(%v)", err)
  176. }
  177. uid, uname := getUIDName(c)
  178. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "更新步骤", TID: v.ID, OName: v.Name})
  179. c.JSON(nil, err)
  180. }
  181. func bindSkill(c *bm.Context) {
  182. var (
  183. oc = &academy.Skill{}
  184. err error
  185. )
  186. v := new(academy.Skill)
  187. if err = c.Bind(v); err != nil {
  188. return
  189. }
  190. if v.ID == 0 {
  191. c.JSON(nil, ecode.RequestErr)
  192. return
  193. }
  194. if err = svc.DB.Where("id=?", v.ID).Find(oc).Error; err != nil {
  195. c.JSON(nil, err)
  196. return
  197. }
  198. if oc == nil {
  199. c.JSON(nil, ecode.NothingFound)
  200. return
  201. }
  202. if err = svc.DB.Model(&academy.Skill{}).Where("id=?", v.ID).Updates(map[string]interface{}{
  203. "state": v.State,
  204. }).Error; err != nil {
  205. log.Error("academy bindSkill error(%v)", err)
  206. }
  207. uid, uname := getUIDName(c)
  208. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "更新职业", TID: v.ID, OName: v.Name})
  209. c.JSON(nil, err)
  210. }
  211. func addSoftware(c *bm.Context) {
  212. var (
  213. err error
  214. now = time.Now().Format("2006-01-02 15:04:05")
  215. )
  216. v := new(academy.Software)
  217. if err = c.Bind(v); err != nil {
  218. return
  219. }
  220. m := &academy.Software{
  221. State: academy.StateNormal,
  222. SkID: v.SkID,
  223. Name: v.Name,
  224. Desc: v.Desc,
  225. CTime: now,
  226. MTime: now,
  227. }
  228. if err = svc.DB.Create(m).Error; err != nil {
  229. log.Error("academy addSoftware error(%v)", err)
  230. c.JSON(nil, err)
  231. return
  232. }
  233. uid, uname := getUIDName(c)
  234. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "添加步骤", TID: m.ID, OName: m.Name})
  235. c.JSON(map[string]interface{}{
  236. "id": m.ID,
  237. }, nil)
  238. }
  239. func upSoftware(c *bm.Context) {
  240. var (
  241. oc = &academy.Software{}
  242. err error
  243. )
  244. v := new(academy.Software)
  245. if err = c.Bind(v); err != nil {
  246. return
  247. }
  248. if v.ID == 0 {
  249. c.JSON(nil, ecode.RequestErr)
  250. return
  251. }
  252. if err = svc.DB.Where("id=?", v.ID).Find(oc).Error; err != nil {
  253. c.JSON(nil, err)
  254. return
  255. }
  256. if oc == nil {
  257. c.JSON(nil, ecode.NothingFound)
  258. return
  259. }
  260. if err = svc.DB.Model(&academy.Software{}).Where("id=?", v.ID).Updates(map[string]interface{}{
  261. "name": v.Name,
  262. "desc": v.Desc,
  263. }).Error; err != nil {
  264. log.Error("academy upSoftware error(%v)", err)
  265. }
  266. uid, uname := getUIDName(c)
  267. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "更新步骤", TID: v.ID, OName: v.Name})
  268. c.JSON(nil, err)
  269. }
  270. func bindSoftware(c *bm.Context) {
  271. var (
  272. oc = &academy.Software{}
  273. err error
  274. )
  275. v := new(academy.Software)
  276. if err = c.Bind(v); err != nil {
  277. return
  278. }
  279. if v.ID == 0 {
  280. c.JSON(nil, ecode.RequestErr)
  281. return
  282. }
  283. if err = svc.DB.Where("id=?", v.ID).Find(oc).Error; err != nil {
  284. c.JSON(nil, err)
  285. return
  286. }
  287. if oc == nil {
  288. c.JSON(nil, ecode.NothingFound)
  289. return
  290. }
  291. if err = svc.DB.Model(&academy.Software{}).Where("id=?", v.ID).Updates(map[string]interface{}{
  292. "state": v.State,
  293. }).Error; err != nil {
  294. log.Error("academy bindSoftware error(%v)", err)
  295. }
  296. uid, uname := getUIDName(c)
  297. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "更新职业", TID: v.ID, OName: v.Name})
  298. c.JSON(nil, err)
  299. }
  300. func listOccupation(c *bm.Context) {
  301. var (
  302. ocs []*academy.Occupation
  303. sks []*academy.Skill
  304. sfs []*academy.Software
  305. sfMap map[int64][]*academy.Software
  306. skMap map[int64][]*academy.Skill
  307. sfIDs, skIDs, oIDs []int64
  308. softMap, skillMap, occMap map[int64]int
  309. g, _ = errgroup.WithContext(c)
  310. err error
  311. )
  312. g.Go(func() error { //软件
  313. if err = svc.DB.Find(&sfs).Error; err != nil {
  314. log.Error("listSoftware error(%v)", err)
  315. return err
  316. }
  317. if len(sfs) == 0 {
  318. return nil
  319. }
  320. sfMap = make(map[int64][]*academy.Software)
  321. sfIDs = make([]int64, 0, len(sfs))
  322. for _, v := range sfs {
  323. sfMap[v.SkID] = append(sfMap[v.SkID], v) //按技能聚合软件
  324. sfIDs = append(sfIDs, v.ID)
  325. }
  326. softMap, _ = arcCountBySids(sfIDs) //获取软件对应稿件数量
  327. if len(softMap) == 0 {
  328. return nil
  329. }
  330. for _, v := range sfs {
  331. if n, ok := softMap[v.ID]; ok { //映射软件对应稿件数量
  332. v.Count = n
  333. }
  334. }
  335. return nil
  336. })
  337. g.Go(func() error { //技能
  338. if err = svc.DB.Find(&sks).Error; err != nil {
  339. log.Error("listSkill error(%v)", err)
  340. return err
  341. }
  342. if len(sks) == 0 {
  343. return nil
  344. }
  345. skMap = make(map[int64][]*academy.Skill)
  346. skIDs = make([]int64, 0, len(sks))
  347. for _, v := range sks {
  348. skMap[v.OID] = append(skMap[v.OID], v) //按职业聚合技能
  349. skIDs = append(skIDs, v.ID)
  350. }
  351. skillMap, _ = arcCountBySkids(skIDs) //获取技能对应稿件数量
  352. return nil
  353. })
  354. g.Go(func() error { //职业
  355. if err = svc.DB.Order("rank ASC").Find(&ocs).Error; err != nil {
  356. log.Error("listOccupation error(%v)", err)
  357. return err
  358. }
  359. if len(ocs) == 0 {
  360. return nil
  361. }
  362. oIDs = make([]int64, 0, len(ocs))
  363. for _, v := range ocs {
  364. oIDs = append(oIDs, v.ID)
  365. }
  366. occMap, _ = arcCountByPids(oIDs) //获取职业对应稿件数量
  367. return nil
  368. })
  369. if err = g.Wait(); err != nil {
  370. c.JSON(nil, err)
  371. return
  372. }
  373. for _, v := range sks {
  374. if sf, ok := sfMap[v.ID]; ok { //添加软件节点
  375. v.Software = sf
  376. }
  377. if n, ok := skillMap[v.ID]; ok { //映射技能对应稿件数量
  378. v.Count = n
  379. }
  380. }
  381. for _, v := range ocs {
  382. if sk, ok := skMap[v.ID]; ok { //添加技能节点
  383. v.Skill = sk
  384. }
  385. if n, ok := occMap[v.ID]; ok { //映射职业对应稿件数量
  386. v.Count = n
  387. }
  388. }
  389. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  390. "code": 0,
  391. "message": "0",
  392. "data": ocs,
  393. }))
  394. }
  395. func arcCountByPids(tids []int64) (res map[int64]int, err error) {
  396. var (
  397. countSQL = "SELECT pid AS tid, count(DISTINCT aid) AS count FROM academy_arc_skill WHERE state=0 AND pid IN (?) GROUP BY pid"
  398. ats []*academy.ArchiveCount
  399. )
  400. if err = svc.DB.Raw(countSQL, tids).Find(&ats).Error; err != nil {
  401. log.Error("occupation arcCountByPids error(%v)", err)
  402. return
  403. }
  404. if len(ats) == 0 {
  405. return
  406. }
  407. res = make(map[int64]int)
  408. for _, a := range ats {
  409. res[a.TID] = a.Count
  410. }
  411. return
  412. }
  413. func arcCountBySkids(tids []int64) (res map[int64]int, err error) {
  414. var (
  415. countSQL = "SELECT skid AS tid, count(DISTINCT aid) AS count FROM academy_arc_skill WHERE state=0 AND skid IN (?) GROUP BY skid"
  416. ats []*academy.ArchiveCount
  417. )
  418. if err = svc.DB.Raw(countSQL, tids).Find(&ats).Error; err != nil {
  419. log.Error("skill arcCountByPids error(%v)", err)
  420. return
  421. }
  422. if len(ats) == 0 {
  423. return
  424. }
  425. res = make(map[int64]int)
  426. for _, a := range ats {
  427. res[a.TID] = a.Count
  428. }
  429. return
  430. }
  431. func arcCountBySids(tids []int64) (res map[int64]int, err error) {
  432. var (
  433. countSQL = "SELECT sid AS tid, count(DISTINCT aid) AS count FROM academy_arc_skill WHERE state=0 AND sid IN (?) GROUP BY sid"
  434. ats []*academy.ArchiveCount
  435. )
  436. if err = svc.DB.Raw(countSQL, tids).Find(&ats).Error; err != nil {
  437. log.Error("software arcCountBySids error(%v)", err)
  438. return
  439. }
  440. if len(ats) == 0 {
  441. return
  442. }
  443. res = make(map[int64]int)
  444. for _, a := range ats {
  445. res[a.TID] = a.Count
  446. }
  447. return
  448. }
  449. func orderOccupation(c *bm.Context) {
  450. var err error
  451. v := new(struct {
  452. ID int64 `form:"id" validate:"required"`
  453. Rank int64 `form:"rank" validate:"required"`
  454. SwitchID int64 `form:"switch_id" validate:"required"`
  455. SwitchRank int64 `form:"switch_rank" validate:"required"`
  456. })
  457. if err = c.BindWith(v, binding.Form); err != nil {
  458. return
  459. }
  460. tx := svc.DB.Begin()
  461. var oc academy.Occupation
  462. if err = tx.Where("id=?", v.ID).First(&oc).Error; err != nil {
  463. log.Error("academy orderOccupation error(%v)", err)
  464. c.JSON(nil, err)
  465. tx.Rollback()
  466. return
  467. }
  468. var soc academy.Occupation
  469. if err = tx.Where("id=?", v.SwitchID).First(&soc).Error; err != nil {
  470. log.Error("academy orderOccupation error(%v)", err)
  471. c.JSON(nil, err)
  472. tx.Rollback()
  473. return
  474. }
  475. if err = tx.Model(&academy.Occupation{}).Where("id=?", v.ID).Updates(
  476. map[string]interface{}{
  477. "rank": v.SwitchRank,
  478. },
  479. ).Error; err != nil {
  480. log.Error("academy orderOccupation error(%v)", err)
  481. c.JSON(nil, err)
  482. tx.Rollback()
  483. return
  484. }
  485. if err = tx.Model(&academy.Occupation{}).Where("id=?", v.SwitchID).Updates(
  486. map[string]interface{}{
  487. "rank": v.Rank,
  488. },
  489. ).Error; err != nil {
  490. log.Error("academy orderOccupation error(%v)", err)
  491. c.JSON(nil, err)
  492. tx.Rollback()
  493. return
  494. }
  495. tx.Commit()
  496. uid, uname := getUIDName(c)
  497. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "更新职业排序", TID: v.ID, OName: ""})
  498. c.JSON(nil, err)
  499. }
  500. func checkSkillArcExist(aid, sid int64) (res *academy.ArcSkill, err error) {
  501. var as academy.ArcSkill
  502. err = svc.DB.Where("aid=?", aid).Where("sid=?", sid).Find(&as).Error
  503. if err != nil {
  504. if err == gorm.ErrRecordNotFound {
  505. err = nil
  506. }
  507. log.Error("academy checkSkillArcExist aid(%d)|sid(%d)|error(%v)", aid, sid, err)
  508. }
  509. res = &as
  510. return
  511. }
  512. func viewSkillArc(c *bm.Context) {
  513. var (
  514. err error
  515. as = &academy.ArcSkill{}
  516. )
  517. v := new(academy.ArcSkill)
  518. if err = c.Bind(v); err != nil {
  519. return
  520. }
  521. if v.AID == 0 {
  522. c.JSON(nil, ecode.RequestErr)
  523. return
  524. }
  525. arc, err := svc.Archive(c, v.AID)
  526. if err != nil {
  527. c.JSON(nil, err)
  528. return
  529. }
  530. if arc != nil {
  531. as.AID = v.AID
  532. as.Title = arc.Title
  533. as.Pic = arc.Pic
  534. }
  535. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  536. "code": 0,
  537. "message": "0",
  538. "data": as,
  539. }))
  540. }
  541. func addSkillArc(c *bm.Context) {
  542. var (
  543. err error
  544. now = time.Now().Format("2006-01-02 15:04:05")
  545. )
  546. v := new(academy.ArcSkill)
  547. if err = c.Bind(v); err != nil {
  548. return
  549. }
  550. if v.AID == 0 || v.PID == 0 || v.SkID == 0 || v.SID == 0 || v.Type == 0 {
  551. log.Error("academy addSkillArc v(%+v)", v)
  552. c.JSON(nil, ecode.RequestErr)
  553. return
  554. }
  555. res, err := checkSkillArcExist(v.AID, v.SID)
  556. if err != nil {
  557. c.JSON(nil, err)
  558. return
  559. }
  560. if res != nil && res.ID != 0 {
  561. c.JSON(nil, ecode.CreativeAcademyDuplicateSoftIDErr)
  562. return
  563. }
  564. m := &academy.ArcSkill{
  565. State: academy.StateNormal,
  566. AID: v.AID,
  567. Type: v.Type,
  568. PID: v.PID,
  569. SkID: v.SkID,
  570. SID: v.SID,
  571. CTime: now,
  572. MTime: now,
  573. }
  574. if err = svc.DB.Create(m).Error; err != nil {
  575. log.Error("academy addSoftware error(%v)", err)
  576. c.JSON(nil, err)
  577. return
  578. }
  579. uid, uname := getUIDName(c)
  580. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "添加技能稿件", OIDs: xstr.JoinInts([]int64{v.AID})})
  581. c.JSON(nil, err)
  582. }
  583. func upSkillArc(c *bm.Context) {
  584. var err error
  585. v := new(academy.ArcSkill)
  586. if err = c.Bind(v); err != nil {
  587. return
  588. }
  589. if v.ID == 0 || v.AID == 0 || v.PID == 0 || v.SkID == 0 || v.SID == 0 || v.Type == 0 {
  590. log.Error("academy upSkillArc v(%+v)", v)
  591. c.JSON(nil, ecode.RequestErr)
  592. return
  593. }
  594. if err = svc.DB.Model(&academy.ArcSkill{}).Where("id=?", v.ID).Updates(map[string]interface{}{
  595. "aid": v.AID,
  596. "type": v.Type,
  597. "pid": v.PID,
  598. "skid": v.SkID,
  599. "sid": v.SID,
  600. }).Error; err != nil {
  601. log.Error("academy upSkillArc error(%v)", err)
  602. }
  603. uid, uname := getUIDName(c)
  604. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "更新技能稿件", OIDs: xstr.JoinInts([]int64{v.AID})})
  605. c.JSON(nil, err)
  606. }
  607. func bindSkillArc(c *bm.Context) {
  608. var err error
  609. v := new(academy.ArcSkill)
  610. if err = c.Bind(v); err != nil {
  611. return
  612. }
  613. if v.AID == 0 || v.SID == 0 {
  614. log.Error("academy bindSkillArc v(%+v)", v)
  615. c.JSON(nil, ecode.RequestErr)
  616. return
  617. }
  618. res, err := checkSkillArcExist(v.AID, v.SID)
  619. if err != nil {
  620. c.JSON(nil, err)
  621. return
  622. }
  623. if res != nil && res.ID == 0 {
  624. c.JSON(nil, ecode.NothingFound)
  625. return
  626. }
  627. if err = svc.DB.Model(&academy.ArcSkill{}).Where("aid=?", v.AID).Where("sid=?", v.SID).Updates(map[string]interface{}{
  628. "state": v.State,
  629. }).Error; err != nil {
  630. log.Error("academy bindSkillArc error(%v)", err)
  631. }
  632. uid, uname := getUIDName(c)
  633. svc.SendAcademyLog(c, &academy.LogParam{UID: uid, UName: uname, Action: "更新技能稿件状态", TID: v.ID, OName: v.Title})
  634. c.JSON(nil, err)
  635. }
  636. func skillArcList(c *bm.Context) {
  637. var (
  638. items []*academy.ArcSkill
  639. aids []int64
  640. total int
  641. err error
  642. )
  643. v := new(academy.ArcSkill)
  644. if err = c.Bind(v); err != nil {
  645. return
  646. }
  647. if v.Pn < 1 {
  648. v.Pn = 1
  649. }
  650. if v.Ps > 20 {
  651. v.Ps = 20
  652. }
  653. db := svc.DB.Model(&academy.ArcSkill{})
  654. if v.PID != 0 {
  655. db = db.Where("pid=?", v.PID)
  656. }
  657. if v.SkID != 0 {
  658. db = db.Where("skid=?", v.SkID)
  659. }
  660. db.Count(&total)
  661. if err = db.Order("ctime DESC").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&items).Error; err != nil {
  662. log.Error("academy skillArcList error(%v)", err)
  663. c.JSON(nil, err)
  664. return
  665. }
  666. for _, v := range items {
  667. aids = append(aids, v.AID)
  668. }
  669. arcs, err := svc.Archives(c, aids)
  670. if err != nil {
  671. log.Error("academy skillArcList error(%v)", err)
  672. }
  673. for _, v := range items {
  674. if a, ok := arcs[v.AID]; ok {
  675. v.Pic = a.Pic
  676. v.Title = a.Title
  677. }
  678. }
  679. data := &academy.ArcSkills{
  680. Items: items,
  681. Pager: &academy.Pager{
  682. Num: v.Pn,
  683. Size: v.Ps,
  684. Total: total,
  685. },
  686. }
  687. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  688. "code": 0,
  689. "message": "0",
  690. "data": data,
  691. }))
  692. }
  693. func subSearchKeywords(c *bm.Context) {
  694. var err error
  695. bs, err := ioutil.ReadAll(c.Request.Body)
  696. if err != nil {
  697. log.Error("subSearchKeywords ioutil.ReadAll error(%v)", err)
  698. c.JSON(nil, ecode.RequestErr)
  699. return
  700. }
  701. c.Request.Body.Close()
  702. var v []*academy.SearchKeywords
  703. err = json.Unmarshal(bs, &v)
  704. if err != nil {
  705. log.Error("subSearchKeywords json.Unmarshal v(%+v) error(%v)", v, err)
  706. c.JSON(nil, ecode.RequestErr)
  707. return
  708. }
  709. if err = svc.SubSearchKeywords(v); err != nil {
  710. c.JSON(nil, err)
  711. return
  712. }
  713. c.JSON(nil, nil)
  714. }
  715. func searchKeywords(c *bm.Context) {
  716. res, err := svc.SearchKeywords()
  717. if err != nil {
  718. c.JSON(nil, err)
  719. return
  720. }
  721. c.JSON(res, nil)
  722. }