// 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              : GoodsReceiptLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfGoodsReceiptLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.GoodsReceiptLst_GoodsReceiptId.UrlParameterName: grmi.NewConditionItem(meta.GoodsReceiptLst_GoodsReceiptId, grmi.Equal, true),
		meta.GoodsReceiptLst_GRPos.UrlParameterName:          grmi.NewConditionItem(meta.GoodsReceiptLst_GRPos, grmi.Equal, true),
		meta.GoodsReceiptLst_GRLPos.UrlParameterName:         grmi.NewConditionItem(meta.GoodsReceiptLst_GRLPos, grmi.Equal, true),
		meta.GoodsReceiptLst_WarehouseId.UrlParameterName:    grmi.NewConditionItem(meta.GoodsReceiptLst_WarehouseId, grmi.Equal, false),
		meta.GoodsReceiptLst_StorageAreaId.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptLst_StorageAreaId, grmi.Equal, false),
		meta.GoodsReceiptLst_StorageBinId.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptLst_StorageBinId, grmi.Equal, false),
		meta.GoodsReceiptLst_StorageRackId.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptLst_StorageRackId, grmi.Equal, false),
		meta.GoodsReceiptLst_Qty.UrlParameterName:            grmi.NewConditionItem(meta.GoodsReceiptLst_Qty, grmi.Equal, false),
		meta.GoodsReceiptLst_CtrlPara1.UrlParameterName:      grmi.NewConditionItem(meta.GoodsReceiptLst_CtrlPara1, grmi.Equal, false),
		meta.GoodsReceiptLst_CtrlPara2.UrlParameterName:      grmi.NewConditionItem(meta.GoodsReceiptLst_CtrlPara2, grmi.Equal, false),
		meta.GoodsReceiptLst_CtrlPara3.UrlParameterName:      grmi.NewConditionItem(meta.GoodsReceiptLst_CtrlPara3, grmi.Equal, false),
		meta.GoodsReceiptLst_CtrlPara4.UrlParameterName:      grmi.NewConditionItem(meta.GoodsReceiptLst_CtrlPara4, grmi.Equal, false),
		meta.GoodsReceiptLst_CtrlPara5.UrlParameterName:      grmi.NewConditionItem(meta.GoodsReceiptLst_CtrlPara5, grmi.Equal, false),
		meta.GoodsReceiptLst_CtrlPara6.UrlParameterName:      grmi.NewConditionItem(meta.GoodsReceiptLst_CtrlPara6, grmi.Equal, false),
		meta.GoodsReceiptLst_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptLst_LastModify, grmi.Approximate, false),
		meta.GoodsReceiptLst_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.GoodsReceiptLst_LastUser, grmi.Equal, false),
		meta.GoodsReceiptLst_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsReceiptLstService.InsertOne
 *
 ******************************************************************************/
func (impl *GoodsReceiptLstServiceImplement) InsertOne(user *models.Usertab, entity *model.GoodsReceiptLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewGoodsReceiptLstDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.GoodsReceiptId, entity.GRPos, entity.GRLPos)
	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/wm/GoodsReceiptLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *GoodsReceiptLstServiceImplement) DeleteOne(user *models.Usertab, goodsReceiptId string, gRPos int, gRLPos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsReceiptLstService.SelectOne
 *
 ******************************************************************************/
func (impl *GoodsReceiptLstServiceImplement) SelectOne(user *models.Usertab, goodsReceiptId string, gRPos int, gRLPos int) (*model.GoodsReceiptLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsReceiptLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *GoodsReceiptLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.GoodsReceiptLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewGoodsReceiptLstDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.GoodsReceiptId, entity.GRPos, entity.GRLPos)
	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/GoodsReceiptLstService.Insert
 *
 ******************************************************************************/
func (impl *GoodsReceiptLstServiceImplement) Insert(user *models.Usertab, entities *[]model.GoodsReceiptLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.GoodsReceiptId, entity.GRPos, entity.GRLPos)
		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/GoodsReceiptLstService.Delete
 *
 ******************************************************************************/
func (impl *GoodsReceiptLstServiceImplement) Delete(user *models.Usertab, entities *[]model.GoodsReceiptLst) error {

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

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

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

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

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

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

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

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

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