// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	"LAPP_ACURA_MOM_BACKEND/grmi"
	baseMeta "LAPP_ACURA_MOM_BACKEND/meta/base"
	meta "LAPP_ACURA_MOM_BACKEND/meta/om"
	baseModel "LAPP_ACURA_MOM_BACKEND/models/base"
	model "LAPP_ACURA_MOM_BACKEND/models/om"
	"fmt"
	"github.com/go-xorm/xorm"
	"xorm.io/core"
)

/******************************************************************************
 *
 * @Struct Name              : SerialOrderOPDetailRecvDataLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : SerialOrderOPDetailRecvDataLst的数据访问对象实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-09-16 14:27:35
 *
 ******************************************************************************/
type SerialOrderOPDetailRecvDataLstDAOImplement struct {
	session *xorm.Session
	meta    *grmi.Entity
	plantNr int
	userid  string
}

/******************************************************************************
 *
 * @Function Name            : NewSerialOrderOPDetailRecvDataLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SerialOrderOPDetailRecvDataLstDAOImplement实例
 *
 * @Function Parameters      : xorm会话
 *
 * @Function Parameters      : 基本主键
 *
 * @Return Value             : SerialOrderOPDetailRecvDataLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-09-16 14:27:35
 *
 ******************************************************************************/
func NewSerialOrderOPDetailRecvDataLstDAOImplement(session *xorm.Session, plantNr int, userid string) *SerialOrderOPDetailRecvDataLstDAOImplement {
	return &SerialOrderOPDetailRecvDataLstDAOImplement{session, &meta.SerialOrderOPDetailRecvDataLst, plantNr, userid}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.InsertOne
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) InsertOne(entity *model.SerialOrderOPDetailRecvDataLst) error {
	entity.PlantNr = impl.plantNr
	entity.LastUser = impl.userid
	_, err := impl.session.Table(impl.meta.TableName).Insert(entity)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.DeleteOne
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) DeleteOne(pos int64) error {
	_, err := impl.session.Table(impl.meta.TableName).ID(core.PK{impl.plantNr, pos}).Delete(impl.meta.Indicator)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}

	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.SelectOne
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) SelectOne(pos int64) (*model.SerialOrderOPDetailRecvDataLst, error) {
	var data model.SerialOrderOPDetailRecvDataLst
	ok, err := impl.session.Table(impl.meta.TableName).ID(core.PK{impl.plantNr, pos}).Get(&data)
	if err != nil {
		return nil, grmi.NewDataBaseError(err)
	}
	if !ok {
		return nil, nil
	}
	return &data, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.UpdateOne
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) UpdateOne(entity *model.SerialOrderOPDetailRecvDataLst) error {
	entity.PlantNr = impl.plantNr
	entity.LastUser = impl.userid
	_, err := impl.session.Table(impl.meta.TableName).Cols(impl.meta.UpdatingMembers...).ID(entity.GetKey()).Update(entity)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.Insert
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) Insert(entities *[]model.SerialOrderOPDetailRecvDataLst) error {
	for i := 0; i < len(*entities); i++ {
		entity := &(*entities)[i]
		entity.PlantNr = impl.plantNr
		entity.LastUser = impl.userid
	}
	_, err := impl.session.Table(impl.meta.TableName).Insert(entities)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.Delete
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) Delete(entities *[]model.SerialOrderOPDetailRecvDataLst) error {
	for _, entity := range *entities {
		entity.PlantNr = impl.plantNr
		_, err := impl.session.Table(impl.meta.TableName).ID(entity.GetKey()).Delete(impl.meta.Indicator)
		if err != nil {
			return grmi.NewDataBaseError(err)
		}
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.DeleteWhere
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) DeleteWhere(predicates []grmi.Predicate) error {

	parameters := []interface{}{impl.plantNr}
	where := fmt.Sprintf("%s = ?", meta.SerialOrderOPDetailRecvDataLst_PlantNr.ColumnName)

	session := impl.session.Table(impl.meta.TableName)
	if predicates != nil {
		for _, predicate := range predicates {
			where += predicate.Build()
			parameters = append(parameters, predicate.Values...)
		}
		session = session.Where(where, parameters...)
	} else {
		session = session.Where(where, parameters...)
	}

	_, err := session.Delete(impl.meta.Indicator)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.Select
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) Select(predicates []grmi.Predicate, orderByFields []grmi.Field) ([]model.SerialOrderOPDetailRecvDataLst, error) {

	parameters := []interface{}{impl.plantNr}
	where := fmt.Sprintf("%s = ?", meta.SerialOrderOPDetailRecvDataLst_PlantNr.ColumnName)

	session := impl.session.Table(impl.meta.TableName)
	if predicates != nil {
		for _, predicate := range predicates {
			where += predicate.Build()
			parameters = append(parameters, predicate.Values...)
		}
		session = session.Where(where, parameters...)
	} else {
		session = session.Where(where, parameters...)
	}

	session = session.OrderBy(meta.SerialOrderOPDetailRecvDataLst_PlantNr.ColumnName)
	if orderByFields != nil {
		for _, field := range orderByFields {
			session = session.OrderBy(field.ColumnName)
		}
	}

	data := make([]model.SerialOrderOPDetailRecvDataLst, 0, 10)
	err := session.Find(&data)
	if err != nil {
		return nil, grmi.NewDataBaseError(err)
	}
	return data, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.SelectAndPaging
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) SelectAndPaging(paging *grmi.Paging, predicates []grmi.Predicate, orderByFields []grmi.Field) (grmi.PagingResult, error) {

	parameters := []interface{}{impl.plantNr}
	where := fmt.Sprintf("%s = ?", meta.SerialOrderOPDetailRecvDataLst_PlantNr.ColumnName)

	session := impl.session.Table(impl.meta.TableName)
	if predicates != nil {
		for _, predicate := range predicates {
			where += predicate.Build()
			parameters = append(parameters, predicate.Values...)
		}
		session = session.Where(where, parameters...)
	} else {
		session = session.Where(where, parameters...)
	}

	count, err := session.Count(impl.meta.Indicator)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}

	orderBy := " order by " + meta.SerialOrderOPDetailRecvDataLst_PlantNr.ColumnName
	if orderByFields != nil {
		for _, field := range orderByFields {
			if orderBy == " order by " {
				orderBy += field.ColumnName
			} else {
				orderBy += ", " + field.ColumnName
			}
		}
	}
	parameters = append(parameters, paging.Offset(), paging.Size)
	data := make([]model.SerialOrderOPDetailRecvDataLst, 0, 10)
	where = " where " + where
	err = impl.session.Table(impl.meta.TableName).SQL("select * from "+impl.meta.TableName+where+orderBy+" offset ? row fetch next ? row only", parameters...).Find(&data)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	return grmi.PagingResult{Records: data, Count: count, PageNumber: paging.Number, PageSize: paging.Size}, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.Update
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) Update(entities *[]model.SerialOrderOPDetailRecvDataLst) error {
	for i := 0; i < len(*entities); i++ {
		entity := &(*entities)[i]
		entity.PlantNr = impl.plantNr
		entity.LastUser = impl.userid
		_, err := impl.session.Table(impl.meta.TableName).Cols(impl.meta.UpdatingMembers...).ID(entity.GetKey()).Update(entity)
		if err != nil {
			return grmi.NewDataBaseError(err)
		}
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.UpdateWhere
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) UpdateWhere(predicates []grmi.Predicate, entity *model.SerialOrderOPDetailRecvDataLst, columns ...string) error {

	parameters := []interface{}{impl.plantNr}
	where := fmt.Sprintf("%s = ?", meta.SerialOrderOPDetailRecvDataLst_PlantNr.ColumnName)

	session := impl.session.Table(impl.meta.TableName)
	if predicates != nil {
		for _, predicate := range predicates {
			where += predicate.Build()
			parameters = append(parameters, predicate.Values...)
		}
		session = session.Where(where, parameters...)
	} else {
		session = session.Where(where, parameters...)
	}

	columns = append(columns, meta.SerialOrderOPDetailRecvDataLst_LastUser.Name)
	entity.LastUser = impl.userid
	_, err := session.Cols(columns...).Update(entity)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.SelectOPDetailData
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) SelectOPDetailData(serialOrderId string, workLineId string) ([]model.SerialOrderOPData, error) {
	data := make([]model.SerialOrderOPData, 0, 10)
	sql := `SELECT recvData.*, OM_SerialOrderOPDetail.*, OPCComRule.*, Operation.* FROM ( SELECT * FROM OM_SerialOrderOPDetailRecvDataLst WHERE OM_SerialOrderOPDetailRecvDataLst.PlantNr = ? and OM_SerialOrderOPDetailRecvDataLst.SerialOrderId = ? ) recvData LEFT JOIN OM_SerialOrderOPDetail ON recvData.PlantNr = OM_SerialOrderOPDetail.PlantNr AND recvData.SerialOrderId = OM_SerialOrderOPDetail.SerialOrderId AND recvData.PO = OM_SerialOrderOPDetail.PO AND recvData.StepNo = OM_SerialOrderOPDetail.StepNo AND recvData.OriginPos = OM_SerialOrderOPDetail.Pos LEFT JOIN Operation ON OM_SerialOrderOPDetail.PlantNr = Operation.PlantNr AND OM_SerialOrderOPDetail.OperationId = Operation.OperationId LEFT JOIN OPCComRule ON Operation.PlantNr = OPCComRule.PlantNr AND Operation.OperationId = OPCComRule.OperationId LEFT JOIN WorkLineProcessLst ON recvData.PlantNr = WorkLineProcessLst.PlantNr AND recvData.PO = WorkLineProcessLst.PO WHERE WorkLineProcessLst.WorkLineId = ? ORDER BY WorkLineProcessLst.Pos, recvData.StepNo, recvData.OriginPos`
	err := impl.session.Table(impl.meta.TableName).SQL(sql, impl.plantNr, serialOrderId, workLineId).Find(&data)
	if err != nil {
		return nil, grmi.NewDataBaseError(err)
	}
	return data, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.SelectOPCReadData
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) SelectOPCReadData(serialOrderId string, projectId string) ([]model.RecvDataWithOPC, error) {
	result := make([]model.RecvDataWithOPC, 0, 10)
	condition := fmt.Sprintf("%s = %s and %s = %s and %s = %s and %s = %s",
		meta.SerialOrderOPDetailRecvDataLst_PlantNr.ColumnName,
		baseMeta.OPCComRule_PlantNr.ColumnName,
		meta.SerialOrderOPDetailRecvDataLst_PO.ColumnName,
		baseMeta.OPCComRule_PO.ColumnName,
		meta.SerialOrderOPDetailRecvDataLst_StepNo.ColumnName,
		baseMeta.OPCComRule_StepNo.ColumnName,
		meta.SerialOrderOPDetailRecvDataLst_OriginRuleId.ColumnName,
		baseMeta.OPCComRule_RuleId.ColumnName)
	session := impl.session.Table(meta.SerialOrderOPDetailRecvDataLst.TableName).Join("LEFT", baseMeta.OPCComRule.TableName, condition)
	where := fmt.Sprintf("%s = ? and %s = ? and %s = ?",
		meta.SerialOrderOPDetailRecvDataLst_SerialOrderId.ColumnName,
		baseMeta.OPCComRule_ProjectId.ColumnName,
		baseMeta.OPCComRule_ActionType.ColumnName)
	parameters := []interface{}{impl.plantNr, serialOrderId, projectId, baseModel.ACTION_TYPE_READ}
	err := session.Where(where, parameters...).Find(&result)
	return result, err
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.GetOpRecvDataByPO
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) GetOpRecvDataByPO(serialOrderId string, poId string) ([]model.OperationDetailTraceData, error) {
	data := make([]model.OperationDetailTraceData, 0, 10)
	detailCondition := fmt.Sprintf("%s = %s and %s = %s and %s = %s and %s = %s and %s = %s",
		meta.SerialOrderOPDetailRecvDataLst_PlantNr.ColumnName,
		meta.SerialOrderOPDetail_PlantNr.ColumnName,
		meta.SerialOrderOPDetailRecvDataLst_SerialOrderId.ColumnName,
		meta.SerialOrderOPDetail_SerialOrderId.ColumnName,
		meta.SerialOrderOPDetailRecvDataLst_PO.ColumnName,
		meta.SerialOrderOPDetail_PO.ColumnName,
		meta.SerialOrderOPDetailRecvDataLst_StepNo.ColumnName,
		meta.SerialOrderOPDetail_StepNo.ColumnName,
		meta.SerialOrderOPDetailRecvDataLst_OriginPos.ColumnName,
		meta.SerialOrderOPDetail_Pos.ColumnName)
	opcCondition := fmt.Sprintf("%s = %s and %s = %s and %s = %s",
		meta.SerialOrderOPDetail_PlantNr.ColumnName,
		baseMeta.OPCComRule_PlantNr.ColumnName,
		meta.SerialOrderOPDetail_OperationId.ColumnName,
		baseMeta.OPCComRule_OperationId.ColumnName,
		meta.SerialOrderOPDetail_IntPara5.ColumnName,
		baseMeta.OPCComRule_RuleId.ColumnName)
	operationCondition := fmt.Sprintf("%s = %s and %s = %s", meta.SerialOrderOPDetail_PlantNr.ColumnName,
		baseMeta.Operation_PlantNr.ColumnName,
		meta.SerialOrderOPDetail_OperationId.ColumnName,
		baseMeta.Operation_OperationId.ColumnName)
	tightenWrenchCondition := fmt.Sprintf("%s = %s and %s = %s and %s = %s",
		meta.SerialOrderOPDetail_PlantNr.ColumnName,
		baseMeta.TightenWrenchRuleInfo_PlantNr.ColumnName,
		meta.SerialOrderOPDetail_OperationId.ColumnName,
		baseMeta.TightenWrenchRuleInfo_OperationId.ColumnName,
		meta.SerialOrderOPDetail_IntPara5.ColumnName,
		baseMeta.TightenWrenchRuleInfo_RuleId.ColumnName)
	session := impl.session.Table(impl.meta.TableName).
		Join("LEFT", meta.SerialOrderOPDetail.TableName, detailCondition).
		Join("LEFT", baseMeta.OPCComRule.TableName, opcCondition).
		Join("LEFT", baseMeta.Operation.TableName, operationCondition).
		Join("LEFT", baseMeta.TightenWrenchRuleInfo.TableName, tightenWrenchCondition)
	parameters := []interface{}{impl.plantNr, serialOrderId, poId}
	where := fmt.Sprintf("%s = ? and %s = ? and %s = ?",
		meta.SerialOrderOPDetailRecvDataLst_PlantNr.ColumnName,
		meta.SerialOrderOPDetailRecvDataLst_SerialOrderId.ColumnName,
		meta.SerialOrderOPDetailRecvDataLst_PO.ColumnName)

	err := session.Select("OM_SerialOrderOPDetailRecvDataLst.*,OM_SerialOrderOPDetail.*,OPCComRule.*,Operation.*,TightenWrenchRuleInfo.*").Where(where, parameters...).Asc(meta.SerialOrderOPDetailRecvDataLst_StepNo.ColumnName).Asc(meta.SerialOrderOPDetailRecvDataLst_OriginPos.ColumnName).Asc(meta.SerialOrderOPDetailRecvDataLst_CreateTime.ColumnName).Find(&data)
	if err != nil {
		return nil, grmi.NewDataBaseError(err)
	}
	return data, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/om/SerialOrderOPDetailRecvDataLstDAO.SelectOneByAssembly
 *
 ******************************************************************************/
func (impl *SerialOrderOPDetailRecvDataLstDAOImplement) SelectOneByAssembly(RecvData string) (*model.SerialOrderOPDetailRecvDataLst, error) {
	var data model.SerialOrderOPDetailRecvDataLst
	ok, err := impl.session.Table(impl.meta.TableName).Where("RecvData = ? and StepType = ?", RecvData, "扫码校验").Get(&data)
	if err != nil {
		return nil, grmi.NewDataBaseError(err)
	}
	if !ok {
		return nil, nil
	}
	return &data, nil
}

