123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- package platform
- import (
- "errors"
- "go-nc/configs/global"
- "go-nc/internal/utils"
- "go-nc/model"
- "go-nc/model/common"
- "strconv"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/jinzhu/copier"
- )
- // 获取资费列表: 分页查询
- func GetTariffList(c *gin.Context) {
- type Param struct {
- common.Pagination
- }
- var param Param
- param.Pagination = common.NewPagination()
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- //获取token
- userInfoInterface, _ := c.Get("userInfo")
- userInfo, _ := userInfoInterface.(model.Sys_user)
- var total int64
- var list []model.Sim_traffic
- if userInfo.UserType == "1" {
- // 查询总记录数
- if err := global.App.DB.Model(&model.Sim_traffic{}).Count(&total).Error; err != nil {
- c.Error(err)
- return
- }
- // 分页查询
- if err := global.App.DB.
- Limit(param.PageSize).
- Offset((param.Current - 1) * param.PageSize).
- Find(&list).Error; err != nil {
- c.Error(err)
- return
- }
- }
- if userInfo.UserType == "2" {
- // 查询总记录数
- if err := global.App.DB.Where("user_id = ?", userInfo.Id).Model(&model.Sim_traffic{}).Count(&total).Error; err != nil {
- c.Error(err)
- return
- }
- // 分页查询
- if err := global.App.DB.Where("user_id = ?", userInfo.Id).
- Limit(param.PageSize).
- Offset((param.Current - 1) * param.PageSize).
- Find(&list).Error; err != nil {
- c.Error(err)
- return
- }
- }
- type RecordsData struct {
- model.Sim_traffic
- UserName string `json:"userName"`
- CardSum int64 `json:"cardSum"`
- }
- var recordsData []RecordsData
- copier.Copy(&recordsData, &list)
- for i, v := range recordsData {
- // 客户名称
- var userItem model.Sys_user
- if err := global.App.DB.Select("name").Where("id = ?", v.UserId).First(&userItem).Error; err != nil {
- c.Error(err)
- return
- }
- recordsData[i].UserName = userItem.Name
- // 卡数量
- cardTotal := int64(0)
- global.App.DB.Where("traffic_id = ?", v.Id).Count(&cardTotal)
- recordsData[i].CardSum = cardTotal
- }
- // 序列化
- records := make([]interface{}, len(recordsData))
- for i, user := range recordsData {
- records[i] = user
- }
- // 返回数据
- data := common.BuildPagination(records, param.Current, param.PageSize, total)
- c.Set("res_data", data)
- }
- // 添加资费计划
- func AddTariff(c *gin.Context) {
- type Param struct {
- Label string `json:"label" validate:"required" ` // 资费名称
- SimDataPlanId string `json:"simDataPlanId" validate:"required" ` // 流量包ID
- UserId uint `json:"userId" validate:"required" ` // 用户ID
- Source string `json:"source" validate:"required" ` // 来源
- BillingCycle string `json:"billingCycle" validate:"required" ` // 计费周期
- BillingMethod string `json:"billingMethod" validate:"required" ` // 计费方式
- EndDate string `json:"endDate" validate:"required"` // 结算周期
- Pricing int64 `json:"pricing"` // 价格
- Currency string `json:"currency" validate:"required"` // 币种
- TrafficBilling string `json:"trafficBilling"` // 流量资费计费
- TrafficBillingType string `json:"trafficBillingType"` // 流量资费计费类型
- TrafficBillingAmount string `json:"trafficBillingAmount"` // 流量资费计费金额
- MRCAmount string `json:"mrcAmount"` // MRC金额
- NetworkAccessFee string `json:"networkAccessFee"` // 网络接入费
- }
- 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
- }
- // 添加数据
- var data model.Sim_traffic
- copier.Copy(&data, ¶m)
- if err := global.App.DB.Create(&data).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", data)
- }
- // 更新资费计划
- func UpdateTariff(c *gin.Context) {
- type Param struct {
- Id string `json:"id" validate:"required"`
- Label string `json:"label" validate:"required" ` // 资费名称
- SimDataPlanId string `json:"simDataPlanId" validate:"required" ` // 流量包ID
- UserId uint `json:"userId" validate:"required" ` // 用户ID
- Source string `json:"source" validate:"required" ` // 来源
- BillingCycle string `json:"billingCycle" validate:"required" ` // 计费周期
- BillingMethod string `json:"billingMethod" validate:"required" ` // 计费方式
- EndDate string `json:"endDate" validate:"required"` // 结算周期
- Pricing int64 `json:"pricing" ` // 价格
- Currency string `json:"currency" validate:"required"` // 币种
- TrafficBilling string `json:"trafficBilling"` // 流量资费计费
- TrafficBillingType string `json:"trafficBillingType"` // 流量资费计费类型
- TrafficBillingAmount string `json:"trafficBillingAmount"` // 流量资费计费金额
- MRCAmount string `json:"mrcAmount"` // MRC金额
- NetworkAccessFee string `json:"networkAccessFee"` // 网络接入费
- }
- 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
- }
- // 根据id更新数据
- var data model.Sim_traffic
- copier.Copy(&data, ¶m)
- if data.BillingMethod == "2" {
- data.TrafficBilling = ""
- data.TrafficBillingType = ""
- data.TrafficBillingAmount = ""
- }
- if err := global.App.DB.Updates(&data).Update("traffic_billing", data.TrafficBilling).Update("traffic_billing_type", data.TrafficBillingType).Update("traffic_billing_amount", data.TrafficBillingAmount).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", data)
- }
- // 删除资费计划
- func DeleteTariff(c *gin.Context) {
- id := c.Query("id")
- if id == "" {
- c.Error(errors.New("缺少参数:id"))
- return
- }
- if err := global.App.DB.Delete(&model.Sim_traffic{}, id).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", "删除成功!")
- }
- // 获取资费下的卡
- func GetTariffCard(c *gin.Context) {
- id := c.Query("id")
- if id == "" {
- c.Error(errors.New("缺少参数:id"))
- return
- }
- var data []model.Sim_card
- if err := global.App.DB.Where("tariff_id = ?", id).Find(&data).Error; err != nil {
- c.Error(err)
- return
- }
- type records struct {
- Iccid string `json:"iccid"`
- ServiceUsageMode string `json:"serviceUsageMode"`
- ExpireTime string `json:"expireTime"`
- Status string `json:"status"`
- }
- var recordsData []records
- copier.Copy(&recordsData, &data)
- c.Set("res_data", recordsData)
- }
- // id 换取资费信息
- func GetTariffById(c *gin.Context) {
- id := c.Query("id")
- if id == "" {
- c.Error(errors.New("缺少参数:id"))
- return
- }
- var data model.Sim_traffic
- if err := global.App.DB.Where("id = ?", id).First(&data).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", data)
- }
- // 资费商品列表
- func GetTariffProductList(c *gin.Context) {
- tariffId := c.Query("tariffId")
- if tariffId == "" {
- c.Error(errors.New("缺少参数:id"))
- return
- }
- var tariff model.Sim_traffic
- if err := global.App.DB.Where("id = ?", tariffId).First(&tariff).Error; err != nil {
- c.Error(err)
- return
- }
- var data []model.Sim_traffic_product
- if err := global.App.DB.Where("traffic_id = ?", tariffId).Find(&data).Error; err != nil {
- c.Error(err)
- return
- }
- type records struct {
- model.Sim_traffic_product
- TrafficBilling string `json:"trafficBilling"` // 流量资费计费
- TrafficBillingType string `json:"trafficBillingType"`
- }
- var recordsData []records
- copier.Copy(&recordsData, &data)
- for i, v := range recordsData {
- billing, _ := strconv.ParseFloat(tariff.TrafficBilling, 64)
- recordsData[i].TrafficBilling = strconv.FormatFloat(billing*float64(v.Period), 'f', 0, 64)
- recordsData[i].TrafficBillingType = tariff.TrafficBillingType
- }
- c.Set("res_data", recordsData)
- }
- // 添加资费商品
- func AddTariffProduct(c *gin.Context) {
- type Param struct {
- TrafficId string `json:"trafficId" validate:"required"` // 资费ID
- Price string `json:"price" validate:"required"` // 价格
- Currency string `json:"currency" validate:"required"` // 币种
- Period int `json:"period" validate:"required"` // 期限
- Label string `json:"label" 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
- }
- // 添加数据
- var data model.Sim_traffic_product
- copier.Copy(&data, ¶m)
- if err := global.App.DB.Create(&data).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", data)
- }
- // 修改资费商品
- func UpdateTariffProduct(c *gin.Context) {
- type Param struct {
- Id string `json:"id" validate:"required"`
- Price string `json:"price" validate:"required"` // 价格
- Currency string `json:"currency" validate:"required"` // 币种
- Period int `json:"period" validate:"required"` // 期限
- Label string `json:"label" 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
- }
- // 根据id更新数据
- var data model.Sim_traffic_product
- copier.Copy(&data, ¶m)
- if err := global.App.DB.Model(&data).
- Updates(data).FirstOrCreate(&data).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", data)
- }
- // 删除资费商品
- func DeleteTariffProduct(c *gin.Context) {
- id := c.Query("id")
- if id == "" {
- c.Error(errors.New("缺少参数:id"))
- return
- }
- if err := global.App.DB.Where("id = ?", id).Delete(&model.Sim_traffic_product{}).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", "删除成功!")
- }
- // 资费续有效期
- func RenewTariff(c *gin.Context) {
- type Param struct {
- Id string `json:"id" validate:"required"`
- EndDate string `json:"endDate" 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
- }
- // 格式化日期
- date, err := time.Parse("2006-01-02", param.EndDate)
- if err != nil {
- c.Error(err)
- return
- }
- endDate := time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, date.Location())
- // 资费信息
- var traffic model.Sim_traffic
- if err := global.App.DB.Where("id = ?", param.Id).First(&traffic).Error; err != nil {
- c.Error(err)
- return
- }
- // 生成续费记录
- record := model.Cmi_traffic_order{
- TrafficId: traffic.Id,
- UserId: traffic.UserId,
- Source: traffic.Source,
- BeforeEndDate: traffic.EndDate,
- EndDate: endDate,
- Quantity: 0,
- Amount: 0,
- }
- if err := global.App.DB.Model(&model.Cmi_traffic_order{}).Create(&record).Error; err != nil {
- c.Error(err)
- return
- }
- if err := global.App.DB.Model(model.Sim_traffic{}).Where("id = ?", param.Id).
- Update("end_date", param.EndDate).
- Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", "success")
- }
- // 自费续费订单信息
- func GetTrafficOrderList(c *gin.Context) {
- type Param struct {
- common.Pagination
- }
- var param Param
- param.Pagination = common.NewPagination()
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- //获取token
- userInfoInterface, _ := c.Get("userInfo")
- userInfo, _ := userInfoInterface.(model.Sys_user)
- var total int64
- var list []model.Cmi_traffic_order
- if userInfo.UserType == "1" {
- // 查询总记录数
- if err := global.App.DB.Model(&model.Cmi_traffic_order{}).Count(&total).Error; err != nil {
- c.Error(err)
- return
- }
- // 分页查询
- if err := global.App.DB.
- Limit(param.PageSize).
- Offset((param.Current - 1) * param.PageSize).
- Find(&list).Error; err != nil {
- c.Error(err)
- return
- }
- }
- if userInfo.UserType == "2" {
- // 查询总记录数
- if err := global.App.DB.Model(&model.Cmi_traffic_order{}).Where("user_id = ?", userInfo.Id).Count(&total).Error; err != nil {
- c.Error(err)
- return
- }
- // 分页查询
- if err := global.App.DB.
- Limit(param.PageSize).
- Offset((param.Current-1)*param.PageSize).
- Where("user_id = ?", userInfo.Id).
- Find(&list).Error; err != nil {
- c.Error(err)
- return
- }
- }
- // 资费信息
- type RecordData struct {
- model.Cmi_traffic_order
- TrafficName string `json:"trafficName"`
- UserName string `json:"userName"`
- }
- var recordsData []RecordData
- for _, v := range list {
- var traffic model.Sim_traffic
- if err := global.App.DB.Select("label").Where("id = ?", v.TrafficId).First(&traffic).Error; err != nil {
- c.Error(err)
- return
- }
- var user model.Sys_user
- if err := global.App.DB.Select("name").Where("id = ?", v.UserId).First(&user).Error; err != nil {
- c.Error(err)
- return
- }
- recordsData = append(recordsData, RecordData{
- Cmi_traffic_order: v,
- TrafficName: traffic.Label,
- UserName: user.Name,
- })
- }
- // 序列化
- records := make([]interface{}, len(recordsData))
- for i, user := range recordsData {
- records[i] = user
- }
- // 返回数据
- data := common.Pagination{
- Records: records,
- Current: param.Current,
- PageSize: param.PageSize,
- Total: total,
- }
- c.Set("res_data", data)
- }
|