// 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"
)

/******************************************************************************
 *
 * @Description              : ArcDisplayInfo的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-21 11:39:48
 *
 ******************************************************************************/
var DefaultConditionOfArcDisplayInfo = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArcDisplayInfo_Pos.UrlParameterName:            grmi.NewConditionItem(meta.ArcDisplayInfo_Pos, grmi.Equal, true),
		meta.ArcDisplayInfo_DescCN.UrlParameterName:         grmi.NewConditionItem(meta.ArcDisplayInfo_DescCN, grmi.Equal, true),
		meta.ArcDisplayInfo_StandardValue.UrlParameterName:  grmi.NewConditionItem(meta.ArcDisplayInfo_StandardValue, grmi.Equal, false),
		meta.ArcDisplayInfo_StatisticField.UrlParameterName: grmi.NewConditionItem(meta.ArcDisplayInfo_StatisticField, grmi.Equal, false),
		meta.ArcDisplayInfo_Title.UrlParameterName:          grmi.NewConditionItem(meta.ArcDisplayInfo_Title, grmi.Equal, false),
		meta.ArcDisplayInfo_Uom.UrlParameterName:            grmi.NewConditionItem(meta.ArcDisplayInfo_Uom, grmi.Equal, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArcDisplayInfo的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-21 11:39:48
 *
 ******************************************************************************/
var DefaultConditionOfArcDisplayInfoAndPaging = grmi.NewCondition(
	DefaultConditionOfArcDisplayInfo.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArcDisplayInfoServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArcDisplayInfo的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-21 11:39:48
 *
 ******************************************************************************/
type ArcDisplayInfoServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArcDisplayInfoDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArcDisplayInfoDAOImplement实例
 *
 * @Return Value             : ArcDisplayInfoDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-21 11:39:48
 *
 ******************************************************************************/
func NewArcDisplayInfoServiceImplement() *ArcDisplayInfoServiceImplement {
	return &ArcDisplayInfoServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/report/ArcDisplayInfoService.InsertOne
 *
 ******************************************************************************/
func (impl *ArcDisplayInfoServiceImplement) InsertOne(entity *model.ArcDisplayInfo) error {

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArcDisplayInfoDAO(session)
	record, err := dao.SelectOne(entity.Pos, entity.DescCN)
	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/ArcDisplayInfoService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArcDisplayInfoServiceImplement) DeleteOne(pos int, descCN string) error {

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/report/ArcDisplayInfoService.SelectOne
 *
 ******************************************************************************/
func (impl *ArcDisplayInfoServiceImplement) SelectOne(pos int, descCN string) (*model.ArcDisplayInfo, error) {

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/report/ArcDisplayInfoService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArcDisplayInfoServiceImplement) UpdateOne(entity *model.ArcDisplayInfo) error {

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArcDisplayInfoDAO(session)
	record, err := dao.SelectOne(entity.Pos, entity.DescCN)
	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/ArcDisplayInfoService.Insert
 *
 ******************************************************************************/
func (impl *ArcDisplayInfoServiceImplement) Insert(entities *[]model.ArcDisplayInfo) error {

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArcDisplayInfoDAO(session)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.Pos, entity.DescCN)
		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/ArcDisplayInfoService.Delete
 *
 ******************************************************************************/
func (impl *ArcDisplayInfoServiceImplement) Delete(entities *[]model.ArcDisplayInfo) error {

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArcDisplayInfoDAO(session)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.Pos, entity.DescCN)
		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/ArcDisplayInfoService.Select
 *
 ******************************************************************************/
func (impl *ArcDisplayInfoServiceImplement) Select(urlParameters map[string]string) (interface{}, error) {

	condition := DefaultConditionOfArcDisplayInfoAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewArcDisplayInfoDAO(session)
	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/ArcDisplayInfoService.Update
 *
 ******************************************************************************/
func (impl *ArcDisplayInfoServiceImplement) Update(entities *[]model.ArcDisplayInfo) error {

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArcDisplayInfoDAO(session)

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

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