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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/report"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/report"
	model "LAPP_LF_MOM_BACKEND/models/report"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : Display的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-24 15:31:29
 *
 ******************************************************************************/
var DefaultConditionOfDisplay = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Display_DisplayId.UrlParameterName:   grmi.NewConditionItem(meta.Display_DisplayId, grmi.Equal, true),
		meta.Display_DisplayName.UrlParameterName: grmi.NewConditionItem(meta.Display_DisplayName, grmi.Equal, false),
		meta.Display_Title.UrlParameterName:       grmi.NewConditionItem(meta.Display_Title, grmi.Equal, false),
		meta.Display_Source.UrlParameterName:      grmi.NewConditionItem(meta.Display_Source, grmi.Equal, false),
		meta.Display_DPCtrlPara1.UrlParameterName: grmi.NewConditionItem(meta.Display_DPCtrlPara1, grmi.Equal, false),
		meta.Display_DPCtrlPara2.UrlParameterName: grmi.NewConditionItem(meta.Display_DPCtrlPara2, grmi.Equal, false),
		meta.Display_DPCtrlPara3.UrlParameterName: grmi.NewConditionItem(meta.Display_DPCtrlPara3, grmi.Equal, false),
		meta.Display_DPCtrlPara4.UrlParameterName: grmi.NewConditionItem(meta.Display_DPCtrlPara4, grmi.Equal, false),
		meta.Display_DPCtrlPara5.UrlParameterName: grmi.NewConditionItem(meta.Display_DPCtrlPara5, grmi.Equal, false),
		meta.Display_DPCtrlPara6.UrlParameterName: grmi.NewConditionItem(meta.Display_DPCtrlPara6, grmi.Equal, false),
		meta.Display_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.Display_LastModify, grmi.Approximate, false),
		meta.Display_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.Display_LastUser, grmi.Equal, false),
		meta.Display_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.Display_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/report/DisplayService.InsertOne
 *
 ******************************************************************************/
func (impl *DisplayServiceImplement) InsertOne(user *models.Usertab, entity *model.Display) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDisplayDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.DisplayId)
	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/report/DisplayService.DeleteOne
 *
 ******************************************************************************/
func (impl *DisplayServiceImplement) DeleteOne(user *models.Usertab, displayId int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/report/DisplayService.SelectOne
 *
 ******************************************************************************/
func (impl *DisplayServiceImplement) SelectOne(user *models.Usertab, displayId int) (*model.Display, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/report/DisplayService.UpdateOne
 *
 ******************************************************************************/
func (impl *DisplayServiceImplement) UpdateOne(user *models.Usertab, entity *model.Display) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDisplayDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.DisplayId)
	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/report/DisplayService.Insert
 *
 ******************************************************************************/
func (impl *DisplayServiceImplement) Insert(user *models.Usertab, entities *[]model.Display) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DisplayId)
		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/report/DisplayService.Delete
 *
 ******************************************************************************/
func (impl *DisplayServiceImplement) Delete(user *models.Usertab, entities *[]model.Display) error {

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

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

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

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

	condition := DefaultConditionOfDisplayAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewDisplayDAO(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_LF_MOM_BACKEND/services/report/DisplayService.Update
 *
 ******************************************************************************/
func (impl *DisplayServiceImplement) Update(user *models.Usertab, entities *[]model.Display) error {

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

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

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

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