// 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              : ArticleRelLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
var DefaultConditionOfArticleRelLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArticleRelLst_ArtId.UrlParameterName:       grmi.NewConditionItem(meta.ArticleRelLst_ArtId, grmi.Equal, true),
		meta.ArticleRelLst_PID.UrlParameterName:         grmi.NewConditionItem(meta.ArticleRelLst_PID, grmi.Equal, true),
		meta.ArticleRelLst_OriginID.UrlParameterName:    grmi.NewConditionItem(meta.ArticleRelLst_OriginID, grmi.Equal, false),
		meta.ArticleRelLst_ComposeQty.UrlParameterName:  grmi.NewConditionItem(meta.ArticleRelLst_ComposeQty, grmi.Equal, false),
		meta.ArticleRelLst_ARCtrlPara1.UrlParameterName: grmi.NewConditionItem(meta.ArticleRelLst_ARCtrlPara1, grmi.Equal, false),
		meta.ArticleRelLst_ARCtrlPara2.UrlParameterName: grmi.NewConditionItem(meta.ArticleRelLst_ARCtrlPara2, grmi.Equal, false),
		meta.ArticleRelLst_ARCtrlPara3.UrlParameterName: grmi.NewConditionItem(meta.ArticleRelLst_ARCtrlPara3, grmi.Equal, false),
		meta.ArticleRelLst_ARCtrlPara4.UrlParameterName: grmi.NewConditionItem(meta.ArticleRelLst_ARCtrlPara4, grmi.Equal, false),
		meta.ArticleRelLst_ARCtrlPara5.UrlParameterName: grmi.NewConditionItem(meta.ArticleRelLst_ARCtrlPara5, grmi.Equal, false),
		meta.ArticleRelLst_ARCtrlPara6.UrlParameterName: grmi.NewConditionItem(meta.ArticleRelLst_ARCtrlPara6, grmi.Equal, false),
		meta.ArticleRelLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.ArticleRelLst_LastModify, grmi.Approximate, false),
		meta.ArticleRelLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.ArticleRelLst_LastUser, grmi.Equal, false),
		meta.ArticleRelLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.ArticleRelLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArticleRelLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
var DefaultConditionOfArticleRelLstAndPaging = grmi.NewCondition(
	DefaultConditionOfArticleRelLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArticleRelLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArticleRelLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
type ArticleRelLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArticleRelLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArticleRelLstDAOImplement实例
 *
 * @Return Value             : ArticleRelLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
func NewArticleRelLstServiceImplement() *ArticleRelLstServiceImplement {
	return &ArticleRelLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleRelLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ArticleRelLstServiceImplement) InsertOne(user *models.Usertab, entity *model.ArticleRelLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticleRelLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ArtId, entity.PID)
	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/ArticleRelLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArticleRelLstServiceImplement) DeleteOne(user *models.Usertab, artId string, pID string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleRelLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ArticleRelLstServiceImplement) SelectOne(user *models.Usertab, artId string, pID string) (*model.ArticleRelLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleRelLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArticleRelLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.ArticleRelLst) error {

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewArticleRelLstDAO(session, user.Pid, user.Userid)
	li := *entities
	if len(li) == 0 {
		_ = session.Rollback()
		return grmi.NewBusinessError("传入数据为空")
	}
	pid := li[0].PID
	originId := li[0].OriginID
	for _, entity := range *entities {
		if entity.OriginID != originId {
			_ = session.Rollback()
			return grmi.NewBusinessError("总成ID不一致")
		}
		if entity.PID != pid {
			_ = session.Rollback()
			return grmi.NewBusinessError("父级ID不一致")
		}
	}
	err := dao.DeleteWhere([]grmi.Predicate{meta.ArticleRelLst_PID.NewPredicate(grmi.Equal, pid)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.Insert(entities)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleRelLstService.Delete
 *
 ******************************************************************************/
func (impl *ArticleRelLstServiceImplement) Delete(user *models.Usertab, entities *[]model.ArticleRelLst) error {

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

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

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

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

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

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

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

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

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