// 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              : InventoryCheckLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 10:08:33
 *
 ******************************************************************************/
var DefaultConditionOfInventoryCheckLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.InventoryCheckLst_InventoryCheckId.UrlParameterName: grmi.NewConditionItem(meta.InventoryCheckLst_InventoryCheckId, grmi.Equal, true),
		meta.InventoryCheckLst_ICDPos.UrlParameterName:           grmi.NewConditionItem(meta.InventoryCheckLst_ICDPos, grmi.Equal, true),
		meta.InventoryCheckLst_ICLPos.UrlParameterName:           grmi.NewConditionItem(meta.InventoryCheckLst_ICLPos, grmi.Equal, true),
		meta.InventoryCheckLst_StorageBinId.UrlParameterName:     grmi.NewConditionItem(meta.InventoryCheckLst_StorageBinId, grmi.Equal, false),
		meta.InventoryCheckLst_WarehouseId.UrlParameterName:      grmi.NewConditionItem(meta.InventoryCheckLst_WarehouseId, grmi.Equal, false),
		meta.InventoryCheckLst_ArtId.UrlParameterName:            grmi.NewConditionItem(meta.InventoryCheckLst_ArtId, grmi.Equal, false),
		meta.InventoryCheckLst_ArtName.UrlParameterName:          grmi.NewConditionItem(meta.InventoryCheckLst_ArtName, grmi.Equal, false),
		meta.InventoryCheckLst_ArtType.UrlParameterName:          grmi.NewConditionItem(meta.InventoryCheckLst_ArtType, grmi.Equal, false),
		meta.InventoryCheckLst_OriginQty.UrlParameterName:        grmi.NewConditionItem(meta.InventoryCheckLst_OriginQty, grmi.Equal, false),
		meta.InventoryCheckLst_ActQty.UrlParameterName:           grmi.NewConditionItem(meta.InventoryCheckLst_ActQty, grmi.Equal, false),
		meta.InventoryCheckLst_QtyUom.UrlParameterName:           grmi.NewConditionItem(meta.InventoryCheckLst_QtyUom, grmi.Equal, false),
		meta.InventoryCheckLst_ICLCtrlPara1.UrlParameterName:     grmi.NewConditionItem(meta.InventoryCheckLst_ICLCtrlPara1, grmi.Equal, false),
		meta.InventoryCheckLst_ICLCtrlPara2.UrlParameterName:     grmi.NewConditionItem(meta.InventoryCheckLst_ICLCtrlPara2, grmi.Equal, false),
		meta.InventoryCheckLst_ICLCtrlPara3.UrlParameterName:     grmi.NewConditionItem(meta.InventoryCheckLst_ICLCtrlPara3, grmi.Equal, false),
		meta.InventoryCheckLst_ICLCtrlPara4.UrlParameterName:     grmi.NewConditionItem(meta.InventoryCheckLst_ICLCtrlPara4, grmi.Equal, false),
		meta.InventoryCheckLst_ICLCtrlPara5.UrlParameterName:     grmi.NewConditionItem(meta.InventoryCheckLst_ICLCtrlPara5, grmi.Equal, false),
		meta.InventoryCheckLst_ICLCtrlPara6.UrlParameterName:     grmi.NewConditionItem(meta.InventoryCheckLst_ICLCtrlPara6, grmi.Equal, false),
		meta.InventoryCheckLst_CheckResult.UrlParameterName:      grmi.NewConditionItem(meta.InventoryCheckLst_CheckResult, grmi.Equal, false),
		meta.InventoryCheckLst_ResultExplain.UrlParameterName:    grmi.NewConditionItem(meta.InventoryCheckLst_ResultExplain, grmi.Equal, false),
		meta.InventoryCheckLst_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.InventoryCheckLst_LastModify, grmi.Approximate, false),
		meta.InventoryCheckLst_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.InventoryCheckLst_LastUser, grmi.Equal, false),
		meta.InventoryCheckLst_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.InventoryCheckLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/InventoryCheckLstService.InsertOne
 *
 ******************************************************************************/
func (impl *InventoryCheckLstServiceImplement) InsertOne(user *models.Usertab, entity *model.InventoryCheckLst) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/InventoryCheckLstService.SelectOne
 *
 ******************************************************************************/
func (impl *InventoryCheckLstServiceImplement) SelectOne(user *models.Usertab, inventoryCheckId string, iCDPos int, iCLPos int) (*model.InventoryCheckLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/InventoryCheckLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *InventoryCheckLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.InventoryCheckLst) error {

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

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

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.InventoryCheckId, entity.ICDPos, entity.ICLPos)
		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/InventoryCheckLstService.Delete
 *
 ******************************************************************************/
func (impl *InventoryCheckLstServiceImplement) Delete(user *models.Usertab, entities *[]model.InventoryCheckLst) error {

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

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

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

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

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

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

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

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

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