// 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              : DisplayParam的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-24 15:31:29
 *
 ******************************************************************************/
var DefaultConditionOfDisplayParam = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.DisplayParam_DisplayId.UrlParameterName:  grmi.NewConditionItem(meta.DisplayParam_DisplayId, grmi.Equal, true),
		meta.DisplayParam_Pos.UrlParameterName:        grmi.NewConditionItem(meta.DisplayParam_Pos, grmi.Equal, true),
		meta.DisplayParam_ParamName.UrlParameterName:  grmi.NewConditionItem(meta.DisplayParam_ParamName, grmi.Equal, false),
		meta.DisplayParam_CtrlPara1.UrlParameterName:  grmi.NewConditionItem(meta.DisplayParam_CtrlPara1, grmi.Equal, false),
		meta.DisplayParam_CtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.DisplayParam_CtrlPara2, grmi.Equal, false),
		meta.DisplayParam_CtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.DisplayParam_CtrlPara3, grmi.Equal, false),
		meta.DisplayParam_CtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.DisplayParam_CtrlPara4, grmi.Equal, false),
		meta.DisplayParam_CtrlPara5.UrlParameterName:  grmi.NewConditionItem(meta.DisplayParam_CtrlPara5, grmi.Equal, false),
		meta.DisplayParam_CtrlPara6.UrlParameterName:  grmi.NewConditionItem(meta.DisplayParam_CtrlPara6, grmi.Equal, false),
		meta.DisplayParam_LastModify.UrlParameterName: grmi.NewConditionItem(meta.DisplayParam_LastModify, grmi.Approximate, false),
		meta.DisplayParam_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.DisplayParam_LastUser, grmi.Equal, false),
		meta.DisplayParam_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.DisplayParam_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : DisplayParam的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-24 15:31:29
 *
 ******************************************************************************/
var DefaultConditionOfDisplayParamAndPaging = grmi.NewCondition(
	DefaultConditionOfDisplayParam.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : DisplayParamServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : DisplayParam的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-24 15:31:29
 *
 ******************************************************************************/
type DisplayParamServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewDisplayParamDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个DisplayParamDAOImplement实例
 *
 * @Return Value             : DisplayParamDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-24 15:31:29
 *
 ******************************************************************************/
func NewDisplayParamServiceImplement() *DisplayParamServiceImplement {
	return &DisplayParamServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DisplayParamService.InsertOne
 *
 ******************************************************************************/
func (impl *DisplayParamServiceImplement) InsertOne(user *global.User, entity *model.DisplayParam) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDisplayParamDAO(session, user.UserId)
	record, err := dao.SelectOne(entity.DisplayId, 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/DisplayParamService.DeleteOne
 *
 ******************************************************************************/
func (impl *DisplayParamServiceImplement) DeleteOne(user *global.User, displayId int, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DisplayParamService.SelectOne
 *
 ******************************************************************************/
func (impl *DisplayParamServiceImplement) SelectOne(user *global.User, displayId int, pos int) (*model.DisplayParam, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/report/DisplayParamService.UpdateOne
 *
 ******************************************************************************/
func (impl *DisplayParamServiceImplement) UpdateOne(user *global.User, entity *model.DisplayParam) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDisplayParamDAO(session, user.UserId)
	record, err := dao.SelectOne(entity.DisplayId, 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/DisplayParamService.Insert
 *
 ******************************************************************************/
func (impl *DisplayParamServiceImplement) Insert(user *global.User, entities *[]model.DisplayParam) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DisplayId, 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/DisplayParamService.Delete
 *
 ******************************************************************************/
func (impl *DisplayParamServiceImplement) Delete(user *global.User, entities *[]model.DisplayParam) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DisplayId, 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/DisplayParamService.Select
 *
 ******************************************************************************/
func (impl *DisplayParamServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfDisplayParamAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewDisplayParamDAO(session, 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_ACURA_MOM_BACKEND/services/report/DisplayParamService.Update
 *
 ******************************************************************************/
func (impl *DisplayParamServiceImplement) Update(user *global.User, entities *[]model.DisplayParam) error {

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

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

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

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