// 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              : Service的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfService = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Service_MainServiceNr.UrlParameterName:        grmi.NewConditionItem(meta.Service_MainServiceNr, grmi.Equal, true),
		meta.Service_Descr.UrlParameterName:                grmi.NewConditionItem(meta.Service_Descr, grmi.Equal, false),
		meta.Service_MaintType.UrlParameterName:            grmi.NewConditionItem(meta.Service_MaintType, grmi.Equal, false),
		meta.Service_Priority.UrlParameterName:             grmi.NewConditionItem(meta.Service_Priority, grmi.Equal, false),
		meta.Service_EnabledToggle.UrlParameterName:        grmi.NewConditionItem(meta.Service_EnabledToggle, grmi.Equal, false),
		meta.Service_MaintByDateToggle.UrlParameterName:    grmi.NewConditionItem(meta.Service_MaintByDateToggle, grmi.Equal, false),
		meta.Service_DueDateInterval.UrlParameterName:      grmi.NewConditionItem(meta.Service_DueDateInterval, grmi.Equal, false),
		meta.Service_NodifyDays.UrlParameterName:           grmi.NewConditionItem(meta.Service_NodifyDays, grmi.Equal, false),
		meta.Service_DueDateOn.UrlParameterName:            grmi.NewConditionItem(meta.Service_DueDateOn, grmi.Approximate, false),
		meta.Service_MaintByHourToggle.UrlParameterName:    grmi.NewConditionItem(meta.Service_MaintByHourToggle, grmi.Equal, false),
		meta.Service_DueHourInterval.UrlParameterName:      grmi.NewConditionItem(meta.Service_DueHourInterval, grmi.Equal, false),
		meta.Service_NodifyHours.UrlParameterName:          grmi.NewConditionItem(meta.Service_NodifyHours, grmi.Equal, false),
		meta.Service_DueHourOn.UrlParameterName:            grmi.NewConditionItem(meta.Service_DueHourOn, grmi.Approximate, false),
		meta.Service_MaintByMileageToggle.UrlParameterName: grmi.NewConditionItem(meta.Service_MaintByMileageToggle, grmi.Equal, false),
		meta.Service_DueMileageInterval.UrlParameterName:   grmi.NewConditionItem(meta.Service_DueMileageInterval, grmi.Equal, false),
		meta.Service_NodifyMileages.UrlParameterName:       grmi.NewConditionItem(meta.Service_NodifyMileages, grmi.Equal, false),
		meta.Service_DueMileageAt.UrlParameterName:         grmi.NewConditionItem(meta.Service_DueMileageAt, grmi.Equal, false),
		meta.Service_NeedSchedule.UrlParameterName:         grmi.NewConditionItem(meta.Service_NeedSchedule, grmi.Equal, false),
		meta.Service_MainTimeLen.UrlParameterName:          grmi.NewConditionItem(meta.Service_MainTimeLen, grmi.Equal, false),
		meta.Service_TimeUnit.UrlParameterName:             grmi.NewConditionItem(meta.Service_TimeUnit, grmi.Equal, false),
		meta.Service_SchedType.UrlParameterName:            grmi.NewConditionItem(meta.Service_SchedType, grmi.Equal, false),
		meta.Service_Department.UrlParameterName:           grmi.NewConditionItem(meta.Service_Department, grmi.Equal, false),
		meta.Service_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.Service_LastModify, grmi.Approximate, false),
		meta.Service_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.Service_LastUser, grmi.Equal, false),
		meta.Service_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.Service_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/ServiceService.InsertOne
 *
 ******************************************************************************/
func (impl *ServiceServiceImplement) InsertOne(user *models.Usertab, entity *model.Service) error {

	grmi.Log(user, "/services/pm/implments/PM_Service.service.impl.go", "InsertOneService", "插入一个Service")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewServiceDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.MainServiceNr)
	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/ServiceService.DeleteOne
 *
 ******************************************************************************/
func (impl *ServiceServiceImplement) DeleteOne(user *models.Usertab, mainServiceNr int) error {

	grmi.Log(user, "/services/pm/implments/PM_Service.service.impl.go", "DeleteOneService", "删除一个Service")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/ServiceService.SelectOne
 *
 ******************************************************************************/
func (impl *ServiceServiceImplement) SelectOne(user *models.Usertab, mainServiceNr int) (*model.Service, error) {

	grmi.Log(user, "/services/pm/implments/PM_Service.service.impl.go", "SelectOneService", "查询一个Service")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/ServiceService.UpdateOne
 *
 ******************************************************************************/
func (impl *ServiceServiceImplement) UpdateOne(user *models.Usertab, entity *model.Service) error {

	grmi.Log(user, "/services/pm/implments/PM_Service.service.impl.go", "UpdateOneService", "修改一个Service")

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

	grmi.Log(user, "/services/pm/implments/PM_Service.service.impl.go", "InsertService", "插入多个Service")

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

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

	grmi.Log(user, "/services/pm/implments/PM_Service.service.impl.go", "DeleteService", "删除多个Service")

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

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

	grmi.Log(user, "/services/pm/implments/PM_Service.service.impl.go", "SelectService", "查询Service")

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

	grmi.Log(user, "/services/pm/implments/PM_Service.service.impl.go", "UpdateService", "修改多个Service")

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

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

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