作者 yangfu

feat:设备数据建模

... ... @@ -350,7 +350,9 @@ func (deviceService *DeviceService) SearchDevice(operateInfo *domain.OperateInfo
for i := range devices {
item := devices[i]
newJobDto := &dto.DeviceDto{}
newJobDto.WorkStation = workshops.FindWorkStation(item.WorkStation.WorkshopId, item.WorkStation.LineId, item.WorkStation.SectionId)
if item.WorkStation != nil && item.WorkStation.WorkshopId > 0 {
newJobDto.WorkStation = workshops.FindWorkStationOrNil(item.WorkStation.WorkshopId, item.WorkStation.LineId, item.WorkStation.SectionId)
}
newJobDto.LoadDto(item, operateInfo.OrgId)
result = append(result, newJobDto)
}
... ...
... ... @@ -117,3 +117,19 @@ func CreateProductPlanDispatchRecordRepository(options map[string]interface{}) (
}
return repository.NewProductPlanDispatchRecordRepository(transactionContext)
}
func CreateDeviceRunningRecordRepository(options map[string]interface{}) (domain.DeviceRunningRecordRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewDeviceRunningRecordRepository(transactionContext)
}
func CreateDeviceDailyRunningRecordRepository(options map[string]interface{}) (domain.DeviceDailyRunningRecordRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewDeviceDailyRunningRecordRepository(transactionContext)
}
... ...
... ... @@ -25,7 +25,7 @@ type CreateProductGroupCommand struct {
// 班组长Id
GroupLeaderId int `cname:"班组长" json:"groupLeaderId,omitempty"`
// 帮组成员列表
GroupMembers []int `cname:"帮组成员列表" json:"groupMembers" valid:"Required"`
GroupMembers []int `cname:"帮组成员列表" json:"groupMembers"`
// 上班班次 1:全天 2:白班 4:中班 8:夜班
WorkOn int `cname:"上班班次 1:全天 2:白班 4:中班 8:夜班" json:"workOn" valid:"Required"`
}
... ...
... ... @@ -22,7 +22,7 @@ type UpdateProductGroupCommand struct {
// 班组长Id
GroupLeaderId int `cname:"班组长" json:"groupLeaderId,omitempty"`
// 帮组成员列表
GroupMembers []int `cname:"帮组成员列表" json:"groupMembers" valid:"Required"`
GroupMembers []int `cname:"帮组成员列表" json:"groupMembers"`
// 上班班次 1:全天 2:白班 4:中班 8:夜班
WorkOn int `cname:"上班班次 1:全天 2:白班 4:中班 8:夜班" json:"workOn" valid:"Required"`
}
... ...
... ... @@ -16,9 +16,9 @@ type ProductGroupDto struct {
// 班组名称
GroupName string `json:"groupName,omitempty"`
// 班组长
GroupLeader string `json:"groupLeader,omitempty"`
GroupLeader string `json:"groupLeader"`
// 帮组成员列表
GroupMembers string `json:"groupMembers,omitempty"`
GroupMembers string `json:"groupMembers"`
// 上班班次 1:全天 2:白班 4:中班 8:夜班
WorkOn int `json:"workOn,omitempty"`
// 工作位置
... ... @@ -32,7 +32,9 @@ type ProductGroupDto struct {
func (d *ProductGroupDto) LoadDto(m *domain.ProductGroup, orgId int) *ProductGroupDto {
d.ProductGroupId = m.ProductGroupId
d.GroupName = m.GroupName
d.GroupLeader = m.GroupLeader.UserName
if m.GroupLeader != nil {
d.GroupLeader = m.GroupLeader.UserName
}
var members []string
for i := range m.GroupMembers {
members = append(members, m.GroupMembers[i].UserName)
... ...
... ... @@ -45,15 +45,19 @@ func (productGroupService *ProductGroupService) CreateProductGroup(operateInfo *
}
var leader *domain.User
var members []*domain.User
var members = make([]*domain.User, 0)
userService := domainService.NewUserService()
leader, err = userService.User(cmd.GroupLeaderId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
if cmd.GroupLeaderId > 0 {
leader, err = userService.User(cmd.GroupLeaderId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
members, err = userService.Users(cmd.GroupMembers)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
if len(cmd.GroupMembers) > 0 {
members, err = userService.Users(cmd.GroupMembers)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
var org *domain.Org
... ... @@ -130,6 +134,9 @@ func (productGroupService *ProductGroupService) GetProductGroup(getProductGroupQ
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
//if productGroup.GroupLeader==nil{
// productGroup.GroupLeader = &domain.User{}
//}
return productGroup, nil
}
... ... @@ -292,15 +299,19 @@ func (productGroupService *ProductGroupService) UpdateProductGroup(cmd *command.
}
var leader *domain.User
var members []*domain.User
var members = make([]*domain.User, 0)
userService := domainService.NewUserService()
leader, err = userService.User(cmd.GroupLeaderId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
if cmd.GroupLeaderId > 0 {
leader, err = userService.User(cmd.GroupLeaderId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
members, err = userService.Users(cmd.GroupMembers)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
if len(cmd.GroupMembers) > 0 {
members, err = userService.Users(cmd.GroupMembers)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
productGroup.GroupLeader = leader
productGroup.GroupMembers = members
... ...
... ... @@ -12,7 +12,7 @@ type SetOnlineCommand struct {
// 生产计划ID
ProductPlanId int `cname:"生产计划ID" json:"productPlanId" valid:"Required"`
// 车间ID
WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"`
WorkshopId int `cname:"车间ID" json:"workshopId" `
// 生产线ID
LineId int `cname:"生产线ID" json:"lineId" valid:"Required"`
// 工段ID
... ...
... ... @@ -9,10 +9,10 @@ import (
)
type SwitchCommand struct {
// 下线计划调度ID
FromProductPlanDispatchRecordId int `cname:"下线计划ID" json:"fromProductPlanId,omitempty"`
// 上线计划ID
ToProductPlanId int `cname:"上线计划ID" json:"toProductPlanId,omitempty"`
// 已上线计划ID
FromProductPlanDispatchRecordId int `cname:"已上线计划ID" json:"productPlanDispatchRecordId,omitempty" valid:"Required"`
// 计划ID
ToProductPlanId int `cname:"计划ID" json:"productPlanId,omitempty" valid:"Required"`
}
func (switchCommand *SwitchCommand) Valid(validation *validation.Validation) {
... ...
... ... @@ -7,7 +7,7 @@ import (
type ProductPlanDispatchRecordDto struct {
// 生产计划ID
ProductPlanId int `json:"productPlanId,omitempty"`
ProductPlanId int `json:"productPlanDispatchRecordId,omitempty"`
// 批号
BatchNumber string `json:"batchNumber,omitempty"`
// 生产日期
... ...
... ... @@ -28,6 +28,8 @@ type SearchProductPlanQuery struct {
BatchNumber string `cname:"批号" json:"batchNumber"`
// 车间名称
WorkshopName string `cname:"车间名称" json:"workshopName"`
// 车间ID
WorkshopId string `cname:"车间ID" json:"workshopId"`
}
func (cmd *SearchProductPlanQuery) Valid(validation *validation.Validation) {
... ...
... ... @@ -395,6 +395,7 @@ func (productPlanService *ProductPlanService) SetOnline(cmd *command.SetOnlineCo
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
cmd.WorkshopId = productPlan.Workshop.WorkshopId
var workStation *domain.WorkStation
_, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId)
if err != nil {
... ... @@ -575,7 +576,9 @@ func (productPlanService *ProductPlanService) SearchProductPlanOnlineDispatchRec
} else {
productPlanRepository = value
}
count, productPlans, err := productPlanRepository.Find(utils.ObjectToMap(cmd))
queryOptions := utils.ObjectToMap(cmd)
//queryOptions["planDispatchStatus"] = 1
count, productPlans, err := productPlanRepository.Find(queryOptions)
if err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
... ...
package domain
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"time"
)
const DefaultTimeWindow = 1
// 设备每日运行记录(汇总)
type DeviceDailyRunningRecord struct {
// 设备每日运行记录ID
DeviceDailyRunningRecordId int `json:"deviceDailyRunningRecordId"`
// 企业id
CompanyId int `json:"companyId"`
// 组织ID
OrgId int `json:"orgId"`
// 工作位置
WorkStation *WorkStation `json:"workStation"`
// 设备Id
DeviceId int `json:"deviceId"`
// 设备编号
DeviceCode string `json:"deviceCode"`
// 设备运行记录信息
DeviceRunningRecordInfo *DeviceRunningRecordInfo `json:"deviceRunningRecordInfo"`
// 创建时间
CreatedAt time.Time `json:"createdAt"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt"`
// 删除时间
DeletedAt time.Time `json:"deletedAt"`
}
type DeviceDailyRunningRecordRepository interface {
Save(deviceDailyRunningRecord *DeviceDailyRunningRecord) (*DeviceDailyRunningRecord, error)
Remove(deviceDailyRunningRecord *DeviceDailyRunningRecord) (*DeviceDailyRunningRecord, error)
FindOne(queryOptions map[string]interface{}) (*DeviceDailyRunningRecord, error)
Find(queryOptions map[string]interface{}) (int64, []*DeviceDailyRunningRecord, error)
}
func (deviceDailyRunningRecord *DeviceDailyRunningRecord) Identify() interface{} {
if deviceDailyRunningRecord.DeviceDailyRunningRecordId == 0 {
return nil
}
return deviceDailyRunningRecord.DeviceDailyRunningRecordId
}
func (deviceDailyRunningRecord *DeviceDailyRunningRecord) Update(data map[string]interface{}) error {
return nil
}
// 设备运行记录信息
type DeviceRunningRecordInfo struct {
// 当前状态
// bit0: 运行、停止
// bit1: 正常、故障
CurrentStatus int `json:"currentStatus"`
// 设备OEE = tu * pu * qu
OEE float64 `json:"oee"`
// 时间利用率 TimeUtilization 运行时间 / (当前时间-当日零时)
TimeUtilization float64 `json:"tu"`
// 性能利用率 PerformanceUtilization
// 1. 当前设备实际产出数量/理论数量(理论数量=60*60*12/标准工时)
// 2. 没有数量100%
PerformanceUtilization float64 `json:"pu"`
// 合格率 QualificationUtilization
// 1.按工段的合格率
// 2.默认100%
QualificationUtilization float64 `json:"qu"`
// 运行时长 单位:h
UpTime float64 `json:"upTime"`
// 生成数量
Count int `json:"count"`
// 设备温度 单位:摄氏度
Temp float64 `json:"temp"`
// 时间点
//TimeLine []string `json:"timeLine"`
// 时间点对应的设备状态 按小时 1
TimeLineDeviceStatus map[string]*HourDeviceStatus `json:"timeLineDeviceStatus"`
}
func NewDeviceRunningRecordInfo() *DeviceRunningRecordInfo {
return &DeviceRunningRecordInfo{
TimeLineDeviceStatus: make(map[string]*HourDeviceStatus),
}
}
func (d *DeviceRunningRecordInfo) AddDeviceRunningData(t time.Time, data *DeviceRunningData) {
d.CurrentStatus = data.StartupState | (1 << data.ComStatus)
d.ResetUpTime()
d.Count += data.Count
//d.Temp = data.FrontTemp
if data.Temp1 > 0 {
d.Temp = data.Temp1
} else if data.FrontTemp > 0 {
d.Temp = data.FrontTemp
}
d.AddTimeLineDeviceStatus(t, data)
//d.OEE
d.TimeUtilization = d.UpTime * 100 / (time.Now().Sub(utils.GetZeroTime(time.Now())).Hours())
//d.PerformanceUtilization
//d.QualificationUtilization
}
// 添加新的设备状态
func (d *DeviceRunningRecordInfo) AddTimeLineDeviceStatus(t time.Time, data *DeviceRunningData) {
if t.IsZero() {
return
}
key := fmt.Sprintf("%v", t.Hour())
var v *HourDeviceStatus
var ok bool
if v, ok = d.TimeLineDeviceStatus[key]; !ok {
v = NewHourDeviceStatus()
d.TimeLineDeviceStatus[key] = v
}
v.UpdateUp(t, data.StartupState)
v.UpdateCom(t, data.ComStatus)
}
// 重置运行时长
func (d *DeviceRunningRecordInfo) ResetUpTime() float64 {
var upTime float64
for _, v := range d.TimeLineDeviceStatus {
t := v.CountTime(v.Up)
upTime += t.Hours()
}
d.UpTime = upTime
return upTime
}
// 重置设备运行OEE
func (d *DeviceRunningRecordInfo) ResetOEE(pu, qu float64) float64 {
d.PerformanceUtilization = pu
d.QualificationUtilization = qu
d.OEE = (d.TimeUtilization + d.PerformanceUtilization + d.QualificationUtilization) / 3
return d.OEE
}
// 单个小时内的设备状态
type HourDeviceStatus struct {
// 时间窗口 1-60 代表时间段范围
// 例如: w=1 则标识下面的状态按1分钟记录一次状态
// up 启动 bit0-bit59的位用来存启动状态 1:启动 0:关闭
// com通讯 bit0-bit59的位用来存通讯状态 1:正常 0:故障
// 如果 w=5 表示按5分钟记录一次状态 使用到 bit0-bit11
Window int `json:"w"`
// 启动
// bit0:1 代表
Up int `json:"up"`
// 通讯
Com int `json:"com"`
}
// 更新启动状态
func (d *HourDeviceStatus) UpdateUp(t time.Time, up int) {
m := t.Minute()
bit := 1 << (m / d.Window)
if up&1 == 0 {
return
}
if d.Up&bit > 0 {
return
}
d.Up |= bit
return
}
// 更新通讯状态
func (d *HourDeviceStatus) UpdateCom(t time.Time, c int) {
m := t.Minute()
bit := 1 << (m / d.Window)
if c&1 == 0 {
return
}
if d.Com&bit > 0 {
return
}
d.Com |= bit
return
}
// 计算状态持续的时间
func (d *HourDeviceStatus) CountTime(v int) time.Duration {
l := 60 / d.Window
count := 0
index := 1
for i := 0; i < l; i++ {
if index&v > 0 {
count++
}
index <<= 1
}
return time.Duration(d.Window*count) * time.Minute / time.Hour
}
func NewHourDeviceStatus() *HourDeviceStatus {
return &HourDeviceStatus{
Window: DefaultTimeWindow,
Up: 0,
Com: 0,
}
}
... ...
package domain
// 设备运行数据
type DeviceRunningData struct {
// 启动状态:1:启动,0:停止
StartupState int `json:"startupState"`
// 通讯状态:1:通讯正常,0:设备未上电或与采集端通讯故障
ComStatus int `json:"comStatus"`
// 匹配数目
Count int `json:"count,string"`
// 炸机前段温度:炸机前段当前温度 YZJ1 油炸机
FrontTemp float64 `json:"frontTemp"`
// 炸机前段温度:炸机前段当前温度 YZJ2 油炸机
Temp1 float64 `json:"temp1"`
// 当前产品种类(产品编号)
ProductType string `json:"productType"`
// 设备编号
DeviceCode string `json:"deviceCode"`
}
... ...
package domain
import "time"
// 设备运行记录
type DeviceRunningRecord struct {
// 设备运行记录ID
DeviceRunningRecordId int `json:"deviceRunningRecordId"`
// 企业id
CompanyId int `json:"companyId"`
// 组织ID
OrgId int `json:"orgId"`
// 工作位置
WorkStation *WorkStation `json:"workStation"`
// 设备Id
DeviceId int `json:"deviceId"`
// 设备编号
DeviceCode string `json:"deviceCode"`
// 设备运行记录信息
DeviceRunningRecordInfo string `json:"deviceRunningRecordInfo"`
// 创建时间
CreatedAt time.Time `json:"createdAt"`
}
type DeviceRunningRecordRepository interface {
Save(deviceRunningRecord *DeviceRunningRecord) (*DeviceRunningRecord, error)
Remove(deviceRunningRecord *DeviceRunningRecord) (*DeviceRunningRecord, error)
FindOne(queryOptions map[string]interface{}) (*DeviceRunningRecord, error)
Find(queryOptions map[string]interface{}) (int64, []*DeviceRunningRecord, error)
}
func (deviceRunningRecord *DeviceRunningRecord) Identify() interface{} {
if deviceRunningRecord.DeviceRunningRecordId == 0 {
return nil
}
return deviceRunningRecord.DeviceRunningRecordId
}
func (deviceRunningRecord *DeviceRunningRecord) Update(data map[string]interface{}) error {
return nil
}
... ...
... ... @@ -4,8 +4,8 @@ package domain
type ProductRecordInfo struct {
// 生产日期
ProductDate string `json:"productDate"`
// 原始上报数据
OriginalWeigh float64 `json:"originalWeigh,omitempty"`
// 原始上报数据(kg,串)
Original float64 `json:"original,omitempty"`
// 换算后的产能
Weigh float64 `json:"weigh"`
// 产能 - 审核前
... ...
... ... @@ -9,9 +9,9 @@ const (
// 用户对象
type User struct {
// 用户Id 用户唯一标识
UserId int `json:"userId,omitempty"`
UserId int `json:"userId"`
// 用户姓名
UserName string `json:"userName,omitempty"`
UserName string `json:"userName"`
// 员工类型 1:固定 2:派遣 3.临时
EmployeeType int `json:"employeeType,omitempty"`
// IC卡号
... ...
... ... @@ -23,14 +23,29 @@ type WorkStation struct {
}
func NewWorkStation(w *Workshop, l *ProductLine, s *ProductSection) *WorkStation {
return &WorkStation{
WorkStationId: fmt.Sprintf("%v.%v.%v", w.WorkshopId, l.LineId, s.SectionId),
WorkshopId: w.WorkshopId,
WorkshopName: w.WorkshopName,
LineId: l.LineId,
LineName: l.LineName,
SectionId: s.SectionId,
SectionName: s.SectionName,
item := &WorkStation{
//WorkshopId: w.WorkshopId,
//WorkshopName: w.WorkshopName,
//LineId: l.LineId,
//LineName: l.LineName,
//SectionId: s.SectionId,
//SectionName: s.SectionName,
//Principal: w.Principal,
}
if w != nil && l != nil && s != nil {
item.WorkStationId = fmt.Sprintf("%v.%v.%v", w.WorkshopId, l.LineId, s.SectionId)
}
if w != nil {
item.WorkshopId = w.WorkshopId
item.WorkshopName = w.WorkshopName
}
if l != nil {
item.LineId = l.LineId
item.LineName = l.LineName
}
if s != nil {
item.SectionId = s.SectionId
item.SectionName = s.SectionName
}
return item
}
... ...
... ... @@ -16,6 +16,17 @@ func (m Workshops) FindWorkStation(workshopId, lineId, sectionId int) *WorkStati
return &WorkStation{} //返回空的对象
}
func (m Workshops) FindWorkStationOrNil(workshopId, lineId, sectionId int) *WorkStation {
for i := range m {
item := m[i]
workstation, err := item.FindWorkStationOrNil(workshopId, lineId, sectionId)
if err == nil && workstation != nil {
return workstation
}
}
return &WorkStation{} //返回空的对象
}
func (m Workshops) FindWorkshopsByName(workshopName string) []int {
result := make([]int, 0)
if len(workshopName) == 0 {
... ...
... ... @@ -108,7 +108,7 @@ func (ptr *PGProductRecordService) SubmitProductRecord(productRecordType int, qu
UpdatedAt: time.Now(),
ProductRecordInfo: &domain.ProductRecordInfo{
ProductDate: plan.ProductDate.Format("2006-01-02"),
OriginalWeigh: request.Weigh,
Original: request.Weigh,
Weigh: weight,
WeighBefore: weight,
UnitConversionId: request.UnitConversionId,
... ...
package models
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"time"
)
type DeviceDailyRunningRecord struct {
tableName string `comment:"设备每日运行记录(汇总)" pg:"manufacture.device_daily_running_record"`
// 设备每日运行记录ID
DeviceDailyRunningRecordId int `comment:"设备每日运行记录ID" pg:"pk:device_daily_running_record_id"`
// 企业id
CompanyId int `comment:"企业id"`
// 组织ID
OrgId int `comment:"组织ID"`
// 工作位置
WorkStation *domain.WorkStation `comment:"工作位置"`
// 设备Id
DeviceId int `comment:"设备Id"`
// 设备编号
DeviceCode string `comment:"设备编号"`
// 设备运行记录信息
DeviceRunningRecordInfo *domain.DeviceRunningRecordInfo `comment:"设备运行记录信息"`
// 创建时间
CreatedAt time.Time `comment:"创建时间"`
// 更新时间
UpdatedAt time.Time `comment:"更新时间"`
// 删除时间
DeletedAt time.Time `comment:"删除时间"`
}
... ...
package models
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"time"
)
type DeviceRunningRecord struct {
tableName string `comment:"设备运行记录" pg:"manufacture.device_running_record"`
// 设备运行记录ID
DeviceRunningRecordId int `comment:"设备运行记录ID" pg:"pk:device_running_record_id"`
// 企业id
CompanyId int `comment:"企业id"`
// 组织ID
OrgId int `comment:"组织ID"`
// 工作位置
WorkStation *domain.WorkStation `comment:"工作位置"`
// 设备Id
DeviceId int `comment:"设备Id"`
// 设备编号
DeviceCode string `comment:"设备编号"`
// 设备运行记录信息
DeviceRunningRecordInfo string `comment:"设备运行记录信息"`
// 创建时间
CreatedAt time.Time `comment:"创建时间"`
}
... ...
package transform
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
)
func TransformToDeviceDailyRunningRecordDomainModelFromPgModels(deviceDailyRunningRecordModel *models.DeviceDailyRunningRecord) (*domain.DeviceDailyRunningRecord, error) {
return &domain.DeviceDailyRunningRecord{
DeviceDailyRunningRecordId: deviceDailyRunningRecordModel.DeviceDailyRunningRecordId,
CompanyId: deviceDailyRunningRecordModel.CompanyId,
OrgId: deviceDailyRunningRecordModel.OrgId,
WorkStation: deviceDailyRunningRecordModel.WorkStation,
DeviceId: deviceDailyRunningRecordModel.DeviceId,
DeviceCode: deviceDailyRunningRecordModel.DeviceCode,
DeviceRunningRecordInfo: deviceDailyRunningRecordModel.DeviceRunningRecordInfo,
CreatedAt: deviceDailyRunningRecordModel.CreatedAt,
UpdatedAt: deviceDailyRunningRecordModel.UpdatedAt,
DeletedAt: deviceDailyRunningRecordModel.DeletedAt,
}, nil
}
... ...
package transform
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
)
func TransformToDeviceRunningRecordDomainModelFromPgModels(deviceRunningRecordModel *models.DeviceRunningRecord) (*domain.DeviceRunningRecord, error) {
return &domain.DeviceRunningRecord{
DeviceRunningRecordId: deviceRunningRecordModel.DeviceRunningRecordId,
CompanyId: deviceRunningRecordModel.CompanyId,
OrgId: deviceRunningRecordModel.OrgId,
WorkStation: deviceRunningRecordModel.WorkStation,
DeviceId: deviceRunningRecordModel.DeviceId,
DeviceCode: deviceRunningRecordModel.DeviceCode,
DeviceRunningRecordInfo: deviceRunningRecordModel.DeviceRunningRecordInfo,
CreatedAt: deviceRunningRecordModel.CreatedAt,
}, nil
}
... ...
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/snowflake"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/transform"
)
type DeviceDailyRunningRecordRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *DeviceDailyRunningRecordRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *DeviceDailyRunningRecordRepository) Save(deviceDailyRunningRecord *domain.DeviceDailyRunningRecord) (*domain.DeviceDailyRunningRecord, error) {
sqlBuildFields := []string{
"device_daily_running_record_id",
"company_id",
"org_id",
"work_station",
"device_id",
"device_code",
"device_running_record_info",
"created_at",
"updated_at",
"deleted_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "deviceDailyRunningRecord_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if deviceDailyRunningRecord.Identify() == nil {
if _, err := tx.QueryOne(
pg.Scan(
&deviceDailyRunningRecord.DeviceDailyRunningRecordId,
&deviceDailyRunningRecord.CompanyId,
&deviceDailyRunningRecord.OrgId,
&deviceDailyRunningRecord.WorkStation,
&deviceDailyRunningRecord.DeviceId,
&deviceDailyRunningRecord.DeviceCode,
&deviceDailyRunningRecord.DeviceRunningRecordInfo,
&deviceDailyRunningRecord.CreatedAt,
&deviceDailyRunningRecord.UpdatedAt,
&deviceDailyRunningRecord.DeletedAt,
),
fmt.Sprintf("INSERT INTO manufacture.device_daily_running_record (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
deviceDailyRunningRecord.DeviceDailyRunningRecordId,
deviceDailyRunningRecord.CompanyId,
deviceDailyRunningRecord.OrgId,
deviceDailyRunningRecord.WorkStation,
deviceDailyRunningRecord.DeviceId,
deviceDailyRunningRecord.DeviceCode,
deviceDailyRunningRecord.DeviceRunningRecordInfo,
deviceDailyRunningRecord.CreatedAt,
deviceDailyRunningRecord.UpdatedAt,
deviceDailyRunningRecord.DeletedAt,
); err != nil {
return deviceDailyRunningRecord, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&deviceDailyRunningRecord.DeviceDailyRunningRecordId,
&deviceDailyRunningRecord.CompanyId,
&deviceDailyRunningRecord.OrgId,
&deviceDailyRunningRecord.WorkStation,
&deviceDailyRunningRecord.DeviceId,
&deviceDailyRunningRecord.DeviceCode,
&deviceDailyRunningRecord.DeviceRunningRecordInfo,
&deviceDailyRunningRecord.CreatedAt,
&deviceDailyRunningRecord.UpdatedAt,
&deviceDailyRunningRecord.DeletedAt,
),
fmt.Sprintf("UPDATE manufacture.device_daily_running_record SET %s WHERE device_daily_running_record_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
deviceDailyRunningRecord.DeviceDailyRunningRecordId,
deviceDailyRunningRecord.CompanyId,
deviceDailyRunningRecord.OrgId,
deviceDailyRunningRecord.WorkStation,
deviceDailyRunningRecord.DeviceId,
deviceDailyRunningRecord.DeviceCode,
deviceDailyRunningRecord.DeviceRunningRecordInfo,
deviceDailyRunningRecord.CreatedAt,
deviceDailyRunningRecord.UpdatedAt,
deviceDailyRunningRecord.DeletedAt,
deviceDailyRunningRecord.Identify(),
); err != nil {
return deviceDailyRunningRecord, err
}
}
return deviceDailyRunningRecord, nil
}
func (repository *DeviceDailyRunningRecordRepository) Remove(deviceDailyRunningRecord *domain.DeviceDailyRunningRecord) (*domain.DeviceDailyRunningRecord, error) {
tx := repository.transactionContext.PgTx
deviceDailyRunningRecordModel := new(models.DeviceDailyRunningRecord)
deviceDailyRunningRecordModel.DeviceDailyRunningRecordId = deviceDailyRunningRecord.Identify().(int)
if _, err := tx.Model(deviceDailyRunningRecordModel).WherePK().Delete(); err != nil {
return deviceDailyRunningRecord, err
}
return deviceDailyRunningRecord, nil
}
func (repository *DeviceDailyRunningRecordRepository) FindOne(queryOptions map[string]interface{}) (*domain.DeviceDailyRunningRecord, error) {
tx := repository.transactionContext.PgTx
deviceDailyRunningRecordModel := new(models.DeviceDailyRunningRecord)
query := sqlbuilder.BuildQuery(tx.Model(deviceDailyRunningRecordModel), queryOptions)
query.SetWhereByQueryOption("device_daily_running_record.device_daily_running_record_id = ?", "deviceDailyRunningRecordId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
if deviceDailyRunningRecordModel.DeviceDailyRunningRecordId == 0 {
return nil, nil
} else {
return transform.TransformToDeviceDailyRunningRecordDomainModelFromPgModels(deviceDailyRunningRecordModel)
}
}
func (repository *DeviceDailyRunningRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.DeviceDailyRunningRecord, error) {
tx := repository.transactionContext.PgTx
var deviceDailyRunningRecordModels []*models.DeviceDailyRunningRecord
deviceDailyRunningRecords := make([]*domain.DeviceDailyRunningRecord, 0)
query := sqlbuilder.BuildQuery(tx.Model(&deviceDailyRunningRecordModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOrderDirect("device_daily_running_record_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, deviceDailyRunningRecords, err
} else {
for _, deviceDailyRunningRecordModel := range deviceDailyRunningRecordModels {
if deviceDailyRunningRecord, err := transform.TransformToDeviceDailyRunningRecordDomainModelFromPgModels(deviceDailyRunningRecordModel); err != nil {
return 0, deviceDailyRunningRecords, err
} else {
deviceDailyRunningRecords = append(deviceDailyRunningRecords, deviceDailyRunningRecord)
}
}
return int64(count), deviceDailyRunningRecords, nil
}
}
func NewDeviceDailyRunningRecordRepository(transactionContext *pgTransaction.TransactionContext) (*DeviceDailyRunningRecordRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &DeviceDailyRunningRecordRepository{
transactionContext: transactionContext,
}, nil
}
}
... ...
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/snowflake"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/transform"
)
type DeviceRunningRecordRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *DeviceRunningRecordRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *DeviceRunningRecordRepository) Save(deviceRunningRecord *domain.DeviceRunningRecord) (*domain.DeviceRunningRecord, error) {
sqlBuildFields := []string{
"device_running_record_id",
"company_id",
"org_id",
"work_station",
"device_id",
"device_code",
"device_running_record_info",
"created_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "device_running_record_id"))
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "device_running_record_id"))
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "device_running_record_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if deviceRunningRecord.Identify() == nil {
if _, err := tx.QueryOne(
pg.Scan(
&deviceRunningRecord.DeviceRunningRecordId,
&deviceRunningRecord.CompanyId,
&deviceRunningRecord.OrgId,
&deviceRunningRecord.WorkStation,
&deviceRunningRecord.DeviceId,
&deviceRunningRecord.DeviceCode,
&deviceRunningRecord.DeviceRunningRecordInfo,
&deviceRunningRecord.CreatedAt,
),
fmt.Sprintf("INSERT INTO device_running_records (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
//deviceRunningRecord.DeviceRunningRecordId,
deviceRunningRecord.CompanyId,
deviceRunningRecord.OrgId,
deviceRunningRecord.WorkStation,
deviceRunningRecord.DeviceId,
deviceRunningRecord.DeviceCode,
deviceRunningRecord.DeviceRunningRecordInfo,
deviceRunningRecord.CreatedAt,
); err != nil {
return deviceRunningRecord, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&deviceRunningRecord.DeviceRunningRecordId,
&deviceRunningRecord.CompanyId,
&deviceRunningRecord.OrgId,
&deviceRunningRecord.WorkStation,
&deviceRunningRecord.DeviceId,
&deviceRunningRecord.DeviceCode,
&deviceRunningRecord.DeviceRunningRecordInfo,
&deviceRunningRecord.CreatedAt,
),
fmt.Sprintf("UPDATE device_running_records SET %s WHERE device_running_record_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
//deviceRunningRecord.DeviceRunningRecordId,
deviceRunningRecord.CompanyId,
deviceRunningRecord.OrgId,
deviceRunningRecord.WorkStation,
deviceRunningRecord.DeviceId,
deviceRunningRecord.DeviceCode,
deviceRunningRecord.DeviceRunningRecordInfo,
deviceRunningRecord.CreatedAt,
deviceRunningRecord.Identify(),
); err != nil {
return deviceRunningRecord, err
}
}
return deviceRunningRecord, nil
}
func (repository *DeviceRunningRecordRepository) Remove(deviceRunningRecord *domain.DeviceRunningRecord) (*domain.DeviceRunningRecord, error) {
tx := repository.transactionContext.PgTx
deviceRunningRecordModel := new(models.DeviceRunningRecord)
deviceRunningRecordModel.DeviceRunningRecordId = deviceRunningRecord.Identify().(int)
if _, err := tx.Model(deviceRunningRecordModel).WherePK().Delete(); err != nil {
return deviceRunningRecord, err
}
return deviceRunningRecord, nil
}
func (repository *DeviceRunningRecordRepository) FindOne(queryOptions map[string]interface{}) (*domain.DeviceRunningRecord, error) {
tx := repository.transactionContext.PgTx
deviceRunningRecordModel := new(models.DeviceRunningRecord)
query := sqlbuilder.BuildQuery(tx.Model(deviceRunningRecordModel), queryOptions)
query.SetWhereByQueryOption("device_running_record.device_running_record_id = ?", "deviceRunningRecordId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
if deviceRunningRecordModel.DeviceRunningRecordId == 0 {
return nil, nil
} else {
return transform.TransformToDeviceRunningRecordDomainModelFromPgModels(deviceRunningRecordModel)
}
}
func (repository *DeviceRunningRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.DeviceRunningRecord, error) {
tx := repository.transactionContext.PgTx
var deviceRunningRecordModels []*models.DeviceRunningRecord
deviceRunningRecords := make([]*domain.DeviceRunningRecord, 0)
query := sqlbuilder.BuildQuery(tx.Model(&deviceRunningRecordModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOrderDirect("device_running_record_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, deviceRunningRecords, err
} else {
for _, deviceRunningRecordModel := range deviceRunningRecordModels {
if deviceRunningRecord, err := transform.TransformToDeviceRunningRecordDomainModelFromPgModels(deviceRunningRecordModel); err != nil {
return 0, deviceRunningRecords, err
} else {
deviceRunningRecords = append(deviceRunningRecords, deviceRunningRecord)
}
}
return int64(count), deviceRunningRecords, nil
}
}
func NewDeviceRunningRecordRepository(transactionContext *pgTransaction.TransactionContext) (*DeviceRunningRecordRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &DeviceRunningRecordRepository{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -150,6 +150,7 @@ func (repository *ProductPlanDispatchRecordRepository) Find(queryOptions map[str
var productPlanDispatchRecordModels []*models.ProductPlanDispatchRecord
productPlanDispatchRecords := make([]*domain.ProductPlanDispatchRecord, 0)
query := sqlbuilder.BuildQuery(tx.Model(&productPlanDispatchRecordModels), queryOptions)
query.SetWhereByQueryOption("plan_dispatch_status=?", "planDispatchStatus")
query.SetOffsetAndLimit(domain.MaxQueryRow)
query.SetOrderDirect("product_plan_dispatch_record_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
... ...
... ... @@ -174,7 +174,7 @@ func (repository *ProductPlanRepository) Find(queryOptions map[string]interface{
query := sqlbuilder.BuildQuery(tx.Model(&productPlanModels), queryOptions)
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("work_station->>'workshopId'='?'", "workshopId")
if v, ok := queryOptions["batchNumber"]; ok && len(v.(string)) > 0 {
query.Where(fmt.Sprintf(`batch_number like '%%%v%%'`, v))
}
... ...