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

package implments

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

/******************************************************************************
 *
 * @Description              : OdsType的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfOdsType = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OdsType_OdsTypeId.UrlParameterName:  grmi.NewConditionItem(meta.OdsType_OdsTypeId, grmi.Equal, true),
		meta.OdsType_Descr.UrlParameterName:      grmi.NewConditionItem(meta.OdsType_Descr, grmi.Equal, false),
		meta.OdsType_Width.UrlParameterName:      grmi.NewConditionItem(meta.OdsType_Width, grmi.Equal, false),
		meta.OdsType_Height.UrlParameterName:     grmi.NewConditionItem(meta.OdsType_Height, grmi.Equal, false),
		meta.OdsType_Player.UrlParameterName:     grmi.NewConditionItem(meta.OdsType_Player, grmi.Equal, false),
		meta.OdsType_CtrlPara1.UrlParameterName:  grmi.NewConditionItem(meta.OdsType_CtrlPara1, grmi.Equal, false),
		meta.OdsType_CtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.OdsType_CtrlPara2, grmi.Equal, false),
		meta.OdsType_CtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.OdsType_CtrlPara3, grmi.Equal, false),
		meta.OdsType_CtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.OdsType_CtrlPara4, grmi.Equal, false),
		meta.OdsType_LastModify.UrlParameterName: grmi.NewConditionItem(meta.OdsType_LastModify, grmi.Approximate, false),
		meta.OdsType_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.OdsType_LastUser, grmi.Equal, false),
		meta.OdsType_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.OdsType_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : OdsType的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfOdsTypeAndPaging = grmi.NewCondition(
	DefaultConditionOfOdsType.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : OdsTypeServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : OdsType的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type OdsTypeServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewOdsTypeDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个OdsTypeDAOImplement实例
 *
 * @Return Value             : OdsTypeDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewOdsTypeServiceImplement() *OdsTypeServiceImplement {
	return &OdsTypeServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/OdsTypeService.InsertOne
 *
 ******************************************************************************/
func (impl *OdsTypeServiceImplement) InsertOne(user *models.Usertab, entity *model.OdsType) error {

	grmi.Log(user, "/services/ods/implments/ODS_OdsType.service.impl.go", "InsertOneOdsType", "插入一个OdsType")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOdsTypeDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OdsTypeId)
	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/ods/OdsTypeService.DeleteOne
 *
 ******************************************************************************/
func (impl *OdsTypeServiceImplement) DeleteOne(user *models.Usertab, odsTypeId string) error {

	grmi.Log(user, "/services/ods/implments/ODS_OdsType.service.impl.go", "DeleteOneOdsType", "删除一个OdsType")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/OdsTypeService.SelectOne
 *
 ******************************************************************************/
func (impl *OdsTypeServiceImplement) SelectOne(user *models.Usertab, odsTypeId string) (*model.OdsType, error) {

	grmi.Log(user, "/services/ods/implments/ODS_OdsType.service.impl.go", "SelectOneOdsType", "查询一个OdsType")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/OdsTypeService.UpdateOne
 *
 ******************************************************************************/
func (impl *OdsTypeServiceImplement) UpdateOne(user *models.Usertab, entity *model.OdsType) error {

	grmi.Log(user, "/services/ods/implments/ODS_OdsType.service.impl.go", "UpdateOneOdsType", "修改一个OdsType")

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

	grmi.Log(user, "/services/ods/implments/ODS_OdsType.service.impl.go", "InsertOdsType", "插入多个OdsType")

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

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

	grmi.Log(user, "/services/ods/implments/ODS_OdsType.service.impl.go", "DeleteOdsType", "删除多个OdsType")

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

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

	grmi.Log(user, "/services/ods/implments/ODS_OdsType.service.impl.go", "SelectOdsType", "查询OdsType")

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

	grmi.Log(user, "/services/ods/implments/ODS_OdsType.service.impl.go", "UpdateOdsType", "修改多个OdsType")

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

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

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