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

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/base"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/base"
	model "LAPP_ACURA_MOM_BACKEND/models/base"
)

/******************************************************************************
 *
 * @Description              : RPDashBoardHead的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-06 14:56:42
 *
 ******************************************************************************/
var DefaultConditionOfRPDashBoardHead = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.RPDashBoardHead_DashboardId.UrlParameterName: grmi.NewConditionItem(meta.RPDashBoardHead_DashboardId, grmi.Equal, true),
		meta.RPDashBoardHead_Desc.UrlParameterName:        grmi.NewConditionItem(meta.RPDashBoardHead_Desc, grmi.Equal, false),
		meta.RPDashBoardHead_Title.UrlParameterName:       grmi.NewConditionItem(meta.RPDashBoardHead_Title, grmi.Equal, false),
		meta.RPDashBoardHead_DPCtrlPara1.UrlParameterName: grmi.NewConditionItem(meta.RPDashBoardHead_DPCtrlPara1, grmi.Equal, false),
		meta.RPDashBoardHead_DPCtrlPara2.UrlParameterName: grmi.NewConditionItem(meta.RPDashBoardHead_DPCtrlPara2, grmi.Equal, false),
		meta.RPDashBoardHead_DPCtrlPara3.UrlParameterName: grmi.NewConditionItem(meta.RPDashBoardHead_DPCtrlPara3, grmi.Equal, false),
		meta.RPDashBoardHead_DPCtrlPara4.UrlParameterName: grmi.NewConditionItem(meta.RPDashBoardHead_DPCtrlPara4, grmi.Equal, false),
		meta.RPDashBoardHead_DPCtrlPara5.UrlParameterName: grmi.NewConditionItem(meta.RPDashBoardHead_DPCtrlPara5, grmi.Equal, false),
		meta.RPDashBoardHead_DPCtrlPara6.UrlParameterName: grmi.NewConditionItem(meta.RPDashBoardHead_DPCtrlPara6, grmi.Equal, false),
		meta.RPDashBoardHead_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.RPDashBoardHead_LastModify, grmi.Approximate, false),
		meta.RPDashBoardHead_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.RPDashBoardHead_LastUser, grmi.Equal, false),
		meta.RPDashBoardHead_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.RPDashBoardHead_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : RPDashBoardHead的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-06 14:56:42
 *
 ******************************************************************************/
var DefaultConditionOfRPDashBoardHeadAndPaging = grmi.NewCondition(
	DefaultConditionOfRPDashBoardHead.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : RPDashBoardHeadServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : RPDashBoardHead的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-06 14:56:42
 *
 ******************************************************************************/
type RPDashBoardHeadServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewRPDashBoardHeadDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个RPDashBoardHeadDAOImplement实例
 *
 * @Return Value             : RPDashBoardHeadDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-06 14:56:42
 *
 ******************************************************************************/
func NewRPDashBoardHeadServiceImplement() *RPDashBoardHeadServiceImplement {
	return &RPDashBoardHeadServiceImplement{}
}

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardHeadService.InsertOne
 *
 ******************************************************************************/
func (impl *RPDashBoardHeadServiceImplement) InsertOne(user *global.User, entity *model.RPDashBoardHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRPDashBoardHeadDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DashboardId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardHeadService.DeleteOne
 *
 ******************************************************************************/
func (impl *RPDashBoardHeadServiceImplement) DeleteOne(user *global.User, dashboardId string) error {

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

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

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardHeadService.SelectOne
 *
 ******************************************************************************/
func (impl *RPDashBoardHeadServiceImplement) SelectOne(user *global.User, dashboardId string) (*model.RPDashBoardHead, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRPDashBoardHeadDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(dashboardId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardHeadService.UpdateOne
 *
 ******************************************************************************/
func (impl *RPDashBoardHeadServiceImplement) UpdateOne(user *global.User, entity *model.RPDashBoardHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRPDashBoardHeadDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DashboardId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardHeadService.Insert
 *
 ******************************************************************************/
func (impl *RPDashBoardHeadServiceImplement) Insert(user *global.User, entities *[]model.RPDashBoardHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRPDashBoardHeadDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DashboardId)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

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

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardHeadService.Delete
 *
 ******************************************************************************/
func (impl *RPDashBoardHeadServiceImplement) Delete(user *global.User, entities *[]model.RPDashBoardHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRPDashBoardHeadDAO(session, user.PlantNr, user.UserId)

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

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

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardHeadService.Select
 *
 ******************************************************************************/
func (impl *RPDashBoardHeadServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.RPDashBoardHead, error) {

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

	condition := DefaultConditionOfRPDashBoardHeadAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewRPDashBoardHeadDAO(session, user.PlantNr, user.UserId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardHeadService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *RPDashBoardHeadServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

	condition := DefaultConditionOfRPDashBoardHeadAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewRPDashBoardHeadDAO(session, user.PlantNr, user.UserId)
	condition.Fill(urlParameters)
	result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference leit.com/LAPP_ACURA_MOM_BACKEND/services/base/RPDashBoardHeadService.Update
 *
 ******************************************************************************/
func (impl *RPDashBoardHeadServiceImplement) Update(user *global.User, entities *[]model.RPDashBoardHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRPDashBoardHeadDAO(session, user.PlantNr, user.UserId)

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

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