// 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              : StorageArea的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfStorageArea = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.StorageArea_StorageAreaId.UrlParameterName:  grmi.NewConditionItem(meta.StorageArea_StorageAreaId, grmi.Equal, true),
		meta.StorageArea_WarehouseId.UrlParameterName:    grmi.NewConditionItem(meta.StorageArea_WarehouseId, grmi.Equal, true),
		meta.StorageArea_Descr.UrlParameterName:          grmi.NewConditionItem(meta.StorageArea_Descr, grmi.Equal, false),
		meta.StorageArea_Status.UrlParameterName:         grmi.NewConditionItem(meta.StorageArea_Status, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara1.UrlParameterName:  grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara1, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara2, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara3, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara4, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara5.UrlParameterName:  grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara5, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara6.UrlParameterName:  grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara6, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara7.UrlParameterName:  grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara7, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara8.UrlParameterName:  grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara8, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara9.UrlParameterName:  grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara9, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara10.UrlParameterName: grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara10, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara11.UrlParameterName: grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara11, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara12.UrlParameterName: grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara12, grmi.Equal, false),
		meta.StorageArea_AreaCtrlPara13.UrlParameterName: grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara13, grmi.Approximate, false),
		meta.StorageArea_AreaCtrlPara14.UrlParameterName: grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara14, grmi.Approximate, false),
		meta.StorageArea_AreaCtrlPara15.UrlParameterName: grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara15, grmi.Approximate, false),
		meta.StorageArea_AreaCtrlPara16.UrlParameterName: grmi.NewConditionItem(meta.StorageArea_AreaCtrlPara16, grmi.Approximate, false),
		meta.StorageArea_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.StorageArea_LastModify, grmi.Approximate, false),
		meta.StorageArea_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.StorageArea_LastUser, grmi.Equal, false),
		meta.StorageArea_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.StorageArea_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageAreaService.InsertOne
 *
 ******************************************************************************/
func (impl *StorageAreaServiceImplement) InsertOne(user *models.Usertab, entity *model.StorageArea) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageAreaService.DeleteOne
 *
 ******************************************************************************/
func (impl *StorageAreaServiceImplement) DeleteOne(user *models.Usertab, storageAreaId string, warehouseId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewStorageAreaDAO(session, user.Userid)
	rackDao := dal.NewStorageRackDAO(session, user.Userid)
	binDao := dal.NewStorageBinDAO(session, user.Userid)
	workAreaDao := dal.NewWorkAreaDAO(session, user.Userid)
	record, err := dao.SelectOne(storageAreaId, warehouseId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = rackDao.DeleteWhere([]grmi.Predicate{meta.StorageRack_StorageAreaId.NewPredicate(grmi.Equal, storageAreaId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = binDao.DeleteWhere([]grmi.Predicate{meta.StorageBin_StorageAreaId.NewPredicate(grmi.Equal, storageAreaId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = workAreaDao.DeleteWorkAreaLst(model.WORK_AREA_TYPE_STORAGE_RACK, warehouseId, storageAreaId)
	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, storageAreaId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.DeleteOne(storageAreaId, warehouseId)
	if err != nil {
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageAreaService.SelectOne
 *
 ******************************************************************************/
func (impl *StorageAreaServiceImplement) SelectOne(user *models.Usertab, storageAreaId string, warehouseId string) (*model.StorageArea, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/StorageAreaService.UpdateOne
 *
 ******************************************************************************/
func (impl *StorageAreaServiceImplement) UpdateOne(user *models.Usertab, entity *model.StorageArea) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStorageAreaDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.StorageAreaId, entity.WarehouseId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	if record.WarehouseId != entity.WarehouseId {
		houseDao := dal.NewWarehouseDAO(session, user.Userid)
		wareHouse, err := houseDao.SelectOne(entity.WarehouseId)
		if err != nil {
			return err
		}
		if wareHouse == 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/StorageAreaService.Insert
 *
 ******************************************************************************/
func (impl *StorageAreaServiceImplement) Insert(user *models.Usertab, entities *[]model.StorageArea) error {

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

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

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

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

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

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

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

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

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

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

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

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