// 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              : StockTransferDetail的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfStockTransferDetail = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.StockTransferDetail_StockTransferId.UrlParameterName:     grmi.NewConditionItem(meta.StockTransferDetail_StockTransferId, grmi.Equal, true),
		meta.StockTransferDetail_STPos.UrlParameterName:               grmi.NewConditionItem(meta.StockTransferDetail_STPos, grmi.Equal, true),
		meta.StockTransferDetail_ArtId.UrlParameterName:               grmi.NewConditionItem(meta.StockTransferDetail_ArtId, grmi.Equal, false),
		meta.StockTransferDetail_ArtName.UrlParameterName:             grmi.NewConditionItem(meta.StockTransferDetail_ArtName, grmi.Equal, false),
		meta.StockTransferDetail_ArtType.UrlParameterName:             grmi.NewConditionItem(meta.StockTransferDetail_ArtType, grmi.Equal, false),
		meta.StockTransferDetail_Remark.UrlParameterName:              grmi.NewConditionItem(meta.StockTransferDetail_Remark, grmi.Equal, false),
		meta.StockTransferDetail_Auditor.UrlParameterName:             grmi.NewConditionItem(meta.StockTransferDetail_Auditor, grmi.Equal, false),
		meta.StockTransferDetail_AuditTime.UrlParameterName:           grmi.NewConditionItem(meta.StockTransferDetail_AuditTime, grmi.Approximate, false),
		meta.StockTransferDetail_AuditStatus.UrlParameterName:         grmi.NewConditionItem(meta.StockTransferDetail_AuditStatus, grmi.Equal, false),
		meta.StockTransferDetail_BatchId.UrlParameterName:             grmi.NewConditionItem(meta.StockTransferDetail_BatchId, grmi.Equal, false),
		meta.StockTransferDetail_OrderedQty.UrlParameterName:          grmi.NewConditionItem(meta.StockTransferDetail_OrderedQty, grmi.Equal, false),
		meta.StockTransferDetail_OrderType.UrlParameterName:           grmi.NewConditionItem(meta.StockTransferDetail_OrderType, grmi.Equal, false),
		meta.StockTransferDetail_DeliveredQty.UrlParameterName:        grmi.NewConditionItem(meta.StockTransferDetail_DeliveredQty, grmi.Equal, false),
		meta.StockTransferDetail_QtyUom.UrlParameterName:              grmi.NewConditionItem(meta.StockTransferDetail_QtyUom, grmi.Equal, false),
		meta.StockTransferDetail_UnitPrice.UrlParameterName:           grmi.NewConditionItem(meta.StockTransferDetail_UnitPrice, grmi.Equal, false),
		meta.StockTransferDetail_TotalPrice.UrlParameterName:          grmi.NewConditionItem(meta.StockTransferDetail_TotalPrice, grmi.Equal, false),
		meta.StockTransferDetail_Currency.UrlParameterName:            grmi.NewConditionItem(meta.StockTransferDetail_Currency, grmi.Equal, false),
		meta.StockTransferDetail_InspectToggle.UrlParameterName:       grmi.NewConditionItem(meta.StockTransferDetail_InspectToggle, grmi.Equal, false),
		meta.StockTransferDetail_InspectPlanId.UrlParameterName:       grmi.NewConditionItem(meta.StockTransferDetail_InspectPlanId, grmi.Equal, false),
		meta.StockTransferDetail_InspectStatus.UrlParameterName:       grmi.NewConditionItem(meta.StockTransferDetail_InspectStatus, grmi.Equal, false),
		meta.StockTransferDetail_StorageBinId.UrlParameterName:        grmi.NewConditionItem(meta.StockTransferDetail_StorageBinId, grmi.Equal, false),
		meta.StockTransferDetail_StorageAreaId.UrlParameterName:       grmi.NewConditionItem(meta.StockTransferDetail_StorageAreaId, grmi.Equal, false),
		meta.StockTransferDetail_WarehouseId.UrlParameterName:         grmi.NewConditionItem(meta.StockTransferDetail_WarehouseId, grmi.Equal, false),
		meta.StockTransferDetail_TargetStorageBinId.UrlParameterName:  grmi.NewConditionItem(meta.StockTransferDetail_TargetStorageBinId, grmi.Equal, false),
		meta.StockTransferDetail_TargetStorageAreaId.UrlParameterName: grmi.NewConditionItem(meta.StockTransferDetail_TargetStorageAreaId, grmi.Equal, false),
		meta.StockTransferDetail_TargetWarehouseId.UrlParameterName:   grmi.NewConditionItem(meta.StockTransferDetail_TargetWarehouseId, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara1.UrlParameterName:        grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara1, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara2.UrlParameterName:        grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara2, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara3.UrlParameterName:        grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara3, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara4.UrlParameterName:        grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara4, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara5.UrlParameterName:        grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara5, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara6.UrlParameterName:        grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara6, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara7.UrlParameterName:        grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara7, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara8.UrlParameterName:        grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara8, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara9.UrlParameterName:        grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara9, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara10.UrlParameterName:       grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara10, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara11.UrlParameterName:       grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara11, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara12.UrlParameterName:       grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara12, grmi.Equal, false),
		meta.StockTransferDetail_STICtrlPara13.UrlParameterName:       grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara13, grmi.Approximate, false),
		meta.StockTransferDetail_STICtrlPara14.UrlParameterName:       grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara14, grmi.Approximate, false),
		meta.StockTransferDetail_STICtrlPara15.UrlParameterName:       grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara15, grmi.Approximate, false),
		meta.StockTransferDetail_STICtrlPara16.UrlParameterName:       grmi.NewConditionItem(meta.StockTransferDetail_STICtrlPara16, grmi.Approximate, false),
		meta.StockTransferDetail_StorageRackId.UrlParameterName:       grmi.NewConditionItem(meta.StockTransferDetail_StorageRackId, grmi.Equal, false),
		meta.StockTransferDetail_TargetStorageRackId.UrlParameterName: grmi.NewConditionItem(meta.StockTransferDetail_TargetStorageRackId, grmi.Equal, false),
		meta.StockTransferDetail_LastModify.UrlParameterName:          grmi.NewConditionItem(meta.StockTransferDetail_LastModify, grmi.Approximate, false),
		meta.StockTransferDetail_LastUser.UrlParameterName:            grmi.NewConditionItem(meta.StockTransferDetail_LastUser, grmi.Equal, false),
		meta.StockTransferDetail_CreateTime.UrlParameterName:          grmi.NewConditionItem(meta.StockTransferDetail_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StockTransferDetailService.InsertOne
 *
 ******************************************************************************/
func (impl *StockTransferDetailServiceImplement) InsertOne(user *models.Usertab, entity *model.StockTransferDetail) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStockTransferDetailDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.StockTransferId, entity.STPos)
	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/StockTransferDetailService.DeleteOne
 *
 ******************************************************************************/
func (impl *StockTransferDetailServiceImplement) DeleteOne(user *models.Usertab, stockTransferId string, sTPos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StockTransferDetailService.SelectOne
 *
 ******************************************************************************/
func (impl *StockTransferDetailServiceImplement) SelectOne(user *models.Usertab, stockTransferId string, sTPos int) (*model.StockTransferDetail, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StockTransferDetailService.UpdateOne
 *
 ******************************************************************************/
func (impl *StockTransferDetailServiceImplement) UpdateOne(user *models.Usertab, entity *model.StockTransferDetail) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStockTransferDetailDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.StockTransferId, entity.STPos)
	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/StockTransferDetailService.Insert
 *
 ******************************************************************************/
func (impl *StockTransferDetailServiceImplement) Insert(user *models.Usertab, entities *[]model.StockTransferDetail) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.StockTransferId, entity.STPos)
		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/StockTransferDetailService.Delete
 *
 ******************************************************************************/
func (impl *StockTransferDetailServiceImplement) Delete(user *models.Usertab, entities *[]model.StockTransferDetail) error {

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

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

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

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

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

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

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

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

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