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

package implments

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

/******************************************************************************
 *
 * @Description              : OdsHead的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfOdsHead = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OdsHead_OdsId.UrlParameterName:         grmi.NewConditionItem(meta.OdsHead_OdsId, grmi.Equal, true),
		meta.OdsHead_Descr.UrlParameterName:         grmi.NewConditionItem(meta.OdsHead_Descr, grmi.Equal, false),
		meta.OdsHead_OdsTypeId.UrlParameterName:     grmi.NewConditionItem(meta.OdsHead_OdsTypeId, grmi.Equal, false),
		meta.OdsHead_OdsContentIdx.UrlParameterName: grmi.NewConditionItem(meta.OdsHead_OdsContentIdx, grmi.Equal, false),
		meta.OdsHead_CtrlPara1.UrlParameterName:     grmi.NewConditionItem(meta.OdsHead_CtrlPara1, grmi.Equal, false),
		meta.OdsHead_CtrlPara2.UrlParameterName:     grmi.NewConditionItem(meta.OdsHead_CtrlPara2, grmi.Equal, false),
		meta.OdsHead_CtrlPara3.UrlParameterName:     grmi.NewConditionItem(meta.OdsHead_CtrlPara3, grmi.Equal, false),
		meta.OdsHead_CtrlPara4.UrlParameterName:     grmi.NewConditionItem(meta.OdsHead_CtrlPara4, grmi.Equal, false),
		meta.OdsHead_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.OdsHead_LastModify, grmi.Approximate, false),
		meta.OdsHead_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.OdsHead_LastUser, grmi.Equal, false),
		meta.OdsHead_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.OdsHead_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/OdsHeadService.InsertOne
 *
 ******************************************************************************/
func (impl *OdsHeadServiceImplement) InsertOne(user *models.Usertab, entity *model.OdsHead) error {

	grmi.Log(user, "/services/ods/implments/ODS_OdsHead.service.impl.go", "InsertOneOdsHead", "插入一个OdsHead")

	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.NewOdsHeadDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OdsId)
	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].OdsId = entity.OdsId
		entity.Valst[k].Pos = k+1
	}
	lstdao := dal.NewOdsStepLstDAO(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/ods/OdsHeadService.DeleteOne
 *
 ******************************************************************************/
func (impl *OdsHeadServiceImplement) DeleteOne(user *models.Usertab, odsId string) error {

	grmi.Log(user, "/services/ods/implments/ODS_OdsHead.service.impl.go", "DeleteOneOdsHead", "删除一个OdsHead")

	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.NewOdsHeadDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(odsId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(odsId)
	if err != nil {
		session.Rollback()
		return err
	}
	lstdao := dal.NewOdsStepLstDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.OdsStepLst_OdsId.NewPredicate(grmi.Equal, odsId),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/OdsHeadService.SelectOne
 *
 ******************************************************************************/
func (impl *OdsHeadServiceImplement) SelectOne(user *models.Usertab, odsId string) (*model.OdsHead, error) {

	grmi.Log(user, "/services/ods/implments/ODS_OdsHead.service.impl.go", "SelectOneOdsHead", "查询一个OdsHead")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/OdsHeadService.UpdateOne
 *
 ******************************************************************************/
func (impl *OdsHeadServiceImplement) UpdateOne(user *models.Usertab, entity *model.OdsHead) error {

	grmi.Log(user, "/services/ods/implments/ODS_OdsHead.service.impl.go", "UpdateOneOdsHead", "修改一个OdsHead")

	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.NewOdsHeadDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OdsId)
	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].OdsId = entity.OdsId
		entity.Valst[k].Pos = k+1
	}
	lstdao := dal.NewOdsStepLstDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.OdsStepLst_OdsId.NewPredicate(grmi.Equal, entity.OdsId),
	})
	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/ods/OdsHeadService.Insert
 *
 ******************************************************************************/
func (impl *OdsHeadServiceImplement) Insert(user *models.Usertab, entities *[]model.OdsHead) error {

	grmi.Log(user, "/services/ods/implments/ODS_OdsHead.service.impl.go", "InsertOdsHead", "插入多个OdsHead")

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

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

	grmi.Log(user, "/services/ods/implments/ODS_OdsHead.service.impl.go", "DeleteOdsHead", "删除多个OdsHead")

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

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

	grmi.Log(user, "/services/ods/implments/ODS_OdsHead.service.impl.go", "SelectOdsHead", "查询OdsHead")

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

	grmi.Log(user, "/services/ods/implments/ODS_OdsHead.service.impl.go", "UpdateOdsHead", "修改多个OdsHead")

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

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

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