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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/base"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/base"
	model "LAPP_LF_MOM_BACKEND/models/base"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : User的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfUser = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.User_UserId.UrlParameterName:     grmi.NewConditionItem(meta.User_UserId, grmi.Equal, true),
		meta.User_Name.UrlParameterName:       grmi.NewConditionItem(meta.User_Name, grmi.Equal, false),
		meta.User_NickName.UrlParameterName:   grmi.NewConditionItem(meta.User_NickName, grmi.Equal, false),
		meta.User_DepId.UrlParameterName:      grmi.NewConditionItem(meta.User_DepId, grmi.Equal, false),
		meta.User_Phone.UrlParameterName:      grmi.NewConditionItem(meta.User_Phone, grmi.Equal, false),
		meta.User_Avatar.UrlParameterName:     grmi.NewConditionItem(meta.User_Avatar, grmi.Equal, false),
		meta.User_Email.UrlParameterName:      grmi.NewConditionItem(meta.User_Email, grmi.Equal, false),
		meta.User_Status.UrlParameterName:     grmi.NewConditionItem(meta.User_Status, grmi.Equal, false),
		meta.User_Gender.UrlParameterName:     grmi.NewConditionItem(meta.User_Gender, grmi.Equal, false),
		meta.User_Position.UrlParameterName:   grmi.NewConditionItem(meta.User_Position, grmi.Equal, false),
		meta.User_Role.UrlParameterName:       grmi.NewConditionItem(meta.User_Role, grmi.Equal, false),
		meta.User_Remark.UrlParameterName:     grmi.NewConditionItem(meta.User_Remark, grmi.Equal, false),
		meta.User_Password.UrlParameterName:   grmi.NewConditionItem(meta.User_Password, grmi.Equal, false),
		meta.User_RoleId.UrlParameterName:     grmi.NewConditionItem(meta.User_RoleId, grmi.Equal, false),
		meta.User_LastModify.UrlParameterName: grmi.NewConditionItem(meta.User_LastModify, grmi.Approximate, false),
		meta.User_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.User_LastUser, grmi.Equal, false),
		meta.User_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.User_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : User的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfUserAndPaging = grmi.NewCondition(
	DefaultConditionOfUser.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : UserServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : User的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
type UserServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewUserDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个UserDAOImplement实例
 *
 * @Return Value             : UserDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
func NewUserServiceImplement() *UserServiceImplement {
	return &UserServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/UserService.InsertOne
 *
 ******************************************************************************/
func (impl *UserServiceImplement) InsertOne(user *models.Usertab, entity *model.User) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewUserDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(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/base/UserService.DeleteOne
 *
 ******************************************************************************/
func (impl *UserServiceImplement) DeleteOne(user *models.Usertab, userId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/UserService.SelectOne
 *
 ******************************************************************************/
func (impl *UserServiceImplement) SelectOne(user *models.Usertab, userId string) (*model.User, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/UserService.UpdateOne
 *
 ******************************************************************************/
func (impl *UserServiceImplement) UpdateOne(user *models.Usertab, entity *model.User) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewUserDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(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/base/UserService.Insert
 *
 ******************************************************************************/
func (impl *UserServiceImplement) Insert(user *models.Usertab, entities *[]model.User) error {

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

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

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

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

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

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

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

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

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

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

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

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