// 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              : WorkAreaUserLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfWorkAreaUserLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkAreaUserLst_WorkAreaId.UrlParameterName: grmi.NewConditionItem(meta.WorkAreaUserLst_WorkAreaId, grmi.Equal, true),
		meta.WorkAreaUserLst_UserId.UrlParameterName:     grmi.NewConditionItem(meta.WorkAreaUserLst_UserId, grmi.Equal, true),
		meta.WorkAreaUserLst_Pos.UrlParameterName:        grmi.NewConditionItem(meta.WorkAreaUserLst_Pos, grmi.Equal, false),
		meta.WorkAreaUserLst_CtrlLevel.UrlParameterName:  grmi.NewConditionItem(meta.WorkAreaUserLst_CtrlLevel, grmi.Equal, false),
		meta.WorkAreaUserLst_CtrlPara1.UrlParameterName:  grmi.NewConditionItem(meta.WorkAreaUserLst_CtrlPara1, grmi.Equal, false),
		meta.WorkAreaUserLst_CtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.WorkAreaUserLst_CtrlPara2, grmi.Equal, false),
		meta.WorkAreaUserLst_CtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.WorkAreaUserLst_CtrlPara3, grmi.Equal, false),
		meta.WorkAreaUserLst_CtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.WorkAreaUserLst_CtrlPara4, grmi.Equal, false),
		meta.WorkAreaUserLst_LastModify.UrlParameterName: grmi.NewConditionItem(meta.WorkAreaUserLst_LastModify, grmi.Approximate, false),
		meta.WorkAreaUserLst_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.WorkAreaUserLst_LastUser, grmi.Equal, false),
		meta.WorkAreaUserLst_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.WorkAreaUserLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/WorkAreaUserLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkAreaUserLstServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkAreaUserLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkAreaUserLstDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.WorkAreaId, entity.UserId)
	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/WorkAreaUserLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkAreaUserLstServiceImplement) DeleteOne(user *models.Usertab, workAreaId string, userId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/WorkAreaUserLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkAreaUserLstServiceImplement) SelectOne(user *models.Usertab, workAreaId string, userId string) (*model.WorkAreaUserLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/WorkAreaUserLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkAreaUserLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkAreaUserLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkAreaUserLstDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.WorkAreaId, entity.UserId)
	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/WorkAreaUserLstService.Insert
 *
 ******************************************************************************/
func (impl *WorkAreaUserLstServiceImplement) Insert(user *models.Usertab, entities *model.WorkAreaUserRequest) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewWorkAreaUserLstDAO(session, user.Userid)
	var userTab models.Usertab
	userTab.Userid = entities.UserId
	userTab.Pid = user.Pid
	dbUser, err := userTab.Get()
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if dbUser.Pid == 0 {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的用户，请查看")
	}
	workAreaUserLi := entities.WorkAreaUserLstLi
	for _, entity := range workAreaUserLi {
		if entity.UserId != entities.UserId {
			_ = session.Rollback()
			return grmi.NewBusinessError("人员ID不一致")
		}
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.WorkAreaUserLst_UserId.NewPredicate(grmi.Equal, entities.UserId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.Insert(&entities.WorkAreaUserLstLi)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/WorkAreaUserLstService.Delete
 *
 ******************************************************************************/
func (impl *WorkAreaUserLstServiceImplement) Delete(user *models.Usertab, entities *[]model.WorkAreaUserLst) error {

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

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

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

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

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

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

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

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

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