正在显示
8 个修改的文件
包含
118 行增加
和
77 行删除
| @@ -24,6 +24,12 @@ func (crontabService *CrontabService) initTask() { | @@ -24,6 +24,12 @@ func (crontabService *CrontabService) initTask() { | ||
| 24 | return srv.PullMaterialNewest() | 24 | return srv.PullMaterialNewest() |
| 25 | }) | 25 | }) |
| 26 | task.AddTask("pullMaterialK3cloud", pullMaterialK3cloud) | 26 | task.AddTask("pullMaterialK3cloud", pullMaterialK3cloud) |
| 27 | + //PullDataK3Cloud 晚上0时10分执行 | ||
| 28 | + pullMaterialGroupK3cloud := task.NewTask("pullMaterialGroupK3cloud", "0 10 0 * * *", func(ctx context.Context) error { | ||
| 29 | + srv := syncdata.PullDataK3CloudService{} | ||
| 30 | + return srv.PullMaterialGroup() | ||
| 31 | + }) | ||
| 32 | + task.AddTask("pullMaterialGroupK3cloud", pullMaterialGroupK3cloud) | ||
| 27 | } | 33 | } |
| 28 | 34 | ||
| 29 | func (crontabService *CrontabService) StartCrontabTask() { | 35 | func (crontabService *CrontabService) StartCrontabTask() { |
| @@ -34,4 +40,5 @@ func (crontabService *CrontabService) StartCrontabTask() { | @@ -34,4 +40,5 @@ func (crontabService *CrontabService) StartCrontabTask() { | ||
| 34 | 40 | ||
| 35 | func (crontabService *CrontabService) StopCrontabTask() { | 41 | func (crontabService *CrontabService) StopCrontabTask() { |
| 36 | task.StopTask() | 42 | task.StopTask() |
| 43 | + log.Logger.Info("crontab stop!") | ||
| 37 | } | 44 | } |
| @@ -191,3 +191,88 @@ func (srv *PullDataK3CloudService) PullMaterial(timeFilter time.Time) error { | @@ -191,3 +191,88 @@ func (srv *PullDataK3CloudService) PullMaterial(timeFilter time.Time) error { | ||
| 191 | } | 191 | } |
| 192 | return nil | 192 | return nil |
| 193 | } | 193 | } |
| 194 | + | ||
| 195 | +//PullMaterialGroup 拉取物料分组 | ||
| 196 | +func (srv *PullDataK3CloudService) PullMaterialGroup() error { | ||
| 197 | + client, err := newK3cloudClient() | ||
| 198 | + if err != nil { | ||
| 199 | + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 200 | + } | ||
| 201 | + fieldKeys := []string{ | ||
| 202 | + "FID", "FNumber", "FName", "FParentId", | ||
| 203 | + } | ||
| 204 | + var ( | ||
| 205 | + startRow int | ||
| 206 | + allResult []map[string]string | ||
| 207 | + queryErr error | ||
| 208 | + ) | ||
| 209 | + for { | ||
| 210 | + result, err := client.ExecuteBillQuery(k3cloud.RequestExecuteBillQuery{ | ||
| 211 | + FormId: "SAL_MATERIALGROUP", | ||
| 212 | + Data: k3cloud.ExecuteBillQueryData{ | ||
| 213 | + FormId: "SAL_MATERIALGROUP", | ||
| 214 | + FieldKeys: strings.Join(fieldKeys, ","), //查询的字段 | ||
| 215 | + StartRow: startRow, | ||
| 216 | + Limit: 1000, | ||
| 217 | + }, | ||
| 218 | + }) | ||
| 219 | + if err != nil { | ||
| 220 | + queryErr = err | ||
| 221 | + break | ||
| 222 | + } | ||
| 223 | + mp := result.ToMapString() | ||
| 224 | + if len(mp) == 0 { | ||
| 225 | + break | ||
| 226 | + } | ||
| 227 | + allResult = append(allResult, mp...) | ||
| 228 | + startRow += 1000 | ||
| 229 | + } | ||
| 230 | + if queryErr != nil { | ||
| 231 | + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + var ( | ||
| 235 | + materialGroups []models.MaterialGroupK3cloud | ||
| 236 | + materialGroupTemp models.MaterialGroupK3cloud | ||
| 237 | + ) | ||
| 238 | + nowTime := time.Now() | ||
| 239 | + for _, item := range allResult { | ||
| 240 | + id, _ := strconv.Atoi(item["FID"]) | ||
| 241 | + fParentId, _ := strconv.Atoi(item["FParentId"]) | ||
| 242 | + materialGroupTemp = models.MaterialGroupK3cloud{ | ||
| 243 | + Id: id, | ||
| 244 | + ParentId: fParentId, | ||
| 245 | + Number: item["FNumber"], | ||
| 246 | + Name: item["FName"], | ||
| 247 | + DataVersion: nowTime.Unix(), | ||
| 248 | + } | ||
| 249 | + materialGroups = append(materialGroups, materialGroupTemp) | ||
| 250 | + } | ||
| 251 | + | ||
| 252 | + var ( | ||
| 253 | + materialGroupDao *dao.MaterialGroupK3cloudDao | ||
| 254 | + ) | ||
| 255 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 256 | + if err != nil { | ||
| 257 | + return application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 258 | + } | ||
| 259 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 260 | + return application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 261 | + } | ||
| 262 | + defer func() { | ||
| 263 | + transactionContext.RollbackTransaction() | ||
| 264 | + }() | ||
| 265 | + materialGroupDao, err = dao.NewMaterialGroupK3cloudDao(transactionContext.(*pgTransaction.TransactionContext)) | ||
| 266 | + if err != nil { | ||
| 267 | + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 268 | + } | ||
| 269 | + //记录到MaterialGroupK3cloud表 | ||
| 270 | + err = materialGroupDao.SyncDataMaterialGroupK3cloud(materialGroups) | ||
| 271 | + if err != nil { | ||
| 272 | + return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 273 | + } | ||
| 274 | + if err = transactionContext.CommitTransaction(); err != nil { | ||
| 275 | + return application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 276 | + } | ||
| 277 | + return nil | ||
| 278 | +} |
| @@ -23,93 +23,35 @@ func NewMaterialGroupK3cloudDao(transactionContext *pgTransaction.TransactionCon | @@ -23,93 +23,35 @@ func NewMaterialGroupK3cloudDao(transactionContext *pgTransaction.TransactionCon | ||
| 23 | } | 23 | } |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | -func (d *MaterialGroupK3cloudDao) GetLastVersion() (int64, error) { | ||
| 27 | - var materialData []models.MaterialK3cloud | ||
| 28 | - err := d.transactionContext.PgTx.Model(&materialData). | ||
| 29 | - Order("data_version DESC"). | ||
| 30 | - Limit(1). | ||
| 31 | - Select() | ||
| 32 | - if err != nil { | ||
| 33 | - return 0, err | ||
| 34 | - } | ||
| 35 | - if len(materialData) == 0 { | ||
| 36 | - return 0, nil | ||
| 37 | - } | ||
| 38 | - return materialData[0].DataVersion, nil | ||
| 39 | -} | ||
| 40 | - | ||
| 41 | -//SyncDataMaterialK3cloud 同步MaterialK3cloud表数据 | ||
| 42 | -func (d *MaterialGroupK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3cloud) error { | 26 | +//SyncDataMaterialGroupK3cloud 同步MaterialGroupK3cloud表 物料分组数据 |
| 27 | +func (d *MaterialGroupK3cloudDao) SyncDataMaterialGroupK3cloud(data []models.MaterialGroupK3cloud) error { | ||
| 43 | sqlValues := []string{} | 28 | sqlValues := []string{} |
| 44 | var strTemp []string | 29 | var strTemp []string |
| 45 | for i := range data { | 30 | for i := range data { |
| 46 | - strTemp = make([]string, 0, 18) | ||
| 47 | - strTemp = append(strTemp, strconv.Itoa(data[i].MaterialId)) | ||
| 48 | - strTemp = append(strTemp, `'`+data[i].Name+`'`) | 31 | + strTemp = make([]string, 0, 5) |
| 32 | + strTemp = append(strTemp, strconv.Itoa(data[i].Id)) | ||
| 33 | + strTemp = append(strTemp, strconv.Itoa(data[i].ParentId)) | ||
| 49 | strTemp = append(strTemp, `'`+data[i].Number+`'`) | 34 | strTemp = append(strTemp, `'`+data[i].Number+`'`) |
| 50 | - strTemp = append(strTemp, `'`+data[i].Specification+`'`) | ||
| 51 | - strTemp = append(strTemp, `'`+data[i].ForbidStatus+`'`) | ||
| 52 | - strTemp = append(strTemp, strconv.Itoa(data[i].ErpClsId)) | ||
| 53 | - strTemp = append(strTemp, strconv.Itoa(data[i].BaseUnitId)) | ||
| 54 | - strTemp = append(strTemp, `'`+data[i].BaseUnitName+`'`) | ||
| 55 | - if data[i].CreateDate.IsZero() { | ||
| 56 | - strTemp = append(strTemp, `NULL`) | ||
| 57 | - } else { | ||
| 58 | - strTemp = append(strTemp, `'`+data[i].CreateDate.String()+`'`) | ||
| 59 | - } | ||
| 60 | - if data[i].ModifyDate.IsZero() { | ||
| 61 | - strTemp = append(strTemp, `NULL`) | ||
| 62 | - } else { | ||
| 63 | - strTemp = append(strTemp, `'`+data[i].ModifyDate.String()+`'`) | ||
| 64 | - } | ||
| 65 | - if data[i].ForbidDate.IsZero() { | ||
| 66 | - strTemp = append(strTemp, `NULL`) | ||
| 67 | - } else { | ||
| 68 | - strTemp = append(strTemp, `'`+data[i].ForbidDate.String()+`'`) | ||
| 69 | - } | ||
| 70 | - if data[i].ApproveDate.IsZero() { | ||
| 71 | - strTemp = append(strTemp, `NULL`) | ||
| 72 | - } else { | ||
| 73 | - strTemp = append(strTemp, `'`+data[i].ApproveDate.String()+`'`) | ||
| 74 | - } | ||
| 75 | - strTemp = append(strTemp, strconv.Itoa(data[i].MaterialGroup)) | ||
| 76 | - strTemp = append(strTemp, `'`+data[i].MaterialGroupNumber+`'`) | ||
| 77 | - strTemp = append(strTemp, `'`+data[i].MaterialGroupName+`'`) | ||
| 78 | - strTemp = append(strTemp, strconv.Itoa(data[i].RefStatus)) | ||
| 79 | - //关联的产品表id ,使用 product 产品表的自增序列表 | ||
| 80 | - strTemp = append(strTemp, "nextval('manufacture.product_product_id_seq'::regclass)") | 35 | + strTemp = append(strTemp, `'`+data[i].Name+`'`) |
| 81 | strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion))) | 36 | strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion))) |
| 82 | - strTemp = append(strTemp, strconv.Itoa(data[i].UseOrgId)) | ||
| 83 | sqlValues = append(sqlValues, "("+strings.Join(strTemp, ",")+")") | 37 | sqlValues = append(sqlValues, "("+strings.Join(strTemp, ",")+")") |
| 84 | - | ||
| 85 | } | 38 | } |
| 86 | var valueTemp []string | 39 | var valueTemp []string |
| 87 | for i := 0; i < len(sqlValues); i += 100 { | 40 | for i := 0; i < len(sqlValues); i += 100 { |
| 88 | - if i < len(sqlValues)-100 { | ||
| 89 | - valueTemp = sqlValues[i:100] | 41 | + if i <= len(sqlValues)-100 { |
| 42 | + valueTemp = sqlValues[i : i+100] | ||
| 90 | } else { | 43 | } else { |
| 91 | valueTemp = sqlValues[i:] | 44 | valueTemp = sqlValues[i:] |
| 92 | } | 45 | } |
| 93 | - sql := `INSERT INTO "manufacture"."material_k3cloud" ( | ||
| 94 | - "material_id","name","number","specification","forbid_status", | ||
| 95 | - "erp_cls_id","base_unit_id","base_unit_name","create_date", | ||
| 96 | - "modify_date","forbid_date","approve_date","material_group", | ||
| 97 | - "material_group_number","material_group_name","ref_status", | ||
| 98 | - "join_product_id","data_version","use_org_id" ) | ||
| 99 | - VALUES ` + strings.Join(valueTemp, ",") + | ||
| 100 | - ` ON conflict ( material_id ) DO | 46 | + sql := `INSERT INTO "manufacture"."material_group_k3cloud" ( |
| 47 | + "id","parent_id","number","name","data_version") | ||
| 48 | + VALUES ` + strings.Join(valueTemp, ",") + ` | ||
| 49 | + ON conflict ( "id" ) DO | ||
| 101 | UPDATE | 50 | UPDATE |
| 102 | SET ( | 51 | SET ( |
| 103 | - "name","number","specification","forbid_status","erp_cls_id", | ||
| 104 | - "base_unit_id","base_unit_name","create_date","modify_date", | ||
| 105 | - "forbid_date","approve_date","material_group","material_group_number", | ||
| 106 | - "material_group_name","ref_status","data_version" ) = ( | ||
| 107 | - EXCLUDED."name",EXCLUDED."number",EXCLUDED."specification", | ||
| 108 | - EXCLUDED."forbid_status",EXCLUDED."erp_cls_id",EXCLUDED."base_unit_id", | ||
| 109 | - EXCLUDED."base_unit_name",EXCLUDED."create_date",EXCLUDED."modify_date", | ||
| 110 | - EXCLUDED."forbid_date",EXCLUDED."approve_date",EXCLUDED."material_group", | ||
| 111 | - EXCLUDED."material_group_number",EXCLUDED."material_group_name", | ||
| 112 | - EXCLUDED."ref_status",EXCLUDED."data_version" )` | 52 | + "parent_id","number","name","data_version") = ( |
| 53 | + EXCLUDED."parent_id",EXCLUDED."number", | ||
| 54 | + EXCLUDED."name",EXCLUDED."data_version" )` | ||
| 113 | _, err := d.transactionContext.PgTx.Exec(sql) | 55 | _, err := d.transactionContext.PgTx.Exec(sql) |
| 114 | if err != nil { | 56 | if err != nil { |
| 115 | return err | 57 | return err |
| @@ -94,12 +94,11 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo | @@ -94,12 +94,11 @@ func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3clo | ||
| 94 | 94 | ||
| 95 | } | 95 | } |
| 96 | var valueTemp []string | 96 | var valueTemp []string |
| 97 | - fmt.Println("总数", len(data)) | ||
| 98 | for i := 0; i < len(sqlValues); i += 100 { | 97 | for i := 0; i < len(sqlValues); i += 100 { |
| 99 | if i <= len(sqlValues)-100 { | 98 | if i <= len(sqlValues)-100 { |
| 100 | valueTemp = sqlValues[i : i+100] | 99 | valueTemp = sqlValues[i : i+100] |
| 101 | } else { | 100 | } else { |
| 102 | - valueTemp = sqlValues[i-100:] | 101 | + valueTemp = sqlValues[i:] |
| 103 | } | 102 | } |
| 104 | sql := `INSERT INTO "manufacture"."material_k3cloud" ( | 103 | sql := `INSERT INTO "manufacture"."material_k3cloud" ( |
| 105 | "material_id","name","number","specification","forbid_status", | 104 | "material_id","name","number","specification","forbid_status", |
| @@ -37,6 +37,7 @@ func init() { | @@ -37,6 +37,7 @@ func init() { | ||
| 37 | (*models.UnitConversion)(nil), | 37 | (*models.UnitConversion)(nil), |
| 38 | (*models.Workshop)(nil), | 38 | (*models.Workshop)(nil), |
| 39 | (*models.MaterialK3cloud)(nil), | 39 | (*models.MaterialK3cloud)(nil), |
| 40 | + (*models.MaterialGroupK3cloud)(nil), | ||
| 40 | } { | 41 | } { |
| 41 | err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ | 42 | err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ |
| 42 | Temp: false, | 43 | Temp: false, |
| @@ -3,9 +3,9 @@ package models | @@ -3,9 +3,9 @@ package models | ||
| 3 | //MaterialGroupK3cloud 采集自金蝶k3cloud物料分组数据 | 3 | //MaterialGroupK3cloud 采集自金蝶k3cloud物料分组数据 |
| 4 | type MaterialGroupK3cloud struct { | 4 | type MaterialGroupK3cloud struct { |
| 5 | tableName string `comment:"采集自金蝶k3cloud物料数据" pg:"manufacture.material_group_k3cloud,alias:material_group_k3cloud"` | 5 | tableName string `comment:"采集自金蝶k3cloud物料数据" pg:"manufacture.material_group_k3cloud,alias:material_group_k3cloud"` |
| 6 | - Id int64 `pg:",pk"` | 6 | + Id int `pg:",pk"` |
| 7 | Number string `comment:"分组编码" pg:"number"` | 7 | Number string `comment:"分组编码" pg:"number"` |
| 8 | Name string `comment:"分组名称" pg:"name"` | 8 | Name string `comment:"分组名称" pg:"name"` |
| 9 | - ParentId int64 `comment:"父级id" pg:"parent_id"` | 9 | + ParentId int `comment:"父级id" pg:"parent_id"` |
| 10 | DataVersion int64 `comment:"数据版本" pg:"data_version"` | 10 | DataVersion int64 `comment:"数据版本" pg:"data_version"` |
| 11 | } | 11 | } |
| @@ -14,3 +14,9 @@ func (c *TestController) InvokPullMaterialNewest() { | @@ -14,3 +14,9 @@ func (c *TestController) InvokPullMaterialNewest() { | ||
| 14 | err := srv.PullMaterialNewest() | 14 | err := srv.PullMaterialNewest() |
| 15 | c.Response(nil, err) | 15 | c.Response(nil, err) |
| 16 | } | 16 | } |
| 17 | + | ||
| 18 | +func (c *TestController) InvokPullMaterialGroup() { | ||
| 19 | + srv := syncdata.PullDataK3CloudService{} | ||
| 20 | + err := srv.PullMaterialGroup() | ||
| 21 | + c.Response(nil, err) | ||
| 22 | +} |
| @@ -7,4 +7,5 @@ import ( | @@ -7,4 +7,5 @@ import ( | ||
| 7 | 7 | ||
| 8 | func init() { | 8 | func init() { |
| 9 | web.Router("/TestController/PullMaterialNewest", &controllers.TestController{}, "Get:InvokPullMaterialNewest") | 9 | web.Router("/TestController/PullMaterialNewest", &controllers.TestController{}, "Get:InvokPullMaterialNewest") |
| 10 | + web.Router("/TestController/PullMaterialGroup", &controllers.TestController{}, "Get:InvokPullMaterialGroup") | ||
| 10 | } | 11 | } |
-
请 注册 或 登录 后发表评论