// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/base"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/base"
	model "LAPP_ACURA_MOM_BACKEND/models/base"
)

/******************************************************************************
 *
 * @Description              : RoleRel的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-21 10:36:05
 *
 ******************************************************************************/
var DefaultConditionOfRoleRel = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.RoleRel_RoleId.UrlParameterName:     grmi.NewConditionItem(meta.RoleRel_RoleId, grmi.Equal, true),
		meta.RoleRel_WorkLineid.UrlParameterName: grmi.NewConditionItem(meta.RoleRel_WorkLineid, grmi.Equal, true),
		meta.RoleRel_LastModify.UrlParameterName: grmi.NewConditionItem(meta.RoleRel_LastModify, grmi.Approximate, false),
		meta.RoleRel_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.RoleRel_LastUser, grmi.Equal, false),
		meta.RoleRel_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.RoleRel_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : RoleRel的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-21 10:36:05
 *
 ******************************************************************************/
var DefaultConditionOfRoleRelAndPaging = grmi.NewCondition(
	DefaultConditionOfRoleRel.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : RoleRelServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : RoleRel的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-21 10:36:05
 *
 ******************************************************************************/
type RoleRelServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewRoleRelDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个RoleRelDAOImplement实例
 *
 * @Return Value             : RoleRelDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-21 10:36:05
 *
 ******************************************************************************/
func NewRoleRelServiceImplement() *RoleRelServiceImplement {
	return &RoleRelServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleRelService.InsertOne
 *
 ******************************************************************************/
func (impl *RoleRelServiceImplement) InsertOne(user *global.User, entity *model.RoleRel) error {

	grmi.Log(user, "/services/base/implments/RoleRel.service.impl.go", "InsertOneRoleRel", "插入一个RoleRel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRoleRelDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.RoleId, entity.WorkLineid)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleRelService.DeleteOne
 *
 ******************************************************************************/
func (impl *RoleRelServiceImplement) DeleteOne(user *global.User, roleId int, workLineid string) error {

	grmi.Log(user, "/services/base/implments/RoleRel.service.impl.go", "DeleteOneRoleRel", "删除一个RoleRel")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleRelService.SelectOne
 *
 ******************************************************************************/
func (impl *RoleRelServiceImplement) SelectOne(user *global.User, roleId int, workLineid string) (*model.RoleRel, error) {

	grmi.Log(user, "/services/base/implments/RoleRel.service.impl.go", "SelectOneRoleRel", "查询一个RoleRel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRoleRelDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(roleId, workLineid)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleRelService.UpdateOne
 *
 ******************************************************************************/
func (impl *RoleRelServiceImplement) UpdateOne(user *global.User, entity *model.RoleRel) error {

	grmi.Log(user, "/services/base/implments/RoleRel.service.impl.go", "UpdateOneRoleRel", "修改一个RoleRel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRoleRelDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.RoleId, entity.WorkLineid)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleRelService.Insert
 *
 ******************************************************************************/
func (impl *RoleRelServiceImplement) Insert(user *global.User, entities *model.RoleRelInsert) error {

	grmi.Log(user, "/services/base/implments/RoleRel.service.impl.go", "InsertRoleRel", "插入多个RoleRel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	roleDao := dal.NewRoleDAO(session, user.UserId)
	roleId := entities.RoleId
	role, err := roleDao.SelectOne(roleId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if role == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("角色在系统中不存在")
	}
	li := entities.RoleRelLi
	dao := dal.NewRoleRelDAO(session, user.PlantNr, user.UserId)
	workLineDao := dal.NewWorkLineDAO(session, user.PlantNr, user.UserId)
	for _, entity := range li {
		if entity.RoleId != roleId {
			_ = session.Rollback()
			return grmi.NewBusinessError("RoleId不一致")
		}
		workLine, err := workLineDao.SelectOne(entity.WorkLineid)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		if workLine == nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("该产线不存在,workLineId:" + entity.WorkLineid)
		}
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.RoleRel_RoleId.NewPredicate(grmi.Equal, roleId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if len(li) != 0 {
		err = dao.Insert(&li)
		if err != nil {
			_ = session.Rollback()
			return err
		}
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleRelService.Delete
 *
 ******************************************************************************/
func (impl *RoleRelServiceImplement) Delete(user *global.User, entities *[]model.RoleRel) error {

	grmi.Log(user, "/services/base/implments/RoleRel.service.impl.go", "DeleteRoleRel", "删除多个RoleRel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRoleRelDAO(session, user.PlantNr, user.UserId)

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleRelService.Select
 *
 ******************************************************************************/
func (impl *RoleRelServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/base/implments/RoleRel.service.impl.go", "SelectRoleRel", "查询RoleRel")

	condition := DefaultConditionOfRoleRelAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewRoleRelDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/base/RoleRelService.Update
 *
 ******************************************************************************/
func (impl *RoleRelServiceImplement) Update(user *global.User, entities *[]model.RoleRel) error {

	grmi.Log(user, "/services/base/implments/RoleRel.service.impl.go", "UpdateRoleRel", "修改多个RoleRel")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRoleRelDAO(session, user.PlantNr, user.UserId)

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

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