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

package implments

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

/******************************************************************************
 *
 * @Description              : GoodsReceiptDetail的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfGoodsReceiptDetail = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.GoodsReceiptDetail_GoodsReceiptId.UrlParameterName: grmi.NewConditionItem(meta.GoodsReceiptDetail_GoodsReceiptId, grmi.Equal, true),
		meta.GoodsReceiptDetail_GRPos.UrlParameterName:          grmi.NewConditionItem(meta.GoodsReceiptDetail_GRPos, grmi.Equal, true),
		meta.GoodsReceiptDetail_ArtId.UrlParameterName:          grmi.NewConditionItem(meta.GoodsReceiptDetail_ArtId, grmi.Equal, false),
		meta.GoodsReceiptDetail_ArtName.UrlParameterName:        grmi.NewConditionItem(meta.GoodsReceiptDetail_ArtName, grmi.Equal, false),
		meta.GoodsReceiptDetail_ArtType.UrlParameterName:        grmi.NewConditionItem(meta.GoodsReceiptDetail_ArtType, grmi.Equal, false),
		meta.GoodsReceiptDetail_Remark.UrlParameterName:         grmi.NewConditionItem(meta.GoodsReceiptDetail_Remark, grmi.Equal, false),
		meta.GoodsReceiptDetail_Auditor.UrlParameterName:        grmi.NewConditionItem(meta.GoodsReceiptDetail_Auditor, grmi.Equal, false),
		meta.GoodsReceiptDetail_AuditTime.UrlParameterName:      grmi.NewConditionItem(meta.GoodsReceiptDetail_AuditTime, grmi.Approximate, false),
		meta.GoodsReceiptDetail_AuditStatus.UrlParameterName:    grmi.NewConditionItem(meta.GoodsReceiptDetail_AuditStatus, grmi.Equal, false),
		meta.GoodsReceiptDetail_BatchId.UrlParameterName:        grmi.NewConditionItem(meta.GoodsReceiptDetail_BatchId, grmi.Equal, false),
		meta.GoodsReceiptDetail_OrderedQty.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptDetail_OrderedQty, grmi.Equal, false),
		meta.GoodsReceiptDetail_OrderType.UrlParameterName:      grmi.NewConditionItem(meta.GoodsReceiptDetail_OrderType, grmi.Equal, false),
		meta.GoodsReceiptDetail_DeliveredQty.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptDetail_DeliveredQty, grmi.Equal, false),
		meta.GoodsReceiptDetail_QtyUom.UrlParameterName:         grmi.NewConditionItem(meta.GoodsReceiptDetail_QtyUom, grmi.Equal, false),
		meta.GoodsReceiptDetail_UnitPrice.UrlParameterName:      grmi.NewConditionItem(meta.GoodsReceiptDetail_UnitPrice, grmi.Equal, false),
		meta.GoodsReceiptDetail_TotalPrice.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptDetail_TotalPrice, grmi.Equal, false),
		meta.GoodsReceiptDetail_Currency.UrlParameterName:       grmi.NewConditionItem(meta.GoodsReceiptDetail_Currency, grmi.Equal, false),
		meta.GoodsReceiptDetail_InspectToggle.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptDetail_InspectToggle, grmi.Equal, false),
		meta.GoodsReceiptDetail_InspectPlanId.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptDetail_InspectPlanId, grmi.Equal, false),
		meta.GoodsReceiptDetail_InspectStatus.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptDetail_InspectStatus, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara1.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara1, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara2.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara2, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara3.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara3, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara4.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara4, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara5.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara5, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara6.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara6, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara7.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara7, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara8.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara8, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara9.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara9, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara10.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara10, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara11.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara11, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara12.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara12, grmi.Equal, false),
		meta.GoodsReceiptDetail_GRICtrlPara13.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara13, grmi.Approximate, false),
		meta.GoodsReceiptDetail_GRICtrlPara14.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara14, grmi.Approximate, false),
		meta.GoodsReceiptDetail_GRICtrlPara15.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara15, grmi.Approximate, false),
		meta.GoodsReceiptDetail_GRICtrlPara16.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptDetail_GRICtrlPara16, grmi.Approximate, false),
		meta.GoodsReceiptDetail_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptDetail_LastModify, grmi.Approximate, false),
		meta.GoodsReceiptDetail_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.GoodsReceiptDetail_LastUser, grmi.Equal, false),
		meta.GoodsReceiptDetail_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptDetail_CreateTime, grmi.Approximate, false),
		meta.GoodsReceiptDetail_BeforeQty.UrlParameterName:		 grmi.NewConditionItem(meta.GoodsReceiptDetail_BeforeQty, grmi.Equal, false),
		meta.GoodsReceiptDetail_CurQty.UrlParameterName:	     grmi.NewConditionItem(meta.GoodsReceiptDetail_CurQty, grmi.Equal, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : GoodsReceiptDetail的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfGoodsReceiptDetailAndPaging = grmi.NewCondition(
	DefaultConditionOfGoodsReceiptDetail.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : GoodsReceiptDetailServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : GoodsReceiptDetail的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
type GoodsReceiptDetailServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewGoodsReceiptDetailDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个GoodsReceiptDetailDAOImplement实例
 *
 * @Return Value             : GoodsReceiptDetailDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
func NewGoodsReceiptDetailServiceImplement() *GoodsReceiptDetailServiceImplement {
	return &GoodsReceiptDetailServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsReceiptDetailService.InsertOne
 *
 ******************************************************************************/
func (impl *GoodsReceiptDetailServiceImplement) InsertOne(user *models.Usertab, entity *model.GoodsReceiptDetail) error {

	grmi.Log(user, "/services/wm/implments/GoodsReceiptDetail.service.impl.go", "InsertOneGoodsReceiptDetail", "插入一个GoodsReceiptDetail")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewGoodsReceiptDetailDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.GoodsReceiptId, entity.GRPos)
	if err != nil {
		_ = session.Rollback()
		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/wm/GoodsReceiptDetailService.DeleteOne
 *
 ******************************************************************************/
func (impl *GoodsReceiptDetailServiceImplement) DeleteOne(user *models.Usertab, goodsReceiptId string, gRPos int) error {

	grmi.Log(user, "/services/wm/implments/GoodsReceiptDetail.service.impl.go", "DeleteOneGoodsReceiptDetail", "删除一个GoodsReceiptDetail")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsReceiptDetailService.SelectOne
 *
 ******************************************************************************/
func (impl *GoodsReceiptDetailServiceImplement) SelectOne(user *models.Usertab, goodsReceiptId string, gRPos int) (*model.GoodsReceiptDetail, error) {

	grmi.Log(user, "/services/wm/implments/GoodsReceiptDetail.service.impl.go", "SelectOneGoodsReceiptDetail", "查询一个GoodsReceiptDetail")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsReceiptDetailService.UpdateOne
 *
 ******************************************************************************/
func (impl *GoodsReceiptDetailServiceImplement) UpdateOne(user *models.Usertab, entity *model.GoodsReceiptDetail) error {

	grmi.Log(user, "/services/wm/implments/GoodsReceiptDetail.service.impl.go", "UpdateOneGoodsReceiptDetail", "修改一个GoodsReceiptDetail")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewGoodsReceiptDetailDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.GoodsReceiptId, entity.GRPos)
	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/wm/GoodsReceiptDetailService.Insert
 *
 ******************************************************************************/
func (impl *GoodsReceiptDetailServiceImplement) Insert(user *models.Usertab, entities *[]model.GoodsReceiptDetail) error {

	grmi.Log(user, "/services/wm/implments/GoodsReceiptDetail.service.impl.go", "InsertGoodsReceiptDetail", "插入多个GoodsReceiptDetail")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewGoodsReceiptDetailDAO(session, user.Userid)
	binDao := dal.NewStorageBinDAO(session, user.Userid)
	checkLi, err := binDao.Select([]grmi.Predicate{meta.StorageBin_Status.NewPredicate(grmi.Equal, model.BIN_STAUTS_CHECK)}, nil)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if len(checkLi) > 0 {
		_ = session.Rollback()
		return grmi.NewBusinessError("目前存在未完成的盘点单")
	}
	//headDao := dal.NewGoodsReceiptHeadDAO(session, user.Userid)
	artDao := dal.NewArticleDAO(session, user.Userid)
	//binDao := dal.NewStorageBinDAO(session, user.Userid)
	//bookDao := dal.NewInventoryBookDAO(session, user.Userid)
	var headId string
	pos := 1
	for _, entity := range *entities {
		if headId == "" {
			headId = entity.GoodsReceiptId
		}
		if headId != entity.GoodsReceiptId {
			_ = session.Rollback()
			return grmi.NewBusinessError("GoodsReceiptId不一致")
		}
		entity.GRPos = pos
		pos++
		article, err := artDao.SelectOne(entity.ArtId)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		if article == nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("不存在对应的备件主数据")
		}
		 //bin, err := binDao.SelectOne(entity.StorageBinId, entity.WarehouseId, entity.StorageRackId)
		 //if err != nil {
		 //	_ = session.Rollback()
		 //	return err
		 //}
		 //if bin == nil {
		 //	_ = session.Rollback()
		 //	return grmi.NewBusinessError("不存在对应的库位")
		 //}
		 //book, err := bookDao.SelectOne(entity.StorageBinId, entity.WarehouseId, entity.ArtId)
		 //if err != nil {
		 //	_ = session.Rollback()
		 //	return err
		 //}
		 //if book == nil {
		 //	inventoryBook := model.InventoryBook{
			//	StorageBinId:  entity.StorageBinId,
			//	WarehouseId:   entity.WarehouseId,
			//	ArtId:         entity.ArtId,
			//	StorageAreaId: entity.StorageAreaId,
			//	ArtName:       entity.ArtName,
			//	ArtType:       entity.ArtType,
			//	QtyUom:        entity.QtyUom,
			//	Qty:           entity.OrderedQty,
			//}
			//err = bookDao.InsertOne(&inventoryBook)
			// if err != nil {
			//	 _ = session.Rollback()
			//	 return err
			// }
		 //}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsReceiptDetailService.Delete
 *
 ******************************************************************************/
func (impl *GoodsReceiptDetailServiceImplement) Delete(user *models.Usertab, entities *[]model.GoodsReceiptDetail) error {

	grmi.Log(user, "/services/wm/implments/GoodsReceiptDetail.service.impl.go", "DeleteGoodsReceiptDetail", "删除多个GoodsReceiptDetail")

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

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

	grmi.Log(user, "/services/wm/implments/GoodsReceiptDetail.service.impl.go", "SelectGoodsReceiptDetail", "查询GoodsReceiptDetail")

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

	grmi.Log(user, "/services/wm/implments/GoodsReceiptDetail.service.impl.go", "UpdateGoodsReceiptDetail", "修改多个GoodsReceiptDetail")

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

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

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