// 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              : GoodsIssueLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfGoodsIssueLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.GoodsIssueLst_GoodsIssueId.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueLst_GoodsIssueId, grmi.Equal, true),
		meta.GoodsIssueLst_GIPos.UrlParameterName:         grmi.NewConditionItem(meta.GoodsIssueLst_GIPos, grmi.Equal, true),
		meta.GoodsIssueLst_GILPos.UrlParameterName:        grmi.NewConditionItem(meta.GoodsIssueLst_GILPos, grmi.Equal, true),
		meta.GoodsIssueLst_WarehouseId.UrlParameterName:   grmi.NewConditionItem(meta.GoodsIssueLst_WarehouseId, grmi.Equal, false),
		meta.GoodsIssueLst_StorageAreaId.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueLst_StorageAreaId, grmi.Equal, false),
		meta.GoodsIssueLst_StorageBinId.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueLst_StorageBinId, grmi.Equal, false),
		meta.GoodsIssueLst_StorageRackId.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueLst_StorageRackId, grmi.Equal, false),
		meta.GoodsIssueLst_Qty.UrlParameterName:           grmi.NewConditionItem(meta.GoodsIssueLst_Qty, grmi.Equal, false),
		meta.GoodsIssueLst_CtrlPara1.UrlParameterName:     grmi.NewConditionItem(meta.GoodsIssueLst_CtrlPara1, grmi.Equal, false),
		meta.GoodsIssueLst_CtrlPara2.UrlParameterName:     grmi.NewConditionItem(meta.GoodsIssueLst_CtrlPara2, grmi.Equal, false),
		meta.GoodsIssueLst_CtrlPara3.UrlParameterName:     grmi.NewConditionItem(meta.GoodsIssueLst_CtrlPara3, grmi.Equal, false),
		meta.GoodsIssueLst_CtrlPara4.UrlParameterName:     grmi.NewConditionItem(meta.GoodsIssueLst_CtrlPara4, grmi.Equal, false),
		meta.GoodsIssueLst_CtrlPara5.UrlParameterName:     grmi.NewConditionItem(meta.GoodsIssueLst_CtrlPara5, grmi.Equal, false),
		meta.GoodsIssueLst_CtrlPara6.UrlParameterName:     grmi.NewConditionItem(meta.GoodsIssueLst_CtrlPara6, grmi.Equal, false),
		meta.GoodsIssueLst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.GoodsIssueLst_LastModify, grmi.Approximate, false),
		meta.GoodsIssueLst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.GoodsIssueLst_LastUser, grmi.Equal, false),
		meta.GoodsIssueLst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.GoodsIssueLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsIssueLstService.InsertOne
 *
 ******************************************************************************/
func (impl *GoodsIssueLstServiceImplement) InsertOne(user *models.Usertab, entity *model.GoodsIssueLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewGoodsIssueLstDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.GoodsIssueId, entity.GIPos, entity.GILPos)
	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/GoodsIssueLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *GoodsIssueLstServiceImplement) DeleteOne(user *models.Usertab, goodsIssueId string, gIPos int, gILPos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsIssueLstService.SelectOne
 *
 ******************************************************************************/
func (impl *GoodsIssueLstServiceImplement) SelectOne(user *models.Usertab, goodsIssueId string, gIPos int, gILPos int) (*model.GoodsIssueLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsIssueLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *GoodsIssueLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.GoodsIssueLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewGoodsIssueLstDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.GoodsIssueId, entity.GIPos, entity.GILPos)
	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/GoodsIssueLstService.Insert
 *
 ******************************************************************************/
func (impl *GoodsIssueLstServiceImplement) Insert(user *models.Usertab, entities *[]model.GoodsIssueLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.GoodsIssueId, entity.GIPos, entity.GILPos)
		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/GoodsIssueLstService.Delete
 *
 ******************************************************************************/
func (impl *GoodsIssueLstServiceImplement) Delete(user *models.Usertab, entities *[]model.GoodsIssueLst) error {

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

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

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

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

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

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

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

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

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