scrm-go

org_controller.go 8.6KB

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