system_api_controller.go 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. info, _ := service.FindUserInfoByAccount(mobile)
  65. if info.ID > 0 {
  66. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNameException)
  67. return
  68. }
  69. err := service.UpdateAdmin(admin)
  70. if err == nil {
  71. this.ServeSuccessJSON(map[string]interface{}{
  72. "admin": admin,
  73. })
  74. }
  75. }
  76. func (this *SystemApiController) DeleteAdminUser() {
  77. id, _ := this.GetInt64("id")
  78. err := service.DeleteAdmin(id)
  79. if err == nil {
  80. this.ServeSuccessJSON(map[string]interface{}{
  81. "msg": "删除成功",
  82. })
  83. }
  84. }
  85. func (this *SystemApiController) GetUserInitData() {
  86. org, err := service.FindAllOrg()
  87. if err == nil {
  88. this.ServeSuccessJSON(map[string]interface{}{
  89. "org": org,
  90. })
  91. }
  92. }
  93. func (this *SystemApiController) GetFollowOrg() {
  94. id, _ := this.GetInt64("admin_user_id")
  95. FollowOrgs, err := service.FindFollowOrg(id)
  96. unFollow, _ := service.FindUnFollowOrgByIds(id)
  97. fmt.Println(FollowOrgs)
  98. fmt.Println(unFollow)
  99. if err == nil {
  100. this.ServeSuccessJSON(map[string]interface{}{
  101. "follows": FollowOrgs,
  102. "unFollow": unFollow,
  103. })
  104. }
  105. }
  106. func (this *SystemApiController) GetAdminUserById() {
  107. id, _ := this.GetInt64("id")
  108. account, err := service.FindAdminById(id)
  109. if err == nil {
  110. this.ServeSuccessJSON(map[string]interface{}{
  111. "account": account,
  112. })
  113. }
  114. }
  115. func (this *SystemApiController) PostFollowInfo() {
  116. time := time.Now().Unix()
  117. var orgFollow []*models.OrgFollow
  118. dataBody := make(map[string]interface{}, 0)
  119. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  120. if err != nil {
  121. utils.ErrorLog(err.Error())
  122. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  123. return
  124. }
  125. if dataBody["Follow"] != nil && reflect.TypeOf(dataBody["Follow"]).String() == "[]interface {}" {
  126. thisFollow, _ := dataBody["Follow"].([]interface{})
  127. if len(thisFollow) > 0 {
  128. for _, item := range thisFollow {
  129. items := item.(map[string]interface{})
  130. if items["org_id"] == nil || reflect.TypeOf(items["org_id"]).String() != "float64" {
  131. utils.ErrorLog("org_id")
  132. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  133. return
  134. }
  135. org_id := int64(items["org_id"].(float64))
  136. if items["admin_user_id"] == nil || reflect.TypeOf(items["admin_user_id"]).String() != "float64" {
  137. utils.ErrorLog("admin_user_id")
  138. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  139. return
  140. }
  141. admin_user_id := int64(items["admin_user_id"].(float64))
  142. follow := &models.OrgFollow{
  143. Mtime: time,
  144. Status: 1,
  145. Ctime: time,
  146. OrgId: org_id,
  147. AdminUserId: admin_user_id,
  148. }
  149. orgFollow = append(orgFollow, follow)
  150. }
  151. }
  152. }
  153. //防止数据冗余,先查出之前被删的数据是否存在,存在则直接改变删除状态,不插入新数据
  154. for index, follow := range orgFollow {
  155. info, err := service.FindFollowRecordByID(follow)
  156. if err == nil { //存在,则修改删除状态
  157. orgFollow = append(orgFollow[:index], orgFollow[index+1:]...)
  158. if info.Status == 0 {
  159. follow := &models.OrgFollow{
  160. ID: info.ID,
  161. Mtime: info.Mtime,
  162. Status: 1,
  163. Ctime: info.Ctime,
  164. OrgId: info.OrgId,
  165. AdminUserId: info.AdminUserId,
  166. }
  167. service.UpdateFollow(follow)
  168. }
  169. }
  170. }
  171. if len(orgFollow) > 0 {
  172. errs := service.CreateFollowInfo(orgFollow)
  173. if errs == nil {
  174. this.ServeSuccessJSON(map[string]interface{}{
  175. "msg": "提交成功",
  176. })
  177. }
  178. } else {
  179. this.ServeSuccessJSON(map[string]interface{}{
  180. "msg": "提交成功",
  181. })
  182. }
  183. }
  184. func (this *SystemApiController) CancelFollowInfo() {
  185. time := time.Now().Unix()
  186. var orgFollow []*models.OrgFollow
  187. dataBody := make(map[string]interface{}, 0)
  188. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  189. if err != nil {
  190. utils.ErrorLog(err.Error())
  191. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  192. return
  193. }
  194. if dataBody["Follow"] != nil && reflect.TypeOf(dataBody["Follow"]).String() == "[]interface {}" {
  195. thisFollow, _ := dataBody["Follow"].([]interface{})
  196. if len(thisFollow) > 0 {
  197. for _, item := range thisFollow {
  198. items := item.(map[string]interface{})
  199. if items["org_id"] == nil || reflect.TypeOf(items["org_id"]).String() != "float64" {
  200. utils.ErrorLog("org_id")
  201. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  202. return
  203. }
  204. org_id := int64(items["org_id"].(float64))
  205. if items["follow_id"] == nil || reflect.TypeOf(items["follow_id"]).String() != "float64" {
  206. utils.ErrorLog("follow_id")
  207. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  208. return
  209. }
  210. follow_id := int64(items["follow_id"].(float64))
  211. if items["admin_user_id"] == nil || reflect.TypeOf(items["admin_user_id"]).String() != "float64" {
  212. utils.ErrorLog("admin_user_id")
  213. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  214. return
  215. }
  216. admin_user_id := int64(items["admin_user_id"].(float64))
  217. follow := &models.OrgFollow{
  218. ID: follow_id,
  219. Mtime: time,
  220. Status: 0,
  221. Ctime: time,
  222. OrgId: org_id,
  223. AdminUserId: admin_user_id,
  224. }
  225. orgFollow = append(orgFollow, follow)
  226. }
  227. }
  228. }
  229. for _, follow := range orgFollow {
  230. service.UpdateFollow(follow)
  231. }
  232. this.ServeSuccessJSON(map[string]interface{}{
  233. "follow": orgFollow,
  234. })
  235. }
  236. func (this *SystemApiController) PostUnFollow() {
  237. org_id, _ := this.GetInt64("org_id")
  238. admin_user_id, _ := this.GetInt64("admin_user_id")
  239. info, _ := service.FindFollowInfoById(org_id, admin_user_id)
  240. follow := &models.OrgFollow{
  241. ID: info.ID,
  242. Mtime: time.Now().Unix(),
  243. Status: 0,
  244. Ctime: info.Ctime,
  245. OrgId: org_id,
  246. AdminUserId: info.AdminUserId,
  247. }
  248. err := service.UpdateFollow(follow)
  249. if err == nil {
  250. this.ServeSuccessJSON(map[string]interface{}{
  251. "msg": "删除成功",
  252. })
  253. }
  254. }
  255. func (this *SystemApiController) ModifyFollowInfo() {
  256. time := time.Now().Unix()
  257. var orgFollow []*models.OrgFollow
  258. dataBody := make(map[string]interface{}, 0)
  259. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  260. if err != nil {
  261. utils.ErrorLog(err.Error())
  262. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  263. return
  264. }
  265. if dataBody["Follow"] != nil && reflect.TypeOf(dataBody["Follow"]).String() == "[]interface {}" {
  266. thisFollow, _ := dataBody["Follow"].([]interface{})
  267. if len(thisFollow) > 0 {
  268. for _, item := range thisFollow {
  269. items := item.(map[string]interface{})
  270. if items["admin_user_id"] == nil || reflect.TypeOf(items["admin_user_id"]).String() != "float64" {
  271. utils.ErrorLog("admin_user_id")
  272. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  273. return
  274. }
  275. admin_user_id := int64(items["admin_user_id"].(float64))
  276. if items["org_id"] == nil || reflect.TypeOf(items["org_id"]).String() != "float64" {
  277. utils.ErrorLog("org_id")
  278. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  279. return
  280. }
  281. org_id := int64(items["org_id"].(float64))
  282. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  283. utils.ErrorLog("id")
  284. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  285. return
  286. }
  287. id := int64(items["id"].(float64))
  288. if id == 0 {
  289. follow := &models.OrgFollow{
  290. Mtime: time,
  291. Status: 1,
  292. Ctime: time,
  293. OrgId: org_id,
  294. AdminUserId: admin_user_id,
  295. }
  296. orgFollow = append(orgFollow, follow)
  297. }
  298. }
  299. }
  300. }
  301. fmt.Println(orgFollow)
  302. //防止数据冗余,先查出之前被删的数据是否存在,存在则直接改变删除状态,不插入新数据
  303. for index, follow := range orgFollow {
  304. info, err := service.FindFollowRecordByID(follow)
  305. if err == nil { //存在,则修改删除状态
  306. orgFollow = append(orgFollow[:index], orgFollow[index+1:]...)
  307. if info.Status == 0 {
  308. follow := &models.OrgFollow{
  309. ID: info.ID,
  310. Mtime: info.Mtime,
  311. Status: 1,
  312. Ctime: info.Ctime,
  313. OrgId: info.OrgId,
  314. AdminUserId: info.AdminUserId,
  315. }
  316. service.UpdateFollow(follow)
  317. }
  318. }
  319. }
  320. if len(orgFollow) > 0 {
  321. errs := service.CreateFollowInfo(orgFollow)
  322. if errs == nil {
  323. this.ServeSuccessJSON(map[string]interface{}{
  324. "msg": "提交成功",
  325. })
  326. }
  327. } else {
  328. this.ServeSuccessJSON(map[string]interface{}{
  329. "msg": "提交成功",
  330. })
  331. }
  332. }