// 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              : AssetKpi的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfAssetKpi = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.AssetKpi_AssetNr.UrlParameterName:         grmi.NewConditionItem(meta.AssetKpi_AssetNr, grmi.Equal, true),
		meta.AssetKpi_Kpi.UrlParameterName:             grmi.NewConditionItem(meta.AssetKpi_Kpi, grmi.Equal, true),
		meta.AssetKpi_KpiName.UrlParameterName:         grmi.NewConditionItem(meta.AssetKpi_KpiName, grmi.Equal, false),
		meta.AssetKpi_KpiType.UrlParameterName:         grmi.NewConditionItem(meta.AssetKpi_KpiType, grmi.Equal, false),
		meta.AssetKpi_TriggerStatus.UrlParameterName:   grmi.NewConditionItem(meta.AssetKpi_TriggerStatus, grmi.Equal, false),
		meta.AssetKpi_ThresholdVal.UrlParameterName:    grmi.NewConditionItem(meta.AssetKpi_ThresholdVal, grmi.Equal, false),
		meta.AssetKpi_ActualVal.UrlParameterName:       grmi.NewConditionItem(meta.AssetKpi_ActualVal, grmi.Equal, false),
		meta.AssetKpi_ResetAttrNr.UrlParameterName:     grmi.NewConditionItem(meta.AssetKpi_ResetAttrNr, grmi.Equal, false),
		meta.AssetKpi_CounterVal.UrlParameterName:      grmi.NewConditionItem(meta.AssetKpi_CounterVal, grmi.Equal, false),
		meta.AssetKpi_CounterCycle.UrlParameterName:    grmi.NewConditionItem(meta.AssetKpi_CounterCycle, grmi.Equal, false),
		meta.AssetKpi_CounterCycleVal.UrlParameterName: grmi.NewConditionItem(meta.AssetKpi_CounterCycleVal, grmi.Equal, false),
		meta.AssetKpi_MainServiceNr.UrlParameterName:   grmi.NewConditionItem(meta.AssetKpi_MainServiceNr, grmi.Equal, false),
		meta.AssetKpi_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.AssetKpi_LastModify, grmi.Approximate, false),
		meta.AssetKpi_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.AssetKpi_LastUser, grmi.Equal, false),
		meta.AssetKpi_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.AssetKpi_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetKpiService.InsertOne
 *
 ******************************************************************************/
func (impl *AssetKpiServiceImplement) InsertOne(user *models.Usertab, entity *model.AssetKpi) error {

	grmi.Log(user, "/services/pm/implments/PM_AssetKpi.service.impl.go", "InsertOneAssetKpi", "插入一个AssetKpi")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssetKpiDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AssetNr, entity.Kpi)
	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/AssetKpiService.DeleteOne
 *
 ******************************************************************************/
func (impl *AssetKpiServiceImplement) DeleteOne(user *models.Usertab, assetNr int, kpi string) error {

	grmi.Log(user, "/services/pm/implments/PM_AssetKpi.service.impl.go", "DeleteOneAssetKpi", "删除一个AssetKpi")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetKpiService.SelectOne
 *
 ******************************************************************************/
func (impl *AssetKpiServiceImplement) SelectOne(user *models.Usertab, assetNr int, kpi string) (*model.AssetKpi, error) {

	grmi.Log(user, "/services/pm/implments/PM_AssetKpi.service.impl.go", "SelectOneAssetKpi", "查询一个AssetKpi")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetKpiService.UpdateOne
 *
 ******************************************************************************/
func (impl *AssetKpiServiceImplement) UpdateOne(user *models.Usertab, entity *model.AssetKpi) error {

	grmi.Log(user, "/services/pm/implments/PM_AssetKpi.service.impl.go", "UpdateOneAssetKpi", "修改一个AssetKpi")

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

	grmi.Log(user, "/services/pm/implments/PM_AssetKpi.service.impl.go", "InsertAssetKpi", "插入多个AssetKpi")

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

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

	grmi.Log(user, "/services/pm/implments/PM_AssetKpi.service.impl.go", "DeleteAssetKpi", "删除多个AssetKpi")

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

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

	grmi.Log(user, "/services/pm/implments/PM_AssetKpi.service.impl.go", "SelectAssetKpi", "查询AssetKpi")

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

	grmi.Log(user, "/services/pm/implments/PM_AssetKpi.service.impl.go", "UpdateAssetKpi", "修改多个AssetKpi")

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

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

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