// 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              : CodeParser的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfCodeParser = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.CodeParser_ParserId.UrlParameterName:   grmi.NewConditionItem(meta.CodeParser_ParserId, grmi.Equal, true),
		meta.CodeParser_StartPos.UrlParameterName:   grmi.NewConditionItem(meta.CodeParser_StartPos, grmi.Equal, false),
		meta.CodeParser_Length.UrlParameterName:     grmi.NewConditionItem(meta.CodeParser_Length, grmi.Equal, false),
		meta.CodeParser_ParserType.UrlParameterName: grmi.NewConditionItem(meta.CodeParser_ParserType, grmi.Equal, false),
		meta.CodeParser_Descr.UrlParameterName:      grmi.NewConditionItem(meta.CodeParser_Descr, grmi.Equal, false),
		meta.CodeParser_LastModify.UrlParameterName: grmi.NewConditionItem(meta.CodeParser_LastModify, grmi.Approximate, false),
		meta.CodeParser_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.CodeParser_LastUser, grmi.Equal, false),
		meta.CodeParser_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.CodeParser_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : CodeParser的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfCodeParserAndPaging = grmi.NewCondition(
	DefaultConditionOfCodeParser.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : CodeParserServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : CodeParser的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type CodeParserServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewCodeParserDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个CodeParserDAOImplement实例
 *
 * @Return Value             : CodeParserDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewCodeParserServiceImplement() *CodeParserServiceImplement {
	return &CodeParserServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/CodeParserService.InsertOne
 *
 ******************************************************************************/
func (impl *CodeParserServiceImplement) InsertOne(user *models.Usertab, entity *model.CodeParser) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCodeParserDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ParserId)
	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/CodeParserService.DeleteOne
 *
 ******************************************************************************/
func (impl *CodeParserServiceImplement) DeleteOne(user *models.Usertab, parserId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/CodeParserService.SelectOne
 *
 ******************************************************************************/
func (impl *CodeParserServiceImplement) SelectOne(user *models.Usertab, parserId string) (*model.CodeParser, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/CodeParserService.UpdateOne
 *
 ******************************************************************************/
func (impl *CodeParserServiceImplement) UpdateOne(user *models.Usertab, entity *model.CodeParser) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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