reward_summary_dao.go 7.5 KB
package dao

import (
	"fmt"

	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
)

type RewardSumaryDao struct {
	transactionContext *pgTransaction.TransactionContext
}

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

// 功过看板-日榜
type RewardSummaryDay struct {
	Id                 int    `json:"id"` //id
	RecordDateStr      string `json:"record_date_str"`
	SectionName        string `json:"sectionName"`        //工段名称
	WorkerName         string `json:"workerName"`         //员工名称
	SummaryResult      string `json:"summaryResult"`      //奖惩结果
	YieldResult        string `json:"yieldResult"`        //产效功过结果
	UpToStandardResult string `json:"upToStandardResult"` //合格率功过结果
	AccidentResult1    string `json:"accidentResult1"`    //质量事故功过结果
	AccidentResult2    string `json:"accidentResul2"`     //安全事故功过结果
	AccidentResult3    string `json:"accidentResul3"`     //异物 功过结果
}

// 功过看板-日榜
func (d *RewardSumaryDao) SeachRewardSummaryDay(companyId int, orgId int, recordDate string) ([]RewardSummaryDay, error) {
	sqlStr := `SELECT 
	"id",record_date_str, 
	work_station->>'sectionName' as section_name,
	worker->>'userName' as worker_name,
	summary_result,
	yield_result,
	up_to_standard_result,
	accident_result1,
	accident_result2,
	accident_result3
	FROM manufacture."reward_summary"
	WHERE company_id=?
	and org_id=?
	and record_date_str=?`
	tx := d.transactionContext.PgTx
	var result []RewardSummaryDay
	_, err := tx.Query(&result, sqlStr, companyId, orgId, recordDate)
	return result, err
}

// 功过看板-产效
type RewardSummaryYield struct {
	RecordDateStr string `json:"record_date_str"`
	SectionName   string `json:"sectionName"` //工段名称
	WorkerName    string `json:"workerName"`  //员工名称
	Yield         string `json:"yield"`       //产效
	YieldResult   string `json:"yieldResult"` //产效功过结果
	YieldMax      string `json:"yildMax"`     //产效历史最佳
}

// 功过看板-产效
func (d *RewardSumaryDao) SeachRewardSummaryYield(companyId int, orgId int, recordDate string) ([]RewardSummaryYield, error) {
	sqlStr := `SELECT 
	record_date_str, 
	work_station->>'sectionName' as section_name,
	worker->>'userName' as worker_name,
	yield_result,
	yield,
	'24' as yield_max
	FROM manufacture."reward_summary"
	WHERE company_id=?
	and org_id=?
	and record_date_str=?`
	tx := d.transactionContext.PgTx
	var result []RewardSummaryYield
	_, err := tx.Query(&result, sqlStr, companyId, orgId, recordDate)
	return result, err

}

// 功过看板-合格率
type RewardSummaryUpToStandard struct {
	RecordDateStr      string `json:"record_date_str"`
	SectionName        string `json:"sectionName"`        //工段名称
	WorkerName         string `json:"workerName"`         //员工名称
	UpToStandard       string `json:"upToStandard"`       //合格率
	UpToStandardResult string `json:"upToStandardResult"` //合格率功过结果
	UpToStandardMax    string `json:"upToStandardMax"`    //合格率历史最佳
}

// 功过看板-合格率
func (d *RewardSumaryDao) SeachRewardUpToStandard(companyId int, orgId int, recordDate string) ([]RewardSummaryUpToStandard, error) {
	sqlStr := `SELECT 
	record_date_str, 
	work_station->>'sectionName' as section_name,
	worker->>'userName' as worker_name,
	up_to_standard_result,
	up_to_standard,
	'90' as up_to_standard_max
	FROM manufacture."reward_summary"
	WHERE company_id=23
	and org_id=487
	and record_date_str='2022-10-29'`
	tx := d.transactionContext.PgTx
	var result []RewardSummaryUpToStandard
	_, err := tx.Query(&result, sqlStr, companyId, orgId, recordDate)
	return result, err
}

// 功过看板-异常
type RewardAccident struct {
	Id                 string `json:"id"`
	RecordDateStr      string `json:"record_date_str"`
	SectionName        string `json:"sectionName"`        //工段名称
	WorkerName         string `json:"workerName"`         //员工名称
	SummaryResult      string `json:"summaryResult"`      //奖惩结果
	YieldResult        string `json:"yieldResult"`        //产效功过结果
	UpToStandardResult string `json:"upToStandardResult"` //合格率功过结果
	AccidentResult1    string `json:"accidentResult1"`    //质量事故功过结果
	AccidentResult2    string `json:"accidentResul2"`     //安全事故功过结果
	AccidentResult3    string `json:"accidentResul3"`     //异物 功过结果
}

// 功过看板-异常
func (d *RewardSumaryDao) SeachRewardAccident(companyId int, orgId int, recordDate string) ([]RewardAccident, error) {
	sqlStr := `SELECT 
	"id",record_date_str, 
	work_station->>'sectionName' as section_name,
	worker->>'userName' as worker_name,
	up_to_standard_result,
	up_to_standard,
	'90' as up_to_standard_max
	FROM manufacture."reward_summary"
	WHERE company_id=?
	and org_id=?
	and record_date_str=?`
	tx := d.transactionContext.PgTx
	var result []RewardAccident
	_, err := tx.Query(&result, sqlStr, companyId, orgId, recordDate)
	return result, err
}

// 功过看板-异常
type RewardSummaryMonth struct {
	SectionId       string `json:"sectionId"`       //工段名称
	SectionName     string `json:"sectionName"`     //工段名称
	WorkerName      string `json:"workerName"`      //员工名称
	WorkerId        string `json:"workerId"`        //员工名称
	AccidentAmount1 string `json:"accidentAmount1"` //质量事故损失金额
	AccidentNum1    string `json:"accidentNum1"`    //质量事故次数
	AccidentNum3    string `json:"accidentNum3"`    //金属异物事故  次数
	AccidentNum4    string `json:"accidentNum4"`    //非属异物事故  次数
	AccidentAmount2 string `json:"accidentAmount2"` //安全事故 损失金额
	AccidentNum2    string `json:"accidentNum2"`    //安全事故 次数
	YieldAvg        string `json:"yieldAvg"`        //平均产效
	YieldMax        string `json:"yieldMax"`        //产效历史最佳
	UpToStandard    string `json:"upToStandard"`    //合格率
	AmountFine      string `json:"amountFine"`      //惩,的金额
	AmountReward    string `json:"amountReward"`    //奖,的金额
	AmountFinal     string `json:"amountFinal"`     //最终的 奖惩结果
}

// 功过看板-月榜
func (d *RewardSumaryDao) SeachRewardSummaryMonth(companyId int, orgId int, recordMonth string) ([]RewardSummaryMonth, error) {
	sqlStr := `SELECT 
work_station->>'sectionId' AS section_id,
work_station->>'sectionName' as section_name,
worker->>'userId' as worker_id,
worker->>'userName' as worker_name,
count(accident_num1) as accident_num1,
count(accident_num2) as accident_num2,
count(accident_num3) as accident_num3,
count(accident_num4) as accident_num4,
count(accident_amount1) as accident_amount1,
count(accident_amount2) as accident_amount2,
'12' as yield_avg,
'13' as yield_max,
'-39' as amount_fine,
'25' as amount_reward,
'-14' as amount_final,
'12' as up_to_standard,
 FROM manufacture.reward_summary
 WHERE company_id=?
 and org_id=?
 and record_month_str=?
GROUP BY 
worker->>'userId' ,
worker->>'userName' ,
work_station->>'sectionId',
work_station->>'sectionName' `
	tx := d.transactionContext.PgTx
	var result []RewardSummaryMonth
	_, err := tx.Query(&result, sqlStr, companyId, orgId, recordMonth)
	return result, err
}