liuxiaolong
2020-08-17 0494779a1de00553ee8a47ca712ffdcafaea9381
save push log and add spaceInfo,pushLog
6个文件已修改
185 ■■■■■ 已修改文件
controllers/car.go 96 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/db.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/log.go 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
routers/router.go 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/carService.go 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/msgPush.go 23 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/car.go
@@ -6,8 +6,10 @@
    "car-service/service"
    "fmt"
    "github.com/astaxie/beego"
    "github.com/satori/go.uuid"
    "net/http"
    "sort"
    "strings"
    "sync"
    "time"
)
@@ -41,9 +43,22 @@
                    if left <=lowerLimit && left != prePushLeft {
                        go func() {
                            message := fmt.Sprintf("%s 剩余车位:%d个", time.Now().Format("2006-01-02 15:04:05"), left)
                            b, e := service.PushByAlias("育英中学停车", message)
                            t := time.Now().Format("2006-01-02 15:04:05")
                            message := fmt.Sprintf("%s 剩余车位:%d个", t, left)
                            b, e, aliasArr := service.PushByAlias("育英中学停车", message)
                            //记录推送日志
                            logE := models.Log{
                                Id: uuid.NewV4().String(),
                                CreateTime: t,
                                Result: b,
                                Phones: strings.Join(aliasArr, ","),
                            }
                            if e != nil {
                                logE.Content = e.Error()
                            } else {
                                logE.Content = message
                            }
                            logE.Insert()
                            prePushLeft = left
                            fmt.Println("b:", b,"e:",e, "message:", message)
@@ -88,8 +103,20 @@
func nightPush(){
    message := fmt.Sprintf("%s 请尽快驶出停车场", time.Now().Format("2006-01-02 15:04:05"))
    b, e := service.NightPush("育英中学停车", message)
    b, e, aliasArr := service.NightPush("育英中学停车", message)
    //记录推送日志
    logE := models.Log{
        Id: uuid.NewV4().String(),
        CreateTime: time.Now().Format("2006-01-02 15:04:05"),
        Result: b,
        Phones: strings.Join(aliasArr, ","),
    }
    if e != nil {
        logE.Content = e.Error()
    } else {
        logE.Content = message
    }
    logE.Insert()
    fmt.Println("b:", b,"e:",e, "message:", message)
}
@@ -164,7 +191,20 @@
func (c *CarController) TestPush() {
    left := getSpaceLeft()
    message := fmt.Sprintf("%s 剩余车位:%d个", time.Now().Format("2006-01-02 15:04:05"), left)
    b, e := service.PushByAlias("育英中学停车", message)
    b, e, aliasArr := service.PushByAlias("育英中学停车", message)
    //记录推送日志
    logE := models.Log{
        Id: uuid.NewV4().String(),
        CreateTime: time.Now().Format("2006-01-02 15:04:05"),
        Result: b,
        Phones: strings.Join(aliasArr, ","),
    }
    if e != nil {
        logE.Content = e.Error()
    } else {
        logE.Content = message
    }
    logE.Insert()
    fmt.Println("b:", b,"e:",e, "message:", message)
    resp := code.Code{}
@@ -181,6 +221,50 @@
    c.ServeJSON()
}
// @router /spaceInfo [get]
func (c *CarController) SpaceInfo() {
    sv := service.NewCarService()
    spaceInfo := sv.FindHikSpaceInfo()
    c.Data["json"] = code.Code{
        Success: true,
        Status: http.StatusOK,
        Data: spaceInfo,
    }
    c.ServeJSON()
}
// @router /pushLog [get]
func (c *CarController) PushLog() {
    st := c.GetString("startTime")
    et := c.GetString("endTime")
    if st == "" {
        st = time.Now().Format("2006-01-02")
    }
    if et == "" {
        et = time.Now().AddDate(0,0,1).Format("2006-01-02")
    }
    curPage, err := c.GetInt("curPage")
    if err != nil {
        curPage = 1
    }
    pageSize, err := c.GetInt("pageSize")
    if err != nil {
        pageSize = 20
    }
    var l models.Log
    total, logs := l.Find(curPage, pageSize, st, et)
    resp := code.Code{
        Success: true,
        Status:  http.StatusOK,
        Data:    map[string]interface{}{
            "total": total,
            "list": logs,
        },
    }
    c.Data["json"] = resp
    c.ServeJSON()
}
// @router /crossRecord [get]
func (c *CarController) CrossRecord() {
    sv := service.NewCarService()
models/db.go
@@ -13,6 +13,6 @@
    dbPath := rootPath +"/"+ dbUrl
    orm.RegisterDriver("sqlite", orm.DRSqlite)
    _ = orm.RegisterDataBase("default", "sqlite3", dbPath)
    orm.RegisterModel(new(User), new(UserCar), new(SysUpgrade), new(UserClient), new(Restriction))
    orm.RegisterModel(new(User), new(UserCar), new(SysUpgrade), new(UserClient), new(Restriction), new(Log))
    _ = orm.RunSyncdb("default", false, true)
}
models/log.go
@@ -1 +1,36 @@
package models
import (
    "github.com/astaxie/beego/orm"
    "strconv"
)
//cid和别名绑定记录
type Log struct {
    Id               string         `orm:"pk;size(50);column(id)" json:"id"`
    CreateTime         string         `orm:"column(createTime)" json:"createTime"` //创建时间
    Result             bool         `orm:"column(result)" json:"result"` //推送结果
    Phones             string         `orm:"size(8000);column(phones)" json:"phones"` //推送目标手机号
    Content         string         `orm:"column(content)" json:"content"` //剩余车位
}
func (l *Log) TableName() string {
    return "log"
}
func (l *Log) Insert() (int64,error) {
    o := orm.NewOrm()
    return o.Insert(l)
}
func (l *Log) Find(curPage int, pageSize int, startTime string, endTime string) (int,[]Log) {
    var list []Log
    o := orm.NewOrm()
    var total int
    sql := "select count(*) from "+l.TableName()+" where createTime >='"+startTime+"' and createTime <= '"+endTime+"'"
    o.Raw(sql).QueryRow(&total)
    sn := (curPage-1)*pageSize
    sql += " order by createTime desc limit "+strconv.Itoa(sn)+","+strconv.Itoa(pageSize)+""
    o.Raw(sql).QueryRows(&list)
    return total, list
}
routers/router.go
@@ -60,4 +60,7 @@
    beego.Router(preApi+"/car/crossRecord", &controllers.CarController{}, "*:CrossRecord")
    beego.Router(preApi+"/car/testPush", &controllers.CarController{}, "*:TestPush")
    beego.Router(preApi+"/car/pushLog", &controllers.CarController{}, "*:PushLog")
    beego.Router(preApi+"/car/spaceInfo", &controllers.CarController{}, "*:SpaceInfo")
}
service/carService.go
@@ -142,6 +142,32 @@
    SpaceType                 string         `json:"spaceType"`
}
func (sv *CarService) FindHikSpaceInfo() []SpaceNo {
    url := "/artemis/api/pms/v1/parkingSpace/spaceNo"
    reqBody := map[string]interface{} {
        "pageNo": 1,
        "pageSize": 1000,
    }
    pageResult := sv.getHikPageResult(url, reqBody)
    if pageResult != nil {
        rb, err := json.Marshal(pageResult.List)
        if err != nil {
            fmt.Println("marshal pageResult.List err:", err)
            return nil
        }
        var spaceList []SpaceNo
        err = json.Unmarshal(rb, &spaceList)
        if err != nil {
            fmt.Println("unmarshal spaceNos err:", err)
            return nil
        } else {
            return spaceList
        }
    }
    return nil
}
func (sv *CarService) FindSpaceNo(userId string) models.PosResult {
    resultList := make(models.PosResult,0)
    var myPlateNosMap = make(map[string]string)
service/msgPush.go
@@ -246,7 +246,7 @@
}
//对已注册的用户进行消息推送。调用此接口前需调用创建消息接口设置消息内容
func PushByAlias(title string, msg string) (bool, error) {
func PushByAlias(title string, msg string) (bool, error, []string) {
    var aliasArr []string
    pushUserM := make(map[string]string)
@@ -258,7 +258,7 @@
        }
    }
    if len(pushUserM) == 0 {
        return true,errors.New("len(pushUserM) == 0")
        return false,errors.New("len(pushUserM) == 0"),aliasArr
    }
    carPersonM := make(map[string]string) //以车牌号为key,value是hik的personId
    csv := NewCarService()
@@ -290,13 +290,13 @@
    }
    lenAS := len(aliasArr)
    if  lenAS == 0 {
        return true, errors.New("aliasArr is empty")
        return false, errors.New("aliasArr is empty"),aliasArr
    }
    cResult, taskId, ce := createPushMsg(title, msg)
    fmt.Println("createPushMsg taskId:", taskId, "cResult:",cResult, "err:", ce)
    if !cResult {
        return false, errors.New("创建推送前置消息失败")
        return false, errors.New("创建推送前置消息失败"),aliasArr
    }
    //alias 单次推送长度上限是200
@@ -330,10 +330,10 @@
        }
    }
    if isSuccess {
        return true, nil
        return true, nil,aliasArr
    }
    return false, errors.New("推送失败")
    return false, errors.New("推送失败"),aliasArr
}
func doPush(taskId string, aliasArr []string) (bool,error) {
@@ -384,7 +384,7 @@
    return false, errors.New("推送失败")
}
func NightPush(title string, msg string) (bool, error) {
func NightPush(title string, msg string) (bool, error, []string) {
    var aliasArr []string
    pushUserM := make(map[string]string)
@@ -396,7 +396,7 @@
        }
    }
    if len(pushUserM) == 0 {
        return true,nil
        return true,nil, aliasArr
    }
    carPersonM := make(map[string]string)
    csv := NewCarService()
@@ -422,16 +422,17 @@
    if len(aliasArr) == 0 {
        fmt.Println("没有推送目标,aliasArr is empty")
        return true, nil
        return true, nil, aliasArr
    }
    cResult, taskId, ce := createPushMsg(title, msg)
    if !cResult {
        fmt.Println("createPushMsg taskId:", taskId, "err:", ce)
        return false, errors.New("创建推送前置消息失败")
        return false, errors.New("创建推送前置消息失败"), aliasArr
    }
    return doPush(taskId, aliasArr)
    b,e := doPush(taskId, aliasArr)
    return b,e, aliasArr
}
func PushAll(title string, msg string) (bool,error) {