// 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              : StorageBin的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 11:25:15
 *
 ******************************************************************************/
var DefaultConditionOfStorageBin = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.StorageBin_StorageBinId.UrlParameterName:       grmi.NewConditionItem(meta.StorageBin_StorageBinId, grmi.Equal, true),
		meta.StorageBin_WarehouseId.UrlParameterName:        grmi.NewConditionItem(meta.StorageBin_WarehouseId, grmi.Equal, true),
		meta.StorageBin_StorageAreaId.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_StorageAreaId, grmi.Equal, false),
		meta.StorageBin_Descr.UrlParameterName:              grmi.NewConditionItem(meta.StorageBin_Descr, grmi.Equal, false),
		meta.StorageBin_Status.UrlParameterName:             grmi.NewConditionItem(meta.StorageBin_Status, grmi.Equal, false),
		meta.StorageBin_ActualStock.UrlParameterName:        grmi.NewConditionItem(meta.StorageBin_ActualStock, grmi.Equal, false),
		meta.StorageBin_BlockedStock.UrlParameterName:       grmi.NewConditionItem(meta.StorageBin_BlockedStock, grmi.Equal, false),
		meta.StorageBin_AvailableStock.UrlParameterName:     grmi.NewConditionItem(meta.StorageBin_AvailableStock, grmi.Equal, false),
		meta.StorageBin_ReservedStock.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_ReservedStock, grmi.Equal, false),
		meta.StorageBin_ReservableStock.UrlParameterName:    grmi.NewConditionItem(meta.StorageBin_ReservableStock, grmi.Equal, false),
		meta.StorageBin_RemovalReason.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_RemovalReason, grmi.Equal, false),
		meta.StorageBin_RemovalPerson.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_RemovalPerson, grmi.Equal, false),
		meta.StorageBin_LastStorageBlock.UrlParameterName:   grmi.NewConditionItem(meta.StorageBin_LastStorageBlock, grmi.Approximate, false),
		meta.StorageBin_StorageBlockReason.UrlParameterName: grmi.NewConditionItem(meta.StorageBin_StorageBlockReason, grmi.Equal, false),
		meta.StorageBin_StoragePerson.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_StoragePerson, grmi.Equal, false),
		meta.StorageBin_StorageCapacity.UrlParameterName:    grmi.NewConditionItem(meta.StorageBin_StorageCapacity, grmi.Equal, false),
		meta.StorageBin_LastRemoval.UrlParameterName:        grmi.NewConditionItem(meta.StorageBin_LastRemoval, grmi.Approximate, false),
		meta.StorageBin_LastStorage.UrlParameterName:        grmi.NewConditionItem(meta.StorageBin_LastStorage, grmi.Approximate, false),
		meta.StorageBin_RemainStock.UrlParameterName:        grmi.NewConditionItem(meta.StorageBin_RemainStock, grmi.Equal, false),
		meta.StorageBin_LastStockTaking.UrlParameterName:    grmi.NewConditionItem(meta.StorageBin_LastStockTaking, grmi.Approximate, false),
		meta.StorageBin_Weight.UrlParameterName:             grmi.NewConditionItem(meta.StorageBin_Weight, grmi.Equal, false),
		meta.StorageBin_WeightUom.UrlParameterName:          grmi.NewConditionItem(meta.StorageBin_WeightUom, grmi.Equal, false),
		meta.StorageBin_Length.UrlParameterName:             grmi.NewConditionItem(meta.StorageBin_Length, grmi.Equal, false),
		meta.StorageBin_Width.UrlParameterName:              grmi.NewConditionItem(meta.StorageBin_Width, grmi.Equal, false),
		meta.StorageBin_Height.UrlParameterName:             grmi.NewConditionItem(meta.StorageBin_Height, grmi.Equal, false),
		meta.StorageBin_SizeUom.UrlParameterName:            grmi.NewConditionItem(meta.StorageBin_SizeUom, grmi.Equal, false),
		meta.StorageBin_MovementClass.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_MovementClass, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara1.UrlParameterName:       grmi.NewConditionItem(meta.StorageBin_BinCtrlPara1, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara2.UrlParameterName:       grmi.NewConditionItem(meta.StorageBin_BinCtrlPara2, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara3.UrlParameterName:       grmi.NewConditionItem(meta.StorageBin_BinCtrlPara3, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara4.UrlParameterName:       grmi.NewConditionItem(meta.StorageBin_BinCtrlPara4, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara5.UrlParameterName:       grmi.NewConditionItem(meta.StorageBin_BinCtrlPara5, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara6.UrlParameterName:       grmi.NewConditionItem(meta.StorageBin_BinCtrlPara6, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara7.UrlParameterName:       grmi.NewConditionItem(meta.StorageBin_BinCtrlPara7, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara8.UrlParameterName:       grmi.NewConditionItem(meta.StorageBin_BinCtrlPara8, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara9.UrlParameterName:       grmi.NewConditionItem(meta.StorageBin_BinCtrlPara9, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara10.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_BinCtrlPara10, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara11.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_BinCtrlPara11, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara12.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_BinCtrlPara12, grmi.Equal, false),
		meta.StorageBin_BinCtrlPara13.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_BinCtrlPara13, grmi.Approximate, false),
		meta.StorageBin_BinCtrlPara14.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_BinCtrlPara14, grmi.Approximate, false),
		meta.StorageBin_BinCtrlPara15.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_BinCtrlPara15, grmi.Approximate, false),
		meta.StorageBin_BinCtrlPara16.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_BinCtrlPara16, grmi.Approximate, false),
		meta.StorageBin_StorageRackId.UrlParameterName:      grmi.NewConditionItem(meta.StorageBin_StorageRackId, grmi.Equal, false),
		meta.StorageBin_RowIndex.UrlParameterName:           grmi.NewConditionItem(meta.StorageBin_RowIndex, grmi.Equal, false),
		meta.StorageBin_ColIndex.UrlParameterName:           grmi.NewConditionItem(meta.StorageBin_ColIndex, grmi.Equal, false),
		meta.StorageBin_LastModify.UrlParameterName:         grmi.NewConditionItem(meta.StorageBin_LastModify, grmi.Approximate, false),
		meta.StorageBin_LastUser.UrlParameterName:           grmi.NewConditionItem(meta.StorageBin_LastUser, grmi.Equal, false),
		meta.StorageBin_CreateTime.UrlParameterName:         grmi.NewConditionItem(meta.StorageBin_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : StorageBin的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 11:25:15
 *
 ******************************************************************************/
var DefaultConditionOfStorageBinAndPaging = grmi.NewCondition(
	DefaultConditionOfStorageBin.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : StorageBinServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : StorageBin的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 11:25:15
 *
 ******************************************************************************/
type StorageBinServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewStorageBinDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个StorageBinDAOImplement实例
 *
 * @Return Value             : StorageBinDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 11:25:15
 *
 ******************************************************************************/
func NewStorageBinServiceImplement() *StorageBinServiceImplement {
	return &StorageBinServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageBinService.InsertOne
 *
 ******************************************************************************/
func (impl *StorageBinServiceImplement) InsertOne(user *models.Usertab, entity *model.StorageBin) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()

	dao := dal.NewStorageBinDAO(session, user.Userid)
	houseDao := dal.NewWarehouseDAO(session, user.Userid)
	areaDao := dal.NewStorageAreaDAO(session, user.Userid)
	rackDao := dal.NewStorageRackDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.StorageBinId, entity.WarehouseId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	wareHouse, err := houseDao.SelectOne(entity.WarehouseId)
	if err != nil {
		return err
	}
	if wareHouse == nil {
		return grmi.NewBusinessError("不存在对应的库房")
	}
	area, err := areaDao.SelectOne(entity.StorageAreaId, entity.WarehouseId)
	if err != nil {
		return err
	}
	if area == nil {
		return grmi.NewBusinessError("不存在对应的库区")
	}
	rack, err := rackDao.SelectOne(entity.StorageRackId, entity.WarehouseId)
	if err != nil {
		return err
	}
	if rack == nil {
		return grmi.NewBusinessError("不存在对应的库架")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageBinService.DeleteOne
 *
 ******************************************************************************/
func (impl *StorageBinServiceImplement) DeleteOne(user *models.Usertab, storageBinId string, warehouseId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageBinService.SelectOne
 *
 ******************************************************************************/
func (impl *StorageBinServiceImplement) SelectOne(user *models.Usertab, storageBinId string, warehouseId string) (*model.StorageBin, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageBinService.UpdateOne
 *
 ******************************************************************************/
func (impl *StorageBinServiceImplement) UpdateOne(user *models.Usertab, entity *model.StorageBin) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageBinService.Insert
 *
 ******************************************************************************/
func (impl *StorageBinServiceImplement) Insert(user *models.Usertab, entities *[]model.StorageBin) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.StorageBinId, entity.WarehouseId)
		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/StorageBinService.Delete
 *
 ******************************************************************************/
func (impl *StorageBinServiceImplement) Delete(user *models.Usertab, entities *[]model.StorageBin) error {

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

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

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

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

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

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

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

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

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