sim.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package controller
  2. import (
  3. v1 "demo/api/v1"
  4. "demo/internal/app"
  5. "demo/internal/model"
  6. "github.com/gogf/gf/v2/net/ghttp"
  7. "github.com/pkg/errors"
  8. "github.com/siddontang/go/log"
  9. "time"
  10. )
  11. // GetSim
  12. // @Summary SIM卡查看
  13. // @Description add by json account
  14. // @Tags SIM卡管理
  15. // @Accept json
  16. // @Produce json
  17. // @Param index path int true "int "
  18. // @Param pagesize path int true "int "
  19. // @Param card path string true "string sim卡号"
  20. // @Param company path string true "string 公司"
  21. // @Param status path int true "string 状态"
  22. // @Success 200 {object} v1.GetSimRes
  23. // @Router /sim/list [get]
  24. func GetSim(r *ghttp.Request) {
  25. var req *v1.GetSimReq
  26. appG := app.Ghttp{C: r}
  27. err := appG.C.Parse(&req)
  28. if err != nil {
  29. err = errors.Wrap(err, "GetSim-error")
  30. log.Error(err)
  31. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  32. return
  33. }
  34. simList, count, err := srv.GetSimList(req.Index, req.PageSize, req.Card, req.Company, req.Status)
  35. if err != nil {
  36. return
  37. }
  38. resp := v1.GetSimRes{}
  39. dataList := make([]*v1.Sim, 0)
  40. for _, item := range simList {
  41. var data v1.Sim
  42. data.Id = item.Sim.Id
  43. data.Card = item.Card
  44. data.TotalFlow = item.TotalFlow
  45. data.Used = item.Used
  46. data.Remaining = item.Remaining
  47. data.Company = item.Company
  48. if !item.PurchaseDate.IsZero() {
  49. data.PurchaseDate = item.PurchaseDate.Format("2006-01-02")
  50. }
  51. if !item.ActivateDate.IsZero() {
  52. data.ActivateDate = item.ActivateDate.Format("2006-01-02")
  53. }
  54. if !item.TopUpDate.IsZero() {
  55. data.TopUpDate = item.TopUpDate.Format("2006-01-02 15:04:05")
  56. }
  57. if !item.EndDate.IsZero() {
  58. data.EndDate = item.EndDate.Format("2006-01-02")
  59. }
  60. data.Status = item.Sim.Status
  61. data.ReceiverId = item.ReceiverId
  62. data.Pastureid = item.Sim.Pastureid
  63. //data.BarId = item.BarId
  64. data.BarName = item.BarName
  65. data.PastureName = item.PastureName
  66. data.PastureCode = item.PastureCode
  67. data.ReceiverCode = item.Receiver.IMEI
  68. dataList = append(dataList, &data)
  69. }
  70. resp.List = dataList
  71. resp.Total = count
  72. appG.Response(app.StatusOK, app.SUCCESS, resp)
  73. }
  74. //AddSim
  75. //@Summary SIM卡添加
  76. //@Description add by json account
  77. //@Tags SIM卡管理
  78. //@Accept json
  79. //@Produce json
  80. //@Param account body v1.AddSimReq true "Add account"
  81. //@Router /sim/add [post]
  82. func AddSim(r *ghttp.Request) {
  83. var req *v1.AddSimReq
  84. appG := app.Ghttp{C: r}
  85. err := appG.C.Parse(&req)
  86. if err != nil {
  87. err = errors.Wrap(err, "AddSim-error")
  88. log.Error(err)
  89. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  90. return
  91. }
  92. layout := "2006-01-02"
  93. var purchaseDate, activateDate, topUpDate, endDate time.Time
  94. if req.PurchaseDate != "" {
  95. purchaseDate, err = time.Parse(layout, req.PurchaseDate)
  96. if err != nil {
  97. err = errors.Wrap(err, "AddSim-error")
  98. log.Error(err)
  99. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  100. return
  101. }
  102. }
  103. if req.ActivateDate != "" {
  104. activateDate, err = time.Parse(layout, req.ActivateDate)
  105. if err != nil {
  106. err = errors.Wrap(err, "AddSim-error")
  107. log.Error(err)
  108. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  109. return
  110. }
  111. }
  112. if req.TopUpDate != "" {
  113. topUpDate, err = time.Parse("2006-01-02 15:04:05", req.TopUpDate)
  114. if err != nil {
  115. err = errors.Wrap(err, "AddSim-error")
  116. log.Error(err)
  117. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  118. return
  119. }
  120. }
  121. if req.EndDate != "" {
  122. endDate, err = time.Parse(layout, req.EndDate)
  123. if err != nil {
  124. err = errors.Wrap(err, "AddSim-error")
  125. log.Error(err)
  126. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  127. return
  128. }
  129. }
  130. exist, err := srv.CheckSimCode(0, req.Card)
  131. if err != nil {
  132. err = errors.Wrap(err, "AddSim-error")
  133. log.Error(err)
  134. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  135. return
  136. }
  137. if exist {
  138. appG.Response(app.StatusInternalServerError, app.ERROR, "SIM卡号已经存在!!!")
  139. return
  140. }
  141. id, err := srv.AddSim(model.Sim{
  142. Card: req.Card,
  143. TotalFlow: req.TotalFlow,
  144. Used: req.Used,
  145. Remaining: req.Remaining,
  146. Company: req.Company,
  147. PurchaseDate: purchaseDate,
  148. ActivateDate: activateDate,
  149. TopUpDate: topUpDate,
  150. EndDate: endDate,
  151. Status: req.Status,
  152. //ReceiverId: req.ReceiverId,
  153. Pastureid: req.Pastureid,
  154. //BarId: req.BarId,
  155. //BarName: req.BarName,
  156. })
  157. if err != nil {
  158. return
  159. }
  160. if req.ReceiverId != 0 && id != 0 {
  161. err = srv.UpdateReceiverSimId(&model.Receiver{
  162. Id: req.ReceiverId,
  163. SimID: id,
  164. })
  165. if err != nil {
  166. return
  167. }
  168. }
  169. appG.Response(app.StatusOK, app.SUCCESS, true)
  170. }
  171. //EditSim
  172. //@Summary SIM卡修改
  173. //@Description add by json account
  174. //@Tags SIM卡管理
  175. //@Accept json
  176. //@Produce json
  177. //@Param account body v1.EditSimReq true "Add account"
  178. //@Router /sim/edit [post]
  179. func EditSim(r *ghttp.Request) {
  180. var req *v1.EditSimReq
  181. appG := app.Ghttp{C: r}
  182. err := appG.C.Parse(&req)
  183. if err != nil {
  184. err = errors.Wrap(err, "EditSim-error")
  185. log.Error(err)
  186. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  187. return
  188. }
  189. layout := "2006-01-02"
  190. var purchaseDate, activateDate, topUpDate, endDate time.Time
  191. if req.PurchaseDate != "" {
  192. purchaseDate, err = time.Parse(layout, req.PurchaseDate)
  193. if err != nil {
  194. err = errors.Wrap(err, "AddSim-error")
  195. log.Error(err)
  196. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  197. return
  198. }
  199. }
  200. if req.ActivateDate != "" {
  201. activateDate, err = time.Parse(layout, req.ActivateDate)
  202. if err != nil {
  203. err = errors.Wrap(err, "AddSim-error")
  204. log.Error(err)
  205. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  206. return
  207. }
  208. }
  209. if req.TopUpDate != "" {
  210. topUpDate, err = time.Parse("2006-01-02 15:04:05", req.TopUpDate)
  211. if err != nil {
  212. err = errors.Wrap(err, "AddSim-error")
  213. log.Error(err)
  214. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  215. return
  216. }
  217. }
  218. if req.EndDate != "" {
  219. endDate, err = time.Parse(layout, req.EndDate)
  220. if err != nil {
  221. err = errors.Wrap(err, "AddSim-error")
  222. log.Error(err)
  223. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  224. return
  225. }
  226. }
  227. exist, err := srv.CheckSimCode(req.Id, req.Card)
  228. if err != nil {
  229. err = errors.Wrap(err, "AddSim-error")
  230. log.Error(err)
  231. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  232. return
  233. }
  234. if exist {
  235. appG.Response(app.StatusInternalServerError, app.ERROR, "SIM卡号已经存在!!!")
  236. return
  237. }
  238. err = srv.EditSim(model.Sim{
  239. Id: req.Id,
  240. Card: req.Card,
  241. TotalFlow: req.TotalFlow,
  242. Used: req.Used,
  243. Remaining: req.Remaining,
  244. Company: req.Company,
  245. PurchaseDate: purchaseDate,
  246. ActivateDate: activateDate,
  247. TopUpDate: topUpDate,
  248. EndDate: endDate,
  249. Status: req.Status,
  250. //ReceiverId: req.ReceiverId,
  251. Pastureid: req.Pastureid,
  252. //BarId: req.BarId,
  253. //BarName: req.BarName,
  254. })
  255. if err != nil {
  256. return
  257. }
  258. if req.Status == 0 {
  259. err = srv.UpdateReceiverSimId(&model.Receiver{
  260. Id: req.ReceiverId,
  261. SimID: 0,
  262. })
  263. if err != nil {
  264. return
  265. }
  266. }
  267. //if req.ReceiverId != 0 {
  268. err = srv.UpdateReceiverSimId(&model.Receiver{
  269. Id: req.ReceiverId,
  270. SimID: req.Id,
  271. })
  272. if err != nil {
  273. return
  274. }
  275. //} else {
  276. // err = srv.UpdateReceiverSimId(&model.Receiver{
  277. // Id: req.Id,
  278. // SimID: 0,
  279. // })
  280. // if err != nil {
  281. // return
  282. // }
  283. //}
  284. appG.Response(app.StatusOK, app.SUCCESS, true)
  285. }
  286. // GetSimPull
  287. // @Summary SIM卡下拉
  288. // @Description add by json account
  289. // @Tags 公共接口
  290. // @Accept json
  291. // @Produce json
  292. // @Success 200 {object} v1.GetSimPullRes
  293. // @Router /sim/pull [get]
  294. func GetSimPull(r *ghttp.Request) {
  295. appG := app.Ghttp{C: r}
  296. simList, err := srv.GetSimPull()
  297. if err != nil {
  298. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  299. return
  300. }
  301. resp := v1.GetSimPullRes{}
  302. dataList := make([]*v1.SimPull, 0)
  303. for _, item := range simList {
  304. var arg v1.SimPull
  305. arg.Id = item.Id
  306. arg.Card = item.Card
  307. dataList = append(dataList, &arg)
  308. }
  309. resp.List = dataList
  310. appG.Response(app.StatusOK, app.SUCCESS, resp)
  311. }