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

package implments

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

/******************************************************************************
 *
 * @Description              : EtlLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfEtlLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.EtlLst_EtlId.UrlParameterName:      grmi.NewConditionItem(meta.EtlLst_EtlId, grmi.Equal, true),
		meta.EtlLst_Pos.UrlParameterName:        grmi.NewConditionItem(meta.EtlLst_Pos, grmi.Equal, true),
		meta.EtlLst_Table.UrlParameterName:      grmi.NewConditionItem(meta.EtlLst_Table, grmi.Equal, false),
		meta.EtlLst_Field.UrlParameterName:      grmi.NewConditionItem(meta.EtlLst_Field, grmi.Equal, false),
		meta.EtlLst_FieldName.UrlParameterName:  grmi.NewConditionItem(meta.EtlLst_FieldName, grmi.Equal, false),
		meta.EtlLst_FieldType.UrlParameterName:  grmi.NewConditionItem(meta.EtlLst_FieldType, grmi.Equal, false),
		meta.EtlLst_FuncSpec.UrlParameterName:   grmi.NewConditionItem(meta.EtlLst_FuncSpec, grmi.Equal, false),
		meta.EtlLst_ToField.UrlParameterName:    grmi.NewConditionItem(meta.EtlLst_ToField, grmi.Equal, false),
		meta.EtlLst_LastModify.UrlParameterName: grmi.NewConditionItem(meta.EtlLst_LastModify, grmi.Approximate, false),
		meta.EtlLst_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.EtlLst_LastUser, grmi.Equal, false),
		meta.EtlLst_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.EtlLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : EtlLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfEtlLstAndPaging = grmi.NewCondition(
	DefaultConditionOfEtlLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : EtlLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : EtlLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
type EtlLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewEtlLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个EtlLstDAOImplement实例
 *
 * @Return Value             : EtlLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
func NewEtlLstServiceImplement() *EtlLstServiceImplement {
	return &EtlLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/EtlLstService.InsertOne
 *
 ******************************************************************************/
func (impl *EtlLstServiceImplement) InsertOne(user *models.Usertab, entity *model.EtlLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewEtlLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.EtlId, entity.Pos)
	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/base/EtlLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *EtlLstServiceImplement) DeleteOne(user *models.Usertab, etlId int, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/EtlLstService.SelectOne
 *
 ******************************************************************************/
func (impl *EtlLstServiceImplement) SelectOne(user *models.Usertab, etlId int, pos int) (*model.EtlLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/EtlLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *EtlLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.EtlLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewEtlLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.EtlId, entity.Pos)
	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/base/EtlLstService.Insert
 *
 ******************************************************************************/
func (impl *EtlLstServiceImplement) Insert(user *models.Usertab, entities *[]model.EtlLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.EtlId, entity.Pos)
		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/base/EtlLstService.Delete
 *
 ******************************************************************************/
func (impl *EtlLstServiceImplement) Delete(user *models.Usertab, entities *[]model.EtlLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.EtlId, entity.Pos)
		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/base/EtlLstService.Select
 *
 ******************************************************************************/
func (impl *EtlLstServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfEtlLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewEtlLstDAO(session, user.Pid, 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/base/EtlLstService.Update
 *
 ******************************************************************************/
func (impl *EtlLstServiceImplement) Update(user *models.Usertab, entities *[]model.EtlLst) error {

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

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

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

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