// 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              : PLCErrorInfo的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-14 17:25:00
 *
 ******************************************************************************/
var DefaultConditionOfPLCErrorInfo = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PLCErrorInfo_ErrorId.UrlParameterName:    grmi.NewConditionItem(meta.PLCErrorInfo_ErrorId, grmi.Equal, true),
		meta.PLCErrorInfo_Source.UrlParameterName:     grmi.NewConditionItem(meta.PLCErrorInfo_Source, grmi.Equal, true),
		meta.PLCErrorInfo_Status.UrlParameterName:     grmi.NewConditionItem(meta.PLCErrorInfo_Status, grmi.Equal, false),
		meta.PLCErrorInfo_Step.UrlParameterName:       grmi.NewConditionItem(meta.PLCErrorInfo_Step, grmi.Equal, false),
		meta.PLCErrorInfo_ErrorInfo.UrlParameterName:  grmi.NewConditionItem(meta.PLCErrorInfo_ErrorInfo, grmi.Equal, false),
		meta.PLCErrorInfo_SeatData.UrlParameterName:   grmi.NewConditionItem(meta.PLCErrorInfo_SeatData, grmi.Equal, false),
		meta.PLCErrorInfo_Location.UrlParameterName:   grmi.NewConditionItem(meta.PLCErrorInfo_Location, grmi.Equal, false),
		meta.PLCErrorInfo_ErrorType.UrlParameterName:  grmi.NewConditionItem(meta.PLCErrorInfo_ErrorType, grmi.Equal, false),
		meta.PLCErrorInfo_SaveStr1.UrlParameterName:   grmi.NewConditionItem(meta.PLCErrorInfo_SaveStr1, grmi.Equal, false),
		meta.PLCErrorInfo_SaveStr2.UrlParameterName:   grmi.NewConditionItem(meta.PLCErrorInfo_SaveStr2, grmi.Equal, false),
		meta.PLCErrorInfo_SaveStr3.UrlParameterName:   grmi.NewConditionItem(meta.PLCErrorInfo_SaveStr3, grmi.Equal, false),
		meta.PLCErrorInfo_SaveInt1.UrlParameterName:   grmi.NewConditionItem(meta.PLCErrorInfo_SaveInt1, grmi.Equal, false),
		meta.PLCErrorInfo_SaveInt2.UrlParameterName:   grmi.NewConditionItem(meta.PLCErrorInfo_SaveInt2, grmi.Equal, false),
		meta.PLCErrorInfo_SaveInt3.UrlParameterName:   grmi.NewConditionItem(meta.PLCErrorInfo_SaveInt3, grmi.Equal, false),
		meta.PLCErrorInfo_SaveFloat1.UrlParameterName: grmi.NewConditionItem(meta.PLCErrorInfo_SaveFloat1, grmi.Equal, false),
		meta.PLCErrorInfo_SaveFloat2.UrlParameterName: grmi.NewConditionItem(meta.PLCErrorInfo_SaveFloat2, grmi.Equal, false),
		meta.PLCErrorInfo_SaveFloat3.UrlParameterName: grmi.NewConditionItem(meta.PLCErrorInfo_SaveFloat3, grmi.Equal, false),
		meta.PLCErrorInfo_LastModify.UrlParameterName: grmi.NewConditionItem(meta.PLCErrorInfo_LastModify, grmi.Approximate, false),
		meta.PLCErrorInfo_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.PLCErrorInfo_LastUser, grmi.Equal, false),
		meta.PLCErrorInfo_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.PLCErrorInfo_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : PLCErrorInfo的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-14 17:25:00
 *
 ******************************************************************************/
var DefaultConditionOfPLCErrorInfoAndPaging = grmi.NewCondition(
	DefaultConditionOfPLCErrorInfo.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : PLCErrorInfoServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : PLCErrorInfo的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-14 17:25:00
 *
 ******************************************************************************/
type PLCErrorInfoServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewPLCErrorInfoDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个PLCErrorInfoDAOImplement实例
 *
 * @Return Value             : PLCErrorInfoDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-14 17:25:00
 *
 ******************************************************************************/
func NewPLCErrorInfoServiceImplement() *PLCErrorInfoServiceImplement {
	return &PLCErrorInfoServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/PLCErrorInfoService.InsertOne
 *
 ******************************************************************************/
func (impl *PLCErrorInfoServiceImplement) InsertOne(user *global.User, entity *model.PLCErrorInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPLCErrorInfoDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ErrorId, entity.Source)
	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/PLCErrorInfoService.DeleteOne
 *
 ******************************************************************************/
func (impl *PLCErrorInfoServiceImplement) DeleteOne(user *global.User, errorId string, source string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/PLCErrorInfoService.SelectOne
 *
 ******************************************************************************/
func (impl *PLCErrorInfoServiceImplement) SelectOne(user *global.User, errorId string, source string) (*model.PLCErrorInfo, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/PLCErrorInfoService.UpdateOne
 *
 ******************************************************************************/
func (impl *PLCErrorInfoServiceImplement) UpdateOne(user *global.User, entity *model.PLCErrorInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPLCErrorInfoDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ErrorId, entity.Source)
	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/PLCErrorInfoService.Insert
 *
 ******************************************************************************/
func (impl *PLCErrorInfoServiceImplement) Insert(user *global.User, entities *[]model.PLCErrorInfo) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ErrorId, entity.Source)
		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/PLCErrorInfoService.Delete
 *
 ******************************************************************************/
func (impl *PLCErrorInfoServiceImplement) Delete(user *global.User, entities *[]model.PLCErrorInfo) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ErrorId, entity.Source)
		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/PLCErrorInfoService.Select
 *
 ******************************************************************************/
func (impl *PLCErrorInfoServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.PLCErrorInfo, error) {

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

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

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

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

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

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

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

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