// 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              : StorageRack的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-05 10:40:38
 *
 ******************************************************************************/
var DefaultConditionOfStorageRack = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.StorageRack_StorageRackId.UrlParameterName: grmi.NewConditionItem(meta.StorageRack_StorageRackId, grmi.Equal, true),
		meta.StorageRack_WarehouseId.UrlParameterName:   grmi.NewConditionItem(meta.StorageRack_WarehouseId, grmi.Equal, true),
		meta.StorageRack_StorageAreaId.UrlParameterName: grmi.NewConditionItem(meta.StorageRack_StorageAreaId, grmi.Equal, false),
		meta.StorageRack_Descr.UrlParameterName:         grmi.NewConditionItem(meta.StorageRack_Descr, grmi.Equal, false),
		meta.StorageRack_Status.UrlParameterName:        grmi.NewConditionItem(meta.StorageRack_Status, grmi.Equal, false),
		meta.StorageRack_SRCtrlPara1.UrlParameterName:   grmi.NewConditionItem(meta.StorageRack_SRCtrlPara1, grmi.Equal, false),
		meta.StorageRack_SRCtrlPara2.UrlParameterName:   grmi.NewConditionItem(meta.StorageRack_SRCtrlPara2, grmi.Equal, false),
		meta.StorageRack_SRCtrlPara3.UrlParameterName:   grmi.NewConditionItem(meta.StorageRack_SRCtrlPara3, grmi.Equal, false),
		meta.StorageRack_SRCtrlPara4.UrlParameterName:   grmi.NewConditionItem(meta.StorageRack_SRCtrlPara4, grmi.Equal, false),
		meta.StorageRack_SRCtrlPara5.UrlParameterName:   grmi.NewConditionItem(meta.StorageRack_SRCtrlPara5, grmi.Equal, false),
		meta.StorageRack_SRCtrlPara6.UrlParameterName:   grmi.NewConditionItem(meta.StorageRack_SRCtrlPara6, grmi.Equal, false),
		meta.StorageRack_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.StorageRack_LastModify, grmi.Approximate, false),
		meta.StorageRack_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.StorageRack_LastUser, grmi.Equal, false),
		meta.StorageRack_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.StorageRack_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : StorageRack的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-05 10:40:38
 *
 ******************************************************************************/
var DefaultConditionOfStorageRackAndPaging = grmi.NewCondition(
	DefaultConditionOfStorageRack.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : StorageRackServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : StorageRack的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-05 10:40:38
 *
 ******************************************************************************/
type StorageRackServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewStorageRackDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个StorageRackDAOImplement实例
 *
 * @Return Value             : StorageRackDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-05 10:40:38
 *
 ******************************************************************************/
func NewStorageRackServiceImplement() *StorageRackServiceImplement {
	return &StorageRackServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageRackService.InsertOne
 *
 ******************************************************************************/
func (impl *StorageRackServiceImplement) InsertOne(user *models.Usertab, entity *model.StorageRack) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStorageRackDAO(session, user.Userid)
	houseDao := dal.NewWarehouseDAO(session, user.Userid)
	areaDao := dal.NewStorageAreaDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.StorageRackId, 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("不存在对应的库区")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageRackService.DeleteOne
 *
 ******************************************************************************/
func (impl *StorageRackServiceImplement) DeleteOne(user *models.Usertab, storageRackId string, warehouseId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewStorageRackDAO(session, user.Userid)
	workAreaDao := dal.NewWorkAreaDAO(session, user.Userid)
	binDao := dal.NewStorageBinDAO(session, user.Userid)
	record, err := dao.SelectOne(storageRackId, warehouseId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = workAreaDao.DeleteWorkAreaLst(model.WORK_AREA_TYPE_STORAGE_RACK, warehouseId, storageRackId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = workAreaDao.DeleteWhere([]grmi.Predicate{meta.WorkArea_WorkAreaType.NewPredicate(grmi.Equal, model.WORK_AREA_TYPE_STORAGE_RACK), meta.WorkArea_RelevanceId.NewPredicate(grmi.Equal, warehouseId), meta.WorkArea_WACtrlPara3.NewPredicate(grmi.Equal, storageRackId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = binDao.DeleteWhere([]grmi.Predicate{meta.StorageBin_StorageRackId.NewPredicate(grmi.Equal, storageRackId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.DeleteOne(storageRackId, warehouseId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageRackService.SelectOne
 *
 ******************************************************************************/
func (impl *StorageRackServiceImplement) SelectOne(user *models.Usertab, storageRackId string, warehouseId string) (*model.StorageRack, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageRackService.UpdateOne
 *
 ******************************************************************************/
func (impl *StorageRackServiceImplement) UpdateOne(user *models.Usertab, entity *model.StorageRack) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStorageRackDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.StorageRackId, entity.WarehouseId)
	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/StorageRackService.Insert
 *
 ******************************************************************************/
func (impl *StorageRackServiceImplement) Insert(user *models.Usertab, entities *[]model.StorageRack) error {

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

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

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

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

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

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

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

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

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

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

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

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