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

/******************************************************************************
 *
 * @Description              : InventoryBook的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfInventoryBook = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.InventoryBook_StorageBinId.UrlParameterName:    grmi.NewConditionItem(meta.InventoryBook_StorageBinId, grmi.Equal, true),
		meta.InventoryBook_WarehouseId.UrlParameterName:     grmi.NewConditionItem(meta.InventoryBook_WarehouseId, grmi.Equal, true),
		meta.InventoryBook_ArtId.UrlParameterName:           grmi.NewConditionItem(meta.InventoryBook_ArtId, grmi.Equal, true),
		meta.InventoryBook_StorageAreaId.UrlParameterName:   grmi.NewConditionItem(meta.InventoryBook_StorageAreaId, grmi.Equal, false),
		meta.InventoryBook_SKU.UrlParameterName:             grmi.NewConditionItem(meta.InventoryBook_SKU, grmi.Equal, false),
		meta.InventoryBook_ArtName.UrlParameterName:         grmi.NewConditionItem(meta.InventoryBook_ArtName, grmi.Equal, false),
		meta.InventoryBook_ArtType.UrlParameterName:         grmi.NewConditionItem(meta.InventoryBook_ArtType, grmi.Equal, false),
		meta.InventoryBook_QtyUom.UrlParameterName:          grmi.NewConditionItem(meta.InventoryBook_QtyUom, grmi.Equal, false),
		meta.InventoryBook_Qty.UrlParameterName:             grmi.NewConditionItem(meta.InventoryBook_Qty, grmi.Equal, false),
		meta.InventoryBook_IBCtrlPara1.UrlParameterName:     grmi.NewConditionItem(meta.InventoryBook_IBCtrlPara1, grmi.Equal, false),
		meta.InventoryBook_IBCtrlPara2.UrlParameterName:     grmi.NewConditionItem(meta.InventoryBook_IBCtrlPara2, grmi.Equal, false),
		meta.InventoryBook_IBCtrlPara3.UrlParameterName:     grmi.NewConditionItem(meta.InventoryBook_IBCtrlPara3, grmi.Equal, false),
		meta.InventoryBook_IBCtrlPara4.UrlParameterName:     grmi.NewConditionItem(meta.InventoryBook_IBCtrlPara4, grmi.Equal, false),
		meta.InventoryBook_IBCtrlPara5.UrlParameterName:     grmi.NewConditionItem(meta.InventoryBook_IBCtrlPara5, grmi.Equal, false),
		meta.InventoryBook_IBCtrlPara6.UrlParameterName:     grmi.NewConditionItem(meta.InventoryBook_IBCtrlPara6, grmi.Equal, false),
		meta.InventoryBook_LastCheckDate.UrlParameterName:   grmi.NewConditionItem(meta.InventoryBook_LastCheckDate, grmi.Approximate, false),
		meta.InventoryBook_LastCheckPerson.UrlParameterName: grmi.NewConditionItem(meta.InventoryBook_LastCheckPerson, grmi.Equal, false),
		meta.InventoryBook_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.InventoryBook_LastModify, grmi.Approximate, false),
		meta.InventoryBook_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.InventoryBook_LastUser, grmi.Equal, false),
		meta.InventoryBook_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.InventoryBook_CreateTime, grmi.Approximate, false),
		meta.InventoryBook_StorageRackId.UrlParameterName:   grmi.NewConditionItem(meta.InventoryBook_StorageRackId, grmi.Equal,false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/InventoryBookService.InsertOne
 *
 ******************************************************************************/
func (impl *InventoryBookServiceImplement) InsertOne(user *models.Usertab, entity *model.InventoryBook) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewInventoryBookDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.StorageBinId, entity.WarehouseId, entity.ArtId)
	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/InventoryBookService.DeleteOne
 *
 ******************************************************************************/
func (impl *InventoryBookServiceImplement) DeleteOne(user *models.Usertab, storageBinId string, warehouseId string, artId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/InventoryBookService.SelectOne
 *
 ******************************************************************************/
func (impl *InventoryBookServiceImplement) SelectOne(user *models.Usertab, storageBinId string, warehouseId string, artId string) (*model.InventoryBook, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/InventoryBookService.UpdateOne
 *
 ******************************************************************************/
func (impl *InventoryBookServiceImplement) UpdateOne(user *models.Usertab, entity *model.InventoryBook) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewInventoryBookDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.StorageBinId, entity.WarehouseId, entity.ArtId)
	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/InventoryBookService.Insert
 *
 ******************************************************************************/
func (impl *InventoryBookServiceImplement) Insert(user *models.Usertab, entities *[]model.InventoryBook) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.StorageBinId, entity.WarehouseId, entity.ArtId)
		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/InventoryBookService.Delete
 *
 ******************************************************************************/
func (impl *InventoryBookServiceImplement) Delete(user *models.Usertab, entities *[]model.InventoryBook) error {

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

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

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

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

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

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

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

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

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

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

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

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