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

package implments

import (
	baseDal "LAPP_ACURA_MOM_BACKEND/dao/base"
	dal "LAPP_ACURA_MOM_BACKEND/dao/me"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	baseMeta "LAPP_ACURA_MOM_BACKEND/meta/base"
	meta "LAPP_ACURA_MOM_BACKEND/meta/me"
	baseModel "LAPP_ACURA_MOM_BACKEND/models/base"
	model "LAPP_ACURA_MOM_BACKEND/models/me"
)

/******************************************************************************
 *
 * @Description              : BomLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-09 14:13:50
 *
 ******************************************************************************/
var DefaultConditionOfBomLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.BomLst_BomId.UrlParameterName:         grmi.NewConditionItem(meta.BomLst_BomId, grmi.Equal, true),
		meta.BomLst_FmatNr.UrlParameterName:        grmi.NewConditionItem(meta.BomLst_FmatNr, grmi.Equal, true),
		meta.BomLst_CmatNr.UrlParameterName:        grmi.NewConditionItem(meta.BomLst_CmatNr, grmi.Equal, true),
		meta.BomLst_FmatName.UrlParameterName:      grmi.NewConditionItem(meta.BomLst_FmatName, grmi.Equal, false),
		meta.BomLst_FmatSpec.UrlParameterName:      grmi.NewConditionItem(meta.BomLst_FmatSpec, grmi.Equal, false),
		meta.BomLst_FmatQty.UrlParameterName:       grmi.NewConditionItem(meta.BomLst_FmatQty, grmi.Equal, false),
		meta.BomLst_FmatUom.UrlParameterName:       grmi.NewConditionItem(meta.BomLst_FmatUom, grmi.Equal, false),
		meta.BomLst_CmatName.UrlParameterName:      grmi.NewConditionItem(meta.BomLst_CmatName, grmi.Equal, false),
		meta.BomLst_CmatQty.UrlParameterName:       grmi.NewConditionItem(meta.BomLst_CmatQty, grmi.Equal, false),
		meta.BomLst_CmatUom.UrlParameterName:       grmi.NewConditionItem(meta.BomLst_CmatUom, grmi.Equal, false),
		meta.BomLst_CmatType.UrlParameterName:      grmi.NewConditionItem(meta.BomLst_CmatType, grmi.Equal, false),
		meta.BomLst_Position.UrlParameterName:      grmi.NewConditionItem(meta.BomLst_Position, grmi.Equal, false),
		meta.BomLst_LeadTime.UrlParameterName:      grmi.NewConditionItem(meta.BomLst_LeadTime, grmi.Equal, false),
		meta.BomLst_TimeUom.UrlParameterName:       grmi.NewConditionItem(meta.BomLst_TimeUom, grmi.Equal, false),
		meta.BomLst_LossRate.UrlParameterName:      grmi.NewConditionItem(meta.BomLst_LossRate, grmi.Equal, false),
		meta.BomLst_BackFlushItem.UrlParameterName: grmi.NewConditionItem(meta.BomLst_BackFlushItem, grmi.Equal, false),
		meta.BomLst_CriticalItem.UrlParameterName:  grmi.NewConditionItem(meta.BomLst_CriticalItem, grmi.Equal, false),
		meta.BomLst_ValidFrom.UrlParameterName:     grmi.NewConditionItem(meta.BomLst_ValidFrom, grmi.Equal, false),
		meta.BomLst_ValidTo.UrlParameterName:       grmi.NewConditionItem(meta.BomLst_ValidTo, grmi.Equal, false),
		meta.BomLst_ShipableAU.UrlParameterName:    grmi.NewConditionItem(meta.BomLst_ShipableAU, grmi.Equal, false),
		meta.BomLst_BomlstCval1.UrlParameterName:   grmi.NewConditionItem(meta.BomLst_BomlstCval1, grmi.Equal, false),
		meta.BomLst_BomlstCval2.UrlParameterName:   grmi.NewConditionItem(meta.BomLst_BomlstCval2, grmi.Equal, false),
		meta.BomLst_BomlstIval1.UrlParameterName:   grmi.NewConditionItem(meta.BomLst_BomlstIval1, grmi.Equal, false),
		meta.BomLst_BomlstIval2.UrlParameterName:   grmi.NewConditionItem(meta.BomLst_BomlstIval2, grmi.Equal, false),
		meta.BomLst_LevelNr.UrlParameterName:       grmi.NewConditionItem(meta.BomLst_LevelNr, grmi.Equal, false),
		meta.BomLst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.BomLst_LastModify, grmi.Approximate, false),
		meta.BomLst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.BomLst_LastUser, grmi.Equal, false),
		meta.BomLst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.BomLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/BomLstService.InsertOne
 *
 ******************************************************************************/
func (impl *BomLstServiceImplement) InsertOne(user *global.User, entity *model.BomLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewBomLstDAO(session, user.PlantNr, user.UserId)
	headDao := dal.NewBomHeadDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.BomId, entity.FmatNr, entity.CmatNr)
	if err != nil {
		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.PlantNr, user.UserId)
	articleLi, err := articleDao.Select([]grmi.Predicate{baseMeta.Article_ArtId.NewPredicate(grmi.Equal, entity.FmatNr)}, nil)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("查询物料失败, error:" + err.Error())
	}
	if len(articleLi) == 0 {
		_ = session.Rollback()
		return grmi.NewBusinessError("物料不存在, 物料ID:" + entity.FmatNr)
	}
	article := articleLi[0]
	if article.ArtSpec1 != baseModel.ART_TYPE_FINISHED && article.ArtSpec1 != baseModel.ART_TYPE_CARSET && article.ArtSpec1 != baseModel.ART_TYPE_SEMI_FINISHED {
		_ = session.Rollback()
		return grmi.NewBusinessError("物料类型不支持该操作,artId:" + article.ArtId)
	}
	articleLi2, err := articleDao.Select([]grmi.Predicate{baseMeta.Article_ArtId.NewPredicate(grmi.Equal, entity.CmatNr)}, nil)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("查询物料失败, error:" + err.Error())
	}
	if len(articleLi2) == 0 {
		_ = session.Rollback()
		return grmi.NewBusinessError("物料不存在, 物料ID:" + entity.CmatNr)
	}
	article2 := articleLi2[0]
	if article.ArtSpec1 == baseModel.ART_TYPE_CARSET {
		if article2.ArtSpec1 != baseModel.ART_TYPE_FINISHED {
			_ = session.Rollback()
			return grmi.NewBusinessError("传递的子物料不是总成, artId:" + article2.ArtId)
		}
	} else {
		if article2.ArtSpec1 == baseModel.ART_TYPE_FINISHED || article2.ArtSpec1 == baseModel.ART_TYPE_CARSET {
			_ = session.Rollback()
			return grmi.NewBusinessError("传递的物料是总成或虚拟物料ID, artId:" + article2.ArtId)
		}
	}

	if entity.FmatNr == entity.CmatNr {
		_ = session.Rollback()
		return grmi.NewBusinessError("上级和下级物料ID相同")
	}
	bomHead, err := headDao.SelectOne(entity.BomId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if bomHead == nil {
		articleLi, err := articleDao.Select([]grmi.Predicate{baseMeta.Article_ArtId.NewPredicate(grmi.Equal, entity.BomId)}, nil)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询物料失败, error:" + err.Error())
		}
		if len(articleLi) == 0 {
			_ = session.Rollback()
			return grmi.NewBusinessError("物料不存在, 物料ID:" + entity.BomId)
		}
		article := articleLi[0]
		if article.ArtSpec1 != baseModel.ART_TYPE_FINISHED && article.ArtSpec1 != baseModel.ART_TYPE_CARSET && article.ArtSpec1 != baseModel.ART_TYPE_SEMI_FINISHED {
			_ = session.Rollback()
			return grmi.NewBusinessError("物料类型不支持该操作")
		}
		headData := model.BomHead{
			BomId: entity.BomId,
			MatNr: entity.BomId,
			Descr: article.Descr1,
		}
		err = headDao.InsertOne(&headData)
		if err != nil {
			_ = session.Rollback()
			return err
		}
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/BomLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *BomLstServiceImplement) DeleteOne(user *global.User, bomId string, fmatNr string, cmatNr string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewBomLstDAO(session, user.PlantNr, user.UserId)
	headDao := dal.NewBomHeadDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(bomId, fmatNr, cmatNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(bomId, fmatNr, cmatNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	lstLi, err := dao.Select([]grmi.Predicate{meta.BomLst_BomId.NewPredicate(grmi.Equal, bomId)}, nil)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if len(lstLi) == 0 {
		err = headDao.DeleteOne(bomId)
		if err != nil {
			_ = session.Rollback()
			return err
		}
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/BomLstService.SelectOne
 *
 ******************************************************************************/
func (impl *BomLstServiceImplement) SelectOne(user *global.User, bomId string, fmatNr string, cmatNr string) (*model.BomLst, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewBomLstDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(bomId, fmatNr, cmatNr)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/BomLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *BomLstServiceImplement) UpdateOne(user *global.User, entity *model.BomLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewBomLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.BomId, entity.FmatNr, entity.CmatNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/BomLstService.Insert
 *
 ******************************************************************************/
func (impl *BomLstServiceImplement) Insert(user *global.User, entities *[]model.BomLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewBomLstDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.BomId, entity.FmatNr, entity.CmatNr)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/BomLstService.Delete
 *
 ******************************************************************************/
func (impl *BomLstServiceImplement) Delete(user *global.User, entities *[]model.BomLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewBomLstDAO(session, user.PlantNr, user.UserId)

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/BomLstService.Select
 *
 ******************************************************************************/
func (impl *BomLstServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfBomLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewBomLstDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/me/BomLstService.Update
 *
 ******************************************************************************/
func (impl *BomLstServiceImplement) Update(user *global.User, entities *[]model.BomLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewBomLstDAO(session, user.PlantNr, user.UserId)

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

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