package simApi import ( "errors" "go-nc/configs/global" "go-nc/internal/utils" "go-nc/model" "go-nc/model/common" "go-nc/pkg/sim" "go-nc/pkg/sim/grace" "path/filepath" "strconv" "time" "github.com/360EntSecGroup-Skylar/excelize" "github.com/gin-gonic/gin" "github.com/jinzhu/copier" ) // 购卡申请 func SimApply(c *gin.Context) { type Param struct { UserId uint `json:"user_id"` Source string `json:"source" validate:"required"` // 来源 TrafficId string `json:"trafficId" validate:"required"` // 资费Id PeriodOfSilence string `json:"periodOfSilence" validate:"required"` // 静默期 IsTrafficPool string `json:"isTrafficPool" validate:"required"` // 是否是流量池 Quantity int `json:"quantity" validate:"required"` // 采购数量 SimType string `json:"simType" validate:"required"` // 卡类型 TmsStatus string `json:"tmsStatus"` // 物流状态:1:未发货 2:已发货 3:已收货 ModerationStatus string `json:"moderationStatus"` // 订单审核状态:1: 待审核 2: 审核通过 3: 已驳回 // ContractImg string `json:"contractImg"` // 合同图片 // ModerationNotes string `json:"moderationNotes"` // 审核备注 Status string `json:"status"` PoolId string `json:"poolId"` } var param Param if err := c.ShouldBindJSON(¶m); err != nil { c.Error(errors.New("缺少参数:" + err.Error())) return } // 获取用户信息 userInfoInterface, _ := c.Get("userInfo") userInfo, _ := userInfoInterface.(model.Sys_user) if userInfo.UserType != "2" { c.Error(errors.New("无操作权限!")) return } param.UserId = userInfo.Id param.TmsStatus = "1" param.ModerationStatus = "1" param.Status = "1" err := utils.ValidateStruct(param) if err != nil { c.Error(err) return } var db model.Cmi_sim_order copier.Copy(&db, ¶m) if err := global.App.DB.Model(&model.Cmi_sim_order{}).Create(&db).Error; err != nil { c.Error(err) return } c.Set("res_data", db) } // 购卡申请信息列表 func SimApplyList(c *gin.Context) { type Param struct { Id string `json:"id"` UserName string `json:"userName"` common.Pagination } var param Param param.Pagination = common.NewPagination() if err := c.ShouldBindJSON(¶m); err != nil { c.Error(errors.New("缺少参数:" + err.Error())) return } // 获取用户信息 userInfoInterface, _ := c.Get("userInfo") userInfo, _ := userInfoInterface.(model.Sys_user) var total int64 var dbList []model.Cmi_sim_order // 用户类型为2查询用户下的订单,1查询全部订单 if userInfo.UserType == "2" { // 查询总记录数 if err := global.App.DB.Model(&model.Cmi_sim_order{}). Where("user_id = ?", userInfo.Id). Where("id LIKE ?", "%"+param.Id+"%"). Where("status = 1"). Count(&total).Error; err != nil { c.Error(err) return } if err := global.App.DB. Where("user_id = ?", userInfo.Id). Where("id LIKE ?", "%"+param.Id+"%"). Where("status = 1"). Limit(param.PageSize). Offset((param.Current - 1) * param.PageSize). Find(&dbList).Error; err != nil { c.Error(err) return } } if userInfo.UserType == "1" { // 查询总记录数 if err := global.App.DB.Model(&model.Cmi_sim_order{}). Where("id LIKE ?", "%"+param.Id+"%"). Where("status = 1"). Count(&total).Error; err != nil { c.Error(err) return } if err := global.App.DB. Where("id LIKE ?", "%"+param.Id+"%"). Where("status = 1"). Limit(param.PageSize). Offset((param.Current - 1) * param.PageSize). Find(&dbList).Error; err != nil { c.Error(err) return } } type recordsData struct { model.Cmi_sim_order UserName string `json:"userName"` TrafficName string `json:"trafficName"` PoolName string `json:"poolName"` EndDate time.Time `json:"endDate"` } var recordsDataList []recordsData for _, v := range dbList { var traffic model.Sim_traffic if err := global.App.DB.Model(&model.Sim_traffic{}).Select("label", "end_date").Where("id = ?", v.TrafficId).Find(&traffic).Error; err != nil { c.Error(err) return } var user model.Sys_user if err := global.App.DB.Model(&model.Sys_user{}).Select("name").Where("id = ?", v.UserId).Find(&user).Error; err != nil { c.Error(err) return } var pool model.Sim_pool if v.PoolId != "" { if err := global.App.DB.Model(&model.Sim_pool{}).Select("label").Where("id = ?", v.PoolId).First(&pool).Error; err != nil { c.Error(err) return } } recordsDataList = append(recordsDataList, recordsData{ Cmi_sim_order: v, TrafficName: traffic.Label, UserName: user.Name, PoolName: pool.Label, EndDate: traffic.EndDate, }) } // 序列化 records := make([]interface{}, len(recordsDataList)) for i, user := range recordsDataList { records[i] = user } // 返回数据 data := common.Pagination{ Records: records, Current: param.Current, PageSize: param.PageSize, Total: total, } c.Set("res_data", data) } // 购卡审核 func SimApplyModeration(c *gin.Context) { type Param struct { Id string `json:"id" validate:"required"` ModerationStatus string `json:"moderationStatus" validate:"required"` ModerationNotes string `json:"moderationNotes" validate:"required"` } var param Param if err := c.ShouldBindJSON(¶m); err != nil { c.Error(errors.New("缺少参数:" + err.Error())) return } err := utils.ValidateStruct(param) if err != nil { c.Error(err) return } // 获取用户信息 userInfoInterface, _ := c.Get("userInfo") userInfo, _ := userInfoInterface.(model.Sys_user) if userInfo.UserType != "1" { c.Error(errors.New("无操作权限!")) return } // 查询订单状态 var order model.Cmi_sim_order if err := global.App.DB.Where("id = ?", param.Id).First(&order).Error; err != nil { c.Error(err) return } if order.ModerationStatus != "1" && order.Status != "1" { c.Error(errors.New("该订单已审核,不可操作!")) return } // 修改订单状态 if err := global.App.DB.Model(&model.Cmi_sim_order{}). Where("id = ?", param.Id). Update("moderation_status", param.ModerationStatus). Update("moderation_notes", param.ModerationNotes). Error; err != nil { c.Error(err) return } // 是否是退卡订单&& 审核通过 -- 进行停卡操作 if order.Status == "2" && param.ModerationStatus == "2" { // 查询退卡iccid var iccids []string 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 { c.Error(err) return } // 停卡 for _, v := range iccids { sim.StopSim("grace", v) } } c.Set("res_data", "操作成功!") } // 上传合同 func SimUploadContract(c *gin.Context) { type Param struct { Id string `json:"id" validate:"required"` ContractImg string `json:"contractImg" validate:"required"` } var param Param if err := c.ShouldBindJSON(¶m); err != nil { c.Error(errors.New("缺少参数:" + err.Error())) return } err := utils.ValidateStruct(param) if err != nil { c.Error(err) return } // 修改订单状态 if err := global.App.DB.Model(&model.Cmi_sim_order{}). Where("id = ? AND status = '1'", param.Id). Update("contract_img", param.ContractImg). Error; err != nil { c.Error(err) return } c.Set("res_data", "上传成功!") } // 购卡:分配卡号 func SimAssignSim(c *gin.Context) { file, err := c.FormFile("file") if err != nil { c.Error(errors.New("文件上传失败!")) return } formValues := c.Request.Form orderId := formValues.Get("orderId") if orderId == "" { c.Error(errors.New("缺少参数:orderId")) return } // 保存上传的文件到项目根目录 filepath := filepath.Join(utils.GetProjectRoot(), "public") if err := c.SaveUploadedFile(file, filepath+"/"+file.Filename); err != nil { c.Error(errors.New("文件保存失败!")) return } // 打开Excel文件 f, err := excelize.OpenFile(filepath + "/" + file.Filename) if err != nil { c.Error(errors.New("打开Excel文件失败")) return } rows := f.GetRows("Sheet1") rows = rows[1:] // 查询订单信息 var order model.Cmi_sim_order 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 { c.Error(err) return } // 通过资费获取流量包ID var traffic model.Sim_traffic if err := global.App.DB.Select("sim_data_plan_id").Where("id = ?", order.TrafficId).First(&traffic).Error; err != nil { c.Error(err) return } opts := sim.GetCardInfoOptions{ TariffId: order.TrafficId, PoolId: order.PoolId, UserId: order.UserId, } iccids := make([]string, len(rows)) for i, row := range rows { iccids[i] = row[0] } // 入库 for _, iccid := range rows { sim.GetCardInfo(order.Source, iccid[0], &opts) cardItem := model.Cmi_sim_order_card{ OrderId: order.Id, Iccid: iccid[0], Status: "1", } // 查询 orderCardSum := int64(0) global.App.DB.Model(&model.Cmi_sim_order_card{}).Where("order_id = ? AND iccid in (?)", order.Id, iccids).Count(&orderCardSum) if orderCardSum >= int64(order.Quantity) { c.Error(errors.New("已分配到采购最大数量!")) return } // Cmi_sim_order_card 查询卡没有创建,有则不管 global.App.DB.Model(&model.Cmi_sim_order_card{}).Where("order_id = ? AND iccid = ?", order.Id, iccid[0]).FirstOrCreate(&cardItem) // 绑定套餐 productId, _ := strconv.Atoi(traffic.SimDataPlanId) sim.RechargeCard(order.Source, grace.RechargeSimPackage{ Iccid: iccid[0], Type: "1", ProductId: productId, Num: traffic.SimDataPlanId, }) } // 订单状态改为已发货 global.App.DB.Model(&model.Cmi_sim_order{}). Where("id = ?", orderId). Update("tms_status", "2") c.Set("res_data", rows) } // 查看购卡订单的卡 func SimOrderCard(c *gin.Context) { orderId := c.Query("id") if orderId == "" { c.Error(errors.New("缺少参数:id")) return } var data []model.Cmi_sim_order_card if err := global.App.DB.Where("order_id = ? OR return_order_id = ?", orderId, orderId).Find(&data).Error; err != nil { c.Error(err) return } c.Set("res_data", data) } // 退卡 func SimReturnCard(c *gin.Context) { type Param struct { OrderId string `json:"id" validate:"required"` ICCID []string `json:"iccids" validate:"required"` } var param Param if err := c.ShouldBindJSON(¶m); err != nil { c.Error(errors.New("缺少参数:" + err.Error())) return } err := utils.ValidateStruct(param) if err != nil { c.Error(err) return } tx := global.App.DB.Begin() // 创建退卡订单 var order model.Cmi_sim_order if err := tx.Model(&model.Cmi_sim_order{}).Where("id = ?", param.OrderId).First(&order).Error; err != nil { tx.Rollback() c.Error(err) return } // 创建退卡订单 var returnOrder model.Cmi_sim_order copier.Copy(&returnOrder, &order) returnOrder.Status = "2" returnOrder.ModerationStatus = "1" if err := tx.Model(&model.Cmi_sim_order{}).Create(&returnOrder).Error; err != nil { tx.Rollback() c.Error(err) return } // 创建卡订单id if err := tx.Model(&model.Cmi_sim_order_card{}). Where("iccid in (?)", param.ICCID). Where("order_id = ?", order.Id). Update("status", "2"). Update("return_order_id", returnOrder.Id).Error; err != nil { tx.Rollback() c.Error(err) return } tx.Commit() c.Set("res_data", "操作成功!") } // 退卡申请信息列表 func SimReturnCardList(c *gin.Context) { type Param struct { Id string `json:"id"` UserName string `json:"userName"` common.Pagination } var param Param param.Pagination = common.NewPagination() if err := c.ShouldBindJSON(¶m); err != nil { c.Error(errors.New("缺少参数:" + err.Error())) return } // 获取用户信息 userInfoInterface, _ := c.Get("userInfo") userInfo, _ := userInfoInterface.(model.Sys_user) var total int64 var dbList []model.Cmi_sim_order // 用户类型为2查询用户下的订单,1查询全部订单 if userInfo.UserType == "2" { // 查询总记录数 if err := global.App.DB.Model(&model.Cmi_sim_order{}). Where("status = ?", "2"). Where("user_id = ?", userInfo.Id). Where("id LIKE ?", "%"+param.Id+"%"). Count(&total).Error; err != nil { c.Error(err) return } if err := global.App.DB. Where("status = ?", "2"). Where("user_id = ?", userInfo.Id). Where("id LIKE ?", "%"+param.Id+"%"). Limit(param.PageSize). Offset((param.Current - 1) * param.PageSize). Find(&dbList).Error; err != nil { c.Error(err) return } } if userInfo.UserType == "1" { // 查询总记录数 if err := global.App.DB.Model(&model.Cmi_sim_order{}). Where("status = ?", "2"). Where("id LIKE ?", "%"+param.Id+"%"). Count(&total).Error; err != nil { c.Error(err) return } if err := global.App.DB. Where("id LIKE ?", "%"+param.Id+"%"). Where("status = ?", "2"). Limit(param.PageSize). Offset((param.Current - 1) * param.PageSize). Find(&dbList).Error; err != nil { c.Error(err) return } } type recordsData struct { model.Cmi_sim_order UserName string `json:"userName"` TrafficName string `json:"trafficName"` EndDate time.Time `json:"endDate"` } var recordsDataList []recordsData for _, v := range dbList { var traffic model.Sim_traffic if err := global.App.DB.Model(&model.Sim_traffic{}).Select("label", "end_date").Where("id = ?", v.TrafficId).Find(&traffic).Error; err != nil { c.Error(err) return } var user model.Sys_user if err := global.App.DB.Model(&model.Sys_user{}).Select("name").Where("id = ?", v.UserId).Find(&user).Error; err != nil { c.Error(err) return } recordsDataList = append(recordsDataList, recordsData{ Cmi_sim_order: v, TrafficName: traffic.Label, UserName: user.Name, EndDate: traffic.EndDate, }) } // 序列化 records := make([]interface{}, len(recordsDataList)) for i, user := range recordsDataList { records[i] = user } // 返回数据 data := common.Pagination{ Records: records, Current: param.Current, PageSize: param.PageSize, Total: total, } c.Set("res_data", data) } // 设置金额 func SimSetAmount(c *gin.Context) { type Param struct { Amount float64 `json:"amount" default:"0"` ReturnAmount float64 `json:"returnAmount" default:"0"` OrderId string `json:"id" validate:"required"` } var param Param param.Amount = 0 param.ReturnAmount = 0 if err := c.ShouldBindJSON(¶m); err != nil { c.Error(errors.New("缺少参数:" + err.Error())) return } err := utils.ValidateStruct(param) if err != nil { c.Error(err) return } userInfoInterface, _ := c.Get("userInfo") userInfo, _ := userInfoInterface.(model.Sys_user) if userInfo.UserType != "1" { c.Error(errors.New("权限不足")) return } if param.Amount != 0 { if err := global.App.DB.Model(&model.Cmi_sim_order{}). Where("id = ?", param.OrderId). Update("amount", param.Amount). Error; err != nil { c.Error(err) return } } if param.Amount != 0 { if err := global.App.DB.Model(&model.Cmi_sim_order{}). Where("id = ?", param.OrderId). Update("return_amount", param.ReturnAmount). Error; err != nil { c.Error(err) return } } c.Set("res_data", "操作成功!") }