123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- package controller
- import (
- v1 "demo/api/v1"
- "demo/internal/app"
- "demo/internal/model"
- "fmt"
- "github.com/Anderson-Lu/gofasion/gofasion"
- "github.com/gogf/gf/v2/net/ghttp"
- "github.com/pkg/errors"
- "github.com/siddontang/go/log"
- "io/ioutil"
- "net/http"
- "os"
- "strings"
- )
- // GetBarList
- // @Summary 栏舍查看
- // @Description add by json account
- // @Tags 公共接口
- // @Accept json
- // @Produce json
- // @Param index path int true "int "
- // @Param pagesize path int true "int "
- // @Param barname path string true "string 栏舍名称"
- // @Param pastureid path string true "string 牧场编码"
- // @Success 200 {object} v1.GetBarListRes
- // @Router /public/bar [get]
- func GetBarList(r *ghttp.Request) {
- var req *v1.GetBarListReq
- appG := app.Ghttp{C: r}
- err := appG.C.Parse(&req)
- if err != nil {
- err = errors.Wrap(err, "GetNeckRingListing-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- barList, count, err := srv.GetBarList(req.Index, req.PageSize, req.BarName, req.Pastureid)
- if err != nil {
- return
- }
- resp := v1.GetBarListRes{}
- dataList := make([]*v1.BarList, 0)
- for _, item := range barList {
- var data v1.BarList
- data.Id = item.Id
- data.Pastureid = item.Pastureid
- data.Bname = item.Bname
- data.Bcode = item.Bcode
- dataList = append(dataList, &data)
- }
- resp.List = dataList
- resp.Total = count
- appG.Response(app.StatusOK, app.SUCCESS, resp)
- }
- // GetPastureList
- // @Summary 牧场信息查看
- // @Description add by json account
- // @Tags 公共接口
- // @Accept json
- // @Produce json
- // @Param index path int true "int "
- // @Param pagesize path int true "int "
- // @Success 200 {object} v1.GetPastureListRes
- // @Router /public/pasture [get]
- func GetPastureList(r *ghttp.Request) {
- var req *v1.GetPastureListReq
- appG := app.Ghttp{C: r}
- err := appG.C.Parse(&req)
- if err != nil {
- err = errors.Wrap(err, "GetPastureList-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- pastureList, count, err := srv.GetPastureList(req.Index, req.PageSize)
- if err != nil {
- return
- }
- resp := v1.GetPastureListRes{}
- dataList := make([]*v1.PastureList, 0)
- for _, item := range pastureList {
- var data v1.PastureList
- data.PastureCode = item.PastureCode
- data.Pastureid = item.Pastureid
- data.PastureName = item.PastureName
- dataList = append(dataList, &data)
- }
- resp.List = dataList
- resp.Total = count
- appG.Response(app.StatusOK, app.SUCCESS, resp)
- }
- // GetHousephyList
- // @Summary 物理栏舍查看
- // @Description add by json account
- // @Tags 公共接口
- // @Accept json
- // @Produce json
- // @Param index path int true "int "
- // @Param pagesize path int true "int "
- // @Param pastureid path string true "string "
- // @Success 200 {object} v1.GetBarListRes
- // @Router /public/housephy [get]
- func GetHousephyList(r *ghttp.Request) {
- var req *v1.GetBarListReq
- appG := app.Ghttp{C: r}
- err := appG.C.Parse(&req)
- if err != nil {
- err = errors.Wrap(err, "GetHousephyList-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- barList, count, err := srv.GetHousephyList(req.Index, req.PageSize, req.Pastureid)
- if err != nil {
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- resp := v1.GetBarListRes{}
- dataList := make([]*v1.BarList, 0)
- for _, item := range barList {
- var data v1.BarList
- data.Id = item.Id
- data.Pastureid = item.Pastureid
- data.Bname = item.Bname
- data.Bcode = item.Bcode
- dataList = append(dataList, &data)
- }
- resp.List = dataList
- resp.Total = count
- appG.Response(app.StatusOK, app.SUCCESS, resp)
- }
- // GetImage
- // @Summary 接收器图片查看
- // @Description add by json account
- // @Tags 接收器管理
- // @Accept json
- // @Produce json
- // @Param id path int true "int "
- // @Success 200 {object} v1.GetBarListRes
- // @Router /image [get]
- func GetImage(r *ghttp.Request) {
- appG := app.Ghttp{C: r}
- id := r.Get("id").String()
- housephy, err := srv.GetHousephyImage(id)
- if err != nil {
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- //file, err := os.Open(fmt.Sprintf("image/%s", housephy.Image))
- //if err != nil {
- // fmt.Println(err)
- // return
- //}
- //
- //f, _ := ioutil.ReadAll(file)
- //r.Response.Writer.Write(f)
- if housephy.Image != "" {
- appG.Response(app.StatusOK, app.SUCCESS, fmt.Sprintf("image/%s", housephy.Image))
- } else {
- appG.Response(app.StatusOK, app.SUCCESS, nil)
- }
- }
- // UploadImage
- // @Summary 上传接收器图片
- // @Description add by json account
- // @Tags 接收器管理
- // @Accept json
- // @Produce json
- // @Param id path int true "int "
- // @Router /housephy/image [post]
- func UploadImage(r *ghttp.Request) {
- appG := app.Ghttp{C: r}
- id := r.Get("id").String()
- file := r.GetUploadFile("file")
- os.Remove(fmt.Sprintf("image/%s", file.Filename))
- _, err := file.Save("image")
- if err != nil {
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- housephy, err := srv.GetHousephyImage(id)
- if err != nil {
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- if housephy.Image != "" {
- os.Remove(fmt.Sprintf("image/%s", housephy.Image))
- }
- err = srv.UpdateHousephyImage(&model.Housephy{Id: id, Image: file.Filename})
- if err != nil {
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- appG.Response(app.StatusOK, app.SUCCESS, true)
- }
- func GetDataByNames(r *ghttp.Request) {
- appG := app.Ghttp{C: r}
- dataByte, _ := ioutil.ReadAll(r.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlParams := fsion.Array()
- queryData := make(map[string]interface{})
- for _, v := range sqlParams {
- fsion2 := gofasion.NewFasion(v.Json())
- //logging.Info("GetDataByNames", c.Keys, fsion2.Get("name").ValueStr(), c.Request.RemoteAddr)
- fmt.Println(fsion2.Get("name").ValueStr())
- sql, p := srv.GetSqlByNameDB(fsion2.Get("name").ValueStr())
- if sql == "" {
- errorm := make(map[string]interface{})
- errorm["code"] = app.ERROR
- errorm["message"] = "sql没找到"
- queryData[fsion2.Get("name").ValueStr()] = errorm
- continue
- } else if sql != "" {
- offset := fsion2.Get("offset").ValueInt()
- pagecount := fsion2.Get("pagecount").ValueInt()
- returntype := fsion2.Get("returntype").ValueStr()
- s_params := make([]interface{}, 0)
- if fsion2.HasKey("parammaps") {
- parammaps := fsion2.Get("parammaps")
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if r.Get("jwt_username").String() != "" {
- s_params = append(s_params, r.Get("jwt_username").String())
- } else {
- s_params = append(s_params, "")
- }
- } else {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- }
- } else if fsion2.HasKey("params") {
- params := fsion2.Get("params").Array()
- for _, v_params := range params {
- s_params = append(s_params, v_params.ValueStr())
- }
- }
- queryData1, err := getDataBySql(sql, offset, pagecount, returntype, s_params)
- if err != nil {
- errorm := make(map[string]interface{})
- errorm["code"] = app.ERROR
- errorm["message"] = err
- queryData[fsion2.Get("name").ValueStr()] = errorm
- //logging.Error("GetDataByNames err: ", err)
- continue
- } else {
- queryData[fsion2.Get("name").ValueStr()] = queryData1
- }
- }
- }
- appG.Response(http.StatusOK, app.SUCCESS, queryData)
- }
- func getDataBySql(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
- if offset > 0 {
- offset = offset - 1
- }
- //if pagecount == 0 {
- // pagecount = setting.AppSetting.PageSize
- //}else if pagecount>setting.AppSetting.PageSize{
- // pagecount = setting.AppSetting.PageSize
- //}
- if returntype == "list" {
- //queryData, err := srv.QueryByList(sqlstr, offset, pagecount, params)
- //return queryData, err
- } else if returntype == "listlist" {
- //queryData, err := srv.QueryByListList(sqlstr, offset, pagecount, params)
- //return queryData, err
- } else {
- queryData, err := srv.QueryByMap(sqlstr, offset, pagecount, params)
- return queryData, err
- }
- return nil, nil
- }
|