// 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              : Script的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfScript = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Script_Script.UrlParameterName:          grmi.NewConditionItem(meta.Script_Script, grmi.Equal, true),
		meta.Script_Bez.UrlParameterName:             grmi.NewConditionItem(meta.Script_Bez, grmi.Equal, false),
		meta.Script_ScriptTable.UrlParameterName:     grmi.NewConditionItem(meta.Script_ScriptTable, grmi.Equal, false),
		meta.Script_ScriptType.UrlParameterName:      grmi.NewConditionItem(meta.Script_ScriptType, grmi.Equal, false),
		meta.Script_FilePath.UrlParameterName:        grmi.NewConditionItem(meta.Script_FilePath, grmi.Equal, false),
		meta.Script_Separator.UrlParameterName:       grmi.NewConditionItem(meta.Script_Separator, grmi.Equal, false),
		meta.Script_UseHead.UrlParameterName:         grmi.NewConditionItem(meta.Script_UseHead, grmi.Equal, false),
		meta.Script_ClearTable.UrlParameterName:      grmi.NewConditionItem(meta.Script_ClearTable, grmi.Equal, false),
		meta.Script_StartRow.UrlParameterName:        grmi.NewConditionItem(meta.Script_StartRow, grmi.Equal, false),
		meta.Script_StartCol.UrlParameterName:        grmi.NewConditionItem(meta.Script_StartCol, grmi.Equal, false),
		meta.Script_AppendNewRecord.UrlParameterName: grmi.NewConditionItem(meta.Script_AppendNewRecord, grmi.Equal, false),
		meta.Script_UpdateRecord.UrlParameterName:    grmi.NewConditionItem(meta.Script_UpdateRecord, grmi.Equal, false),
		meta.Script_CallSpecFunc.UrlParameterName:    grmi.NewConditionItem(meta.Script_CallSpecFunc, grmi.Equal, false),
		meta.Script_CallFunction.UrlParameterName:    grmi.NewConditionItem(meta.Script_CallFunction, grmi.Equal, false),
		meta.Script_InputPara1.UrlParameterName:      grmi.NewConditionItem(meta.Script_InputPara1, grmi.Equal, false),
		meta.Script_Para1Type.UrlParameterName:       grmi.NewConditionItem(meta.Script_Para1Type, grmi.Equal, false),
		meta.Script_Para1Descr.UrlParameterName:      grmi.NewConditionItem(meta.Script_Para1Descr, grmi.Equal, false),
		meta.Script_InputPara2.UrlParameterName:      grmi.NewConditionItem(meta.Script_InputPara2, grmi.Equal, false),
		meta.Script_Para2Type.UrlParameterName:       grmi.NewConditionItem(meta.Script_Para2Type, grmi.Equal, false),
		meta.Script_Para2Descr.UrlParameterName:      grmi.NewConditionItem(meta.Script_Para2Descr, grmi.Equal, false),
		meta.Script_InputPara3.UrlParameterName:      grmi.NewConditionItem(meta.Script_InputPara3, grmi.Equal, false),
		meta.Script_Para3Type.UrlParameterName:       grmi.NewConditionItem(meta.Script_Para3Type, grmi.Equal, false),
		meta.Script_Para3Descr.UrlParameterName:      grmi.NewConditionItem(meta.Script_Para3Descr, grmi.Equal, false),
		meta.Script_InputPara4.UrlParameterName:      grmi.NewConditionItem(meta.Script_InputPara4, grmi.Equal, false),
		meta.Script_Para4Type.UrlParameterName:       grmi.NewConditionItem(meta.Script_Para4Type, grmi.Equal, false),
		meta.Script_Para4Descr.UrlParameterName:      grmi.NewConditionItem(meta.Script_Para4Descr, grmi.Equal, false),
		meta.Script_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.Script_LastModify, grmi.Approximate, false),
		meta.Script_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.Script_LastUser, grmi.Equal, false),
		meta.Script_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.Script_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ScriptService.InsertOne
 *
 ******************************************************************************/
func (impl *ScriptServiceImplement) InsertOne(user *models.Usertab, entity *model.Script) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewScriptDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.Script)
	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/ScriptService.DeleteOne
 *
 ******************************************************************************/
func (impl *ScriptServiceImplement) DeleteOne(user *models.Usertab, script string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ScriptService.SelectOne
 *
 ******************************************************************************/
func (impl *ScriptServiceImplement) SelectOne(user *models.Usertab, script string) (*model.Script, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ScriptService.UpdateOne
 *
 ******************************************************************************/
func (impl *ScriptServiceImplement) UpdateOne(user *models.Usertab, entity *model.Script) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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