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

package implments

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

/******************************************************************************
 *
 * @Description              : Location的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfLocation = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Location_LocaNr.UrlParameterName:     grmi.NewConditionItem(meta.Location_LocaNr, grmi.Equal, true),
		meta.Location_LocaName.UrlParameterName:   grmi.NewConditionItem(meta.Location_LocaName, grmi.Equal, false),
		meta.Location_LocaPara1.UrlParameterName:  grmi.NewConditionItem(meta.Location_LocaPara1, grmi.Equal, false),
		meta.Location_LocaPara2.UrlParameterName:  grmi.NewConditionItem(meta.Location_LocaPara2, grmi.Equal, false),
		meta.Location_LocaPara3.UrlParameterName:  grmi.NewConditionItem(meta.Location_LocaPara3, grmi.Equal, false),
		meta.Location_LocaPara4.UrlParameterName:  grmi.NewConditionItem(meta.Location_LocaPara4, grmi.Equal, false),
		meta.Location_LastModify.UrlParameterName: grmi.NewConditionItem(meta.Location_LastModify, grmi.Approximate, false),
		meta.Location_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.Location_LastUser, grmi.Equal, false),
		meta.Location_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.Location_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : Location的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfLocationAndPaging = grmi.NewCondition(
	DefaultConditionOfLocation.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : LocationServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Location的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
type LocationServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewLocationDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个LocationDAOImplement实例
 *
 * @Return Value             : LocationDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
func NewLocationServiceImplement() *LocationServiceImplement {
	return &LocationServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/LocationService.InsertOne
 *
 ******************************************************************************/
func (impl *LocationServiceImplement) InsertOne(user *models.Usertab, entity *model.Location) error {

	grmi.Log(user, "/services/pm/implments/PM_Location.service.impl.go", "InsertOneLocation", "插入一个Location")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewLocationDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.LocaNr)
	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/pm/LocationService.DeleteOne
 *
 ******************************************************************************/
func (impl *LocationServiceImplement) DeleteOne(user *models.Usertab, locaNr int) error {

	grmi.Log(user, "/services/pm/implments/PM_Location.service.impl.go", "DeleteOneLocation", "删除一个Location")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/LocationService.SelectOne
 *
 ******************************************************************************/
func (impl *LocationServiceImplement) SelectOne(user *models.Usertab, locaNr int) (*model.Location, error) {

	grmi.Log(user, "/services/pm/implments/PM_Location.service.impl.go", "SelectOneLocation", "查询一个Location")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/LocationService.UpdateOne
 *
 ******************************************************************************/
func (impl *LocationServiceImplement) UpdateOne(user *models.Usertab, entity *model.Location) error {

	grmi.Log(user, "/services/pm/implments/PM_Location.service.impl.go", "UpdateOneLocation", "修改一个Location")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewLocationDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.LocaNr)
	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/pm/LocationService.Insert
 *
 ******************************************************************************/
func (impl *LocationServiceImplement) Insert(user *models.Usertab, entities *[]model.Location) error {

	grmi.Log(user, "/services/pm/implments/PM_Location.service.impl.go", "InsertLocation", "插入多个Location")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.LocaNr)
		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/pm/LocationService.Delete
 *
 ******************************************************************************/
func (impl *LocationServiceImplement) Delete(user *models.Usertab, entities *[]model.Location) error {

	grmi.Log(user, "/services/pm/implments/PM_Location.service.impl.go", "DeleteLocation", "删除多个Location")

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

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

	grmi.Log(user, "/services/pm/implments/PM_Location.service.impl.go", "SelectLocation", "查询Location")

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

	grmi.Log(user, "/services/pm/implments/PM_Location.service.impl.go", "UpdateLocation", "修改多个Location")

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

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

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