// 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              : WorkPlacePersonLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-25 14:20:56
 *
 ******************************************************************************/
var DefaultConditionOfWorkPlacePersonLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkPlacePersonLst_WorkPlaceNr.UrlParameterName: grmi.NewConditionItem(meta.WorkPlacePersonLst_WorkPlaceNr, grmi.Equal, true),
		meta.WorkPlacePersonLst_PersonNr.UrlParameterName:    grmi.NewConditionItem(meta.WorkPlacePersonLst_PersonNr, grmi.Equal, true),
		meta.WorkPlacePersonLst_Pos.UrlParameterName:         grmi.NewConditionItem(meta.WorkPlacePersonLst_Pos, grmi.Equal, false),
		meta.WorkPlacePersonLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.WorkPlacePersonLst_LastModify, grmi.Approximate, false),
		meta.WorkPlacePersonLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.WorkPlacePersonLst_LastUser, grmi.Equal, false),
		meta.WorkPlacePersonLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.WorkPlacePersonLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkPlacePersonLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-25 14:20:56
 *
 ******************************************************************************/
var DefaultConditionOfWorkPlacePersonLstAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkPlacePersonLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkPlacePersonLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkPlacePersonLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-25 14:20:56
 *
 ******************************************************************************/
type WorkPlacePersonLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkPlacePersonLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkPlacePersonLstDAOImplement实例
 *
 * @Return Value             : WorkPlacePersonLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-25 14:20:56
 *
 ******************************************************************************/
func NewWorkPlacePersonLstServiceImplement() *WorkPlacePersonLstServiceImplement {
	return &WorkPlacePersonLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlacePersonLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkPlacePersonLstServiceImplement) InsertOne(user *global.User, entity *model.WorkPlacePersonLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkPlacePersonLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.WorkPlaceNr, entity.PersonNr)
	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/WorkPlacePersonLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkPlacePersonLstServiceImplement) DeleteOne(user *global.User, workPlaceNr int, personNr int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlacePersonLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkPlacePersonLstServiceImplement) SelectOne(user *global.User, workPlaceNr int, personNr int) (*model.WorkPlacePersonLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlacePersonLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkPlacePersonLstServiceImplement) UpdateOne(user *global.User, entity *model.WorkPlacePersonLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkPlacePersonLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.WorkPlaceNr, entity.PersonNr)
	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/WorkPlacePersonLstService.Insert
 *
 ******************************************************************************/
func (impl *WorkPlacePersonLstServiceImplement) Insert(user *global.User, entities *[]model.WorkPlacePersonLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkPlaceNr, entity.PersonNr)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlacePersonLstService.Delete
 *
 ******************************************************************************/
func (impl *WorkPlacePersonLstServiceImplement) Delete(user *global.User, entities *[]model.WorkPlacePersonLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkPlaceNr, entity.PersonNr)
		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/WorkPlacePersonLstService.Select
 *
 ******************************************************************************/
func (impl *WorkPlacePersonLstServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfWorkPlacePersonLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewWorkPlacePersonLstDAO(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/WorkPlacePersonLstService.Update
 *
 ******************************************************************************/
func (impl *WorkPlacePersonLstServiceImplement) Update(user *global.User, entities *[]model.WorkPlacePersonLst) error {

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

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

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

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