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) }