// 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              : PersonLoginHistory的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-25 14:20:56
 *
 ******************************************************************************/
var DefaultConditionOfPersonLoginHistory = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PersonLoginHistory_RecNr.UrlParameterName:       grmi.NewConditionItem(meta.PersonLoginHistory_RecNr, grmi.Equal, true),
		meta.PersonLoginHistory_PersonNr.UrlParameterName:    grmi.NewConditionItem(meta.PersonLoginHistory_PersonNr, grmi.Equal, false),
		meta.PersonLoginHistory_WorkPlaceNr.UrlParameterName: grmi.NewConditionItem(meta.PersonLoginHistory_WorkPlaceNr, grmi.Equal, false),
		meta.PersonLoginHistory_LoginTime.UrlParameterName:   grmi.NewConditionItem(meta.PersonLoginHistory_LoginTime, grmi.Approximate, false),
		meta.PersonLoginHistory_LogoutTime.UrlParameterName:  grmi.NewConditionItem(meta.PersonLoginHistory_LogoutTime, grmi.Approximate, false),
		meta.PersonLoginHistory_Status.UrlParameterName:      grmi.NewConditionItem(meta.PersonLoginHistory_Status, grmi.Equal, false),
		meta.PersonLoginHistory_TerminalId.UrlParameterName:  grmi.NewConditionItem(meta.PersonLoginHistory_TerminalId, grmi.Equal, false),
		meta.PersonLoginHistory_FunctionId.UrlParameterName:  grmi.NewConditionItem(meta.PersonLoginHistory_FunctionId, grmi.Equal, false),
		meta.PersonLoginHistory_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.PersonLoginHistory_LastModify, grmi.Approximate, false),
		meta.PersonLoginHistory_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.PersonLoginHistory_LastUser, grmi.Equal, false),
		meta.PersonLoginHistory_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.PersonLoginHistory_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/PersonLoginHistoryService.InsertOne
 *
 ******************************************************************************/
func (impl *PersonLoginHistoryServiceImplement) InsertOne(user *global.User, entity *model.PersonLoginHistory) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPersonLoginHistoryDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.RecNr)
	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/PersonLoginHistoryService.DeleteOne
 *
 ******************************************************************************/
func (impl *PersonLoginHistoryServiceImplement) DeleteOne(user *global.User, recNr int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/PersonLoginHistoryService.SelectOne
 *
 ******************************************************************************/
func (impl *PersonLoginHistoryServiceImplement) SelectOne(user *global.User, recNr int) (*model.PersonLoginHistory, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/PersonLoginHistoryService.UpdateOne
 *
 ******************************************************************************/
func (impl *PersonLoginHistoryServiceImplement) UpdateOne(user *global.User, entity *model.PersonLoginHistory) error {

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

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

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.RecNr)
		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/PersonLoginHistoryService.Delete
 *
 ******************************************************************************/
func (impl *PersonLoginHistoryServiceImplement) Delete(user *global.User, entities *[]model.PersonLoginHistory) error {

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

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

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

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

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

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

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

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

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