// 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              : Template的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfTemplate = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Template_MainTemplateNr.UrlParameterName:        grmi.NewConditionItem(meta.Template_MainTemplateNr, grmi.Equal, true),
		meta.Template_Descr.UrlParameterName:                 grmi.NewConditionItem(meta.Template_Descr, grmi.Equal, false),
		meta.Template_TrackByDateToggle.UrlParameterName:     grmi.NewConditionItem(meta.Template_TrackByDateToggle, grmi.Equal, false),
		meta.Template_TrackByPriMeterToggle.UrlParameterName: grmi.NewConditionItem(meta.Template_TrackByPriMeterToggle, grmi.Equal, false),
		meta.Template_PriMeterUom.UrlParameterName:           grmi.NewConditionItem(meta.Template_PriMeterUom, grmi.Equal, false),
		meta.Template_TrackBySecMeterToggle.UrlParameterName: grmi.NewConditionItem(meta.Template_TrackBySecMeterToggle, grmi.Equal, false),
		meta.Template_SecMeterUom.UrlParameterName:           grmi.NewConditionItem(meta.Template_SecMeterUom, grmi.Equal, false),
		meta.Template_LastModify.UrlParameterName:            grmi.NewConditionItem(meta.Template_LastModify, grmi.Approximate, false),
		meta.Template_LastUser.UrlParameterName:              grmi.NewConditionItem(meta.Template_LastUser, grmi.Equal, false),
		meta.Template_CreateTime.UrlParameterName:            grmi.NewConditionItem(meta.Template_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/TemplateService.InsertOne
 *
 ******************************************************************************/
func (impl *TemplateServiceImplement) InsertOne(user *models.Usertab, entity *model.Template) error {

	grmi.Log(user, "/services/pm/implments/PM_Template.service.impl.go", "InsertOneTemplate", "插入一个Template")

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

	grmi.Log(user, "/services/pm/implments/PM_Template.service.impl.go", "DeleteOneTemplate", "删除一个Template")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/TemplateService.SelectOne
 *
 ******************************************************************************/
func (impl *TemplateServiceImplement) SelectOne(user *models.Usertab, mainTemplateNr int) (*model.Template, error) {

	grmi.Log(user, "/services/pm/implments/PM_Template.service.impl.go", "SelectOneTemplate", "查询一个Template")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/TemplateService.UpdateOne
 *
 ******************************************************************************/
func (impl *TemplateServiceImplement) UpdateOne(user *models.Usertab, entity *model.Template) error {

	grmi.Log(user, "/services/pm/implments/PM_Template.service.impl.go", "UpdateOneTemplate", "修改一个Template")

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

	grmi.Log(user, "/services/pm/implments/PM_Template.service.impl.go", "InsertTemplate", "插入多个Template")

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

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

	grmi.Log(user, "/services/pm/implments/PM_Template.service.impl.go", "DeleteTemplate", "删除多个Template")

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

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

	grmi.Log(user, "/services/pm/implments/PM_Template.service.impl.go", "SelectTemplate", "查询Template")

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

	grmi.Log(user, "/services/pm/implments/PM_Template.service.impl.go", "UpdateTemplate", "修改多个Template")

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

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

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