// 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              : LabelParserHead的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-02 09:38:09
 *
 ******************************************************************************/
var DefaultConditionOfLabelParserHead = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.LabelParserHead_ParserId.UrlParameterName:   grmi.NewConditionItem(meta.LabelParserHead_ParserId, grmi.Equal, true),
		meta.LabelParserHead_Length.UrlParameterName:     grmi.NewConditionItem(meta.LabelParserHead_Length, grmi.Equal, false),
		meta.LabelParserHead_ParserType.UrlParameterName: grmi.NewConditionItem(meta.LabelParserHead_ParserType, grmi.Equal, false),
		meta.LabelParserHead_Descr.UrlParameterName:      grmi.NewConditionItem(meta.LabelParserHead_Descr, grmi.Equal, false),
		meta.LabelParserHead_Example.UrlParameterName:    grmi.NewConditionItem(meta.LabelParserHead_Example, grmi.Equal, false),
		meta.LabelParserHead_LastModify.UrlParameterName: grmi.NewConditionItem(meta.LabelParserHead_LastModify, grmi.Approximate, false),
		meta.LabelParserHead_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.LabelParserHead_LastUser, grmi.Equal, false),
		meta.LabelParserHead_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.LabelParserHead_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/LabelParserHeadService.InsertOne
 *
 ******************************************************************************/
func (impl *LabelParserHeadServiceImplement) InsertOne(user *models.Usertab, entity *model.LabelParserHead) error {

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

	engine := db.Eloquent.Master()


	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewLabelParserHeadDAO(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 {
		session.Rollback()
		return err
	}

	/**处理数据**/
	for k, _ := range entity.Valst {
		entity.Valst[k].ParserId = entity.ParserId
		entity.Valst[k].Pos = k+1
	}
	lstdao := dal.NewLabelParserDetailLstDAO(session, user.Pid, user.Userid)
	err = lstdao.Insert(&entity.Valst)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/LabelParserHeadService.DeleteOne
 *
 ******************************************************************************/
func (impl *LabelParserHeadServiceImplement) DeleteOne(user *models.Usertab, parserId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewLabelParserHeadDAO(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 {
		session.Rollback()
		return err
	}
	lstdao := dal.NewLabelParserDetailLstDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.LabelParserDetailLst_ParserId.NewPredicate(grmi.Equal, parserId),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()

	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/LabelParserHeadService.SelectOne
 *
 ******************************************************************************/
func (impl *LabelParserHeadServiceImplement) SelectOne(user *models.Usertab, parserId string) (*model.LabelParserHead, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewLabelParserHeadDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(parserId)
	if err != nil {
		return nil, err
	}
	lstdao := dal.NewLabelParserDetailLstDAO(session, user.Pid, user.Userid)
	list, err := lstdao.Select([]grmi.Predicate{
		meta.LabelParserDetailLst_ParserId.NewPredicate(grmi.Equal, parserId),
	}, nil)
	if err != nil {
		return nil, err
	}
	result.Valst = list
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/LabelParserHeadService.UpdateOne
 *
 ******************************************************************************/
func (impl *LabelParserHeadServiceImplement) UpdateOne(user *models.Usertab, entity *model.LabelParserHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewLabelParserHeadDAO(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 {
		session.Rollback()
		return err
	}
	/**处理数据**/
	for k, _ := range entity.Valst {
		entity.Valst[k].ParserId = entity.ParserId
		entity.Valst[k].Pos = k+1
	}
	lstdao := dal.NewLabelParserDetailLstDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.LabelParserDetailLst_ParserId.NewPredicate(grmi.Equal, entity.ParserId),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	err = lstdao.Insert(&entity.Valst)
	if err != nil {
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/LabelParserHeadService.Insert
 *
 ******************************************************************************/
func (impl *LabelParserHeadServiceImplement) Insert(user *models.Usertab, entities *[]model.LabelParserHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewLabelParserHeadDAO(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/LabelParserHeadService.Delete
 *
 ******************************************************************************/
func (impl *LabelParserHeadServiceImplement) Delete(user *models.Usertab, entities *[]model.LabelParserHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewLabelParserHeadDAO(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/LabelParserHeadService.Select
 *
 ******************************************************************************/
func (impl *LabelParserHeadServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewLabelParserHeadDAO(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
}
