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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/api"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/api"
	model "LAPP_LF_MOM_BACKEND/models/api"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : A30Mes的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-11 17:38:55
 *
 ******************************************************************************/
var DefaultConditionOfA30Mes = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.A30Mes_ClassName.UrlParameterName:      grmi.NewConditionItem(meta.A30Mes_ClassName, grmi.Equal, true),
		meta.A30Mes_ClassId.UrlParameterName:        grmi.NewConditionItem(meta.A30Mes_ClassId, grmi.Equal, false),
		meta.A30Mes_ClassStartTime.UrlParameterName: grmi.NewConditionItem(meta.A30Mes_ClassStartTime, grmi.Approximate, false),
		meta.A30Mes_ClassEndTime.UrlParameterName:   grmi.NewConditionItem(meta.A30Mes_ClassEndTime, grmi.Approximate, false),
		meta.A30Mes_CycleTime.UrlParameterName:      grmi.NewConditionItem(meta.A30Mes_CycleTime, grmi.Equal, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : A30Mes的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-11 17:38:55
 *
 ******************************************************************************/
var DefaultConditionOfA30MesAndPaging = grmi.NewCondition(
	DefaultConditionOfA30Mes.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : A30MesServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : A30Mes的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-11 17:38:55
 *
 ******************************************************************************/
type A30MesServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewA30MesDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个A30MesDAOImplement实例
 *
 * @Return Value             : A30MesDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-11 17:38:55
 *
 ******************************************************************************/
func NewA30MesServiceImplement() *A30MesServiceImplement {
	return &A30MesServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/A30MesService.InsertOne
 *
 ******************************************************************************/
func (impl *A30MesServiceImplement) InsertOne(user *models.Usertab, entity *model.A30Mes) error {

	grmi.Log(user, "/services/api/implments/A30Mes.service.impl.go", "InsertOneA30Mes", "插入一个A30Mes")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/A30MesService.DeleteOne
 *
 ******************************************************************************/
func (impl *A30MesServiceImplement) DeleteOne(user *models.Usertab, className string) error {

	grmi.Log(user, "/services/api/implments/A30Mes.service.impl.go", "DeleteOneA30Mes", "删除一个A30Mes")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/A30MesService.SelectOne
 *
 ******************************************************************************/
func (impl *A30MesServiceImplement) SelectOne(user *models.Usertab, className string) (*model.A30Mes, error) {

	grmi.Log(user, "/services/api/implments/A30Mes.service.impl.go", "SelectOneA30Mes", "查询一个A30Mes")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewA30MesDAO(session, user.Userid)
	result, err := dao.SelectOne(className)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/A30MesService.UpdateOne
 *
 ******************************************************************************/
func (impl *A30MesServiceImplement) UpdateOne(user *models.Usertab, entity *model.A30Mes) error {

	grmi.Log(user, "/services/api/implments/A30Mes.service.impl.go", "UpdateOneA30Mes", "修改一个A30Mes")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/A30MesService.Insert
 *
 ******************************************************************************/
func (impl *A30MesServiceImplement) Insert(user *models.Usertab, entities *[]model.A30Mes) error {

	grmi.Log(user, "/services/api/implments/A30Mes.service.impl.go", "InsertA30Mes", "插入多个A30Mes")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewA30MesDAO(session, user.Userid)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/A30MesService.Delete
 *
 ******************************************************************************/
func (impl *A30MesServiceImplement) Delete(user *models.Usertab, entities *[]model.A30Mes) error {

	grmi.Log(user, "/services/api/implments/A30Mes.service.impl.go", "DeleteA30Mes", "删除多个A30Mes")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewA30MesDAO(session, user.Userid)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/A30MesService.Select
 *
 ******************************************************************************/
func (impl *A30MesServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/api/implments/A30Mes.service.impl.go", "SelectA30Mes", "查询A30Mes")

	condition := DefaultConditionOfA30MesAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewA30MesDAO(session, user.Userid)
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/A30MesService.Update
 *
 ******************************************************************************/
func (impl *A30MesServiceImplement) Update(user *models.Usertab, entities *[]model.A30Mes) error {

	grmi.Log(user, "/services/api/implments/A30Mes.service.impl.go", "UpdateA30Mes", "修改多个A30Mes")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewA30MesDAO(session, user.Userid)

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

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