// 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              : InventoryCheckDetail的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
var DefaultConditionOfInventoryCheckDetail = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.InventoryCheckDetail_InventoryCheckId.UrlParameterName: grmi.NewConditionItem(meta.InventoryCheckDetail_InventoryCheckId, grmi.Equal, true),
		meta.InventoryCheckDetail_ICDPos.UrlParameterName:           grmi.NewConditionItem(meta.InventoryCheckDetail_ICDPos, grmi.Equal, true),
		meta.InventoryCheckDetail_Descr.UrlParameterName:            grmi.NewConditionItem(meta.InventoryCheckDetail_Descr, grmi.Equal, false),
		meta.InventoryCheckDetail_StorageBinId.UrlParameterName:     grmi.NewConditionItem(meta.InventoryCheckDetail_StorageBinId, grmi.Equal, false),
		meta.InventoryCheckDetail_WarehouseId.UrlParameterName:      grmi.NewConditionItem(meta.InventoryCheckDetail_WarehouseId, grmi.Equal, false),
		meta.InventoryCheckDetail_ICCtrlPara1.UrlParameterName:      grmi.NewConditionItem(meta.InventoryCheckDetail_ICCtrlPara1, grmi.Equal, false),
		meta.InventoryCheckDetail_ICCtrlPara2.UrlParameterName:      grmi.NewConditionItem(meta.InventoryCheckDetail_ICCtrlPara2, grmi.Equal, false),
		meta.InventoryCheckDetail_ICCtrlPara3.UrlParameterName:      grmi.NewConditionItem(meta.InventoryCheckDetail_ICCtrlPara3, grmi.Equal, false),
		meta.InventoryCheckDetail_ICCtrlPara4.UrlParameterName:      grmi.NewConditionItem(meta.InventoryCheckDetail_ICCtrlPara4, grmi.Equal, false),
		meta.InventoryCheckDetail_ICCtrlPara5.UrlParameterName:      grmi.NewConditionItem(meta.InventoryCheckDetail_ICCtrlPara5, grmi.Equal, false),
		meta.InventoryCheckDetail_ICCtrlPara6.UrlParameterName:      grmi.NewConditionItem(meta.InventoryCheckDetail_ICCtrlPara6, grmi.Equal, false),
		meta.InventoryCheckDetail_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.InventoryCheckDetail_LastModify, grmi.Approximate, false),
		meta.InventoryCheckDetail_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.InventoryCheckDetail_LastUser, grmi.Equal, false),
		meta.InventoryCheckDetail_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.InventoryCheckDetail_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : InventoryCheckDetail的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
var DefaultConditionOfInventoryCheckDetailAndPaging = grmi.NewCondition(
	DefaultConditionOfInventoryCheckDetail.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : InventoryCheckDetailServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : InventoryCheckDetail的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
type InventoryCheckDetailServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewInventoryCheckDetailDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个InventoryCheckDetailDAOImplement实例
 *
 * @Return Value             : InventoryCheckDetailDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
func NewInventoryCheckDetailServiceImplement() *InventoryCheckDetailServiceImplement {
	return &InventoryCheckDetailServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/InventoryCheckDetailService.InsertOne
 *
 ******************************************************************************/
func (impl *InventoryCheckDetailServiceImplement) InsertOne(user *models.Usertab, entity *model.InventoryCheckDetail) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewInventoryCheckDetailDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.InventoryCheckId, entity.ICDPos)
	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/InventoryCheckDetailService.DeleteOne
 *
 ******************************************************************************/
func (impl *InventoryCheckDetailServiceImplement) DeleteOne(user *models.Usertab, inventoryCheckId string, iCDPos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/InventoryCheckDetailService.SelectOne
 *
 ******************************************************************************/
func (impl *InventoryCheckDetailServiceImplement) SelectOne(user *models.Usertab, inventoryCheckId string, iCDPos int) (*model.InventoryCheckDetail, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/InventoryCheckDetailService.UpdateOne
 *
 ******************************************************************************/
func (impl *InventoryCheckDetailServiceImplement) UpdateOne(user *models.Usertab, entity *model.InventoryCheckDetail) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewInventoryCheckDetailDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.InventoryCheckId, entity.ICDPos)
	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/InventoryCheckDetailService.Insert
 *
 ******************************************************************************/
func (impl *InventoryCheckDetailServiceImplement) Insert(user *models.Usertab, entities *[]model.InventoryCheckDetail) error {

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

	//engine := db.Eloquent.Master()
	//session := engine.NewSession()
	//defer session.Close()
	//if err := session.Begin(); err != nil {
	//	return err
	//}
	//dao := dal.NewInventoryCheckDetailDAO(session, user.Userid)
	//artDao := dal.NewArticleDAO(session, user.Userid)
	////binDao := dal.NewStorageBinDAO(session, user.Userid)
	//lstLi := *entities
	//if len(lstLi) == 0 {
	//	return grmi.NewBusinessError("插入数据为空")
	//}
	//headId := lstLi[0].InventoryCheckId
	//err := dao.DeleteWhere([]grmi.Predicate{meta.InventoryCheckDetail_InventoryCheckId.NewPredicate(grmi.Equal, headId)})
	//if err != nil {
	//	_ = session.Rollback()
	//	return err
	//}
	//var pos int
	//for index, detail := range lstLi {
	//	detail.Pos = pos + 1
	//	pos++
	//	if detail.InventoryCheckId != headId {
	//		_ = session.Rollback()
	//		return grmi.NewBusinessError("InventoryCheckId不一致")
	//	}
	//	article, err := artDao.SelectOne(detail.ArtId)
	//	if err != nil {
	//		_ = session.Rollback()
	//		return err
	//	}
	//	if article == nil {
	//		_ = session.Rollback()
	//		return grmi.NewBusinessError("备件主数据不存在")
	//	}
	//	//binLocation, err := binDao.SelectOne(detail.WarehouseId, detail.StorageBinId)
	//	//if err != nil {
	//	//	_ = session.Rollback()
	//	//	return err
	//	//}
	//	//if binLocation == nil {
	//	//	_ = session.Rollback()
	//	//	return grmi.NewBusinessError("不存在该库位")
	//	//}
	//	lstLi[index] = detail
	//}
	//err = dao.Insert(entities)
	//if err != nil {
	//	_ = session.Rollback()
	//	return err
	//}
	//_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/InventoryCheckDetailService.Delete
 *
 ******************************************************************************/
func (impl *InventoryCheckDetailServiceImplement) Delete(user *models.Usertab, entities *[]model.InventoryCheckDetail) error {

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

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

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

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

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

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

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

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

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