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

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/base"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/base"
	model "LAPP_ACURA_MOM_BACKEND/models/base"
)

/******************************************************************************
 *
 * @Description              : WorkOrderIdentifyRuleDetail的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-30 14:27:09
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrderIdentifyRuleDetail = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkOrderIdentifyRuleDetail_CollerationBasis.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_CollerationBasis, grmi.Equal, true),
		meta.WorkOrderIdentifyRuleDetail_CollerationOperationId.UrlParameterName: grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_CollerationOperationId, grmi.Equal, true),
		meta.WorkOrderIdentifyRuleDetail_IdentifyRuleId.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_IdentifyRuleId, grmi.Equal, true),
		meta.WorkOrderIdentifyRuleDetail_PO.UrlParameterName:                     grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_PO, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_OpenPlan.UrlParameterName:               grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_OpenPlan, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_IdentifyPlan.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_IdentifyPlan, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_CombinePlan.UrlParameterName:            grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_CombinePlan, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_CollerationRuleId.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_CollerationRuleId, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_MapBy.UrlParameterName:                  grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_MapBy, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_StartCheckPos.UrlParameterName:          grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_StartCheckPos, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_CheckLength.UrlParameterName:            grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_CheckLength, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_Formula.UrlParameterName:                grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_Formula, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_OperatorSimbol.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_OperatorSimbol, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_CheckObject.UrlParameterName:            grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_CheckObject, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_AssociatedObject.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_AssociatedObject, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_AssociatedAttrCode.UrlParameterName:     grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_AssociatedAttrCode, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_AssociatedAttrValue.UrlParameterName: grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_AssociatedAttrValue, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_AssociatedProjectId.UrlParameterName:    grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_AssociatedProjectId, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_LastModify.UrlParameterName:             grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_LastModify, grmi.Approximate, false),
		meta.WorkOrderIdentifyRuleDetail_LastUser.UrlParameterName:               grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_LastUser, grmi.Equal, false),
		meta.WorkOrderIdentifyRuleDetail_CreateTime.UrlParameterName:             grmi.NewConditionItem(meta.WorkOrderIdentifyRuleDetail_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkOrderIdentifyRuleDetail的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-30 14:27:09
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrderIdentifyRuleDetailAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkOrderIdentifyRuleDetail.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkOrderIdentifyRuleDetailServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkOrderIdentifyRuleDetail的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-30 14:27:09
 *
 ******************************************************************************/
type WorkOrderIdentifyRuleDetailServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkOrderIdentifyRuleDetailDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkOrderIdentifyRuleDetailDAOImplement实例
 *
 * @Return Value             : WorkOrderIdentifyRuleDetailDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-30 14:27:09
 *
 ******************************************************************************/
func NewWorkOrderIdentifyRuleDetailServiceImplement() *WorkOrderIdentifyRuleDetailServiceImplement {
	return &WorkOrderIdentifyRuleDetailServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkOrderIdentifyRuleDetailService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkOrderIdentifyRuleDetailServiceImplement) InsertOne(user *global.User, entity *model.WorkOrderIdentifyRuleDetail) error {

	grmi.Log(user, "/services/base/implments/WorkOrderIdentifyRuleDetail.service.impl.go", "InsertOneWorkOrderIdentifyRuleDetail", "插入一个WorkOrderIdentifyRuleDetail")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderIdentifyRuleDetailDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.CollerationBasis, entity.CollerationOperationId, entity.IdentifyRuleId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkOrderIdentifyRuleDetailService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkOrderIdentifyRuleDetailServiceImplement) DeleteOne(user *global.User, collerationBasis string, collerationOperationId int, identifyRuleId int) error {

	grmi.Log(user, "/services/base/implments/WorkOrderIdentifyRuleDetail.service.impl.go", "DeleteOneWorkOrderIdentifyRuleDetail", "删除一个WorkOrderIdentifyRuleDetail")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderIdentifyRuleDetailDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(collerationBasis, collerationOperationId, identifyRuleId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(collerationBasis, collerationOperationId, identifyRuleId)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkOrderIdentifyRuleDetailService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkOrderIdentifyRuleDetailServiceImplement) SelectOne(user *global.User, collerationBasis string, collerationOperationId int, identifyRuleId int) (*model.WorkOrderIdentifyRuleDetail, error) {

	grmi.Log(user, "/services/base/implments/WorkOrderIdentifyRuleDetail.service.impl.go", "SelectOneWorkOrderIdentifyRuleDetail", "查询一个WorkOrderIdentifyRuleDetail")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderIdentifyRuleDetailDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(collerationBasis, collerationOperationId, identifyRuleId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkOrderIdentifyRuleDetailService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkOrderIdentifyRuleDetailServiceImplement) UpdateOne(user *global.User, entity *model.WorkOrderIdentifyRuleDetail) error {

	grmi.Log(user, "/services/base/implments/WorkOrderIdentifyRuleDetail.service.impl.go", "UpdateOneWorkOrderIdentifyRuleDetail", "修改一个WorkOrderIdentifyRuleDetail")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderIdentifyRuleDetailDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.CollerationBasis, entity.CollerationOperationId, entity.IdentifyRuleId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkOrderIdentifyRuleDetailService.Insert
 *
 ******************************************************************************/
func (impl *WorkOrderIdentifyRuleDetailServiceImplement) Insert(user *global.User, entities *[]model.WorkOrderIdentifyRuleDetail) error {

	grmi.Log(user, "/services/base/implments/WorkOrderIdentifyRuleDetail.service.impl.go", "InsertWorkOrderIdentifyRuleDetail", "插入多个WorkOrderIdentifyRuleDetail")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderIdentifyRuleDetailDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.CollerationBasis, entity.CollerationOperationId, entity.IdentifyRuleId)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

	err := dao.Insert(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkOrderIdentifyRuleDetailService.Delete
 *
 ******************************************************************************/
func (impl *WorkOrderIdentifyRuleDetailServiceImplement) Delete(user *global.User, entities *[]model.WorkOrderIdentifyRuleDetail) error {

	grmi.Log(user, "/services/base/implments/WorkOrderIdentifyRuleDetail.service.impl.go", "DeleteWorkOrderIdentifyRuleDetail", "删除多个WorkOrderIdentifyRuleDetail")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderIdentifyRuleDetailDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.CollerationBasis, entity.CollerationOperationId, entity.IdentifyRuleId)
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
	}

	err := dao.Delete(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkOrderIdentifyRuleDetailService.Select
 *
 ******************************************************************************/
func (impl *WorkOrderIdentifyRuleDetailServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.WorkOrderIdentifyRuleDetail, error) {

	grmi.Log(user, "/services/base/implments/WorkOrderIdentifyRuleDetail.service.impl.go", "SelectWorkOrderIdentifyRuleDetail", "查询WorkOrderIdentifyRuleDetail")

	condition := DefaultConditionOfWorkOrderIdentifyRuleDetailAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewWorkOrderIdentifyRuleDetailDAO(session, user.PlantNr, user.UserId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkOrderIdentifyRuleDetailService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *WorkOrderIdentifyRuleDetailServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

	grmi.Log(user, "/services/base/implments/WorkOrderIdentifyRuleDetail.service.impl.go", "SelectWorkOrderIdentifyRuleDetail", "查询WorkOrderIdentifyRuleDetail")

	condition := DefaultConditionOfWorkOrderIdentifyRuleDetailAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewWorkOrderIdentifyRuleDetailDAO(session, user.PlantNr, user.UserId)
	condition.Fill(urlParameters)
	result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkOrderIdentifyRuleDetailService.Update
 *
 ******************************************************************************/
func (impl *WorkOrderIdentifyRuleDetailServiceImplement) Update(user *global.User, entities *[]model.WorkOrderIdentifyRuleDetail) error {

	grmi.Log(user, "/services/base/implments/WorkOrderIdentifyRuleDetail.service.impl.go", "UpdateWorkOrderIdentifyRuleDetail", "修改多个WorkOrderIdentifyRuleDetail")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderIdentifyRuleDetailDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.CollerationBasis, entity.CollerationOperationId, entity.IdentifyRuleId)
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
	}

	err := dao.Update(entities)
	if err != nil {
		return err
	}
	return nil
}
