作者 Your Name

更新

... ... @@ -59,6 +59,7 @@ func (data *importAttendance) validField() error {
return nil
}
// 导入生产计划
func (srv ExcelDataService) ImportDataAttendance(importDataCommand *command.ImportDataCommand) (interface{}, error) {
excelImport := excel.NewExcelImport()
excelImport.RowBegin = 2 //第二行开始读取
... ...
package service
import (
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/excel"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/ecelData/command"
productRecordCommand "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productRecord/command"
productRecordService "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productRecord/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils/converter"
)
// ImportProductRecord 导入生产记录
func (srv ExcelDataService) ImportProductRecord(importDataCommand *command.ImportDataCommand) (interface{}, error) {
excelImport := excel.NewExcelImport()
excelImport.RowBegin = 3 //第二行开始读取
excelImport.DataFields = []excel.DataField{
{EnName: "CreatedDate", CnName: "日期"},
{EnName: "WorkshopName", CnName: "车间"},
{EnName: "LineName", CnName: "线别"},
{EnName: "SectionName", CnName: "工段"},
{EnName: "WorkerName", CnName: "姓名"},
{EnName: "ProductGroupName", CnName: "班组"},
{EnName: "Weigh", CnName: "二级品重量"},
}
excelData, err := converter.OpenImportFileFromIoReader(excelImport, importDataCommand.Reader, importDataCommand.FileExt) //excelImport.OpenExcelFromIoReader(importDataCommand.Reader)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
items := make([]productRecordCommand.BatchAddProductRecordCommand, 0, len(excelData))
item := productRecordCommand.BatchAddProductRecordCommand{}
for _, v := range excelData {
item = productRecordCommand.BatchAddProductRecordCommand{
CreatedDate: v["CreatedDate"],
WorkshopName: v["WorkshopName"],
LineName: v["LineName"],
SectionName: v["SectionName"],
WorkerName: v["WorkerName"],
BatchNumber: v["BatchNumber"],
ProductGroupName: v["ProductGroupName"],
Weigh: v["Weigh"],
FailReason: "",
}
items = append(items, item)
}
svr := productRecordService.NewProductRecordService(nil)
failRows, err := svr.BatchAddProductRecord(importDataCommand.Operator, items)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return srv.importResultWithHeader(excelImport.DataFields, failRows, len(items)), nil
}
... ...
... ... @@ -6,6 +6,8 @@ import (
"strings"
"time"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/transaction/pg"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
... ... @@ -414,7 +416,7 @@ func (productRecordService *ProductRecordService) CancelProductRecord(cmd *comma
return struct{}{}, nil
}
//BatchAddProductRecord 从文件导入的数据,批量添加生产记录
// BatchAddProductRecord 从文件导入的数据,批量添加生产记录
func (productRecordService *ProductRecordService) BatchAddProductRecord(operate *domain.OperateInfo, param []command.BatchAddProductRecordCommand) (
failRows []interface{}, err error) {
transactionContext, err := factory.CreateTransactionContext(nil)
... ... @@ -570,7 +572,7 @@ func (productRecordService *ProductRecordService) BatchAddProductRecord(operate
Weigh: weigh,
WeighBefore: weigh,
WeighAfter: weigh,
ApproveStatus: domain.ProductRecordNotApprove,
ApproveStatus: domain.ProductRecordAutoApproved,
ApproveAt: nowTime.Unix(),
ApproveUser: operateUser,
UnitConversionId: productPlanData.Ext.ProductPlanExt.ProductId,
... ... @@ -603,7 +605,13 @@ func (productRecordService *ProductRecordService) BatchAddProductRecord(operate
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
//自动审核
//
return nil, nil
for i := range productRecordList {
err = domainService.SendProductRecordStaticsJob(productRecordList[i])
if err != nil {
e := fmt.Sprintf("【发送产量统计任务失败】 ProductRecordId=%d, %s", productRecordList[i].ProductRecordId, err.Error())
log.Logger.Error(e)
return failRows, err
}
}
return failRows, err
}
... ...
package command
//创建事故记录
type CreatedProductTroubleCommand struct{}
... ...
package query
type GetProductTroubleQuery struct {
// 生产记录ID
Id int `json:"id" valid:"Required"`
}
... ...
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productTrouble/command"
)
// 事故管理
type ProductTroubleService struct{}
func NewProductTroubleService(options map[string]interface{}) *ProductTroubleService {
newService := &ProductTroubleService{}
return newService
}
func (srv ProductTroubleService) SaveProductTrouble(param *command.CreatedProductTroubleCommand) (map[string]interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"id": 0,
}, nil
}
func (srv ProductTroubleService) GetProductTrouble(id int64) (map[string]interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"id": 0,
}, nil
}
func (srv ProductTroubleService) DeleteProductTrouble(id int64) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
func (srv ProductTroubleService) ApproveProductTrouble(id int64) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
... ...
package domain
import (
"errors"
"time"
)
// 事故管理数据结构
type ProductTrouble struct {
Id int `json:"id"` // id
CompanyId int `json:"companyId"` //企业id
OrgId int `json:"orgId"` //组织ID
WorkStation WorkStation `json:"workStation"` // 工作位置
ProductWorker *User `json:"productWorker,omitempty"` // 生产工人
AmountLoss float64 `json:"amountLoss"` // 损失的金额
Types TroubleType `json:"types"` // 事故类型
RecordData time.Time `json:"recordData"` // 事故发生的日期
Remark string `json:"remakr"` // 备注
ApproveStatus ProductTroubleApprove `json:"approveStatus"` // 审核状态 1:未审核 2:已审核 3.自动审核
ApproveAt *time.Time `json:"approveAt"` // 审核时间
ApproveUser *User `json:"approveUser"` // 审核人
CreatedAt time.Time `json:"createdAt,omitempty"` // 创建时间
UpdatedAt time.Time `json:"updatedAt,omitempty"` // 更新时间
DeletedAt *time.Time `json:"deletedAt,omitempty"` // 删除时间
}
// 事故类型
type TroubleType int
// 事故类型 1 安全事故 ,2 质量事故, 3 金属事故 ,4 非金属事故
const (
TroubleType1 TroubleType = 1
TroubleType2 TroubleType = 2
TroubleType3 TroubleType = 3
TroubleType4 TroubleType = 4
)
// 事故管理 审核状态
type ProductTroubleApprove int
// 审核状态 1:未审核 2:已审核
const (
TroubleWaitApprove ProductTroubleApprove = 1
TroubleIsApprove ProductTroubleApprove = 2
)
type ProductTroubleRepository interface {
Save(param *ProductTrouble) (*ProductTrouble, error)
Remove(param *ProductTrouble) (*ProductTrouble, error)
FindOne(queryOptions map[string]interface{}) (*ProductTrouble, error)
Find(queryOptions map[string]interface{}) (int64, []*ProductTrouble, error)
}
func (m *ProductTrouble) SetTypes(v int) error {
troubleType := TroubleType(v)
switch troubleType {
case TroubleType1, TroubleType2, TroubleType3, TroubleType4:
m.Types = troubleType
default:
return errors.New("ProductTrouble.Types 值错误")
}
return nil
}
// 审核事故数据
func (m *ProductTrouble) Approve() error {
nowTime := time.Now()
switch m.ApproveStatus {
case TroubleIsApprove:
return errors.New("事故不需要重复审核")
default:
m.ApproveAt = &nowTime
m.ApproveStatus = TroubleIsApprove
}
return nil
}
... ...
package models
import (
"time"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
)
// 事故管理数据结构
type ProductTrouble struct {
tableName string `comment:" 事故管理" pg:"manufacture.product_trouble,alias:product_trouble"`
Id int ` pg:"pk:id"` // id
CompanyId int //企业id
OrgId int //组织ID
WorkStation domain.WorkStation // 工作位置
ProductWorker *domain.User // 生产工人
AmountLoss float64 // 损失的金额
Types int // 事故类型
RecordData time.Time // 事故发生的日期
Remark string // 备注
ApproveStatus int // 审核状态 1:未审核 2:已审核 3.自动审核
ApproveAt *time.Time // 审核时间
ApproveUser *domain.User // 审核人
CreatedAt time.Time // 创建时间
UpdatedAt time.Time // 更新时间
DeletedAt *time.Time // 删除时间
}
... ...
package repository
import (
"fmt"
"time"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
)
type ProductTroubleRepository struct {
transactionContext *pgTransaction.TransactionContext
}
var _ domain.ProductTroubleRepository = (*ProductTroubleRepository)(nil)
func NewProductTroubleRepository(transactionContext *pgTransaction.TransactionContext) (*ProductTroubleRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &ProductTroubleRepository{
transactionContext: transactionContext,
}, nil
}
}
func (repo *ProductTroubleRepository) Save(param *domain.ProductTrouble) (*domain.ProductTrouble, error) {
m := models.ProductTrouble{
Id: param.Id,
CompanyId: param.CompanyId,
OrgId: param.OrgId,
WorkStation: param.WorkStation,
ProductWorker: param.ProductWorker,
AmountLoss: param.AmountLoss,
Types: int(param.Types),
RecordData: param.RecordData,
Remark: param.Remark,
ApproveStatus: int(param.ApproveStatus),
ApproveAt: param.ApproveAt,
ApproveUser: param.ApproveUser,
CreatedAt: param.CreatedAt,
UpdatedAt: param.UpdatedAt,
DeletedAt: param.DeletedAt,
}
tx := repo.transactionContext.PgTx
if param.Id == 0 {
_, err := tx.Model(&m).Insert()
if err != nil {
return nil, err
}
param.Id = m.Id
} else {
_, err := tx.Model(&m).WherePK().Update()
if err != nil {
return nil, err
}
}
return param, nil
}
func (repo *ProductTroubleRepository) Remove(param *domain.ProductTrouble) (*domain.ProductTrouble, error) {
tx := repo.transactionContext.PgTx
m := new(models.ProductTrouble)
m.Id = param.Id
nowTime := time.Now()
param.DeletedAt = &nowTime
_, err := tx.Model(m).
WherePK().Set("deleted_at=?", nowTime).
Update()
if err != nil {
return param, err
}
return nil, nil
}
func (repo *ProductTroubleRepository) FindOne(queryOptions map[string]interface{}) (*domain.ProductTrouble, error) {
tx := repo.transactionContext.PgTx
m := new(models.ProductTrouble)
query := tx.Model(m).Where("deleted_at isnull")
if v, ok := queryOptions["id"]; ok {
query.Where("id=?", v)
}
err := query.First()
if err != nil {
if err == pg.ErrNoRows {
return nil, domain.ErrorNotFound
} else {
return nil, err
}
}
result := repo.TransformToDomain(m)
return result, nil
}
func (repo *ProductTroubleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ProductTrouble, error) {
tx := repo.transactionContext.PgTx
m := []models.ProductTrouble{}
query := tx.Model(&m).
Where("deleted_at isnull").
Limit(20)
if v, ok := queryOptions["limit"].(int); ok {
query.Limit(v)
}
if v, ok := queryOptions["offset"].(int); ok {
query.Offset(v)
}
cnt, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var listData []*domain.ProductTrouble
for i := range m {
temp := repo.TransformToDomain(&m[i])
listData = append(listData, temp)
}
return int64(cnt), listData, nil
}
func (repo *ProductTroubleRepository) TransformToDomain(param *models.ProductTrouble) *domain.ProductTrouble {
return &domain.ProductTrouble{
Id: param.Id,
CompanyId: param.CompanyId,
OrgId: param.OrgId,
WorkStation: param.WorkStation,
ProductWorker: param.ProductWorker,
AmountLoss: param.AmountLoss,
Types: domain.TroubleType(param.Types),
RecordData: param.RecordData,
Remark: param.Remark,
ApproveStatus: domain.ProductTroubleApprove(param.ApproveStatus),
ApproveAt: param.ApproveAt,
ApproveUser: param.ApproveUser,
CreatedAt: param.CreatedAt,
UpdatedAt: param.UpdatedAt,
DeletedAt: param.DeletedAt,
}
}
... ...
... ... @@ -55,6 +55,8 @@ func (controller *ExcelDataController) FileImport() {
// data, err = excelService.ImportCooperationUser(cmd)
//case domain.ImportOrganization:
// data, err = excelService.ImportOrganization(cmd)
case "ImportProductRecord":
data, err = excelService.ImportProductRecord(cmd)
case "ImportAttendance":
data, err = excelService.ImportDataAttendance(cmd)
default:
... ... @@ -112,7 +114,7 @@ func (controller *ExcelDataController) FileExport() {
controller.responseExcelByFile(controller.Ctx, excelTool, filename)
}
//GetExcelDataFields 获取导出excel数据的可选字段
// GetExcelDataFields 获取导出excel数据的可选字段
func (controller *ExcelDataController) GetExcelDataFields() {
}
... ...