// 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              : ArticlePickingRate的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfArticlePickingRate = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArticlePickingRate_ArtId.UrlParameterName:        grmi.NewConditionItem(meta.ArticlePickingRate_ArtId, grmi.Equal, true),
		meta.ArticlePickingRate_Priority.UrlParameterName:     grmi.NewConditionItem(meta.ArticlePickingRate_Priority, grmi.Equal, false),
		meta.ArticlePickingRate_PickRateArea.UrlParameterName: grmi.NewConditionItem(meta.ArticlePickingRate_PickRateArea, grmi.Equal, false),
		meta.ArticlePickingRate_Force.UrlParameterName:        grmi.NewConditionItem(meta.ArticlePickingRate_Force, grmi.Equal, false),
		meta.ArticlePickingRate_Uom.UrlParameterName:          grmi.NewConditionItem(meta.ArticlePickingRate_Uom, grmi.Equal, false),
		meta.ArticlePickingRate_Availiable.UrlParameterName:   grmi.NewConditionItem(meta.ArticlePickingRate_Availiable, grmi.Equal, false),
		meta.ArticlePickingRate_OnStock.UrlParameterName:      grmi.NewConditionItem(meta.ArticlePickingRate_OnStock, grmi.Equal, false),
		meta.ArticlePickingRate_APRCtrlPara1.UrlParameterName: grmi.NewConditionItem(meta.ArticlePickingRate_APRCtrlPara1, grmi.Equal, false),
		meta.ArticlePickingRate_APRCtrlPara2.UrlParameterName: grmi.NewConditionItem(meta.ArticlePickingRate_APRCtrlPara2, grmi.Equal, false),
		meta.ArticlePickingRate_APRCtrlPara3.UrlParameterName: grmi.NewConditionItem(meta.ArticlePickingRate_APRCtrlPara3, grmi.Equal, false),
		meta.ArticlePickingRate_APRCtrlPara4.UrlParameterName: grmi.NewConditionItem(meta.ArticlePickingRate_APRCtrlPara4, grmi.Equal, false),
		meta.ArticlePickingRate_APRCtrlPara5.UrlParameterName: grmi.NewConditionItem(meta.ArticlePickingRate_APRCtrlPara5, grmi.Equal, false),
		meta.ArticlePickingRate_APRCtrlPara6.UrlParameterName: grmi.NewConditionItem(meta.ArticlePickingRate_APRCtrlPara6, grmi.Equal, false),
		meta.ArticlePickingRate_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.ArticlePickingRate_LastModify, grmi.Approximate, false),
		meta.ArticlePickingRate_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.ArticlePickingRate_LastUser, grmi.Equal, false),
		meta.ArticlePickingRate_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.ArticlePickingRate_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/ArticlePickingRateService.InsertOne
 *
 ******************************************************************************/
func (impl *ArticlePickingRateServiceImplement) InsertOne(user *models.Usertab, entity *model.ArticlePickingRate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticlePickingRateDAO(session, user.Userid)
	artDao := dal.NewArticleDAO(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/ArticlePickingRateService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArticlePickingRateServiceImplement) DeleteOne(user *models.Usertab, artId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticlePickingRateDAO(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/ArticlePickingRateService.SelectOne
 *
 ******************************************************************************/
func (impl *ArticlePickingRateServiceImplement) SelectOne(user *models.Usertab, artId string) (*model.ArticlePickingRate, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/ArticlePickingRateService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArticlePickingRateServiceImplement) UpdateOne(user *models.Usertab, entity *model.ArticlePickingRate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticlePickingRateDAO(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/ArticlePickingRateService.Insert
 *
 ******************************************************************************/
func (impl *ArticlePickingRateServiceImplement) Insert(user *models.Usertab, entities *[]model.ArticlePickingRate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticlePickingRateDAO(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/ArticlePickingRateService.Delete
 *
 ******************************************************************************/
func (impl *ArticlePickingRateServiceImplement) Delete(user *models.Usertab, entities *[]model.ArticlePickingRate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticlePickingRateDAO(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/ArticlePickingRateService.Select
 *
 ******************************************************************************/
func (impl *ArticlePickingRateServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticlePickingRateDAO(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
}
