// 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              : TabColName的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 15:04:54
 *
 ******************************************************************************/
var DefaultConditionOfTabColName = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.TabColName_TabName.UrlParameterName:    grmi.NewConditionItem(meta.TabColName_TabName, grmi.Equal, true),
		meta.TabColName_ColName.UrlParameterName:    grmi.NewConditionItem(meta.TabColName_ColName, grmi.Equal, true),
		meta.TabColName_TextLabel.UrlParameterName:  grmi.NewConditionItem(meta.TabColName_TextLabel, grmi.Equal, false),
		meta.TabColName_LongLabel.UrlParameterName:  grmi.NewConditionItem(meta.TabColName_LongLabel, grmi.Equal, false),
		meta.TabColName_ValFormat.UrlParameterName:  grmi.NewConditionItem(meta.TabColName_ValFormat, grmi.Equal, false),
		meta.TabColName_LastModify.UrlParameterName: grmi.NewConditionItem(meta.TabColName_LastModify, grmi.Approximate, false),
		meta.TabColName_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.TabColName_LastUser, grmi.Equal, false),
		meta.TabColName_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.TabColName_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : TabColName的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 15:04:54
 *
 ******************************************************************************/
var DefaultConditionOfTabColNameAndPaging = grmi.NewCondition(
	DefaultConditionOfTabColName.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : TabColNameServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : TabColName的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 15:04:54
 *
 ******************************************************************************/
type TabColNameServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewTabColNameDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个TabColNameDAOImplement实例
 *
 * @Return Value             : TabColNameDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 15:04:54
 *
 ******************************************************************************/
func NewTabColNameServiceImplement() *TabColNameServiceImplement {
	return &TabColNameServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/TabColNameService.InsertOne
 *
 ******************************************************************************/
func (impl *TabColNameServiceImplement) InsertOne(user *models.Usertab, entity *model.TabColName) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewTabColNameDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.TabName, entity.ColName)
	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/TabColNameService.DeleteOne
 *
 ******************************************************************************/
func (impl *TabColNameServiceImplement) DeleteOne(user *models.Usertab, tabName string, colName string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/TabColNameService.SelectOne
 *
 ******************************************************************************/
func (impl *TabColNameServiceImplement) SelectOne(user *models.Usertab, tabName string, colName string) (*model.TabColName, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/TabColNameService.UpdateOne
 *
 ******************************************************************************/
func (impl *TabColNameServiceImplement) UpdateOne(user *models.Usertab, entity *model.TabColName) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewTabColNameDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.TabName, entity.ColName)
	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/TabColNameService.Insert
 *
 ******************************************************************************/
func (impl *TabColNameServiceImplement) Insert(user *models.Usertab, entities *[]model.TabColName) error {

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

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

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

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

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

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

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

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

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

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

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

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