// 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              : Etl的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfEtl = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Etl_EtlId.UrlParameterName:        grmi.NewConditionItem(meta.Etl_EtlId, grmi.Equal, true),
		meta.Etl_FromDb.UrlParameterName:       grmi.NewConditionItem(meta.Etl_FromDb, grmi.Equal, false),
		meta.Etl_FromTable.UrlParameterName:    grmi.NewConditionItem(meta.Etl_FromTable, grmi.Equal, false),
		meta.Etl_ToDb.UrlParameterName:         grmi.NewConditionItem(meta.Etl_ToDb, grmi.Equal, false),
		meta.Etl_ToTable.UrlParameterName:      grmi.NewConditionItem(meta.Etl_ToTable, grmi.Equal, false),
		meta.Etl_SqlShow.UrlParameterName:      grmi.NewConditionItem(meta.Etl_SqlShow, grmi.Equal, false),
		meta.Etl_Status.UrlParameterName:       grmi.NewConditionItem(meta.Etl_Status, grmi.Equal, false),
		meta.Etl_SearchType.UrlParameterName:   grmi.NewConditionItem(meta.Etl_SearchType, grmi.Equal, false),
		meta.Etl_SearchTime.UrlParameterName:   grmi.NewConditionItem(meta.Etl_SearchTime, grmi.Equal, false),
		meta.Etl_SearchFiled.UrlParameterName:  grmi.NewConditionItem(meta.Etl_SearchFiled, grmi.Equal, false),
		meta.Etl_ToDriverName.UrlParameterName: grmi.NewConditionItem(meta.Etl_ToDriverName, grmi.Equal, false),
		meta.Etl_ToType.UrlParameterName:       grmi.NewConditionItem(meta.Etl_ToType, grmi.Equal, false),
		meta.Etl_ChooseType.UrlParameterName:   grmi.NewConditionItem(meta.Etl_ChooseType, grmi.Equal, false),
		meta.Etl_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.Etl_LastModify, grmi.Approximate, false),
		meta.Etl_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.Etl_LastUser, grmi.Equal, false),
		meta.Etl_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.Etl_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/EtlService.InsertOne
 *
 ******************************************************************************/
func (impl *EtlServiceImplement) InsertOne(user *models.Usertab, entity *model.Etl) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/EtlService.SelectOne
 *
 ******************************************************************************/
func (impl *EtlServiceImplement) SelectOne(user *models.Usertab, etlId int) (*model.Etl, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/EtlService.UpdateOne
 *
 ******************************************************************************/
func (impl *EtlServiceImplement) UpdateOne(user *models.Usertab, entity *model.Etl) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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