// 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/utils"
	"LAPP_LF_MOM_BACKEND/web/models"
	"fmt"
	"time"
)

/******************************************************************************
 *
 * @Description              : GoodsIssueHead的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfGoodsIssueHead = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.GoodsIssueHead_GoodsIssueId.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueHead_GoodsIssueId, grmi.Equal, true),
		meta.GoodsIssueHead_RefOrderId.UrlParameterName:   grmi.NewConditionItem(meta.GoodsIssueHead_RefOrderId, grmi.Equal, false),
		meta.GoodsIssueHead_CustOrderId.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueHead_CustOrderId, grmi.Equal, false),
		meta.GoodsIssueHead_Destination.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueHead_Destination, grmi.Equal, false),
		meta.GoodsIssueHead_Remark.UrlParameterName:       grmi.NewConditionItem(meta.GoodsIssueHead_Remark, grmi.Equal, false),
		meta.GoodsIssueHead_DeliveryDate.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueHead_DeliveryDate, grmi.Approximate, false),
		meta.GoodsIssueHead_Status.UrlParameterName:       grmi.NewConditionItem(meta.GoodsIssueHead_Status, grmi.Equal, false),
		meta.GoodsIssueHead_GIType.UrlParameterName:       grmi.NewConditionItem(meta.GoodsIssueHead_GIType, grmi.Equal, false),
		meta.GoodsIssueHead_GIType1.UrlParameterName:      grmi.NewConditionItem(meta.GoodsIssueHead_GIType1, grmi.Equal, false),
		meta.GoodsIssueHead_GIType2.UrlParameterName:      grmi.NewConditionItem(meta.GoodsIssueHead_GIType2, grmi.Equal, false),
		meta.GoodsIssueHead_GIReason.UrlParameterName:     grmi.NewConditionItem(meta.GoodsIssueHead_GIReason, grmi.Equal, false),
		meta.GoodsIssueHead_Priority.UrlParameterName:     grmi.NewConditionItem(meta.GoodsIssueHead_Priority, grmi.Equal, false),
		meta.GoodsIssueHead_CostCenterId.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueHead_CostCenterId, grmi.Equal, false),
		meta.GoodsIssueHead_BookingKey.UrlParameterName:   grmi.NewConditionItem(meta.GoodsIssueHead_BookingKey, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara1.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara1, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara2, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara3, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara4, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara5.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara5, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara6.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara6, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara7.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara7, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara8.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara8, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara9.UrlParameterName:  grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara9, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara10.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara10, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara11.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara11, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara12.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara12, grmi.Equal, false),
		meta.GoodsIssueHead_GICtrlPara13.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara13, grmi.Approximate, false),
		meta.GoodsIssueHead_GICtrlPara14.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara14, grmi.Approximate, false),
		meta.GoodsIssueHead_GICtrlPara15.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara15, grmi.Approximate, false),
		meta.GoodsIssueHead_GICtrlPara16.UrlParameterName: grmi.NewConditionItem(meta.GoodsIssueHead_GICtrlPara16, grmi.Approximate, false),
		meta.GoodsIssueHead_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.GoodsIssueHead_LastModify, grmi.Approximate, false),
		meta.GoodsIssueHead_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.GoodsIssueHead_LastUser, grmi.Equal, false),
		meta.GoodsIssueHead_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.GoodsIssueHead_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsIssueHeadService.InsertOne
 *
 ******************************************************************************/
func (impl *GoodsIssueHeadServiceImplement) InsertOne(user *models.Usertab, entity *model.GoodsIssueHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	session.Begin()

	articleOriginMap :=  make(map[string]float64)
	articleCountMap := make(map[string]float64)
	articleDao := dal.NewArticleDAO(session, user.Userid)
	articleRepDao := dal.NewArticleReplenishDAO(session, user.Userid)
	bookDao := dal.NewInventoryBookDAO(session, user.Userid)
	binDao := dal.NewStorageBinDAO(session, user.Userid)
	repDao := dal.NewReplenishOrderDAO(session, user.Userid)
	daoWorkAreaUserLst := dal.NewWorkAreaUserLstDAO(session, user.Userid)
	daoWorkArea := dal.NewWorkAreaDAO(session, user.Userid)

	var workAreaCheck model.WorkAreaCheck
	checkLi, err := binDao.Select([]grmi.Predicate{meta.StorageBin_Status.NewPredicate(grmi.Equal, model.BIN_STAUTS_CHECK)}, nil)
	if err != nil {
		session.Rollback()
		fmt.Println(err)
		return err
	}
	if len(checkLi) > 0 {
		session.Rollback()
		return grmi.NewBusinessError("目前存在未完成的盘点单")
	}

	snr := new(models.Snrtab)
	snr.Finr = 100
	//生成ID
	GoodsIssueId, err := snr.GetNextSnrWithSession(session, "GoodsIssue")
	if err != nil {
		session.Rollback()
		fmt.Println(err)
		return err
	}

	entity.GoodsIssueId = GoodsIssueId
	for k, _ := range entity.GoodsIssueDetail{
		entity.GoodsIssueDetail[k].GoodsIssueId = GoodsIssueId
		entity.GoodsIssueDetail[k].GIPos = k + 1
		for k2, _ := range entity.GoodsIssueDetail[k].GoodsIssueLst{
			entity.GoodsIssueDetail[k].GoodsIssueLst[k2].GoodsIssueId = GoodsIssueId
			entity.GoodsIssueDetail[k].GoodsIssueLst[k2].GIPos = entity.GoodsIssueDetail[k].GIPos
			entity.GoodsIssueDetail[k].GoodsIssueLst[k2].GILPos = k2 + 1
		}
	}

	dao := dal.NewGoodsIssueHeadDAO(session, user.Userid)
	//if entity.GoodsIssueId == ""{
	//	session.Rollback()
	//	return grmi.NewBusinessError("GoodsIssueId为空")
	//}
	//record, err := dao.SelectOne(entity.GoodsIssueId)
	//if err != nil {
	//	session.Rollback()
	//	fmt.Println(err)
	//	return err
	//}
	//if record != nil {
	//	return grmi.NewBusinessError("已经存在相同主键的记录!")
	//}
	err = dao.InsertOne(entity)
	if err != nil {
		session.Rollback()
		fmt.Println(err)
		return err
	}

	dao2 := dal.NewGoodsIssueDetailDAO(session, user.Userid)
	//for _, entity := range entity.GoodsIssueDetail {
	//	record, err := dao2.SelectOne(entity.GoodsIssueId, entity.GRPos)
	//	if err != nil {
	//		session.Rollback()
	//		return err
	//	}
	//	if record != nil {
	//		session.Rollback()
	//		return grmi.NewBusinessError("已经存在相同主键的记录!")
	//	}
	//}

	dao3 := dal.NewGoodsIssueLstDAO(session, user.Userid)
	for k, detail := range entity.GoodsIssueDetail {
		_,isIn := articleOriginMap[detail.ArtId]
		if !isIn{
			article, err := articleDao.SelectOne(detail.ArtId)
			if err != nil {
				_ = session.Rollback()
				return err
			}
			articleOriginMap[detail.ArtId] = article.Qty
			articleCountMap[detail.ArtId] = article.Qty
		}
		//变动数量
		entity.GoodsIssueDetail[k].BeforeQty = articleCountMap[detail.ArtId]
		articleCountMap[detail.ArtId] -= detail.OrderQty
		entity.GoodsIssueDetail[k].CurQty = articleCountMap[detail.ArtId]
		if entity.GoodsIssueDetail[k].CurQty < 0{
					session.Rollback()
					return grmi.NewBusinessError("GoodsIssueDetail数量小于0!")
		}

		for _, entity := range detail.GoodsIssueLst {
			workAreaCheck.StorageBinIdList = utils.AddListOnce(workAreaCheck.StorageBinIdList, entity.StorageBinId)
			workAreaCheck.WarehouseIdList = utils.AddListOnce(workAreaCheck.WarehouseIdList, entity.WarehouseId)
			workAreaCheck.StorageAreaIdList = utils.AddListOnce(workAreaCheck.StorageAreaIdList, entity.StorageAreaId)
			workAreaCheck.StorageRackIdList = utils.AddListOnce(workAreaCheck.StorageRackIdList, entity.StorageRackId)
			//record, err := dao3.SelectOne(entity.GoodsIssueId, entity.GRPos, entity.GRLPos)
			//if err != nil {
			//	session.Rollback()
			//	return err
			//}
			//if record != nil {
			//	session.Rollback()
			//	return grmi.NewBusinessError("已经存在相同主键的记录!")
			//}

			// 库存表
			book, err := bookDao.SelectOne(entity.StorageBinId, entity.WarehouseId, detail.ArtId)
			if err != nil {
				_ = session.Rollback()
				return err
			}
			if book == nil || book.Qty-detail.OrderQty < 0{
				_ = session.Rollback()
				return grmi.NewBusinessError(entity.WarehouseId + entity.StorageBinId + "物料"+detail.ArtId + "数量不足!")
			}else{
				book.Qty -= detail.OrderQty
				if book.Qty == 0{
					err = bookDao.DeleteOne(entity.StorageBinId, entity.WarehouseId, detail.ArtId)
					if err != nil {
						_ = session.Rollback()
						return err
					}
				}
				err = bookDao.UpdateOne(book)
				if err != nil {
					_ = session.Rollback()
					return err
				}
			}
		}
		err = dao3.Insert(&detail.GoodsIssueLst)
		if err != nil {
			session.Rollback()
			fmt.Println(err)
			return err
		}
	}

	// 权限
	if model.WM_AUTH_CHECK == model.WM_AUTH_CHECK_ON {
		selectWorkAreaUserLst, err := daoWorkAreaUserLst.Select([]grmi.Predicate{meta.WorkAreaUserLst_UserId.NewPredicate(grmi.Equal, user.Userid)}, nil)
		if err != nil {
			session.Rollback()
			return err
		}
		if len(selectWorkAreaUserLst) == 0 {
			session.Rollback()
			return nil
		}

		var workAreaList []*model.WorkArea
		for _, info := range selectWorkAreaUserLst {
			selectWorkArea, err := daoWorkArea.SelectOne(info.WorkAreaId)
			if err != nil || selectWorkArea == nil {
				continue
			}
			workAreaList = append(workAreaList, selectWorkArea)
		}

		err = model.WM_Check(workAreaCheck, workAreaList)
		if err != nil {
			session.Rollback()
			fmt.Println(err)
			return err
		}
	}

	err = dao2.Insert(&entity.GoodsIssueDetail)
	if err != nil {
		session.Rollback()
		fmt.Println(err)
		return err
	}

	//更新物料表数量
	for id, count := range articleCountMap{
		article, err := articleDao.SelectOne(id)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		if article.Qty != articleOriginMap[id]{
			session.Rollback()
			return grmi.NewBusinessError("article数量不一致!")
		}
		article.Qty = count
		if article.Qty  < 0{
			session.Rollback()
			return grmi.NewBusinessError("article数量小于0!")
		}
		err = articleDao.UpdateOne(article)
		if err != nil {
			_ = session.Rollback()
			return err
		}

		articleRep, err := articleRepDao.SelectOne(id)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		if articleRep != nil && article.Qty < articleRep.MinQty{
			//补货
			check, err := repDao.Select([]grmi.Predicate{meta.ReplenishOrder_ArtId.NewPredicate(grmi.Equal, id),meta.ReplenishOrder_Status.NewPredicate(grmi.Equal, model.REPLENISH_ORDER_STATUS_ON)}, ReplenishOrderArtStatus.OrderByFields)
			if err != nil {
				_ = session.Rollback()
				return err
			}
			if len(check) > 0 {
				fmt.Println("正在补货中")
			}else{
				snr := new(models.Snrtab)
				snr.Finr = 100
				//生成ID
				Replenish, err := snr.GetNextSnr("Replenish")
				if err != nil {
					session.Rollback()
					fmt.Println(err)
					return err
				}
				replenishOrder := model.ReplenishOrder{
					ReplenishId:  Replenish,
					ArtId:         article.ArtId,
					Qty:           articleRep.ReplenishQty,
					ReplenishTime: grmi.DateTime((time.Now())),
					ArtName:       article.Descr1,
					ArtType:       article.ArtType,
					Status:    model.REPLENISH_ORDER_STATUS_ON,
				}
				err = repDao.InsertOne(&replenishOrder)
				if err != nil {
					_ = session.Rollback()
					return err
				}
			}
		}
	}

	session.Commit()

	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsIssueHeadService.DeleteOne
 *
 ******************************************************************************/
func (impl *GoodsIssueHeadServiceImplement) DeleteOne(user *models.Usertab, goodsIssueId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsIssueHeadService.SelectOne
 *
 ******************************************************************************/
func (impl *GoodsIssueHeadServiceImplement) SelectOne(user *models.Usertab, goodsIssueId string) (*model.GoodsIssueHead, error) {

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

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

	dao2 := dal.NewGoodsIssueDetailDAO(session, user.Userid)
	result2, err := dao2.Select([]grmi.Predicate{meta.GoodsIssueDetail_GoodsIssueId.NewPredicate(grmi.Equal,goodsIssueId)}, nil)
	if err != nil {
		return nil, err
	}

	dao3 := dal.NewGoodsIssueLstDAO(session, user.Userid)
	result3, err := dao3.Select([]grmi.Predicate{meta.GoodsIssueLst_GoodsIssueId.NewPredicate(grmi.Equal,goodsIssueId)}, nil)
	if err != nil {
		return nil, err
	}

	result.GoodsIssueDetail = result2

	for _, info3 := range result3{
		for index, info2 := range result.GoodsIssueDetail{
			if info3.GIPos == info2.GIPos{
				result.GoodsIssueDetail[index].GoodsIssueLst = append(info2.GoodsIssueLst, info3)
			}
		}
	}

	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsIssueHeadService.UpdateOne
 *
 ******************************************************************************/
func (impl *GoodsIssueHeadServiceImplement) UpdateOne(user *models.Usertab, entity *model.GoodsIssueHead) error {

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

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

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.GoodsIssueId)
		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/wm/GoodsIssueHeadService.Delete
 *
 ******************************************************************************/
func (impl *GoodsIssueHeadServiceImplement) Delete(user *models.Usertab, entities *[]model.GoodsIssueHead) error {

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

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

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

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

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

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

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

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

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

func (impl *GoodsIssueHeadServiceImplement) ExportExcel(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/wm/implments/GoodsIssueHead.service.impl.go", "ExportExcel", "导出excel表")

	condition := DefaultConditionOfGoodsIssueHeadAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewGoodsIssueHeadDAO(session, user.Userid)
	//condition.OrderByFields = append(condition.OrderByFields, meta.GoodsIssueHead_GoodsIssueId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	temp := make([]interface{}, len(result))
	for index, item := range result {
		temp[index] = item
	}
	titleList := []string{}

	filepath, err := grmi.SaveExcelFile(temp, titleList, "出库", "出库")
	if err != nil {
		return nil, grmi.NewBusinessError(fmt.Sprintf("导出报表失败，请重试, errors: %s", err.Error()))
	}
	return filepath, nil
}
