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

package implments

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

/******************************************************************************
 *
 * @Description              : LabelParserDetailLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-02 09:38:09
 *
 ******************************************************************************/
var DefaultConditionOfLabelParserDetailLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.LabelParserDetailLst_ParserId.UrlParameterName:       grmi.NewConditionItem(meta.LabelParserDetailLst_ParserId, grmi.Equal, true),
		meta.LabelParserDetailLst_Pos.UrlParameterName:            grmi.NewConditionItem(meta.LabelParserDetailLst_Pos, grmi.Equal, true),
		meta.LabelParserDetailLst_Abrev.UrlParameterName:          grmi.NewConditionItem(meta.LabelParserDetailLst_Abrev, grmi.Equal, false),
		meta.LabelParserDetailLst_Descr.UrlParameterName:          grmi.NewConditionItem(meta.LabelParserDetailLst_Descr, grmi.Equal, false),
		meta.LabelParserDetailLst_StartPos.UrlParameterName:       grmi.NewConditionItem(meta.LabelParserDetailLst_StartPos, grmi.Equal, false),
		meta.LabelParserDetailLst_EndPos.UrlParameterName:         grmi.NewConditionItem(meta.LabelParserDetailLst_EndPos, grmi.Equal, false),
		meta.LabelParserDetailLst_DisplayToggle.UrlParameterName:  grmi.NewConditionItem(meta.LabelParserDetailLst_DisplayToggle, grmi.Equal, false),
		meta.LabelParserDetailLst_ValidateToggle.UrlParameterName: grmi.NewConditionItem(meta.LabelParserDetailLst_ValidateToggle, grmi.Equal, false),
		meta.LabelParserDetailLst_StoreToggle.UrlParameterName:    grmi.NewConditionItem(meta.LabelParserDetailLst_StoreToggle, grmi.Equal, false),
		meta.LabelParserDetailLst_ParserMap.UrlParameterName:      grmi.NewConditionItem(meta.LabelParserDetailLst_ParserMap, grmi.Equal, false),
		meta.LabelParserDetailLst_ValidateRule.UrlParameterName:   grmi.NewConditionItem(meta.LabelParserDetailLst_ValidateRule, grmi.Equal, false),
		meta.LabelParserDetailLst_ValidatePara.UrlParameterName:   grmi.NewConditionItem(meta.LabelParserDetailLst_ValidatePara, grmi.Equal, false),
		meta.LabelParserDetailLst_StoreField.UrlParameterName:     grmi.NewConditionItem(meta.LabelParserDetailLst_StoreField, grmi.Equal, false),
		meta.LabelParserDetailLst_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.LabelParserDetailLst_LastModify, grmi.Approximate, false),
		meta.LabelParserDetailLst_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.LabelParserDetailLst_LastUser, grmi.Equal, false),
		meta.LabelParserDetailLst_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.LabelParserDetailLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : LabelParserDetailLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-02 09:38:09
 *
 ******************************************************************************/
var DefaultConditionOfLabelParserDetailLstAndPaging = grmi.NewCondition(
	DefaultConditionOfLabelParserDetailLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : LabelParserDetailLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : LabelParserDetailLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-02 09:38:09
 *
 ******************************************************************************/
type LabelParserDetailLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewLabelParserDetailLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个LabelParserDetailLstDAOImplement实例
 *
 * @Return Value             : LabelParserDetailLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-02 09:38:09
 *
 ******************************************************************************/
func NewLabelParserDetailLstServiceImplement() *LabelParserDetailLstServiceImplement {
	return &LabelParserDetailLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/LabelParserDetailLstService.InsertOne
 *
 ******************************************************************************/
func (impl *LabelParserDetailLstServiceImplement) InsertOne(user *models.Usertab, entity *model.LabelParserDetailLst) error {

	grmi.Log(user, "/services/base/implments/LabelParserDetailLst.service.impl.go", "InsertOneLabelParserDetailLst", "插入一个LabelParserDetailLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewLabelParserDetailLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ParserId, 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_LF_MOM_BACKEND/services/base/LabelParserDetailLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *LabelParserDetailLstServiceImplement) DeleteOne(user *models.Usertab, parserId string, pos int) error {

	grmi.Log(user, "/services/base/implments/LabelParserDetailLst.service.impl.go", "DeleteOneLabelParserDetailLst", "删除一个LabelParserDetailLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/LabelParserDetailLstService.SelectOne
 *
 ******************************************************************************/
func (impl *LabelParserDetailLstServiceImplement) SelectOne(user *models.Usertab, parserId string, pos int) (*model.LabelParserDetailLst, error) {

	grmi.Log(user, "/services/base/implments/LabelParserDetailLst.service.impl.go", "SelectOneLabelParserDetailLst", "查询一个LabelParserDetailLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/LabelParserDetailLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *LabelParserDetailLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.LabelParserDetailLst) error {

	grmi.Log(user, "/services/base/implments/LabelParserDetailLst.service.impl.go", "UpdateOneLabelParserDetailLst", "修改一个LabelParserDetailLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewLabelParserDetailLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ParserId, 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_LF_MOM_BACKEND/services/base/LabelParserDetailLstService.Insert
 *
 ******************************************************************************/
func (impl *LabelParserDetailLstServiceImplement) Insert(user *models.Usertab, entities *[]model.LabelParserDetailLst) error {

	grmi.Log(user, "/services/base/implments/LabelParserDetailLst.service.impl.go", "InsertLabelParserDetailLst", "插入多个LabelParserDetailLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ParserId, 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_LF_MOM_BACKEND/services/base/LabelParserDetailLstService.Delete
 *
 ******************************************************************************/
func (impl *LabelParserDetailLstServiceImplement) Delete(user *models.Usertab, entities *[]model.LabelParserDetailLst) error {

	grmi.Log(user, "/services/base/implments/LabelParserDetailLst.service.impl.go", "DeleteLabelParserDetailLst", "删除多个LabelParserDetailLst")

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

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

	grmi.Log(user, "/services/base/implments/LabelParserDetailLst.service.impl.go", "SelectLabelParserDetailLst", "查询LabelParserDetailLst")

	condition := DefaultConditionOfLabelParserDetailLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewLabelParserDetailLstDAO(session, user.Pid, 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/base/LabelParserDetailLstService.Update
 *
 ******************************************************************************/
func (impl *LabelParserDetailLstServiceImplement) Update(user *models.Usertab, entities *[]model.LabelParserDetailLst) error {

	grmi.Log(user, "/services/base/implments/LabelParserDetailLst.service.impl.go", "UpdateLabelParserDetailLst", "修改多个LabelParserDetailLst")

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

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

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