// 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              : ScriptItem的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfScriptItem = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ScriptItem_Script.UrlParameterName:       grmi.NewConditionItem(meta.ScriptItem_Script, grmi.Equal, true),
		meta.ScriptItem_TabMapCol.UrlParameterName:    grmi.NewConditionItem(meta.ScriptItem_TabMapCol, grmi.Equal, true),
		meta.ScriptItem_ColNo.UrlParameterName:        grmi.NewConditionItem(meta.ScriptItem_ColNo, grmi.Equal, false),
		meta.ScriptItem_ColName.UrlParameterName:      grmi.NewConditionItem(meta.ScriptItem_ColName, grmi.Equal, false),
		meta.ScriptItem_RowNo.UrlParameterName:        grmi.NewConditionItem(meta.ScriptItem_RowNo, grmi.Equal, false),
		meta.ScriptItem_RowName.UrlParameterName:      grmi.NewConditionItem(meta.ScriptItem_RowName, grmi.Equal, false),
		meta.ScriptItem_ColFieldType.UrlParameterName: grmi.NewConditionItem(meta.ScriptItem_ColFieldType, grmi.Equal, false),
		meta.ScriptItem_ColFormat.UrlParameterName:    grmi.NewConditionItem(meta.ScriptItem_ColFormat, grmi.Equal, false),
		meta.ScriptItem_DefaultValue.UrlParameterName: grmi.NewConditionItem(meta.ScriptItem_DefaultValue, grmi.Equal, false),
		meta.ScriptItem_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.ScriptItem_LastModify, grmi.Approximate, false),
		meta.ScriptItem_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.ScriptItem_LastUser, grmi.Equal, false),
		meta.ScriptItem_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.ScriptItem_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ScriptItemService.InsertOne
 *
 ******************************************************************************/
func (impl *ScriptItemServiceImplement) InsertOne(user *models.Usertab, entity *model.ScriptItem) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ScriptItemService.SelectOne
 *
 ******************************************************************************/
func (impl *ScriptItemServiceImplement) SelectOne(user *models.Usertab, script string, tabMapCol string) (*model.ScriptItem, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ScriptItemService.UpdateOne
 *
 ******************************************************************************/
func (impl *ScriptItemServiceImplement) UpdateOne(user *models.Usertab, entity *model.ScriptItem) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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