order.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. package simApi
  2. import (
  3. "errors"
  4. "go-nc/configs/global"
  5. "go-nc/internal/utils"
  6. "go-nc/model"
  7. "go-nc/model/common"
  8. "go-nc/pkg/sim"
  9. "go-nc/pkg/sim/grace"
  10. "path/filepath"
  11. "strconv"
  12. "time"
  13. "github.com/360EntSecGroup-Skylar/excelize"
  14. "github.com/gin-gonic/gin"
  15. "github.com/jinzhu/copier"
  16. )
  17. // 购卡申请
  18. func SimApply(c *gin.Context) {
  19. type Param struct {
  20. UserId uint `json:"user_id"`
  21. Source string `json:"source" validate:"required"` // 来源
  22. TrafficId string `json:"trafficId" validate:"required"` // 资费Id
  23. PeriodOfSilence string `json:"periodOfSilence" validate:"required"` // 静默期
  24. IsTrafficPool string `json:"isTrafficPool" validate:"required"` // 是否是流量池
  25. Quantity int `json:"quantity" validate:"required"` // 采购数量
  26. SimType string `json:"simType" validate:"required"` // 卡类型
  27. TmsStatus string `json:"tmsStatus"` // 物流状态:1:未发货 2:已发货 3:已收货
  28. ModerationStatus string `json:"moderationStatus"` // 订单审核状态:1: 待审核 2: 审核通过 3: 已驳回
  29. // ContractImg string `json:"contractImg"` // 合同图片
  30. // ModerationNotes string `json:"moderationNotes"` // 审核备注
  31. Status string `json:"status"`
  32. PoolId string `json:"poolId"`
  33. }
  34. var param Param
  35. if err := c.ShouldBindJSON(&param); err != nil {
  36. c.Error(errors.New("缺少参数:" + err.Error()))
  37. return
  38. }
  39. // 获取用户信息
  40. userInfoInterface, _ := c.Get("userInfo")
  41. userInfo, _ := userInfoInterface.(model.Sys_user)
  42. if userInfo.UserType != "2" {
  43. c.Error(errors.New("无操作权限!"))
  44. return
  45. }
  46. param.UserId = userInfo.Id
  47. param.TmsStatus = "1"
  48. param.ModerationStatus = "1"
  49. param.Status = "1"
  50. err := utils.ValidateStruct(param)
  51. if err != nil {
  52. c.Error(err)
  53. return
  54. }
  55. var db model.Cmi_sim_order
  56. copier.Copy(&db, &param)
  57. if err := global.App.DB.Model(&model.Cmi_sim_order{}).Create(&db).Error; err != nil {
  58. c.Error(err)
  59. return
  60. }
  61. c.Set("res_data", db)
  62. }
  63. // 购卡申请信息列表
  64. func SimApplyList(c *gin.Context) {
  65. type Param struct {
  66. Id string `json:"id"`
  67. UserName string `json:"userName"`
  68. common.Pagination
  69. }
  70. var param Param
  71. param.Pagination = common.NewPagination()
  72. if err := c.ShouldBindJSON(&param); err != nil {
  73. c.Error(errors.New("缺少参数:" + err.Error()))
  74. return
  75. }
  76. // 获取用户信息
  77. userInfoInterface, _ := c.Get("userInfo")
  78. userInfo, _ := userInfoInterface.(model.Sys_user)
  79. var total int64
  80. var dbList []model.Cmi_sim_order
  81. // 用户类型为2查询用户下的订单,1查询全部订单
  82. if userInfo.UserType == "2" {
  83. // 查询总记录数
  84. if err := global.App.DB.Model(&model.Cmi_sim_order{}).
  85. Where("user_id = ?", userInfo.Id).
  86. Where("id LIKE ?", "%"+param.Id+"%").
  87. Where("status = 1").
  88. Count(&total).Error; err != nil {
  89. c.Error(err)
  90. return
  91. }
  92. if err := global.App.DB.
  93. Where("user_id = ?", userInfo.Id).
  94. Where("id LIKE ?", "%"+param.Id+"%").
  95. Where("status = 1").
  96. Limit(param.PageSize).
  97. Offset((param.Current - 1) * param.PageSize).
  98. Find(&dbList).Error; err != nil {
  99. c.Error(err)
  100. return
  101. }
  102. }
  103. if userInfo.UserType == "1" {
  104. // 查询总记录数
  105. if err := global.App.DB.Model(&model.Cmi_sim_order{}).
  106. Where("id LIKE ?", "%"+param.Id+"%").
  107. Where("status = 1").
  108. Count(&total).Error; err != nil {
  109. c.Error(err)
  110. return
  111. }
  112. if err := global.App.DB.
  113. Where("id LIKE ?", "%"+param.Id+"%").
  114. Where("status = 1").
  115. Limit(param.PageSize).
  116. Offset((param.Current - 1) * param.PageSize).
  117. Find(&dbList).Error; err != nil {
  118. c.Error(err)
  119. return
  120. }
  121. }
  122. type recordsData struct {
  123. model.Cmi_sim_order
  124. UserName string `json:"userName"`
  125. TrafficName string `json:"trafficName"`
  126. PoolName string `json:"poolName"`
  127. EndDate time.Time `json:"endDate"`
  128. }
  129. var recordsDataList []recordsData
  130. for _, v := range dbList {
  131. var traffic model.Sim_traffic
  132. if err := global.App.DB.Model(&model.Sim_traffic{}).Select("label", "end_date").Where("id = ?", v.TrafficId).Find(&traffic).Error; err != nil {
  133. c.Error(err)
  134. return
  135. }
  136. var user model.Sys_user
  137. if err := global.App.DB.Model(&model.Sys_user{}).Select("name").Where("id = ?", v.UserId).Find(&user).Error; err != nil {
  138. c.Error(err)
  139. return
  140. }
  141. var pool model.Sim_pool
  142. if v.PoolId != "" {
  143. if err := global.App.DB.Model(&model.Sim_pool{}).Select("label").Where("id = ?", v.PoolId).First(&pool).Error; err != nil {
  144. c.Error(err)
  145. return
  146. }
  147. }
  148. recordsDataList = append(recordsDataList, recordsData{
  149. Cmi_sim_order: v,
  150. TrafficName: traffic.Label,
  151. UserName: user.Name,
  152. PoolName: pool.Label,
  153. EndDate: traffic.EndDate,
  154. })
  155. }
  156. // 序列化
  157. records := make([]interface{}, len(recordsDataList))
  158. for i, user := range recordsDataList {
  159. records[i] = user
  160. }
  161. // 返回数据
  162. data := common.Pagination{
  163. Records: records,
  164. Current: param.Current,
  165. PageSize: param.PageSize,
  166. Total: total,
  167. }
  168. c.Set("res_data", data)
  169. }
  170. // 购卡审核
  171. func SimApplyModeration(c *gin.Context) {
  172. type Param struct {
  173. Id string `json:"id" validate:"required"`
  174. ModerationStatus string `json:"moderationStatus" validate:"required"`
  175. ModerationNotes string `json:"moderationNotes" validate:"required"`
  176. }
  177. var param Param
  178. if err := c.ShouldBindJSON(&param); err != nil {
  179. c.Error(errors.New("缺少参数:" + err.Error()))
  180. return
  181. }
  182. err := utils.ValidateStruct(param)
  183. if err != nil {
  184. c.Error(err)
  185. return
  186. }
  187. // 获取用户信息
  188. userInfoInterface, _ := c.Get("userInfo")
  189. userInfo, _ := userInfoInterface.(model.Sys_user)
  190. if userInfo.UserType != "1" {
  191. c.Error(errors.New("无操作权限!"))
  192. return
  193. }
  194. // 查询订单状态
  195. var order model.Cmi_sim_order
  196. if err := global.App.DB.Where("id = ?", param.Id).First(&order).Error; err != nil {
  197. c.Error(err)
  198. return
  199. }
  200. if order.ModerationStatus != "1" && order.Status != "1" {
  201. c.Error(errors.New("该订单已审核,不可操作!"))
  202. return
  203. }
  204. // 修改订单状态
  205. if err := global.App.DB.Model(&model.Cmi_sim_order{}).
  206. Where("id = ?", param.Id).
  207. Update("moderation_status", param.ModerationStatus).
  208. Update("moderation_notes", param.ModerationNotes).
  209. Error; err != nil {
  210. c.Error(err)
  211. return
  212. }
  213. // 是否是退卡订单&& 审核通过 -- 进行停卡操作
  214. if order.Status == "2" && param.ModerationStatus == "2" {
  215. // 查询退卡iccid
  216. var iccids []string
  217. if err := global.App.DB.Model(&model.Cmi_sim_order_card{}).Where("order_id = ? AND status = '2'", order.Id).Select("iccid").Scan(&iccids).Error; err != nil {
  218. c.Error(err)
  219. return
  220. }
  221. // 停卡
  222. for _, v := range iccids {
  223. sim.StopSim("grace", v)
  224. }
  225. }
  226. c.Set("res_data", "操作成功!")
  227. }
  228. // 上传合同
  229. func SimUploadContract(c *gin.Context) {
  230. type Param struct {
  231. Id string `json:"id" validate:"required"`
  232. ContractImg string `json:"contractImg" validate:"required"`
  233. }
  234. var param Param
  235. if err := c.ShouldBindJSON(&param); err != nil {
  236. c.Error(errors.New("缺少参数:" + err.Error()))
  237. return
  238. }
  239. err := utils.ValidateStruct(param)
  240. if err != nil {
  241. c.Error(err)
  242. return
  243. }
  244. // 修改订单状态
  245. if err := global.App.DB.Model(&model.Cmi_sim_order{}).
  246. Where("id = ? AND status = '1'", param.Id).
  247. Update("contract_img", param.ContractImg).
  248. Error; err != nil {
  249. c.Error(err)
  250. return
  251. }
  252. c.Set("res_data", "上传成功!")
  253. }
  254. // 购卡:分配卡号
  255. func SimAssignSim(c *gin.Context) {
  256. file, err := c.FormFile("file")
  257. if err != nil {
  258. c.Error(errors.New("文件上传失败!"))
  259. return
  260. }
  261. formValues := c.Request.Form
  262. orderId := formValues.Get("orderId")
  263. if orderId == "" {
  264. c.Error(errors.New("缺少参数:orderId"))
  265. return
  266. }
  267. // 保存上传的文件到项目根目录
  268. filepath := filepath.Join(utils.GetProjectRoot(), "public")
  269. if err := c.SaveUploadedFile(file, filepath+"/"+file.Filename); err != nil {
  270. c.Error(errors.New("文件保存失败!"))
  271. return
  272. }
  273. // 打开Excel文件
  274. f, err := excelize.OpenFile(filepath + "/" + file.Filename)
  275. if err != nil {
  276. c.Error(errors.New("打开Excel文件失败"))
  277. return
  278. }
  279. rows := f.GetRows("Sheet1")
  280. rows = rows[1:]
  281. // 查询订单信息
  282. var order model.Cmi_sim_order
  283. if err := global.App.DB.Model(&model.Cmi_sim_order{}).Select("id", "traffic_id", "pool_id", "user_id", "source", "quantity").Where("id = ?", orderId).First(&order).Error; err != nil {
  284. c.Error(err)
  285. return
  286. }
  287. // 通过资费获取流量包ID
  288. var traffic model.Sim_traffic
  289. if err := global.App.DB.Select("sim_data_plan_id").Where("id = ?", order.TrafficId).First(&traffic).Error; err != nil {
  290. c.Error(err)
  291. return
  292. }
  293. opts := sim.GetCardInfoOptions{
  294. TariffId: order.TrafficId,
  295. PoolId: order.PoolId,
  296. UserId: order.UserId,
  297. }
  298. iccids := make([]string, len(rows))
  299. for i, row := range rows {
  300. iccids[i] = row[0]
  301. }
  302. // 入库
  303. for _, iccid := range rows {
  304. sim.GetCardInfo(order.Source, iccid[0], &opts)
  305. cardItem := model.Cmi_sim_order_card{
  306. OrderId: order.Id,
  307. Iccid: iccid[0],
  308. Status: "1",
  309. }
  310. // 查询
  311. orderCardSum := int64(0)
  312. global.App.DB.Model(&model.Cmi_sim_order_card{}).Where("order_id = ? AND iccid in (?)", order.Id, iccids).Count(&orderCardSum)
  313. if orderCardSum >= int64(order.Quantity) {
  314. c.Error(errors.New("已分配到采购最大数量!"))
  315. return
  316. }
  317. // Cmi_sim_order_card 查询卡没有创建,有则不管
  318. global.App.DB.Model(&model.Cmi_sim_order_card{}).Where("order_id = ? AND iccid = ?", order.Id, iccid[0]).FirstOrCreate(&cardItem)
  319. // 绑定套餐
  320. productId, _ := strconv.Atoi(traffic.SimDataPlanId)
  321. sim.RechargeCard(order.Source, grace.RechargeSimPackage{
  322. Iccid: iccid[0],
  323. Type: "1",
  324. ProductId: productId,
  325. Num: traffic.SimDataPlanId,
  326. })
  327. }
  328. // 订单状态改为已发货
  329. global.App.DB.Model(&model.Cmi_sim_order{}).
  330. Where("id = ?", orderId).
  331. Update("tms_status", "2")
  332. c.Set("res_data", rows)
  333. }
  334. // 查看购卡订单的卡
  335. func SimOrderCard(c *gin.Context) {
  336. orderId := c.Query("id")
  337. if orderId == "" {
  338. c.Error(errors.New("缺少参数:id"))
  339. return
  340. }
  341. var data []model.Cmi_sim_order_card
  342. if err := global.App.DB.Where("order_id = ? OR return_order_id = ?", orderId, orderId).Find(&data).Error; err != nil {
  343. c.Error(err)
  344. return
  345. }
  346. c.Set("res_data", data)
  347. }
  348. // 退卡
  349. func SimReturnCard(c *gin.Context) {
  350. type Param struct {
  351. OrderId string `json:"id" validate:"required"`
  352. ICCID []string `json:"iccids" validate:"required"`
  353. }
  354. var param Param
  355. if err := c.ShouldBindJSON(&param); err != nil {
  356. c.Error(errors.New("缺少参数:" + err.Error()))
  357. return
  358. }
  359. err := utils.ValidateStruct(param)
  360. if err != nil {
  361. c.Error(err)
  362. return
  363. }
  364. tx := global.App.DB.Begin()
  365. // 创建退卡订单
  366. var order model.Cmi_sim_order
  367. if err := tx.Model(&model.Cmi_sim_order{}).Where("id = ?", param.OrderId).First(&order).Error; err != nil {
  368. tx.Rollback()
  369. c.Error(err)
  370. return
  371. }
  372. // 创建退卡订单
  373. var returnOrder model.Cmi_sim_order
  374. copier.Copy(&returnOrder, &order)
  375. returnOrder.Status = "2"
  376. returnOrder.ModerationStatus = "1"
  377. if err := tx.Model(&model.Cmi_sim_order{}).Create(&returnOrder).Error; err != nil {
  378. tx.Rollback()
  379. c.Error(err)
  380. return
  381. }
  382. // 创建卡订单id
  383. if err := tx.Model(&model.Cmi_sim_order_card{}).
  384. Where("iccid in (?)", param.ICCID).
  385. Where("order_id = ?", order.Id).
  386. Update("status", "2").
  387. Update("return_order_id", returnOrder.Id).Error; err != nil {
  388. tx.Rollback()
  389. c.Error(err)
  390. return
  391. }
  392. tx.Commit()
  393. c.Set("res_data", "操作成功!")
  394. }
  395. // 退卡申请信息列表
  396. func SimReturnCardList(c *gin.Context) {
  397. type Param struct {
  398. Id string `json:"id"`
  399. UserName string `json:"userName"`
  400. common.Pagination
  401. }
  402. var param Param
  403. param.Pagination = common.NewPagination()
  404. if err := c.ShouldBindJSON(&param); err != nil {
  405. c.Error(errors.New("缺少参数:" + err.Error()))
  406. return
  407. }
  408. // 获取用户信息
  409. userInfoInterface, _ := c.Get("userInfo")
  410. userInfo, _ := userInfoInterface.(model.Sys_user)
  411. var total int64
  412. var dbList []model.Cmi_sim_order
  413. // 用户类型为2查询用户下的订单,1查询全部订单
  414. if userInfo.UserType == "2" {
  415. // 查询总记录数
  416. if err := global.App.DB.Model(&model.Cmi_sim_order{}).
  417. Where("status = ?", "2").
  418. Where("user_id = ?", userInfo.Id).
  419. Where("id LIKE ?", "%"+param.Id+"%").
  420. Count(&total).Error; err != nil {
  421. c.Error(err)
  422. return
  423. }
  424. if err := global.App.DB.
  425. Where("status = ?", "2").
  426. Where("user_id = ?", userInfo.Id).
  427. Where("id LIKE ?", "%"+param.Id+"%").
  428. Limit(param.PageSize).
  429. Offset((param.Current - 1) * param.PageSize).
  430. Find(&dbList).Error; err != nil {
  431. c.Error(err)
  432. return
  433. }
  434. }
  435. if userInfo.UserType == "1" {
  436. // 查询总记录数
  437. if err := global.App.DB.Model(&model.Cmi_sim_order{}).
  438. Where("status = ?", "2").
  439. Where("id LIKE ?", "%"+param.Id+"%").
  440. Count(&total).Error; err != nil {
  441. c.Error(err)
  442. return
  443. }
  444. if err := global.App.DB.
  445. Where("id LIKE ?", "%"+param.Id+"%").
  446. Where("status = ?", "2").
  447. Limit(param.PageSize).
  448. Offset((param.Current - 1) * param.PageSize).
  449. Find(&dbList).Error; err != nil {
  450. c.Error(err)
  451. return
  452. }
  453. }
  454. type recordsData struct {
  455. model.Cmi_sim_order
  456. UserName string `json:"userName"`
  457. TrafficName string `json:"trafficName"`
  458. EndDate time.Time `json:"endDate"`
  459. }
  460. var recordsDataList []recordsData
  461. for _, v := range dbList {
  462. var traffic model.Sim_traffic
  463. if err := global.App.DB.Model(&model.Sim_traffic{}).Select("label", "end_date").Where("id = ?", v.TrafficId).Find(&traffic).Error; err != nil {
  464. c.Error(err)
  465. return
  466. }
  467. var user model.Sys_user
  468. if err := global.App.DB.Model(&model.Sys_user{}).Select("name").Where("id = ?", v.UserId).Find(&user).Error; err != nil {
  469. c.Error(err)
  470. return
  471. }
  472. recordsDataList = append(recordsDataList, recordsData{
  473. Cmi_sim_order: v,
  474. TrafficName: traffic.Label,
  475. UserName: user.Name,
  476. EndDate: traffic.EndDate,
  477. })
  478. }
  479. // 序列化
  480. records := make([]interface{}, len(recordsDataList))
  481. for i, user := range recordsDataList {
  482. records[i] = user
  483. }
  484. // 返回数据
  485. data := common.Pagination{
  486. Records: records,
  487. Current: param.Current,
  488. PageSize: param.PageSize,
  489. Total: total,
  490. }
  491. c.Set("res_data", data)
  492. }
  493. // 设置金额
  494. func SimSetAmount(c *gin.Context) {
  495. type Param struct {
  496. Amount float64 `json:"amount" default:"0"`
  497. ReturnAmount float64 `json:"returnAmount" default:"0"`
  498. OrderId string `json:"id" validate:"required"`
  499. }
  500. var param Param
  501. param.Amount = 0
  502. param.ReturnAmount = 0
  503. if err := c.ShouldBindJSON(&param); err != nil {
  504. c.Error(errors.New("缺少参数:" + err.Error()))
  505. return
  506. }
  507. err := utils.ValidateStruct(param)
  508. if err != nil {
  509. c.Error(err)
  510. return
  511. }
  512. userInfoInterface, _ := c.Get("userInfo")
  513. userInfo, _ := userInfoInterface.(model.Sys_user)
  514. if userInfo.UserType != "1" {
  515. c.Error(errors.New("权限不足"))
  516. return
  517. }
  518. if param.Amount != 0 {
  519. if err := global.App.DB.Model(&model.Cmi_sim_order{}).
  520. Where("id = ?", param.OrderId).
  521. Update("amount", param.Amount).
  522. Error; err != nil {
  523. c.Error(err)
  524. return
  525. }
  526. }
  527. if param.Amount != 0 {
  528. if err := global.App.DB.Model(&model.Cmi_sim_order{}).
  529. Where("id = ?", param.OrderId).
  530. Update("return_amount", param.ReturnAmount).
  531. Error; err != nil {
  532. c.Error(err)
  533. return
  534. }
  535. }
  536. c.Set("res_data", "操作成功!")
  537. }