// 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              : Warehouse的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfWarehouse = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Warehouse_WarehouseId.UrlParameterName:          grmi.NewConditionItem(meta.Warehouse_WarehouseId, grmi.Equal, true),
		meta.Warehouse_Descr.UrlParameterName:                grmi.NewConditionItem(meta.Warehouse_Descr, grmi.Equal, false),
		meta.Warehouse_Status.UrlParameterName:               grmi.NewConditionItem(meta.Warehouse_Status, grmi.Equal, false),
		meta.Warehouse_WarehouseType.UrlParameterName:        grmi.NewConditionItem(meta.Warehouse_WarehouseType, grmi.Equal, false),
		meta.Warehouse_StorageMethod.UrlParameterName:        grmi.NewConditionItem(meta.Warehouse_StorageMethod, grmi.Equal, false),
		meta.Warehouse_Comment.UrlParameterName:              grmi.NewConditionItem(meta.Warehouse_Comment, grmi.Equal, false),
		meta.Warehouse_AllocationStrategy.UrlParameterName:   grmi.NewConditionItem(meta.Warehouse_AllocationStrategy, grmi.Equal, false),
		meta.Warehouse_RuleSetId.UrlParameterName:            grmi.NewConditionItem(meta.Warehouse_RuleSetId, grmi.Equal, false),
		meta.Warehouse_CompleteStockRemoval.UrlParameterName: grmi.NewConditionItem(meta.Warehouse_CompleteStockRemoval, grmi.Equal, false),
		meta.Warehouse_TransportSystemNr.UrlParameterName:    grmi.NewConditionItem(meta.Warehouse_TransportSystemNr, grmi.Equal, false),
		meta.Warehouse_TransportPoint.UrlParameterName:       grmi.NewConditionItem(meta.Warehouse_TransportPoint, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara1.UrlParameterName:   grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara1, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara2.UrlParameterName:   grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara2, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara3.UrlParameterName:   grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara3, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara4.UrlParameterName:   grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara4, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara5.UrlParameterName:   grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara5, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara6.UrlParameterName:   grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara6, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara7.UrlParameterName:   grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara7, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara8.UrlParameterName:   grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara8, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara9.UrlParameterName:   grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara9, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara10.UrlParameterName:  grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara10, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara11.UrlParameterName:  grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara11, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara12.UrlParameterName:  grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara12, grmi.Equal, false),
		meta.Warehouse_WarehouseCtrlPara13.UrlParameterName:  grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara13, grmi.Approximate, false),
		meta.Warehouse_WarehouseCtrlPara14.UrlParameterName:  grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara14, grmi.Approximate, false),
		meta.Warehouse_WarehouseCtrlPara15.UrlParameterName:  grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara15, grmi.Approximate, false),
		meta.Warehouse_WarehouseCtrlPara16.UrlParameterName:  grmi.NewConditionItem(meta.Warehouse_WarehouseCtrlPara16, grmi.Approximate, false),
		meta.Warehouse_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.Warehouse_LastModify, grmi.Approximate, false),
		meta.Warehouse_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.Warehouse_LastUser, grmi.Equal, false),
		meta.Warehouse_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.Warehouse_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/WarehouseService.InsertOne
 *
 ******************************************************************************/
func (impl *WarehouseServiceImplement) InsertOne(user *models.Usertab, entity *model.Warehouse) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWarehouseDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.WarehouseId)
	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/WarehouseService.DeleteOne
 *
 ******************************************************************************/
func (impl *WarehouseServiceImplement) DeleteOne(user *models.Usertab, warehouseId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/WarehouseService.SelectOne
 *
 ******************************************************************************/
func (impl *WarehouseServiceImplement) SelectOne(user *models.Usertab, warehouseId string) (*model.Warehouse, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/WarehouseService.UpdateOne
 *
 ******************************************************************************/
func (impl *WarehouseServiceImplement) UpdateOne(user *models.Usertab, entity *model.Warehouse) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWarehouseDAO(session, user.Userid)
	record, err := dao.SelectOne(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/WarehouseService.Insert
 *
 ******************************************************************************/
func (impl *WarehouseServiceImplement) Insert(user *models.Usertab, entities *[]model.Warehouse) error {

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

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

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

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

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

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

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

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

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

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

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

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