美文网首页
golang版本的POS数据同步

golang版本的POS数据同步

作者: EasyNetCN | 来源:发表于2020-09-06 11:11 被阅读0次

    此篇随笔仅用于参考。

    本示例实现了用户POS中的客户端数据缓存同步。

    分页参数

    package model
    
    type PageParam struct {
        PageIndex int `json:"pageIndex"`
        PageSize  int `json:"pageSize"`
    }
    
    func (pageParam *PageParam) Start() int {
        return (pageParam.PageIndex - 1) * pageParam.PageSize
    }
    

    同步参数

    
    type SyncParam struct {
        PageParam
        EnterpriseId     int64  `json:"enterpriseId" url:"enterpriseId"`
        ParentId         int64  `json:"parentId" url:"parentId"`
        ReturnTotalCount int    `json:"returnTotalCount" url:"returnTotalCount"`
        ReturnResult     int    `json:"returnResult" url:"returnResult"`
        LastId           *int64 `json:"lastId" url:"lastId"`
        SyncTime         string `json:"syncTime" url:"syncTime"`
    }
    

    同步数据结果

    package model
    
    type SyncResult struct {
        SyncParam  SyncParam     `json:"syncParam"`
        Table      string        `json:"table"`
        TotalCount int64         `json:"totalCount"`
        Result     []interface{} `json:"result"`
    }
    
    func NewSyncResult(syncParam SyncParam) *SyncResult {
        return &SyncResult{SyncParam: syncParam, Result: make([]interface{}, 0)}
    }
    

    一个广告数据同步实现

    package service
    
    import (
        "strings"
    
        "ydyun360.cn/banner-service/model"
        "ydyun360.cn/banner-service/utility"
    
        "xorm.io/xorm"
    )
    
    var (
        tableMap = map[string]string{
            "banner_banner":           "banner",
            "banner_position":         "position",
            "banner_banner_positions": "banner_positions"}
    )
    
    type SyncService interface {
        Sync(table string, syncParam model.SyncParam) (model.SyncResult, error)
    }
    
    func NewSyncService(engine *xorm.Engine) SyncService {
        return &syncService{
            engine: engine,
        }
    }
    
    type syncService struct {
        engine *xorm.Engine
    }
    
    func (s *syncService) Sync(table string, syncParam model.SyncParam) (model.SyncResult, error) {
        syncResult := model.NewSyncResult(syncParam)
    
        if tb, ok := tableMap[table]; ok {
            syncResult.Table = table
    
            countSb := new(strings.Builder)
            querySb := new(strings.Builder)
    
            countSb.WriteString("SELECT COUNT(id) FROM ")
            countSb.WriteString(tb)
    
            querySb.WriteString("SELECT * FROM ")
            querySb.WriteString(tb)
    
            where, params := getWhere(syncParam)
    
            countSb.WriteString(" WHERE ")
            countSb.WriteString(where)
    
            querySb.WriteString(" WHERE ")
            querySb.WriteString(where)
    
            if syncParam.ReturnTotalCount == 1 {
                totalCount := int64(0)
    
                s.engine.SQL(countSb.String(), params...).Get(&totalCount)
    
                syncResult.TotalCount = totalCount
            }
    
            if syncParam.ReturnResult == 1 {
                querySb.WriteString(" ORDER BY id LIMIT ?,?")
    
                params = append(params, syncParam.Start())
                params = append(params, syncParam.PageSize)
    
                dbResult, err := utility.QueryForInterface(s.engine.DB().DB, querySb.String(), params...)
    
                if err != nil {
                    return *syncResult, err
                }
    
                syncResult.Result = make([]interface{}, len(dbResult))
    
                for i, r := range dbResult {
                    syncResult.Result[i] = r
                }
            }
    
        }
    
        return *syncResult, nil
    }
    
    func getWhere(syncParam model.SyncParam) (string, []interface{}) {
        sb := new(strings.Builder)
        params := make([]interface{}, 0)
    
        sb.WriteString("(enterprise_id=? OR enterprise_id=0")
    
        params = append(params, syncParam.EnterpriseId)
    
        if syncParam.ParentId > 0 {
            sb.WriteString(" OR enterprise_id=?")
    
            params = append(params, syncParam.ParentId)
        }
    
        sb.WriteString(")")
    
        if syncParam.LastId != nil {
            sb.WriteString(" AND id>?")
    
            params = append(params, *syncParam.LastId)
        }
    
        if syncParam.SyncTime != "" {
            sb.WriteString(" AND update_time>?")
    
            params = append(params, syncParam.SyncTime)
        }
    
        return sb.String(), params
    }
    

    相关文章

      网友评论

          本文标题:golang版本的POS数据同步

          本文链接:https://www.haomeiwen.com/subject/mnfcektx.html