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

package implments

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

/******************************************************************************
 *
 * @Description              : DashBoardLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-10 17:33:11
 *
 ******************************************************************************/
var DefaultConditionOfDashBoardLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.DashBoardLst_DashboardId.UrlParameterName: grmi.NewConditionItem(meta.DashBoardLst_DashboardId, grmi.Equal, true),
		meta.DashBoardLst_Pos.UrlParameterName:         grmi.NewConditionItem(meta.DashBoardLst_Pos, grmi.Equal, true),
		meta.DashBoardLst_ParamName.UrlParameterName:   grmi.NewConditionItem(meta.DashBoardLst_ParamName, grmi.Equal, false),
		meta.DashBoardLst_Descr.UrlParameterName:       grmi.NewConditionItem(meta.DashBoardLst_Descr, grmi.Equal, false),
		meta.DashBoardLst_IntParam1.UrlParameterName:   grmi.NewConditionItem(meta.DashBoardLst_IntParam1, grmi.Equal, false),
		meta.DashBoardLst_IntParam2.UrlParameterName:   grmi.NewConditionItem(meta.DashBoardLst_IntParam2, grmi.Equal, false),
		meta.DashBoardLst_FloatParam1.UrlParameterName: grmi.NewConditionItem(meta.DashBoardLst_FloatParam1, grmi.Equal, false),
		meta.DashBoardLst_FloatParam2.UrlParameterName: grmi.NewConditionItem(meta.DashBoardLst_FloatParam2, grmi.Equal, false),
		meta.DashBoardLst_CharParam1.UrlParameterName:  grmi.NewConditionItem(meta.DashBoardLst_CharParam1, grmi.Equal, false),
		meta.DashBoardLst_CharParam2.UrlParameterName:  grmi.NewConditionItem(meta.DashBoardLst_CharParam2, grmi.Equal, false),
		meta.DashBoardLst_CharParam3.UrlParameterName:  grmi.NewConditionItem(meta.DashBoardLst_CharParam3, grmi.Equal, false),
		meta.DashBoardLst_CharParam4.UrlParameterName:  grmi.NewConditionItem(meta.DashBoardLst_CharParam4, grmi.Equal, false),
		meta.DashBoardLst_IntParam3.UrlParameterName:   grmi.NewConditionItem(meta.DashBoardLst_IntParam3, grmi.Equal, false),
		meta.DashBoardLst_IntParam4.UrlParameterName:   grmi.NewConditionItem(meta.DashBoardLst_IntParam4, grmi.Equal, false),
		meta.DashBoardLst_IntParam5.UrlParameterName:   grmi.NewConditionItem(meta.DashBoardLst_IntParam5, grmi.Equal, false),
		meta.DashBoardLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.DashBoardLst_LastModify, grmi.Approximate, false),
		meta.DashBoardLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.DashBoardLst_LastUser, grmi.Equal, false),
		meta.DashBoardLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.DashBoardLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : DashBoardLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-10 17:33:11
 *
 ******************************************************************************/
var DefaultConditionOfDashBoardLstAndPaging = grmi.NewCondition(
	DefaultConditionOfDashBoardLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : DashBoardLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : DashBoardLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-10 17:33:11
 *
 ******************************************************************************/
type DashBoardLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewDashBoardLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个DashBoardLstDAOImplement实例
 *
 * @Return Value             : DashBoardLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-10 17:33:11
 *
 ******************************************************************************/
func NewDashBoardLstServiceImplement() *DashBoardLstServiceImplement {
	return &DashBoardLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DashBoardLstService.InsertOne
 *
 ******************************************************************************/
func (impl *DashBoardLstServiceImplement) InsertOne(user *global.User, entity *model.DashBoardLst) error {

	grmi.Log(user, "/services/report/implments/DashBoardLst.service.impl.go", "InsertOneDashBoardLst", "插入一个DashBoardLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DashBoardLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *DashBoardLstServiceImplement) DeleteOne(user *global.User, dashboardId string, pos int) error {

	grmi.Log(user, "/services/report/implments/DashBoardLst.service.impl.go", "DeleteOneDashBoardLst", "删除一个DashBoardLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DashBoardLstService.SelectOne
 *
 ******************************************************************************/
func (impl *DashBoardLstServiceImplement) SelectOne(user *global.User, dashboardId string, pos int) (*model.DashBoardLst, error) {

	grmi.Log(user, "/services/report/implments/DashBoardLst.service.impl.go", "SelectOneDashBoardLst", "查询一个DashBoardLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DashBoardLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *DashBoardLstServiceImplement) UpdateOne(user *global.User, entity *model.DashBoardLst) error {

	grmi.Log(user, "/services/report/implments/DashBoardLst.service.impl.go", "UpdateOneDashBoardLst", "修改一个DashBoardLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DashBoardLstService.Insert
 *
 ******************************************************************************/
func (impl *DashBoardLstServiceImplement) Insert(user *global.User, entities *[]model.DashBoardLst) error {

	grmi.Log(user, "/services/report/implments/DashBoardLst.service.impl.go", "InsertDashBoardLst", "插入多个DashBoardLst")

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DashBoardLstService.Delete
 *
 ******************************************************************************/
func (impl *DashBoardLstServiceImplement) Delete(user *global.User, entities *[]model.DashBoardLst) error {

	grmi.Log(user, "/services/report/implments/DashBoardLst.service.impl.go", "DeleteDashBoardLst", "删除多个DashBoardLst")

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DashBoardLstService.Select
 *
 ******************************************************************************/
func (impl *DashBoardLstServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.DashBoardLst, error) {

	grmi.Log(user, "/services/report/implments/DashBoardLst.service.impl.go", "SelectDashBoardLst", "查询DashBoardLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DashBoardLstService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *DashBoardLstServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

	grmi.Log(user, "/services/report/implments/DashBoardLst.service.impl.go", "SelectDashBoardLst", "查询DashBoardLst")

	condition := DefaultConditionOfDashBoardLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewDashBoardLstDAO(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 LAPP_ACURA_MOM_BACKEND/services/report/DashBoardLstService.Update
 *
 ******************************************************************************/
func (impl *DashBoardLstServiceImplement) Update(user *global.User, entities *[]model.DashBoardLst) error {

	grmi.Log(user, "/services/report/implments/DashBoardLst.service.impl.go", "UpdateDashBoardLst", "修改多个DashBoardLst")

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

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

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