// 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              : WorkArea的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfWorkArea = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkArea_WorkAreaId.UrlParameterName:   grmi.NewConditionItem(meta.WorkArea_WorkAreaId, grmi.Equal, true),
		meta.WorkArea_WorkAreaType.UrlParameterName: grmi.NewConditionItem(meta.WorkArea_WorkAreaType, grmi.Equal, false),
		meta.WorkArea_RelevanceId.UrlParameterName:  grmi.NewConditionItem(meta.WorkArea_RelevanceId, grmi.Equal, false),
		meta.WorkArea_WACtrlPara1.UrlParameterName:  grmi.NewConditionItem(meta.WorkArea_WACtrlPara1, grmi.Equal, false),
		meta.WorkArea_WACtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.WorkArea_WACtrlPara2, grmi.Equal, false),
		meta.WorkArea_WACtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.WorkArea_WACtrlPara3, grmi.Equal, false),
		meta.WorkArea_WACtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.WorkArea_WACtrlPara4, grmi.Equal, false),
		meta.WorkArea_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.WorkArea_LastModify, grmi.Approximate, false),
		meta.WorkArea_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.WorkArea_LastUser, grmi.Equal, false),
		meta.WorkArea_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.WorkArea_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/WorkAreaService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkAreaServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkArea) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkAreaDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.WorkAreaId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	houseDao := dal.NewWarehouseDAO(session, user.Userid)
	areaDao := dal.NewStorageAreaDAO(session, user.Userid)
	rackDao := dal.NewStorageRackDAO(session, user.Userid)
	switch entity.WorkAreaType {
	case model.WORK_AREA_TYPE_WARE_HOUSE:
		wareHouse, err := houseDao.SelectOne(entity.RelevanceId)
		if err != nil {
			return err
		}
		if wareHouse == nil {
			return grmi.NewBusinessError("不存在对应的库房")
		}
	case model.WORK_AREA_TYPE_STORAGE_AREA:
		area, err := areaDao.SelectOne(entity.WACtrlPara3, entity.RelevanceId)
		if err != nil {
			return err
		}
		if area == nil {
			return grmi.NewBusinessError("不存在对应的库区")
		}
	case model.WORK_AREA_TYPE_STORAGE_RACK:
		rack, err := rackDao.SelectOne(entity.WACtrlPara3, entity.RelevanceId)
		if err != nil {
			return err
		}
		if rack == nil {
			return grmi.NewBusinessError("不存在对应的库架")
		}
	default:
		return grmi.NewBusinessError("不支持的类型")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/WorkAreaService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkAreaServiceImplement) DeleteOne(user *models.Usertab, workAreaId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewWorkAreaDAO(session, user.Userid)
	workAreaUserDao := dal.NewWorkAreaUserLstDAO(session, user.Userid)
	record, err := dao.SelectOne(workAreaId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = workAreaUserDao.DeleteWhere([]grmi.Predicate{meta.WorkAreaUserLst_WorkAreaId.NewPredicate(grmi.Equal, workAreaId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.DeleteOne(workAreaId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/WorkAreaService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkAreaServiceImplement) SelectOne(user *models.Usertab, workAreaId string) (*model.WorkArea, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/WorkAreaService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkAreaServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkArea) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkAreaDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.WorkAreaId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	houseDao := dal.NewWarehouseDAO(session, user.Userid)
	areaDao := dal.NewStorageAreaDAO(session, user.Userid)
	rackDao := dal.NewStorageRackDAO(session, user.Userid)
	switch entity.WorkAreaType {
	case model.WORK_AREA_TYPE_WARE_HOUSE:
		wareHouse, err := houseDao.SelectOne(entity.RelevanceId)
		if err != nil {
			return err
		}
		if wareHouse == nil {
			return grmi.NewBusinessError("不存在对应的库房")
		}
	case model.WORK_AREA_TYPE_STORAGE_AREA:
		area, err := areaDao.SelectOne(entity.WACtrlPara3, entity.RelevanceId)
		if err != nil {
			return err
		}
		if area == nil {
			return grmi.NewBusinessError("不存在对应的库区")
		}
	case model.WORK_AREA_TYPE_STORAGE_RACK:
		rack, err := rackDao.SelectOne(entity.WACtrlPara3, entity.RelevanceId)
		if err != nil {
			return err
		}
		if rack == nil {
			return grmi.NewBusinessError("不存在对应的库架")
		}
	default:
		return grmi.NewBusinessError("不支持的类型")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/WorkAreaService.Insert
 *
 ******************************************************************************/
func (impl *WorkAreaServiceImplement) Insert(user *models.Usertab, entities *[]model.WorkArea) error {

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

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

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

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

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

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

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

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

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

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

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

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