// 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              : TightenWrenchRuleInfo的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-23 11:13:02
 *
 ******************************************************************************/
var DefaultConditionOfTightenWrenchRuleInfo = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.TightenWrenchRuleInfo_OperationId.UrlParameterName:       grmi.NewConditionItem(meta.TightenWrenchRuleInfo_OperationId, grmi.Equal, true),
		meta.TightenWrenchRuleInfo_WorkPlaceNr.UrlParameterName:       grmi.NewConditionItem(meta.TightenWrenchRuleInfo_WorkPlaceNr, grmi.Equal, true),
		meta.TightenWrenchRuleInfo_RuleId.UrlParameterName:            grmi.NewConditionItem(meta.TightenWrenchRuleInfo_RuleId, grmi.Equal, true),
		meta.TightenWrenchRuleInfo_PO.UrlParameterName:                grmi.NewConditionItem(meta.TightenWrenchRuleInfo_PO, grmi.Equal, false),
		meta.TightenWrenchRuleInfo_StepNo.UrlParameterName:            grmi.NewConditionItem(meta.TightenWrenchRuleInfo_StepNo, grmi.Equal, false),
		meta.TightenWrenchRuleInfo_StepDesc.UrlParameterName:          grmi.NewConditionItem(meta.TightenWrenchRuleInfo_StepDesc, grmi.Equal, false),
		meta.TightenWrenchRuleInfo_ProjectId.UrlParameterName:         grmi.NewConditionItem(meta.TightenWrenchRuleInfo_ProjectId, grmi.Equal, false),
		meta.TightenWrenchRuleInfo_MainControlAction.UrlParameterName: grmi.NewConditionItem(meta.TightenWrenchRuleInfo_MainControlAction, grmi.Equal, false),
		meta.TightenWrenchRuleInfo_ControlId.UrlParameterName:         grmi.NewConditionItem(meta.TightenWrenchRuleInfo_ControlId, grmi.Equal, false),
		meta.TightenWrenchRuleInfo_ControlTime.UrlParameterName:       grmi.NewConditionItem(meta.TightenWrenchRuleInfo_ControlTime, grmi.Equal, false),
		meta.TightenWrenchRuleInfo_LastModify.UrlParameterName:        grmi.NewConditionItem(meta.TightenWrenchRuleInfo_LastModify, grmi.Approximate, false),
		meta.TightenWrenchRuleInfo_LastUser.UrlParameterName:          grmi.NewConditionItem(meta.TightenWrenchRuleInfo_LastUser, grmi.Equal, false),
		meta.TightenWrenchRuleInfo_CreateTime.UrlParameterName:        grmi.NewConditionItem(meta.TightenWrenchRuleInfo_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : TightenWrenchRuleInfo的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-23 11:13:02
 *
 ******************************************************************************/
var DefaultConditionOfTightenWrenchRuleInfoAndPaging = grmi.NewCondition(
	DefaultConditionOfTightenWrenchRuleInfo.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : TightenWrenchRuleInfoServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : TightenWrenchRuleInfo的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-23 11:13:02
 *
 ******************************************************************************/
type TightenWrenchRuleInfoServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewTightenWrenchRuleInfoDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个TightenWrenchRuleInfoDAOImplement实例
 *
 * @Return Value             : TightenWrenchRuleInfoDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-23 11:13:02
 *
 ******************************************************************************/
func NewTightenWrenchRuleInfoServiceImplement() *TightenWrenchRuleInfoServiceImplement {
	return &TightenWrenchRuleInfoServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/TightenWrenchRuleInfoService.InsertOne
 *
 ******************************************************************************/
func (impl *TightenWrenchRuleInfoServiceImplement) InsertOne(user *global.User, entity *model.TightenWrenchRuleInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewTightenWrenchRuleInfoDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr, entity.RuleId)
	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/TightenWrenchRuleInfoService.DeleteOne
 *
 ******************************************************************************/
func (impl *TightenWrenchRuleInfoServiceImplement) DeleteOne(user *global.User, operationId int, workPlaceNr int, ruleId int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/TightenWrenchRuleInfoService.SelectOne
 *
 ******************************************************************************/
func (impl *TightenWrenchRuleInfoServiceImplement) SelectOne(user *global.User, operationId int, workPlaceNr int, ruleId int) (*model.TightenWrenchRuleInfo, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewTightenWrenchRuleInfoDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(operationId, workPlaceNr, ruleId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/TightenWrenchRuleInfoService.UpdateOne
 *
 ******************************************************************************/
func (impl *TightenWrenchRuleInfoServiceImplement) UpdateOne(user *global.User, entity *model.TightenWrenchRuleInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewTightenWrenchRuleInfoDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr, entity.RuleId)
	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/TightenWrenchRuleInfoService.Insert
 *
 ******************************************************************************/
func (impl *TightenWrenchRuleInfoServiceImplement) Insert(user *global.User, entities *[]model.TightenWrenchRuleInfo) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr, entity.RuleId)
		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/TightenWrenchRuleInfoService.Delete
 *
 ******************************************************************************/
func (impl *TightenWrenchRuleInfoServiceImplement) Delete(user *global.User, entities *[]model.TightenWrenchRuleInfo) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr, entity.RuleId)
		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/TightenWrenchRuleInfoService.Select
 *
 ******************************************************************************/
func (impl *TightenWrenchRuleInfoServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.TightenWrenchRuleInfo, error) {

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

	condition := DefaultConditionOfTightenWrenchRuleInfoAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewTightenWrenchRuleInfoDAO(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/TightenWrenchRuleInfoService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *TightenWrenchRuleInfoServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

	condition := DefaultConditionOfTightenWrenchRuleInfoAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewTightenWrenchRuleInfoDAO(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/TightenWrenchRuleInfoService.Update
 *
 ******************************************************************************/
func (impl *TightenWrenchRuleInfoServiceImplement) Update(user *global.User, entities *[]model.TightenWrenchRuleInfo) error {

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

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

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

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