123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- package controller
- import (
- v1 "demo/api/v1"
- "demo/internal/app"
- "demo/internal/model"
- "github.com/gogf/gf/v2/net/ghttp"
- "github.com/pkg/errors"
- "github.com/siddontang/go/log"
- "time"
- )
- // GetSim
- // @Summary SIM卡查看
- // @Description add by json account
- // @Tags SIM卡管理
- // @Accept json
- // @Produce json
- // @Param index path int true "int "
- // @Param pagesize path int true "int "
- // @Param card path string true "string sim卡号"
- // @Param company path string true "string 公司"
- // @Param status path int true "string 状态"
- // @Success 200 {object} v1.GetSimRes
- // @Router /sim/list [get]
- func GetSim(r *ghttp.Request) {
- var req *v1.GetSimReq
- appG := app.Ghttp{C: r}
- err := appG.C.Parse(&req)
- if err != nil {
- err = errors.Wrap(err, "GetSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- simList, count, err := srv.GetSimList(req.Index, req.PageSize, req.Card, req.Company, req.Status)
- if err != nil {
- return
- }
- resp := v1.GetSimRes{}
- dataList := make([]*v1.Sim, 0)
- for _, item := range simList {
- var data v1.Sim
- data.Id = item.Sim.Id
- data.Card = item.Card
- data.TotalFlow = item.TotalFlow
- data.Used = item.Used
- data.Remaining = item.Remaining
- data.Company = item.Company
- if !item.PurchaseDate.IsZero() {
- data.PurchaseDate = item.PurchaseDate.Format("2006-01-02")
- }
- if !item.ActivateDate.IsZero() {
- data.ActivateDate = item.ActivateDate.Format("2006-01-02")
- }
- if !item.TopUpDate.IsZero() {
- data.TopUpDate = item.TopUpDate.Format("2006-01-02 15:04:05")
- }
- if !item.EndDate.IsZero() {
- data.EndDate = item.EndDate.Format("2006-01-02")
- }
- data.Status = item.Sim.Status
- data.ReceiverId = item.ReceiverId
- data.Pastureid = item.Sim.Pastureid
- //data.BarId = item.BarId
- data.BarName = item.BarName
- data.PastureName = item.PastureName
- data.PastureCode = item.PastureCode
- data.ReceiverCode = item.Receiver.IMEI
- dataList = append(dataList, &data)
- }
- resp.List = dataList
- resp.Total = count
- appG.Response(app.StatusOK, app.SUCCESS, resp)
- }
- //AddSim
- //@Summary SIM卡添加
- //@Description add by json account
- //@Tags SIM卡管理
- //@Accept json
- //@Produce json
- //@Param account body v1.AddSimReq true "Add account"
- //@Router /sim/add [post]
- func AddSim(r *ghttp.Request) {
- var req *v1.AddSimReq
- appG := app.Ghttp{C: r}
- err := appG.C.Parse(&req)
- if err != nil {
- err = errors.Wrap(err, "AddSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- layout := "2006-01-02"
- var purchaseDate, activateDate, topUpDate, endDate time.Time
- if req.PurchaseDate != "" {
- purchaseDate, err = time.Parse(layout, req.PurchaseDate)
- if err != nil {
- err = errors.Wrap(err, "AddSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- }
- if req.ActivateDate != "" {
- activateDate, err = time.Parse(layout, req.ActivateDate)
- if err != nil {
- err = errors.Wrap(err, "AddSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- }
- if req.TopUpDate != "" {
- topUpDate, err = time.Parse("2006-01-02 15:04:05", req.TopUpDate)
- if err != nil {
- err = errors.Wrap(err, "AddSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- }
- if req.EndDate != "" {
- endDate, err = time.Parse(layout, req.EndDate)
- if err != nil {
- err = errors.Wrap(err, "AddSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- }
- exist, err := srv.CheckSimCode(0, req.Card)
- if err != nil {
- err = errors.Wrap(err, "AddSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- if exist {
- appG.Response(app.StatusInternalServerError, app.ERROR, "SIM卡号已经存在!!!")
- return
- }
- id, err := srv.AddSim(model.Sim{
- Card: req.Card,
- TotalFlow: req.TotalFlow,
- Used: req.Used,
- Remaining: req.Remaining,
- Company: req.Company,
- PurchaseDate: purchaseDate,
- ActivateDate: activateDate,
- TopUpDate: topUpDate,
- EndDate: endDate,
- Status: req.Status,
- //ReceiverId: req.ReceiverId,
- Pastureid: req.Pastureid,
- //BarId: req.BarId,
- //BarName: req.BarName,
- })
- if err != nil {
- return
- }
- if req.ReceiverId != 0 && id != 0 {
- err = srv.UpdateReceiverSimId(&model.Receiver{
- Id: req.ReceiverId,
- SimID: id,
- })
- if err != nil {
- return
- }
- }
- appG.Response(app.StatusOK, app.SUCCESS, true)
- }
- //EditSim
- //@Summary SIM卡修改
- //@Description add by json account
- //@Tags SIM卡管理
- //@Accept json
- //@Produce json
- //@Param account body v1.EditSimReq true "Add account"
- //@Router /sim/edit [post]
- func EditSim(r *ghttp.Request) {
- var req *v1.EditSimReq
- appG := app.Ghttp{C: r}
- err := appG.C.Parse(&req)
- if err != nil {
- err = errors.Wrap(err, "EditSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- layout := "2006-01-02"
- var purchaseDate, activateDate, topUpDate, endDate time.Time
- if req.PurchaseDate != "" {
- purchaseDate, err = time.Parse(layout, req.PurchaseDate)
- if err != nil {
- err = errors.Wrap(err, "AddSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- }
- if req.ActivateDate != "" {
- activateDate, err = time.Parse(layout, req.ActivateDate)
- if err != nil {
- err = errors.Wrap(err, "AddSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- }
- if req.TopUpDate != "" {
- topUpDate, err = time.Parse("2006-01-02 15:04:05", req.TopUpDate)
- if err != nil {
- err = errors.Wrap(err, "AddSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- }
- if req.EndDate != "" {
- endDate, err = time.Parse(layout, req.EndDate)
- if err != nil {
- err = errors.Wrap(err, "AddSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- }
- exist, err := srv.CheckSimCode(req.Id, req.Card)
- if err != nil {
- err = errors.Wrap(err, "AddSim-error")
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- if exist {
- appG.Response(app.StatusInternalServerError, app.ERROR, "SIM卡号已经存在!!!")
- return
- }
- err = srv.EditSim(model.Sim{
- Id: req.Id,
- Card: req.Card,
- TotalFlow: req.TotalFlow,
- Used: req.Used,
- Remaining: req.Remaining,
- Company: req.Company,
- PurchaseDate: purchaseDate,
- ActivateDate: activateDate,
- TopUpDate: topUpDate,
- EndDate: endDate,
- Status: req.Status,
- //ReceiverId: req.ReceiverId,
- Pastureid: req.Pastureid,
- //BarId: req.BarId,
- //BarName: req.BarName,
- })
- if err != nil {
- return
- }
- if req.Status == 0 {
- err = srv.UpdateReceiverSimId(&model.Receiver{
- Id: req.ReceiverId,
- SimID: 0,
- })
- if err != nil {
- return
- }
- }
- //if req.ReceiverId != 0 {
- err = srv.UpdateReceiverSimId(&model.Receiver{
- Id: req.ReceiverId,
- SimID: req.Id,
- })
- if err != nil {
- return
- }
- //} else {
- // err = srv.UpdateReceiverSimId(&model.Receiver{
- // Id: req.Id,
- // SimID: 0,
- // })
- // if err != nil {
- // return
- // }
- //}
- appG.Response(app.StatusOK, app.SUCCESS, true)
- }
- // GetSimPull
- // @Summary SIM卡下拉
- // @Description add by json account
- // @Tags 公共接口
- // @Accept json
- // @Produce json
- // @Success 200 {object} v1.GetSimPullRes
- // @Router /sim/pull [get]
- func GetSimPull(r *ghttp.Request) {
- appG := app.Ghttp{C: r}
- simList, err := srv.GetSimPull()
- if err != nil {
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- resp := v1.GetSimPullRes{}
- dataList := make([]*v1.SimPull, 0)
- for _, item := range simList {
- var arg v1.SimPull
- arg.Id = item.Id
- arg.Card = item.Card
- dataList = append(dataList, &arg)
- }
- resp.List = dataList
- appG.Response(app.StatusOK, app.SUCCESS, resp)
- }
|