workshop_plan_completion_dao.go 3.0 KB
package dao

import (
	"fmt"
	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"
	"time"
)

type WorkshopPlanCompletionRecordDao struct {
	transactionContext *pgTransaction.TransactionContext
}

func (dao *WorkshopPlanCompletionRecordDao) Records(companyId, orgId int, workshopId int, begin, end time.Time, records interface{}) error {
	tx := dao.transactionContext.PgTx
	sql := fmt.Sprintf(`
with product_record as (
select 
(case when product_record_type=1 then  cast(product_record_info->>'weigh' as DECIMAL) 
ELSE -(cast(product_record_info->>'weigh' as DECIMAL))
END) as weight,
cast(product_record_info->>'productPlanId' as INTEGER) plan_id
--,created_at
from manufacture.product_records
where company_id = ?
and org_id =?
and work_station->>'workshopId'='?'
and work_station->>'sectionName'='包装'
and product_record_type in (1,2,8)
and created_at>=? 
and created_at<?
), product_record_sum as(
	select sum(weight) weight,plan_id from product_record
	GROUP BY plan_id
)
select
plan_devoted->>'weight' plan_weight -- 计划重量
, coalesce(b.weight,0)    real_weight            --批次实际产能
,a.product_plan_id
,a.product_date
from manufacture.product_plan  a left join product_record_sum b  on a.product_plan_id = b.plan_id
where company_id = ?
and org_id =?
and workshop->>'workshopId'='?'
and product_date >=?
--and created_at<?
--order by created_at desc`)
	if _, err := tx.Query(records, sql,
		companyId, orgId, workshopId, begin, end,
		companyId, orgId, workshopId, begin, end); err != nil {
		return err
	}
	return nil
}

func (dao *WorkshopPlanCompletionRecordDao) FindOne(companyId, orgId int, workshopId int, begin time.Time) (*models.WorkshopPlanCompletionRecord, error) {
	tx := dao.transactionContext.PgTx
	var record = new(models.WorkshopPlanCompletionRecord)
	q := tx.Model(record)
	q.Where("company_id=?", companyId)
	q.Where("org_id=?", orgId)
	q.Where("workshop_id=?", workshopId)
	q.Where("created_at=?", begin)
	err := q.First()
	if err != nil {
		if err.Error() == "pg: no rows in result set" {
			return nil, domain.ErrorNotFound
		} else {
			return nil, err
		}
	}
	return record, nil
}

func (dao *WorkshopPlanCompletionRecordDao) Save(record *models.WorkshopPlanCompletionRecord) error {
	tx := dao.transactionContext.PgTx
	if _, err := tx.Model(record).Insert(); err != nil {
		return err
	}
	return nil
}

func (dao *WorkshopPlanCompletionRecordDao) Update(record *models.WorkshopPlanCompletionRecord) error {
	tx := dao.transactionContext.PgTx
	if _, err := tx.Model(record).WherePK().Update(); err != nil {
		return err
	}
	return nil
}

func NewWorkshopPlanCompletionRecordDao(transactionContext *pgTransaction.TransactionContext) (*WorkshopPlanCompletionRecordDao, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &WorkshopPlanCompletionRecordDao{
			transactionContext: transactionContext,
		}, nil
	}
}