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

package implments

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

/******************************************************************************
 *
 * @Description              : BomHead的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-09 14:13:50
 *
 ******************************************************************************/
var DefaultConditionOfBomHead = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.BomHead_BomId.UrlParameterName:        grmi.NewConditionItem(meta.BomHead_BomId, grmi.Equal, true),
		meta.BomHead_MatNr.UrlParameterName:        grmi.NewConditionItem(meta.BomHead_MatNr, grmi.Equal, false),
		meta.BomHead_Descr.UrlParameterName:        grmi.NewConditionItem(meta.BomHead_Descr, grmi.Equal, false),
		meta.BomHead_BomheadCval1.UrlParameterName: grmi.NewConditionItem(meta.BomHead_BomheadCval1, grmi.Equal, false),
		meta.BomHead_BomheadCval2.UrlParameterName: grmi.NewConditionItem(meta.BomHead_BomheadCval2, grmi.Equal, false),
		meta.BomHead_BomheadIval1.UrlParameterName: grmi.NewConditionItem(meta.BomHead_BomheadIval1, grmi.Equal, false),
		meta.BomHead_BomheadIval2.UrlParameterName: grmi.NewConditionItem(meta.BomHead_BomheadIval2, grmi.Equal, false),
		meta.BomHead_BomheadDval1.UrlParameterName: grmi.NewConditionItem(meta.BomHead_BomheadDval1, grmi.Equal, false),
		meta.BomHead_BomheadDval2.UrlParameterName: grmi.NewConditionItem(meta.BomHead_BomheadDval2, grmi.Equal, false),
		meta.BomHead_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.BomHead_LastModify, grmi.Approximate, false),
		meta.BomHead_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.BomHead_LastUser, grmi.Equal, false),
		meta.BomHead_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.BomHead_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : BomHead的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-09 14:13:50
 *
 ******************************************************************************/
var DefaultConditionOfBomHeadAndPaging = grmi.NewCondition(
	DefaultConditionOfBomHead.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : BomHeadServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : BomHead的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-09 14:13:50
 *
 ******************************************************************************/
type BomHeadServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewBomHeadDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个BomHeadDAOImplement实例
 *
 * @Return Value             : BomHeadDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-09 14:13:50
 *
 ******************************************************************************/
func NewBomHeadServiceImplement() *BomHeadServiceImplement {
	return &BomHeadServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/me/BomHeadService.InsertOne
 *
 ******************************************************************************/
func (impl *BomHeadServiceImplement) InsertOne(user *models.Usertab, entity *model.BomHead) error {

	grmi.Log(user, "/services/me/implments/BomHead.service.impl.go", "InsertOneBomHead", "插入一个BomHead")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewBomHeadDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.BomId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		_ = session.Rollback()
		return err
	}

	articleDao := baseDal.NewArticleDAO(session, user.Pid, user.Userid)
	article, err := articleDao.SelectOne(entity.MatNr)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("查询物料数据失败, error:" + err.Error())
	}
	if article == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的物料数据")
	}
	if article.ArtSpec1 != baseModel.ART_TYPE_FINISHED && article.ArtSpec1 != baseModel.ART_TYPE_VIRTUAL {
		_ = session.Rollback()
		return grmi.NewBusinessError("传递的物料不是总成或虚拟物料ID")
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/me/BomHeadService.DeleteOne
 *
 ******************************************************************************/
func (impl *BomHeadServiceImplement) DeleteOne(user *models.Usertab, bomId string) error {

	grmi.Log(user, "/services/me/implments/BomHead.service.impl.go", "DeleteOneBomHead", "删除一个BomHead")

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

	dao2 := dal.NewBomLstDAO(session, user.Pid, user.Userid)
	err = dao2.DeleteWhere([]grmi.Predicate{meta.BomLst_BomId.NewPredicate(grmi.Equal, bomId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/me/BomHeadService.SelectOne
 *
 ******************************************************************************/
func (impl *BomHeadServiceImplement) SelectOne(user *models.Usertab, bomId string) (*model.BomHead, error) {

	grmi.Log(user, "/services/me/implments/BomHead.service.impl.go", "SelectOneBomHead", "查询一个BomHead")

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

	if result != nil{
		var Me model.BomLst
		Me.PlantNr = result.PlantNr
		Me.BomId = result.BomId
		bomList, err := Me.SetMenu(result.MatNr)
		if err != nil {
			return nil, err
		}
		result.BomLstLi = bomList
	}

	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/me/BomHeadService.UpdateOne
 *
 ******************************************************************************/
func (impl *BomHeadServiceImplement) UpdateOne(user *models.Usertab, entity *model.BomHead) error {

	grmi.Log(user, "/services/me/implments/BomHead.service.impl.go", "UpdateOneBomHead", "修改一个BomHead")

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

	grmi.Log(user, "/services/me/implments/BomHead.service.impl.go", "InsertBomHead", "插入多个BomHead")

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

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

	grmi.Log(user, "/services/me/implments/BomHead.service.impl.go", "DeleteBomHead", "删除多个BomHead")

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

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

	grmi.Log(user, "/services/me/implments/BomHead.service.impl.go", "SelectBomHead", "查询BomHead")

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

	grmi.Log(user, "/services/me/implments/BomHead.service.impl.go", "UpdateBomHead", "修改多个BomHead")

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

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

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


