system_api_controller.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. package admin_api_controllers
  2. import (
  3. "XT_Admin_Api/enums"
  4. "XT_Admin_Api/models"
  5. "XT_Admin_Api/models/admin_models"
  6. "XT_Admin_Api/service"
  7. "XT_Admin_Api/utils"
  8. "encoding/json"
  9. "fmt"
  10. "reflect"
  11. "time"
  12. )
  13. type SystemApiController struct {
  14. AdminBaseAPIAuthController
  15. }
  16. func (this *SystemApiController) GetUserList() {
  17. list, _ := service.GetAllAdmin()
  18. this.ServeSuccessJSON(map[string]interface{}{
  19. "list": list,
  20. })
  21. }
  22. func (this *SystemApiController) CreateAdminUser() {
  23. name := this.GetString("name")
  24. mobile := this.GetString("mobile")
  25. password := this.GetString("password")
  26. admin := &admin_models.AdminAccount{
  27. Account: mobile,
  28. Pwd: password,
  29. Name: name,
  30. Status: 1,
  31. IsSuperAdmin: 2,
  32. Ctime: time.Now().Unix(),
  33. Mtime: time.Now().Unix(),
  34. }
  35. admins, _ := service.FindUserInfoByAccount(mobile)
  36. if admins.ID > 0 {
  37. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNameException)
  38. return
  39. }
  40. err := service.CreateAdmin(admin)
  41. if err == nil {
  42. this.ServeSuccessJSON(map[string]interface{}{
  43. "admin": admin,
  44. })
  45. }
  46. }
  47. func (this *SystemApiController) ModifyAdminUser() {
  48. id, _ := this.GetInt64("id")
  49. name := this.GetString("name")
  50. mobile := this.GetString("mobile")
  51. password := this.GetString("password")
  52. status, _ := this.GetInt64("status")
  53. admins, _ := service.FindAdminById(id)
  54. admin := &admin_models.AdminAccount{
  55. ID: admins.ID,
  56. Account: mobile,
  57. Pwd: password,
  58. Name: name,
  59. Status: status,
  60. IsSuperAdmin: 2,
  61. Ctime: admins.Ctime,
  62. Mtime: time.Now().Unix(),
  63. }
  64. if admins.Account != mobile {
  65. info, _ := service.FindUserInfoByAccount(mobile)
  66. if info.ID > 0 {
  67. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNameException)
  68. return
  69. }
  70. }
  71. err := service.UpdateAdmin(admin)
  72. if err == nil {
  73. this.ServeSuccessJSON(map[string]interface{}{
  74. "admin": admin,
  75. })
  76. }
  77. }
  78. func (this *SystemApiController) DeleteAdminUser() {
  79. id, _ := this.GetInt64("id")
  80. err := service.DeleteAdmin(id)
  81. if err == nil {
  82. this.ServeSuccessJSON(map[string]interface{}{
  83. "msg": "删除成功",
  84. })
  85. }
  86. }
  87. func (this *SystemApiController) GetUserInitData() {
  88. org, err := service.FindAllOrg()
  89. if err == nil {
  90. this.ServeSuccessJSON(map[string]interface{}{
  91. "org": org,
  92. })
  93. }
  94. }
  95. func (this *SystemApiController) GetFollowOrg() {
  96. id, _ := this.GetInt64("admin_user_id")
  97. FollowOrgs, err := service.FindFollowOrg(id)
  98. unFollow, _ := service.FindUnFollowOrgByIds(id)
  99. fmt.Println(FollowOrgs)
  100. fmt.Println(unFollow)
  101. if err == nil {
  102. this.ServeSuccessJSON(map[string]interface{}{
  103. "follows": FollowOrgs,
  104. "unFollow": unFollow,
  105. })
  106. }
  107. }
  108. func (this *SystemApiController) GetAdminUserById() {
  109. id, _ := this.GetInt64("id")
  110. account, err := service.FindAdminById(id)
  111. if err == nil {
  112. this.ServeSuccessJSON(map[string]interface{}{
  113. "account": account,
  114. })
  115. }
  116. }
  117. func (this *SystemApiController) PostFollowInfo() {
  118. time := time.Now().Unix()
  119. var orgFollow []*models.OrgFollow
  120. dataBody := make(map[string]interface{}, 0)
  121. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  122. if err != nil {
  123. utils.ErrorLog(err.Error())
  124. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  125. return
  126. }
  127. if dataBody["Follow"] != nil && reflect.TypeOf(dataBody["Follow"]).String() == "[]interface {}" {
  128. thisFollow, _ := dataBody["Follow"].([]interface{})
  129. if len(thisFollow) > 0 {
  130. for _, item := range thisFollow {
  131. items := item.(map[string]interface{})
  132. if items["org_id"] == nil || reflect.TypeOf(items["org_id"]).String() != "float64" {
  133. utils.ErrorLog("org_id")
  134. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  135. return
  136. }
  137. org_id := int64(items["org_id"].(float64))
  138. if items["admin_user_id"] == nil || reflect.TypeOf(items["admin_user_id"]).String() != "float64" {
  139. utils.ErrorLog("admin_user_id")
  140. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  141. return
  142. }
  143. admin_user_id := int64(items["admin_user_id"].(float64))
  144. follow := &models.OrgFollow{
  145. Mtime: time,
  146. Status: 1,
  147. Ctime: time,
  148. OrgId: org_id,
  149. AdminUserId: admin_user_id,
  150. }
  151. orgFollow = append(orgFollow, follow)
  152. }
  153. }
  154. }
  155. //防止数据冗余,先查出之前被删的数据是否存在,存在则直接改变删除状态,不插入新数据
  156. for index, follow := range orgFollow {
  157. info, err := service.FindFollowRecordByID(follow)
  158. if err == nil { //存在,则修改删除状态
  159. orgFollow = append(orgFollow[:index], orgFollow[index+1:]...)
  160. if info.Status == 0 {
  161. follow := &models.OrgFollow{
  162. ID: info.ID,
  163. Mtime: info.Mtime,
  164. Status: 1,
  165. Ctime: info.Ctime,
  166. OrgId: info.OrgId,
  167. AdminUserId: info.AdminUserId,
  168. }
  169. service.UpdateFollow(follow)
  170. }
  171. }
  172. }
  173. if len(orgFollow) > 0 {
  174. errs := service.CreateFollowInfo(orgFollow)
  175. if errs == nil {
  176. this.ServeSuccessJSON(map[string]interface{}{
  177. "msg": "提交成功",
  178. })
  179. }
  180. } else {
  181. this.ServeSuccessJSON(map[string]interface{}{
  182. "msg": "提交成功",
  183. })
  184. }
  185. }
  186. func (this *SystemApiController) CancelFollowInfo() {
  187. time := time.Now().Unix()
  188. var orgFollow []*models.OrgFollow
  189. dataBody := make(map[string]interface{}, 0)
  190. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  191. if err != nil {
  192. utils.ErrorLog(err.Error())
  193. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  194. return
  195. }
  196. if dataBody["Follow"] != nil && reflect.TypeOf(dataBody["Follow"]).String() == "[]interface {}" {
  197. thisFollow, _ := dataBody["Follow"].([]interface{})
  198. if len(thisFollow) > 0 {
  199. for _, item := range thisFollow {
  200. items := item.(map[string]interface{})
  201. if items["org_id"] == nil || reflect.TypeOf(items["org_id"]).String() != "float64" {
  202. utils.ErrorLog("org_id")
  203. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  204. return
  205. }
  206. org_id := int64(items["org_id"].(float64))
  207. if items["follow_id"] == nil || reflect.TypeOf(items["follow_id"]).String() != "float64" {
  208. utils.ErrorLog("follow_id")
  209. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  210. return
  211. }
  212. follow_id := int64(items["follow_id"].(float64))
  213. if items["admin_user_id"] == nil || reflect.TypeOf(items["admin_user_id"]).String() != "float64" {
  214. utils.ErrorLog("admin_user_id")
  215. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  216. return
  217. }
  218. admin_user_id := int64(items["admin_user_id"].(float64))
  219. follow := &models.OrgFollow{
  220. ID: follow_id,
  221. Mtime: time,
  222. Status: 0,
  223. Ctime: time,
  224. OrgId: org_id,
  225. AdminUserId: admin_user_id,
  226. }
  227. orgFollow = append(orgFollow, follow)
  228. }
  229. }
  230. }
  231. for _, follow := range orgFollow {
  232. service.UpdateFollow(follow)
  233. }
  234. this.ServeSuccessJSON(map[string]interface{}{
  235. "follow": orgFollow,
  236. })
  237. }
  238. func (this *SystemApiController) PostUnFollow() {
  239. org_id, _ := this.GetInt64("org_id")
  240. admin_user_id, _ := this.GetInt64("admin_user_id")
  241. info, _ := service.FindFollowInfoById(org_id, admin_user_id)
  242. follow := &models.OrgFollow{
  243. ID: info.ID,
  244. Mtime: time.Now().Unix(),
  245. Status: 0,
  246. Ctime: info.Ctime,
  247. OrgId: org_id,
  248. AdminUserId: info.AdminUserId,
  249. }
  250. err := service.UpdateFollow(follow)
  251. if err == nil {
  252. this.ServeSuccessJSON(map[string]interface{}{
  253. "msg": "删除成功",
  254. })
  255. }
  256. }
  257. func (this *SystemApiController) ModifyFollowInfo() {
  258. time := time.Now().Unix()
  259. var orgFollow []*models.OrgFollow
  260. dataBody := make(map[string]interface{}, 0)
  261. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  262. if err != nil {
  263. utils.ErrorLog(err.Error())
  264. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  265. return
  266. }
  267. if dataBody["Follow"] != nil && reflect.TypeOf(dataBody["Follow"]).String() == "[]interface {}" {
  268. thisFollow, _ := dataBody["Follow"].([]interface{})
  269. if len(thisFollow) > 0 {
  270. for _, item := range thisFollow {
  271. items := item.(map[string]interface{})
  272. if items["admin_user_id"] == nil || reflect.TypeOf(items["admin_user_id"]).String() != "float64" {
  273. utils.ErrorLog("admin_user_id")
  274. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  275. return
  276. }
  277. admin_user_id := int64(items["admin_user_id"].(float64))
  278. if items["org_id"] == nil || reflect.TypeOf(items["org_id"]).String() != "float64" {
  279. utils.ErrorLog("org_id")
  280. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  281. return
  282. }
  283. org_id := int64(items["org_id"].(float64))
  284. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  285. utils.ErrorLog("id")
  286. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  287. return
  288. }
  289. id := int64(items["id"].(float64))
  290. if id == 0 {
  291. follow := &models.OrgFollow{
  292. Mtime: time,
  293. Status: 1,
  294. Ctime: time,
  295. OrgId: org_id,
  296. AdminUserId: admin_user_id,
  297. }
  298. orgFollow = append(orgFollow, follow)
  299. }
  300. }
  301. }
  302. }
  303. //防止数据冗余,先查出之前被删的数据是否存在,存在则直接改变删除状态,不插入新数据
  304. for index, follow := range orgFollow {
  305. info, err := service.FindFollowRecordByID(follow)
  306. if err == nil { //存在,则修改删除状态
  307. orgFollow = append(orgFollow[:index], orgFollow[index+1:]...)
  308. if info.Status == 0 {
  309. follow := &models.OrgFollow{
  310. ID: info.ID,
  311. Mtime: info.Mtime,
  312. Status: 1,
  313. Ctime: info.Ctime,
  314. OrgId: info.OrgId,
  315. AdminUserId: info.AdminUserId,
  316. }
  317. service.UpdateFollow(follow)
  318. }
  319. }
  320. }
  321. if len(orgFollow) > 0 {
  322. errs := service.CreateFollowInfo(orgFollow)
  323. if errs == nil {
  324. this.ServeSuccessJSON(map[string]interface{}{
  325. "msg": "提交成功",
  326. })
  327. }
  328. } else {
  329. this.ServeSuccessJSON(map[string]interface{}{
  330. "msg": "提交成功",
  331. })
  332. }
  333. }
  334. func (this *SystemApiController) GetWaitFollowOrg() {
  335. admin_id, _ := this.GetInt64("admin_id", 0)
  336. keyword := this.GetString("keyword")
  337. page, _ := this.GetInt64("page", 0)
  338. limit, _ := this.GetInt64("limit", 0)
  339. if page <= 0 {
  340. page = 1
  341. }
  342. if limit <= 0 {
  343. limit = 7
  344. }
  345. list, err, total := service.GetAllWaitFollowOrgList(keyword, page, limit, admin_id)
  346. if err != nil {
  347. } else {
  348. this.ServeSuccessJSON(map[string]interface{}{
  349. "list": list,
  350. "total": total,
  351. })
  352. }
  353. }
  354. func (this *SystemApiController) GetAllFollowOrg() {
  355. admin_id, _ := this.GetInt64("admin_id", 0)
  356. keyword := this.GetString("keyword")
  357. page, _ := this.GetInt64("page", 0)
  358. limit, _ := this.GetInt64("limit", 0)
  359. if page <= 0 {
  360. page = 1
  361. }
  362. if limit <= 0 {
  363. limit = 7
  364. }
  365. list, err, total := service.FindAllFollowOrg(keyword, page, limit, admin_id)
  366. fmt.Println(list)
  367. fmt.Println(err)
  368. fmt.Println(total)
  369. if err != nil {
  370. } else {
  371. this.ServeSuccessJSON(map[string]interface{}{
  372. "list": list,
  373. "total": total,
  374. })
  375. }
  376. }