golang版本的POS数据同步

EasyNetCN · · 338 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

此篇随笔仅用于参考。

本示例实现了用户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
}

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:EasyNetCN

查看原文:golang版本的POS数据同步

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

338 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传