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

package implments

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

/******************************************************************************
 *
 * @Description              : ArticleReplenish的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfArticleReplenish = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArticleReplenish_ArtId.UrlParameterName:         grmi.NewConditionItem(meta.ArticleReplenish_ArtId, grmi.Equal, true),
		meta.ArticleReplenish_MinQty.UrlParameterName:        grmi.NewConditionItem(meta.ArticleReplenish_MinQty, grmi.Equal, false),
		meta.ArticleReplenish_MaxQty.UrlParameterName:        grmi.NewConditionItem(meta.ArticleReplenish_MaxQty, grmi.Equal, false),
		meta.ArticleReplenish_ReplenishQty.UrlParameterName:  grmi.NewConditionItem(meta.ArticleReplenish_ReplenishQty, grmi.Equal, false),
		meta.ArticleReplenish_ReplenishFrom.UrlParameterName: grmi.NewConditionItem(meta.ArticleReplenish_ReplenishFrom, grmi.Equal, false),
		meta.ArticleReplenish_ARCtrlPara1.UrlParameterName:   grmi.NewConditionItem(meta.ArticleReplenish_ARCtrlPara1, grmi.Equal, false),
		meta.ArticleReplenish_ARCtrlPara2.UrlParameterName:   grmi.NewConditionItem(meta.ArticleReplenish_ARCtrlPara2, grmi.Equal, false),
		meta.ArticleReplenish_ARCtrlPara3.UrlParameterName:   grmi.NewConditionItem(meta.ArticleReplenish_ARCtrlPara3, grmi.Equal, false),
		meta.ArticleReplenish_ARCtrlPara4.UrlParameterName:   grmi.NewConditionItem(meta.ArticleReplenish_ARCtrlPara4, grmi.Equal, false),
		meta.ArticleReplenish_ARCtrlPara5.UrlParameterName:   grmi.NewConditionItem(meta.ArticleReplenish_ARCtrlPara5, grmi.Equal, false),
		meta.ArticleReplenish_ARCtrlPara6.UrlParameterName:   grmi.NewConditionItem(meta.ArticleReplenish_ARCtrlPara6, grmi.Equal, false),
		meta.ArticleReplenish_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.ArticleReplenish_LastModify, grmi.Approximate, false),
		meta.ArticleReplenish_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.ArticleReplenish_LastUser, grmi.Equal, false),
		meta.ArticleReplenish_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.ArticleReplenish_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArticleReplenish的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfArticleReplenishAndPaging = grmi.NewCondition(
	DefaultConditionOfArticleReplenish.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArticleReplenishServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArticleReplenish的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
type ArticleReplenishServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArticleReplenishDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArticleReplenishDAOImplement实例
 *
 * @Return Value             : ArticleReplenishDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
func NewArticleReplenishServiceImplement() *ArticleReplenishServiceImplement {
	return &ArticleReplenishServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/ArticleReplenishService.InsertOne
 *
 ******************************************************************************/
func (impl *ArticleReplenishServiceImplement) InsertOne(user *models.Usertab, entity *model.ArticleReplenish) error {

	grmi.Log(user, "/services/wm/implments/ArticleReplenish.service.impl.go", "InsertOneArticleReplenish", "插入一个ArticleReplenish")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticleReplenishDAO(session, user.Userid)
	artDao := dal.NewArticleReplenishDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.ArtId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	article, err := artDao.SelectOne(entity.ArtId)
	if err != nil {
		return err
	}
	if article == nil {
		return grmi.NewBusinessError("不存在对应的物料")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/ArticleReplenishService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArticleReplenishServiceImplement) DeleteOne(user *models.Usertab, artId string) error {

	grmi.Log(user, "/services/wm/implments/ArticleReplenish.service.impl.go", "DeleteOneArticleReplenish", "删除一个ArticleReplenish")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/ArticleReplenishService.SelectOne
 *
 ******************************************************************************/
func (impl *ArticleReplenishServiceImplement) SelectOne(user *models.Usertab, artId string) (*model.ArticleReplenish, error) {

	grmi.Log(user, "/services/wm/implments/ArticleReplenish.service.impl.go", "SelectOneArticleReplenish", "查询一个ArticleReplenish")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/ArticleReplenishService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArticleReplenishServiceImplement) UpdateOne(user *models.Usertab, entity *model.ArticleReplenish) error {

	grmi.Log(user, "/services/wm/implments/ArticleReplenish.service.impl.go", "UpdateOneArticleReplenish", "修改一个ArticleReplenish")

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

	grmi.Log(user, "/services/wm/implments/ArticleReplenish.service.impl.go", "InsertArticleReplenish", "插入多个ArticleReplenish")

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

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

	grmi.Log(user, "/services/wm/implments/ArticleReplenish.service.impl.go", "DeleteArticleReplenish", "删除多个ArticleReplenish")

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

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

	grmi.Log(user, "/services/wm/implments/ArticleReplenish.service.impl.go", "SelectArticleReplenish", "查询ArticleReplenish")

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

	grmi.Log(user, "/services/wm/implments/ArticleReplenish.service.impl.go", "UpdateArticleReplenish", "修改多个ArticleReplenish")

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

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

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