// 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              : ArticleType的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-20 14:37:20
 *
 ******************************************************************************/
var DefaultConditionOfArticleType = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArticleType_ArtTypeId.UrlParameterName:        grmi.NewConditionItem(meta.ArticleType_ArtTypeId, grmi.Equal, true),
		meta.ArticleType_Descr.UrlParameterName:            grmi.NewConditionItem(meta.ArticleType_Descr, grmi.Equal, false),
		meta.ArticleType_ArtTypeCtrlPara1.UrlParameterName: grmi.NewConditionItem(meta.ArticleType_ArtTypeCtrlPara1, grmi.Equal, false),
		meta.ArticleType_ArtTypeCtrlPara2.UrlParameterName: grmi.NewConditionItem(meta.ArticleType_ArtTypeCtrlPara2, grmi.Equal, false),
		meta.ArticleType_ArtTypeCtrlPara3.UrlParameterName: grmi.NewConditionItem(meta.ArticleType_ArtTypeCtrlPara3, grmi.Equal, false),
		meta.ArticleType_ArtTypeCtrlPara4.UrlParameterName: grmi.NewConditionItem(meta.ArticleType_ArtTypeCtrlPara4, grmi.Equal, false),
		meta.ArticleType_Icon.UrlParameterName:             grmi.NewConditionItem(meta.ArticleType_Icon, grmi.Equal, false),
		meta.ArticleType_DisplayColor.UrlParameterName:     grmi.NewConditionItem(meta.ArticleType_DisplayColor, grmi.Equal, false),
		meta.ArticleType_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.ArticleType_LastModify, grmi.Approximate, false),
		meta.ArticleType_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.ArticleType_LastUser, grmi.Equal, false),
		meta.ArticleType_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.ArticleType_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArticleType的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-20 14:37:20
 *
 ******************************************************************************/
var DefaultConditionOfArticleTypeAndPaging = grmi.NewCondition(
	DefaultConditionOfArticleType.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArticleTypeServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArticleType的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-20 14:37:20
 *
 ******************************************************************************/
type ArticleTypeServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArticleTypeDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArticleTypeDAOImplement实例
 *
 * @Return Value             : ArticleTypeDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-20 14:37:20
 *
 ******************************************************************************/
func NewArticleTypeServiceImplement() *ArticleTypeServiceImplement {
	return &ArticleTypeServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleTypeService.InsertOne
 *
 ******************************************************************************/
func (impl *ArticleTypeServiceImplement) InsertOne(user *models.Usertab, entity *model.ArticleType) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleTypeService.SelectOne
 *
 ******************************************************************************/
func (impl *ArticleTypeServiceImplement) SelectOne(user *models.Usertab, artTypeId string) (*model.ArticleType, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleTypeService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArticleTypeServiceImplement) UpdateOne(user *models.Usertab, entity *model.ArticleType) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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