// 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              : GoodsIssueDetail的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfGoodsIssueDetail = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.GoodsIssueDetail_GoodsIssueId.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueDetail_GoodsIssueId, grmi.Equal, true),
		meta.GoodsIssueDetail_GIPos.UrlParameterName:         grmi.NewConditionItem(meta.GoodsIssueDetail_GIPos, grmi.Equal, true),
		meta.GoodsIssueDetail_Remark.UrlParameterName:        grmi.NewConditionItem(meta.GoodsIssueDetail_Remark, grmi.Equal, false),
		meta.GoodsIssueDetail_ArtId.UrlParameterName:         grmi.NewConditionItem(meta.GoodsIssueDetail_ArtId, grmi.Equal, false),
		meta.GoodsIssueDetail_ArtName.UrlParameterName:       grmi.NewConditionItem(meta.GoodsIssueDetail_ArtName, grmi.Equal, false),
		meta.GoodsIssueDetail_ArtType.UrlParameterName:       grmi.NewConditionItem(meta.GoodsIssueDetail_ArtType, grmi.Equal, false),
		meta.GoodsIssueDetail_RequiredQty.UrlParameterName:   grmi.NewConditionItem(meta.GoodsIssueDetail_RequiredQty, grmi.Equal, false),
		meta.GoodsIssueDetail_RequestType.UrlParameterName:   grmi.NewConditionItem(meta.GoodsIssueDetail_RequestType, grmi.Equal, false),
		meta.GoodsIssueDetail_ReservedQty.UrlParameterName:   grmi.NewConditionItem(meta.GoodsIssueDetail_ReservedQty, grmi.Equal, false),
		meta.GoodsIssueDetail_ActualQty.UrlParameterName:     grmi.NewConditionItem(meta.GoodsIssueDetail_ActualQty, grmi.Equal, false),
		meta.GoodsIssueDetail_OrderQty.UrlParameterName:      grmi.NewConditionItem(meta.GoodsIssueDetail_OrderQty, grmi.Equal, false),
		meta.GoodsIssueDetail_DeliveryDate.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueDetail_DeliveryDate, grmi.Approximate, false),
		meta.GoodsIssueDetail_InspectToggle.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueDetail_InspectToggle, grmi.Equal, false),
		meta.GoodsIssueDetail_InspectPlanId.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueDetail_InspectPlanId, grmi.Equal, false),
		meta.GoodsIssueDetail_InspectStatus.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueDetail_InspectStatus, grmi.Equal, false),
		meta.GoodsIssueDetail_Auditor.UrlParameterName:       grmi.NewConditionItem(meta.GoodsIssueDetail_Auditor, grmi.Equal, false),
		meta.GoodsIssueDetail_AuditTime.UrlParameterName:     grmi.NewConditionItem(meta.GoodsIssueDetail_AuditTime, grmi.Approximate, false),
		meta.GoodsIssueDetail_AuditStatus.UrlParameterName:   grmi.NewConditionItem(meta.GoodsIssueDetail_AuditStatus, grmi.Equal, false),
		meta.GoodsIssueDetail_BatchId.UrlParameterName:       grmi.NewConditionItem(meta.GoodsIssueDetail_BatchId, grmi.Equal, false),
		meta.GoodsIssueDetail_GICtrlPara1.UrlParameterName:   grmi.NewConditionItem(meta.GoodsIssueDetail_GICtrlPara1, grmi.Equal, false),
		meta.GoodsIssueDetail_GIICtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara2, grmi.Equal, false),
		meta.GoodsIssueDetail_GIICtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara3, grmi.Equal, false),
		meta.GoodsIssueDetail_GIICtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara4, grmi.Equal, false),
		meta.GoodsIssueDetail_GIICtrlPara5.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara5, grmi.Equal, false),
		meta.GoodsIssueDetail_GIICtrlPara6.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara6, grmi.Equal, false),
		meta.GoodsIssueDetail_GIICtrlPara7.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara7, grmi.Equal, false),
		meta.GoodsIssueDetail_GIICtrlPara8.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara8, grmi.Equal, false),
		meta.GoodsIssueDetail_GIICtrlPara9.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara9, grmi.Equal, false),
		meta.GoodsIssueDetail_GIICtrlPara10.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara10, grmi.Equal, false),
		meta.GoodsIssueDetail_GIICtrlPara11.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara11, grmi.Equal, false),
		meta.GoodsIssueDetail_GIICtrlPara12.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara12, grmi.Equal, false),
		meta.GoodsIssueDetail_GIICtrlPara13.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara13, grmi.Approximate, false),
		meta.GoodsIssueDetail_GIICtrlPara14.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara14, grmi.Approximate, false),
		meta.GoodsIssueDetail_GIICtrlPara15.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara15, grmi.Approximate, false),
		meta.GoodsIssueDetail_GIICtrlPara16.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueDetail_GIICtrlPara16, grmi.Approximate, false),
		meta.GoodsIssueDetail_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.GoodsIssueDetail_LastModify, grmi.Approximate, false),
		meta.GoodsIssueDetail_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.GoodsIssueDetail_LastUser, grmi.Equal, false),
		meta.GoodsIssueDetail_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.GoodsIssueDetail_CreateTime, grmi.Approximate, false),
		meta.GoodsIssueDetail_BeforeQty.UrlParameterName:    grmi.NewConditionItem(meta.GoodsIssueDetail_BeforeQty, grmi.Equal, false),
		meta.GoodsIssueDetail_CurQty.UrlParameterName:	     grmi.NewConditionItem(meta.GoodsIssueDetail_CurQty, grmi.Equal, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsIssueDetailService.InsertOne
 *
 ******************************************************************************/
func (impl *GoodsIssueDetailServiceImplement) InsertOne(user *models.Usertab, entity *model.GoodsIssueDetail) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewGoodsIssueDetailDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.GoodsIssueId, entity.GIPos)
	if err != nil {
		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/GoodsIssueDetailService.DeleteOne
 *
 ******************************************************************************/
func (impl *GoodsIssueDetailServiceImplement) DeleteOne(user *models.Usertab, goodsIssueId string, gIPos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsIssueDetailService.SelectOne
 *
 ******************************************************************************/
func (impl *GoodsIssueDetailServiceImplement) SelectOne(user *models.Usertab, goodsIssueId string, gIPos int) (*model.GoodsIssueDetail, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsIssueDetailService.UpdateOne
 *
 ******************************************************************************/
func (impl *GoodsIssueDetailServiceImplement) UpdateOne(user *models.Usertab, entity *model.GoodsIssueDetail) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewGoodsIssueDetailDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.GoodsIssueId, entity.GIPos)
	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/GoodsIssueDetailService.Insert
 *
 ******************************************************************************/
func (impl *GoodsIssueDetailServiceImplement) Insert(user *models.Usertab, entities *[]model.GoodsIssueDetail) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	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("目前存在未完成的盘点单")
	}
	dao := dal.NewGoodsIssueDetailDAO(session, user.Userid)
	headDao := dal.NewGoodsIssueHeadDAO(session, user.Userid)
	artDao := dal.NewArticleDAO(session, user.Userid)
	//binDao := dal.NewStorageBinDAO(session, user.Userid)
	//bookDao := dal.NewInventoryBookDAO(session, user.Userid)
	pos := 1
	var headId string
	for _, entity := range *entities {
		if headId == "" {
			headId = entity.GoodsIssueId
		}
		if headId != entity.GoodsIssueId {
			return grmi.NewBusinessError("GoodsIssueId不一致")
		}
		entity.GIPos = 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.StorageBinId)
		//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 {
		//	_ = session.Rollback()
		//	return grmi.NewBusinessError("不存在对应备件的台账记录")
		//}
		//if entity.ActualQty > book.Qty {
		//	_ = session.Rollback()
		//	return grmi.NewBusinessError("出库数量大于库存数量")
		//}
		//book.Qty = book.Qty - entity.ActualQty
		//err = bookDao.UpdateOne(book)
		//if err != nil {
		//	_ = session.Rollback()
		//	return err
		//}
	}
	head, err := headDao.SelectOne(headId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if head == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的出货头部数据")
	}
	err = dao.Insert(entities)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsIssueDetailService.Delete
 *
 ******************************************************************************/
func (impl *GoodsIssueDetailServiceImplement) Delete(user *models.Usertab, entities *[]model.GoodsIssueDetail) error {

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

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

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

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

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

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

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

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

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