// 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              : DashBoardHead的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-06 11:05:20
 *
 ******************************************************************************/
var DefaultConditionOfDashBoardHead = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.DashBoardHead_DashboardId.UrlParameterName: grmi.NewConditionItem(meta.DashBoardHead_DashboardId, grmi.Equal, true),
		meta.DashBoardHead_Desc.UrlParameterName:        grmi.NewConditionItem(meta.DashBoardHead_Desc, grmi.Equal, false),
		meta.DashBoardHead_Title.UrlParameterName:       grmi.NewConditionItem(meta.DashBoardHead_Title, grmi.Equal, false),
		meta.DashBoardHead_DPCtrlPara1.UrlParameterName: grmi.NewConditionItem(meta.DashBoardHead_DPCtrlPara1, grmi.Equal, false),
		meta.DashBoardHead_DPCtrlPara2.UrlParameterName: grmi.NewConditionItem(meta.DashBoardHead_DPCtrlPara2, grmi.Equal, false),
		meta.DashBoardHead_DPCtrlPara3.UrlParameterName: grmi.NewConditionItem(meta.DashBoardHead_DPCtrlPara3, grmi.Equal, false),
		meta.DashBoardHead_DPCtrlPara4.UrlParameterName: grmi.NewConditionItem(meta.DashBoardHead_DPCtrlPara4, grmi.Equal, false),
		meta.DashBoardHead_DPCtrlPara5.UrlParameterName: grmi.NewConditionItem(meta.DashBoardHead_DPCtrlPara5, grmi.Equal, false),
		meta.DashBoardHead_DPCtrlPara6.UrlParameterName: grmi.NewConditionItem(meta.DashBoardHead_DPCtrlPara6, grmi.Equal, false),
		meta.DashBoardHead_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.DashBoardHead_LastModify, grmi.Approximate, false),
		meta.DashBoardHead_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.DashBoardHead_LastUser, grmi.Equal, false),
		meta.DashBoardHead_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.DashBoardHead_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : DashBoardHead的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-06 11:05:20
 *
 ******************************************************************************/
var DefaultConditionOfDashBoardHeadAndPaging = grmi.NewCondition(
	DefaultConditionOfDashBoardHead.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : DashBoardHeadServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : DashBoardHead的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-06 11:05:20
 *
 ******************************************************************************/
type DashBoardHeadServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewDashBoardHeadDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个DashBoardHeadDAOImplement实例
 *
 * @Return Value             : DashBoardHeadDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-06 11:05:20
 *
 ******************************************************************************/
func NewDashBoardHeadServiceImplement() *DashBoardHeadServiceImplement {
	return &DashBoardHeadServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DashBoardHeadService.InsertOne
 *
 ******************************************************************************/
func (impl *DashBoardHeadServiceImplement) InsertOne(user *global.User, entity *model.DashBoardHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDashBoardHeadDAO(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 LAPP_ACURA_MOM_BACKEND/services/report/DashBoardHeadService.DeleteOne
 *
 ******************************************************************************/
func (impl *DashBoardHeadServiceImplement) DeleteOne(user *global.User, dashboardId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDashBoardHeadDAO(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 LAPP_ACURA_MOM_BACKEND/services/report/DashBoardHeadService.SelectOne
 *
 ******************************************************************************/
func (impl *DashBoardHeadServiceImplement) SelectOne(user *global.User, dashboardId string) (*model.DashBoardHead, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DashBoardHeadService.UpdateOne
 *
 ******************************************************************************/
func (impl *DashBoardHeadServiceImplement) UpdateOne(user *global.User, entity *model.DashBoardHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDashBoardHeadDAO(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 LAPP_ACURA_MOM_BACKEND/services/report/DashBoardHeadService.Insert
 *
 ******************************************************************************/
func (impl *DashBoardHeadServiceImplement) Insert(user *global.User, entities *[]model.DashBoardHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDashBoardHeadDAO(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 LAPP_ACURA_MOM_BACKEND/services/report/DashBoardHeadService.Delete
 *
 ******************************************************************************/
func (impl *DashBoardHeadServiceImplement) Delete(user *global.User, entities *[]model.DashBoardHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDashBoardHeadDAO(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 LAPP_ACURA_MOM_BACKEND/services/report/DashBoardHeadService.Select
 *
 ******************************************************************************/
func (impl *DashBoardHeadServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.DashBoardHead, error) {

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

	condition := DefaultConditionOfDashBoardHeadAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewDashBoardHeadDAO(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/DashBoardHeadService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *DashBoardHeadServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

	condition := DefaultConditionOfDashBoardHeadAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewDashBoardHeadDAO(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/DashBoardHeadService.Update
 *
 ******************************************************************************/
func (impl *DashBoardHeadServiceImplement) Update(user *global.User, entities *[]model.DashBoardHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDashBoardHeadDAO(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
}
