public.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package controller
  2. import (
  3. v1 "demo/api/v1"
  4. "demo/internal/app"
  5. "demo/internal/model"
  6. "fmt"
  7. "github.com/Anderson-Lu/gofasion/gofasion"
  8. "github.com/gogf/gf/v2/net/ghttp"
  9. "github.com/pkg/errors"
  10. "github.com/siddontang/go/log"
  11. "io/ioutil"
  12. "net/http"
  13. "os"
  14. "strings"
  15. )
  16. // GetBarList
  17. // @Summary 栏舍查看
  18. // @Description add by json account
  19. // @Tags 公共接口
  20. // @Accept json
  21. // @Produce json
  22. // @Param index path int true "int "
  23. // @Param pagesize path int true "int "
  24. // @Param barname path string true "string 栏舍名称"
  25. // @Param pastureid path string true "string 牧场编码"
  26. // @Success 200 {object} v1.GetBarListRes
  27. // @Router /public/bar [get]
  28. func GetBarList(r *ghttp.Request) {
  29. var req *v1.GetBarListReq
  30. appG := app.Ghttp{C: r}
  31. err := appG.C.Parse(&req)
  32. if err != nil {
  33. err = errors.Wrap(err, "GetNeckRingListing-error")
  34. log.Error(err)
  35. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  36. return
  37. }
  38. barList, count, err := srv.GetBarList(req.Index, req.PageSize, req.BarName, req.Pastureid)
  39. if err != nil {
  40. return
  41. }
  42. resp := v1.GetBarListRes{}
  43. dataList := make([]*v1.BarList, 0)
  44. for _, item := range barList {
  45. var data v1.BarList
  46. data.Id = item.Id
  47. data.Pastureid = item.Pastureid
  48. data.Bname = item.Bname
  49. data.Bcode = item.Bcode
  50. dataList = append(dataList, &data)
  51. }
  52. resp.List = dataList
  53. resp.Total = count
  54. appG.Response(app.StatusOK, app.SUCCESS, resp)
  55. }
  56. // GetPastureList
  57. // @Summary 牧场信息查看
  58. // @Description add by json account
  59. // @Tags 公共接口
  60. // @Accept json
  61. // @Produce json
  62. // @Param index path int true "int "
  63. // @Param pagesize path int true "int "
  64. // @Success 200 {object} v1.GetPastureListRes
  65. // @Router /public/pasture [get]
  66. func GetPastureList(r *ghttp.Request) {
  67. var req *v1.GetPastureListReq
  68. appG := app.Ghttp{C: r}
  69. err := appG.C.Parse(&req)
  70. if err != nil {
  71. err = errors.Wrap(err, "GetPastureList-error")
  72. log.Error(err)
  73. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  74. return
  75. }
  76. pastureList, count, err := srv.GetPastureList(req.Index, req.PageSize)
  77. if err != nil {
  78. return
  79. }
  80. resp := v1.GetPastureListRes{}
  81. dataList := make([]*v1.PastureList, 0)
  82. for _, item := range pastureList {
  83. var data v1.PastureList
  84. data.PastureCode = item.PastureCode
  85. data.Pastureid = item.Pastureid
  86. data.PastureName = item.PastureName
  87. dataList = append(dataList, &data)
  88. }
  89. resp.List = dataList
  90. resp.Total = count
  91. appG.Response(app.StatusOK, app.SUCCESS, resp)
  92. }
  93. // GetHousephyList
  94. // @Summary 物理栏舍查看
  95. // @Description add by json account
  96. // @Tags 公共接口
  97. // @Accept json
  98. // @Produce json
  99. // @Param index path int true "int "
  100. // @Param pagesize path int true "int "
  101. // @Param pastureid path string true "string "
  102. // @Success 200 {object} v1.GetBarListRes
  103. // @Router /public/housephy [get]
  104. func GetHousephyList(r *ghttp.Request) {
  105. var req *v1.GetBarListReq
  106. appG := app.Ghttp{C: r}
  107. err := appG.C.Parse(&req)
  108. if err != nil {
  109. err = errors.Wrap(err, "GetHousephyList-error")
  110. log.Error(err)
  111. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  112. return
  113. }
  114. barList, count, err := srv.GetHousephyList(req.Index, req.PageSize, req.Pastureid)
  115. if err != nil {
  116. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  117. return
  118. }
  119. resp := v1.GetBarListRes{}
  120. dataList := make([]*v1.BarList, 0)
  121. for _, item := range barList {
  122. var data v1.BarList
  123. data.Id = item.Id
  124. data.Pastureid = item.Pastureid
  125. data.Bname = item.Bname
  126. data.Bcode = item.Bcode
  127. dataList = append(dataList, &data)
  128. }
  129. resp.List = dataList
  130. resp.Total = count
  131. appG.Response(app.StatusOK, app.SUCCESS, resp)
  132. }
  133. // GetImage
  134. // @Summary 接收器图片查看
  135. // @Description add by json account
  136. // @Tags 接收器管理
  137. // @Accept json
  138. // @Produce json
  139. // @Param id path int true "int "
  140. // @Success 200 {object} v1.GetBarListRes
  141. // @Router /image [get]
  142. func GetImage(r *ghttp.Request) {
  143. appG := app.Ghttp{C: r}
  144. id := r.Get("id").String()
  145. housephy, err := srv.GetHousephyImage(id)
  146. if err != nil {
  147. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  148. return
  149. }
  150. //file, err := os.Open(fmt.Sprintf("image/%s", housephy.Image))
  151. //if err != nil {
  152. // fmt.Println(err)
  153. // return
  154. //}
  155. //
  156. //f, _ := ioutil.ReadAll(file)
  157. //r.Response.Writer.Write(f)
  158. if housephy.Image != "" {
  159. appG.Response(app.StatusOK, app.SUCCESS, fmt.Sprintf("image/%s", housephy.Image))
  160. } else {
  161. appG.Response(app.StatusOK, app.SUCCESS, nil)
  162. }
  163. }
  164. // UploadImage
  165. // @Summary 上传接收器图片
  166. // @Description add by json account
  167. // @Tags 接收器管理
  168. // @Accept json
  169. // @Produce json
  170. // @Param id path int true "int "
  171. // @Router /housephy/image [post]
  172. func UploadImage(r *ghttp.Request) {
  173. appG := app.Ghttp{C: r}
  174. id := r.Get("id").String()
  175. file := r.GetUploadFile("file")
  176. os.Remove(fmt.Sprintf("image/%s", file.Filename))
  177. _, err := file.Save("image")
  178. if err != nil {
  179. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  180. return
  181. }
  182. housephy, err := srv.GetHousephyImage(id)
  183. if err != nil {
  184. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  185. return
  186. }
  187. if housephy.Image != "" {
  188. os.Remove(fmt.Sprintf("image/%s", housephy.Image))
  189. }
  190. err = srv.UpdateHousephyImage(&model.Housephy{Id: id, Image: file.Filename})
  191. if err != nil {
  192. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  193. return
  194. }
  195. appG.Response(app.StatusOK, app.SUCCESS, true)
  196. }
  197. func GetDataByNames(r *ghttp.Request) {
  198. appG := app.Ghttp{C: r}
  199. dataByte, _ := ioutil.ReadAll(r.Request.Body)
  200. fsion := gofasion.NewFasion(string(dataByte))
  201. sqlParams := fsion.Array()
  202. queryData := make(map[string]interface{})
  203. for _, v := range sqlParams {
  204. fsion2 := gofasion.NewFasion(v.Json())
  205. //logging.Info("GetDataByNames", c.Keys, fsion2.Get("name").ValueStr(), c.Request.RemoteAddr)
  206. fmt.Println(fsion2.Get("name").ValueStr())
  207. sql, p := srv.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  208. if sql == "" {
  209. errorm := make(map[string]interface{})
  210. errorm["code"] = app.ERROR
  211. errorm["message"] = "sql没找到"
  212. queryData[fsion2.Get("name").ValueStr()] = errorm
  213. continue
  214. } else if sql != "" {
  215. offset := fsion2.Get("offset").ValueInt()
  216. pagecount := fsion2.Get("pagecount").ValueInt()
  217. returntype := fsion2.Get("returntype").ValueStr()
  218. s_params := make([]interface{}, 0)
  219. if fsion2.HasKey("parammaps") {
  220. parammaps := fsion2.Get("parammaps")
  221. paramslist := strings.Split(p, ",")
  222. if len(paramslist) > 0 && p != "" {
  223. for _, value := range paramslist {
  224. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  225. if r.Get("jwt_username").String() != "" {
  226. s_params = append(s_params, r.Get("jwt_username").String())
  227. } else {
  228. s_params = append(s_params, "")
  229. }
  230. } else {
  231. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  232. }
  233. }
  234. }
  235. } else if fsion2.HasKey("params") {
  236. params := fsion2.Get("params").Array()
  237. for _, v_params := range params {
  238. s_params = append(s_params, v_params.ValueStr())
  239. }
  240. }
  241. queryData1, err := getDataBySql(sql, offset, pagecount, returntype, s_params)
  242. if err != nil {
  243. errorm := make(map[string]interface{})
  244. errorm["code"] = app.ERROR
  245. errorm["message"] = err
  246. queryData[fsion2.Get("name").ValueStr()] = errorm
  247. //logging.Error("GetDataByNames err: ", err)
  248. continue
  249. } else {
  250. queryData[fsion2.Get("name").ValueStr()] = queryData1
  251. }
  252. }
  253. }
  254. appG.Response(http.StatusOK, app.SUCCESS, queryData)
  255. }
  256. func getDataBySql(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
  257. if offset > 0 {
  258. offset = offset - 1
  259. }
  260. //if pagecount == 0 {
  261. // pagecount = setting.AppSetting.PageSize
  262. //}else if pagecount>setting.AppSetting.PageSize{
  263. // pagecount = setting.AppSetting.PageSize
  264. //}
  265. if returntype == "list" {
  266. //queryData, err := srv.QueryByList(sqlstr, offset, pagecount, params)
  267. //return queryData, err
  268. } else if returntype == "listlist" {
  269. //queryData, err := srv.QueryByListList(sqlstr, offset, pagecount, params)
  270. //return queryData, err
  271. } else {
  272. queryData, err := srv.QueryByMap(sqlstr, offset, pagecount, params)
  273. return queryData, err
  274. }
  275. return nil, nil
  276. }