// 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"
)

/******************************************************************************
 *
 * @Description              : GoodsReceiptHead的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfGoodsReceiptHead = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.GoodsReceiptHead_GoodsReceiptId.UrlParameterName: grmi.NewConditionItem(meta.GoodsReceiptHead_GoodsReceiptId, grmi.Equal, true),
		meta.GoodsReceiptHead_RefOrderId.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptHead_RefOrderId, grmi.Equal, false),
		meta.GoodsReceiptHead_GRWarehouseId.UrlParameterName:  grmi.NewConditionItem(meta.GoodsReceiptHead_GRWarehouseId, grmi.Equal, false),
		meta.GoodsReceiptHead_Remark.UrlParameterName:         grmi.NewConditionItem(meta.GoodsReceiptHead_Remark, grmi.Equal, false),
		meta.GoodsReceiptHead_DemandDate.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptHead_DemandDate, grmi.Approximate, false),
		meta.GoodsReceiptHead_RegisterDate.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptHead_RegisterDate, grmi.Approximate, false),
		meta.GoodsReceiptHead_Status.UrlParameterName:         grmi.NewConditionItem(meta.GoodsReceiptHead_Status, grmi.Equal, false),
		meta.GoodsReceiptHead_SupplierId.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptHead_SupplierId, grmi.Equal, false),
		meta.GoodsReceiptHead_SupplierName.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptHead_SupplierName, grmi.Equal, false),
		meta.GoodsReceiptHead_GRType.UrlParameterName:         grmi.NewConditionItem(meta.GoodsReceiptHead_GRType, grmi.Equal, false),
		meta.GoodsReceiptHead_GRType1.UrlParameterName:        grmi.NewConditionItem(meta.GoodsReceiptHead_GRType1, grmi.Equal, false),
		meta.GoodsReceiptHead_GRType2.UrlParameterName:        grmi.NewConditionItem(meta.GoodsReceiptHead_GRType2, grmi.Equal, false),
		meta.GoodsReceiptHead_GRReason.UrlParameterName:       grmi.NewConditionItem(meta.GoodsReceiptHead_GRReason, grmi.Equal, false),
		meta.GoodsReceiptHead_OrderedQty.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptHead_OrderedQty, grmi.Equal, false),
		meta.GoodsReceiptHead_DeliveredQty.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptHead_DeliveredQty, grmi.Equal, false),
		meta.GoodsReceiptHead_Applicant.UrlParameterName:      grmi.NewConditionItem(meta.GoodsReceiptHead_Applicant, grmi.Equal, false),
		meta.GoodsReceiptHead_Department.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptHead_Department, grmi.Equal, false),
		meta.GoodsReceiptHead_ApplyTime.UrlParameterName:      grmi.NewConditionItem(meta.GoodsReceiptHead_ApplyTime, grmi.Approximate, false),
		meta.GoodsReceiptHead_GRCtrlPara1.UrlParameterName:    grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara1, grmi.Equal, false),
		meta.GoodsReceiptHead_GRCtrlPara2.UrlParameterName:    grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara2, grmi.Equal, false),
		meta.GoodsReceiptHead_GRCtrlPara3.UrlParameterName:    grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara3, grmi.Equal, false),
		meta.GoodsReceiptHead_GRCtrlPara4.UrlParameterName:    grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara4, grmi.Equal, false),
		meta.GoodsReceiptHead_GRCtrlPara5.UrlParameterName:    grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara5, grmi.Equal, false),
		meta.GoodsReceiptHead_GRCtrlPara6.UrlParameterName:    grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara6, grmi.Equal, false),
		meta.GoodsReceiptHead_GRCtrlPara7.UrlParameterName:    grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara7, grmi.Equal, false),
		meta.GoodsReceiptHead_GRCtrlPara8.UrlParameterName:    grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara8, grmi.Equal, false),
		meta.GoodsReceiptHead_GRCtrlPara9.UrlParameterName:    grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara9, grmi.Equal, false),
		meta.GoodsReceiptHead_GRCtrlPara10.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara10, grmi.Equal, false),
		meta.GoodsReceiptHead_GRCtrlPara11.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara11, grmi.Equal, false),
		meta.GoodsReceiptHead_GRCtrlPara12.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara12, grmi.Equal, false),
		meta.GoodsReceiptHead_GRCtrlPara13.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara13, grmi.Approximate, false),
		meta.GoodsReceiptHead_GRCtrlPara14.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara14, grmi.Approximate, false),
		meta.GoodsReceiptHead_GRCtrlPara15.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara15, grmi.Approximate, false),
		meta.GoodsReceiptHead_GRCtrlPara16.UrlParameterName:   grmi.NewConditionItem(meta.GoodsReceiptHead_GRCtrlPara16, grmi.Approximate, false),
		meta.GoodsReceiptHead_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptHead_LastModify, grmi.Approximate, false),
		meta.GoodsReceiptHead_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.GoodsReceiptHead_LastUser, grmi.Equal, false),
		meta.GoodsReceiptHead_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.GoodsReceiptHead_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsReceiptHeadService.InsertOne
 *
 ******************************************************************************/
func (impl *GoodsReceiptHeadServiceImplement) InsertOne(user *models.Usertab, entity *model.GoodsReceiptHead) error {

	grmi.Log(user, "/services/wm/implments/GoodsReceiptHead.service.impl.go", "InsertOneGoodsReceiptHead", "插入一个GoodsReceiptHead")
	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)
	bookDao := dal.NewInventoryBookDAO(session, user.Userid)
	binDao := dal.NewStorageBinDAO(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
	GoodsReceiptId, err := snr.GetNextSnrWithSession(session, "GoodsReceipt")
	if err != nil {
		session.Rollback()
		fmt.Println(err)
		return err
	}

	entity.GoodsReceiptId = GoodsReceiptId
	for k, _ := range entity.GoodsReceiptDetail {
		entity.GoodsReceiptDetail[k].GoodsReceiptId = GoodsReceiptId
		entity.GoodsReceiptDetail[k].GRPos = k + 1
		for k2, _ := range entity.GoodsReceiptDetail[k].GoodsReceiptLst {
			entity.GoodsReceiptDetail[k].GoodsReceiptLst[k2].GoodsReceiptId = GoodsReceiptId
			entity.GoodsReceiptDetail[k].GoodsReceiptLst[k2].GRPos = entity.GoodsReceiptDetail[k].GRPos
			entity.GoodsReceiptDetail[k].GoodsReceiptLst[k2].GRLPos = k2 + 1
		}
	}

	dao := dal.NewGoodsReceiptHeadDAO(session, user.Userid)
	//if entity.GoodsReceiptId == ""{
	//	session.Rollback()
	//	return grmi.NewBusinessError("GoodsReceiptId为空")
	//}
	//record, err := dao.SelectOne(entity.GoodsReceiptId)
	//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.NewGoodsReceiptDetailDAO(session, user.Userid)
	//for _, entity := range entity.GoodsReceiptDetail {
	//	record, err := dao2.SelectOne(entity.GoodsReceiptId, entity.GRPos)
	//	if err != nil {
	//		session.Rollback()
	//		return err
	//	}
	//	if record != nil {
	//		session.Rollback()
	//		return grmi.NewBusinessError("已经存在相同主键的记录!")
	//	}
	//}
	dao3 := dal.NewGoodsReceiptLstDAO(session, user.Userid)
	for k, detail := range entity.GoodsReceiptDetail {
		_,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.GoodsReceiptDetail[k].BeforeQty = articleCountMap[detail.ArtId]
		articleCountMap[detail.ArtId] += detail.OrderedQty
		entity.GoodsReceiptDetail[k].CurQty = articleCountMap[detail.ArtId]

		for _, entity := range detail.GoodsReceiptLst {
			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.GoodsReceiptId, 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 {
				inventoryBook := model.InventoryBook{
					StorageBinId:  entity.StorageBinId,
					WarehouseId:   entity.WarehouseId,
					ArtId:         detail.ArtId,
					StorageAreaId: entity.StorageAreaId,
					StorageRackId: entity.StorageRackId,
					ArtName:       detail.ArtName,
					ArtType:       detail.ArtType,
					QtyUom:        detail.QtyUom,
					Qty:           detail.OrderedQty,
				}
				err = bookDao.InsertOne(&inventoryBook)
				if err != nil {
					_ = session.Rollback()
					return err
				}
			} else {
				book.Qty += detail.OrderedQty
				err = bookDao.UpdateOne(book)
				if err != nil {
					_ = session.Rollback()
					return err
				}
			}
		}
		err = dao3.Insert(&detail.GoodsReceiptLst)
		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.GoodsReceiptDetail)
	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
		err = articleDao.UpdateOne(article)
		if err != nil {
			_ = session.Rollback()
			return err
		}
	}

	session.Commit()

	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsReceiptHeadService.DeleteOne
 *
 ******************************************************************************/
func (impl *GoodsReceiptHeadServiceImplement) DeleteOne(user *models.Usertab, goodsReceiptId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsReceiptHeadService.SelectOne
 *
 ******************************************************************************/
func (impl *GoodsReceiptHeadServiceImplement) SelectOne(user *models.Usertab, goodsReceiptId string) (*model.GoodsReceiptHead, error) {

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

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

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

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

	result.GoodsReceiptDetail = result2

	for _, info3 := range result3{
		for index, info2 := range result.GoodsReceiptDetail{
			if info3.GRPos == info2.GRPos{
				result.GoodsReceiptDetail[index].GoodsReceiptLst = append(info2.GoodsReceiptLst, info3)
			}
		}
	}

	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/GoodsReceiptHeadService.UpdateOne
 *
 ******************************************************************************/
func (impl *GoodsReceiptHeadServiceImplement) UpdateOne(user *models.Usertab, entity *model.GoodsReceiptHead) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	condition := DefaultConditionOfGoodsReceiptHeadAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewGoodsReceiptHeadDAO(session, user.Userid)
	//condition.OrderByFields = append(condition.OrderByFields, meta.GoodsReceiptHead_GoodsReceiptId)
	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
}
