org_controller.go 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package admin_user
  2. import (
  3. base_ctl "SCRM/controllers"
  4. "SCRM/enums"
  5. "SCRM/models"
  6. "SCRM/service/district_service"
  7. "SCRM/service/org_service"
  8. "SCRM/utils"
  9. "encoding/json"
  10. "reflect"
  11. "time"
  12. "fmt"
  13. "github.com/astaxie/beego"
  14. )
  15. func OrgInfoCtlRegistRouters() {
  16. beego.Router("/api/orginfo/getinfo", &OrgInfoApiController{}, "get:GetOrgInfo")
  17. beego.Router("/api/orginfo/savegallery", &OrgInfoApiController{}, "post:SaveOrgGallery")
  18. beego.Router("/api/orginfo/deletegallery", &OrgInfoApiController{}, "delete:DeleteOrgGallery")
  19. beego.Router("/api/orginfo/edit", &OrgInfoApiController{}, "post:EditOrgInfo")
  20. }
  21. type OrgInfoApiController struct {
  22. base_ctl.BaseAuthAPIController
  23. }
  24. func (c *OrgInfoApiController) GetOrgInfo() {
  25. adminUserInfo := c.GetAdminUserInfo()
  26. fmt.Println("adminUserInfo是什么", adminUserInfo)
  27. orgInfo := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  28. fmt.Println("orgInfo是设么", orgInfo)
  29. fmt.Println("orgInfo是什么", orgInfo.Province)
  30. provinces, _ := district_service.GetDistrictsByUpid(0)
  31. var citys []*models.District
  32. var districts []*models.District
  33. if orgInfo.Province > 0 {
  34. citys, _ = district_service.GetDistrictsByUpid(orgInfo.Province)
  35. }
  36. if orgInfo.City > 0 {
  37. districts, _ = district_service.GetDistrictsByUpid(orgInfo.City)
  38. }
  39. orgtypes, _ := org_service.GetOrgTypes()
  40. illness, _ := org_service.GetIllnessList()
  41. c.ServeSuccessJSON(map[string]interface{}{
  42. "orginfo": orgInfo,
  43. "provinces": provinces,
  44. "citys": citys,
  45. "districts": districts,
  46. "orgtypes": orgtypes,
  47. "illness": illness,
  48. })
  49. return
  50. }
  51. func (c *OrgInfoApiController) EditOrgInfo() {
  52. adminUserInfo := c.GetAdminUserInfo()
  53. if !adminUserInfo.AdminUser.IsSuperAdmin {
  54. c.ServeFailJsonSend(enums.ErrorCodePermissionDenied, "权限不足")
  55. return
  56. }
  57. dataBody := make(map[string]interface{}, 0)
  58. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  59. if err != nil {
  60. utils.ErrorLog(err.Error())
  61. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  62. return
  63. }
  64. orgInfo := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  65. org := *orgInfo
  66. if dataBody["org_name"] == nil || reflect.TypeOf(dataBody["org_name"]).String() != "string" {
  67. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:机构名称")
  68. return
  69. }
  70. orgName, _ := dataBody["org_name"].(string)
  71. if len(orgName) == 0 {
  72. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "机构名称不能为空")
  73. return
  74. }
  75. org.OrgName = orgName
  76. if dataBody["contact_name"] == nil || reflect.TypeOf(dataBody["contact_name"]).String() != "string" {
  77. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:联系人姓名")
  78. return
  79. }
  80. contactName, _ := dataBody["contact_name"].(string)
  81. if len(contactName) == 0 {
  82. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "联系人姓名不能为空")
  83. return
  84. }
  85. org.ContactName = contactName
  86. // if dataBody["org_short_name"] == nil || reflect.TypeOf(dataBody["org_short_name"]).String() != "string" {
  87. // c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:商家简称")
  88. // return
  89. // }
  90. // orgShortName, _ := dataBody["org_short_name"].(string)
  91. // if len(orgShortName) == 0 {
  92. // c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "商家简称不能为空")
  93. // return
  94. // }
  95. org.OrgShortName = orgName
  96. if dataBody["org_introduction"] == nil || reflect.TypeOf(dataBody["org_introduction"]).String() != "string" {
  97. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:机构介绍")
  98. return
  99. }
  100. orgIntroduction, _ := dataBody["org_introduction"].(string)
  101. if len(orgIntroduction) == 0 {
  102. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "机构介绍不能为空")
  103. return
  104. }
  105. org.OrgIntroduction = orgIntroduction
  106. if dataBody["org_logo"] == nil || reflect.TypeOf(dataBody["org_logo"]).String() != "string" {
  107. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:机构头像")
  108. return
  109. }
  110. orgLogo, _ := dataBody["org_logo"].(string)
  111. if len(orgLogo) == 0 {
  112. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "机构头像不能为空")
  113. return
  114. }
  115. org.OrgLogo = orgLogo
  116. if dataBody["province"] != nil || reflect.TypeOf(dataBody["province"]).String() == "float64" {
  117. province := int64(dataBody["province"].(float64))
  118. org.Province = province
  119. }
  120. if dataBody["city"] != nil || reflect.TypeOf(dataBody["city"]).String() == "float64" {
  121. city := int64(dataBody["city"].(float64))
  122. org.City = city
  123. }
  124. if dataBody["district"] != nil || reflect.TypeOf(dataBody["district"]).String() == "float64" {
  125. district := int64(dataBody["district"].(float64))
  126. org.District = district
  127. }
  128. if dataBody["address"] != nil || reflect.TypeOf(dataBody["address"]).String() == "string" {
  129. address, _ := dataBody["address"].(string)
  130. org.Address = address
  131. }
  132. if dataBody["org_type"] != nil || reflect.TypeOf(dataBody["org_type"]).String() == "float64" {
  133. orgType := int64(dataBody["org_type"].(float64))
  134. org.OrgType = orgType
  135. }
  136. if dataBody["telephone"] != nil || reflect.TypeOf(dataBody["telephone"]).String() == "string" {
  137. telephone, _ := dataBody["telephone"].(string)
  138. org.Telephone = telephone
  139. }
  140. if dataBody["operating_state"] != nil || reflect.TypeOf(dataBody["operating_state"]).String() == "float64" {
  141. operatingState := int64(dataBody["operating_state"].(float64))
  142. org.OperatingState = operatingState
  143. }
  144. if dataBody["business_week"] != nil || reflect.TypeOf(dataBody["business_week"]).String() == "string" {
  145. businessWeek, _ := dataBody["business_week"].(string)
  146. org.BusinessWeek = businessWeek
  147. }
  148. if dataBody["business_time"] != nil || reflect.TypeOf(dataBody["business_time"]).String() == "string" {
  149. businessTime, _ := dataBody["business_time"].(string)
  150. org.BusinessTime = businessTime
  151. }
  152. if dataBody["illness"] != nil || reflect.TypeOf(dataBody["illness"]).String() == "string" {
  153. illness, _ := dataBody["illness"].(string)
  154. org.Illness = illness
  155. }
  156. timeNow := time.Now().Unix()
  157. org.ModifyTime = timeNow
  158. org.OrgGallery = nil
  159. err = org_service.UpdateOrgInfo(&org)
  160. if err != nil {
  161. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  162. return
  163. }
  164. orgInfo.OrgName = orgName
  165. orgInfo.OrgShortName = orgName
  166. orgInfo.OrgIntroduction = orgIntroduction
  167. orgInfo.OrgLogo = orgLogo
  168. orgInfo.Province = org.Province
  169. orgInfo.District = org.District
  170. orgInfo.City = org.City
  171. orgInfo.OrgType = org.OrgType
  172. orgInfo.Telephone = org.Telephone
  173. orgInfo.OperatingState = org.OperatingState
  174. orgInfo.BusinessWeek = org.BusinessWeek
  175. orgInfo.BusinessTime = org.BusinessTime
  176. orgInfo.Illness = org.Illness
  177. orgInfo.ContactName = org.ContactName
  178. c.ServeSuccessJSON(map[string]interface{}{
  179. "msg": "ok",
  180. })
  181. return
  182. }
  183. func (c *OrgInfoApiController) SaveOrgGallery() {
  184. adminUserInfo := c.GetAdminUserInfo()
  185. dataBody := make(map[string]interface{}, 0)
  186. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  187. if err != nil {
  188. utils.ErrorLog(err.Error())
  189. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  190. return
  191. }
  192. if dataBody["type"] == nil || reflect.TypeOf(dataBody["type"]).String() != "float64" {
  193. utils.ErrorLog("type")
  194. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  195. return
  196. }
  197. galleryType := int64(dataBody["type"].(float64))
  198. if galleryType != 1 && galleryType != 2 {
  199. utils.ErrorLog("galleryType != 1&&2")
  200. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  201. return
  202. }
  203. if dataBody["url"] == nil || reflect.TypeOf(dataBody["url"]).String() != "string" {
  204. utils.ErrorLog("url")
  205. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  206. return
  207. }
  208. url, _ := dataBody["url"].(string)
  209. if len(url) == 0 {
  210. utils.ErrorLog("len(url) == 0")
  211. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  212. return
  213. }
  214. timeNow := time.Now().Unix()
  215. var gallery models.OrgGallery
  216. gallery.Type = galleryType
  217. gallery.Url = url
  218. gallery.OrgId = adminUserInfo.CurrentOrgId
  219. gallery.Status = 1
  220. gallery.Ctime = timeNow
  221. gallery.Mtime = timeNow
  222. err = org_service.CreateOrgGalleryItem(&gallery)
  223. if err != nil {
  224. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  225. return
  226. }
  227. orgInfo := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  228. orgInfo.OrgGallery = append(orgInfo.OrgGallery, &gallery)
  229. c.ServeSuccessJSON(map[string]interface{}{
  230. "msg": "ok",
  231. })
  232. return
  233. }
  234. func (c *OrgInfoApiController) DeleteOrgGallery() {
  235. adminUserInfo := c.GetAdminUserInfo()
  236. id, _ := c.GetInt64("id", 0)
  237. if id <= 0 {
  238. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  239. return
  240. }
  241. err := org_service.DeleteOrgGalleryItem(id)
  242. if err != nil {
  243. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  244. return
  245. }
  246. orgInfo := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  247. for index, item := range orgInfo.OrgGallery {
  248. if item.ID == id {
  249. orgInfo.OrgGallery = append(orgInfo.OrgGallery[:index], orgInfo.OrgGallery[index+1:]...)
  250. }
  251. }
  252. c.ServeSuccessJSON(map[string]interface{}{
  253. "msg": "ok",
  254. })
  255. return
  256. }