作者 yangfu

refactor: 功能优化

* 统计功能优化
* 物料管理调整
select count(*) from manufacture.device_running_record where device_running_record_id <=2000000
select count(*) from manufacture.device_collections where created_at <'2022-6-01 16:00:00'
--1.备份设备运行数据-按数量
-- 备份数据 2000000
select * into table manufacture.device_running_record_0_2000
from manufacture.device_running_record
where device_running_record_id <=2000000
-- 删除数据
delete from manufacture.device_running_record where device_running_record_id <=2000000;
-- 重建索引
reindex table manufacture.device_running_record;
--2.备份设备采集数据-按时间
-- 备份数据 2000000
select * into table manufacture.device_collections_history
from manufacture.device_collections
where created_at <'2022-6-01 16:00:00'
-- 删除数据
delete from manufacture.device_collections where created_at <'2022-6-01 16:00:00';
-- 重建索引
reindex table manufacture.device_collections;
--3.查看备份情况
select count(*) from manufacture.device_running_record_0_2000
select count(*) from manufacture.device_collections_history
select count(*) from manufacture.device_collections where created_at <'2022-6-01 16:00:00'
\ No newline at end of file
... ...
... ... @@ -3,9 +3,12 @@ package main
import (
"fmt"
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/crontab"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/mqtt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/task"
"time"
_ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
... ... @@ -27,11 +30,11 @@ func main() {
log.Logger.Info("server start ....")
log.Logger.Debug(fmt.Sprintf("ENABLE_KAFKA_LOG:%v", constant.ENABLE_KAFKA_LOG))
//go mqtt.Start(log.Logger)
//go task.Run()
//cron := crontab.NewCrontabService(nil)
//cron.StartCrontabTask()
//defer cron.StopCrontabTask()
go mqtt.Start(log.Logger)
go task.Run()
cron := crontab.NewCrontabService(nil)
cron.StartCrontabTask()
defer cron.StopCrontabTask()
time.Sleep(time.Second)
log.Logger.Info("server start!")
web.Run()
... ...
... ... @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"reflect"
"regexp"
"strings"
)
... ... @@ -33,7 +34,20 @@ type CreateProductMaterialCommand struct {
}
func (createProductMaterialCommand *CreateProductMaterialCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
createProductMaterialCommand.MaterialNumber = strings.ToUpper(createProductMaterialCommand.MaterialNumber)
match, err := regexp.MatchString("^[A-Z0-9]+$", createProductMaterialCommand.MaterialNumber)
if !match {
validation.Error("物料编码只允许数字加大写字母组合")
return
}
if err != nil {
validation.Error(err.Error())
return
}
if len([]rune(createProductMaterialCommand.Specification)) > 50 {
validation.Error("规格最多允许50个字符")
return
}
}
func (createProductMaterialCommand *CreateProductMaterialCommand) ValidateCommand() error {
... ...
... ... @@ -36,7 +36,10 @@ type UpdateProductMaterialCommand struct {
}
func (updateProductMaterialCommand *UpdateProductMaterialCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
if len([]rune(updateProductMaterialCommand.Specification)) > 50 {
validation.Error("规格最多允许50个字符")
return
}
}
func (updateProductMaterialCommand *UpdateProductMaterialCommand) ValidateCommand() error {
... ...
... ... @@ -91,7 +91,7 @@ func (productMaterialService *ProductMaterialService) GetProductMaterial(operate
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductMaterialQuery.ProductMaterialId)))
} else {
materialService, _ := domainService.NewPGMaterialService(transactionContext.(*pgTransaction.TransactionContext))
productMaterialGroupIdNames, _, err := materialService.AllMaterialGroupParent(operateInfo, productMaterial.ProductMaterialGroupId)
productMaterialGroupIdNames, _, err := materialService.AllMaterialGroupParentByBacktracking(operateInfo, productMaterial.ProductMaterialGroupId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
... ...
... ... @@ -25,7 +25,6 @@ func (dao *DeviceDailyRunningRecordDao) TimeSectionRunningRecord(companyId, orgI
tx := dao.transactionContext.PgTx
sql := fmt.Sprintf(`
WITH ts_product as(
select sum(a.weight) total,a.ts from (
select
... ... @@ -47,8 +46,8 @@ WITH ts_product as(
-- select * from ts_product
, ts_product_list as (
select d.ts,ts_product.total from (
select to_char(c.ts::timestamp at time ZONE 'Asia/shanghai','mm-dd') ts from (
select generate_series(to_timestamp(?,'YYYY-MM-DD HH24:MI:SS'),to_timestamp(?,'YYYY-MM-DD HH24:MI:SS'),'1 day') ts
select to_char(c.ts,'mm-dd') ts from (
select generate_series(to_timestamp(?,'YYYY-MM-DD HH24:MI:SS') at time ZONE 'Asia/shanghai',to_timestamp(?,'YYYY-MM-DD HH24:MI:SS') at time ZONE 'Asia/shanghai','1 day') ts
) c ) d left join ts_product on d.ts = ts_product.ts
)
SELECT ts, coalesce(total,0) total
... ...
... ... @@ -100,7 +100,7 @@ func (ptr *PGCommonStatisticsService) HourProductiveStatistics(queryOptions map[
beginTime = xtime.BeginningOfDay() //time.Now().Add(-time.Hour*5)
endTime = time.Now()
)
if !xtime.IsZero(request.Date.Time()) {
if !xtime.IsZero(request.Date.Time()) && !request.Date.Time().Equal(beginTime) {
beginTime = request.Date.Time().AddDate(0, 0, -1)
endTime = request.Date.Time()
}
... ... @@ -162,13 +162,13 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map
Total float64 `json:"total"`
}
var (
beingTime = xtime.BeginningOfDay().AddDate(0, 0, -5)
endTime = xtime.BeginningOfDay().AddDate(0, 0, 1)
beingTime = xtime.BeginningOfDay().AddDate(0, 0, -6)
endTime = xtime.BeginningOfDay().AddDate(0, 0, 1).Add(-time.Second)
)
//if !xtime.IsZero(time.Time(request.Date)) {
// beingTime = request.Date.Time().AddDate(0, 0, -5)
// endTime = request.Date.Time().AddDate(0, 0, 1)
//}
if !xtime.IsZero(time.Time(request.Date)) {
beingTime = request.Date.Time().AddDate(0, 0, -6)
endTime = request.Date.Time().AddDate(0, 0, 1).Add(-time.Second)
}
workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": request.WorkshopId})
if err != nil || workshop == nil {
return nil, nil
... ...
... ... @@ -215,6 +215,34 @@ func (ptr *PGMaterialService) AllMaterialGroupParent(opt *domain.OperateInfo, pr
return result, listId, err
}
func (ptr *PGMaterialService) AllMaterialGroupParentByBacktracking(opt *domain.OperateInfo, productMaterialGroupId int) ([]*domain.ProductMaterialGroup, []int, error) {
var (
err error
listId []int
productMaterialGroup *domain.ProductMaterialGroup
productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
pid = productMaterialGroupId
mapExists = make(map[int]int)
)
var result = make([]*domain.ProductMaterialGroup, 0)
for {
if pid == 0 {
break
}
if _, exists := mapExists[pid]; exists {
break
}
mapExists[pid] = pid
productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": pid, "allWithDeleted": "1"})
if err != nil || productMaterialGroup == nil {
return nil, listId, fmt.Errorf("物料分组不存在")
}
pid = productMaterialGroup.Pid
result = append([]*domain.ProductMaterialGroup{productMaterialGroup}, result...)
}
return result, listId, err
}
func NewPGMaterialService(transactionContext *pgTransaction.TransactionContext) (*PGMaterialService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ...
... ... @@ -120,6 +120,9 @@ func (repository *ProductMaterialGroupRepository) FindOne(queryOptions map[strin
query.SetWhereByQueryOption("product_material_group_id = ?", "productMaterialGroupId")
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("material_group_number = ?", "materialGroupNumber")
if _, ok := queryOptions["allWithDeleted"]; ok {
query.AllWithDeleted()
}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, domain.ErrorNotFound
... ...