// 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              : ReplenishOrder的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 16:17:08
 *
 ******************************************************************************/
var DefaultConditionOfReplenishOrder = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ReplenishOrder_ReplenishId.UrlParameterName:   grmi.NewConditionItem(meta.ReplenishOrder_ReplenishId, grmi.Equal, true),
		meta.ReplenishOrder_ArtId.UrlParameterName:         grmi.NewConditionItem(meta.ReplenishOrder_ArtId, grmi.Equal, false),
		meta.ReplenishOrder_Qty.UrlParameterName:           grmi.NewConditionItem(meta.ReplenishOrder_Qty, grmi.Equal, false),
		meta.ReplenishOrder_Uom.UrlParameterName:           grmi.NewConditionItem(meta.ReplenishOrder_Uom, grmi.Equal, false),
		meta.ReplenishOrder_ReplenishTime.UrlParameterName: grmi.NewConditionItem(meta.ReplenishOrder_ReplenishTime, grmi.Approximate, false),
		meta.ReplenishOrder_ArtType.UrlParameterName:       grmi.NewConditionItem(meta.ReplenishOrder_ArtType, grmi.Equal, false),
		meta.ReplenishOrder_ArtName.UrlParameterName:       grmi.NewConditionItem(meta.ReplenishOrder_ArtName, grmi.Equal, false),
		meta.ReplenishOrder_Status.UrlParameterName:        grmi.NewConditionItem(meta.ReplenishOrder_Status, grmi.Equal, false),
		meta.ReplenishOrder_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.ReplenishOrder_LastModify, grmi.Approximate, false),
		meta.ReplenishOrder_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.ReplenishOrder_LastUser, grmi.Equal, false),
		meta.ReplenishOrder_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.ReplenishOrder_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ReplenishOrder的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 16:17:08
 *
 ******************************************************************************/
var DefaultConditionOfReplenishOrderAndPaging = grmi.NewCondition(
	DefaultConditionOfReplenishOrder.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ReplenishOrderServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ReplenishOrder的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 16:17:08
 *
 ******************************************************************************/
type ReplenishOrderServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewReplenishOrderDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ReplenishOrderDAOImplement实例
 *
 * @Return Value             : ReplenishOrderDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-08 16:17:08
 *
 ******************************************************************************/
func NewReplenishOrderServiceImplement() *ReplenishOrderServiceImplement {
	return &ReplenishOrderServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/ReplenishOrderService.InsertOne
 *
 ******************************************************************************/
func (impl *ReplenishOrderServiceImplement) InsertOne(user *models.Usertab, entity *model.ReplenishOrder) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReplenishOrderDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.ReplenishId)
	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/ReplenishOrderService.DeleteOne
 *
 ******************************************************************************/
func (impl *ReplenishOrderServiceImplement) DeleteOne(user *models.Usertab, replenishId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/ReplenishOrderService.SelectOne
 *
 ******************************************************************************/
func (impl *ReplenishOrderServiceImplement) SelectOne(user *models.Usertab, replenishId string) (*model.ReplenishOrder, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/ReplenishOrderService.UpdateOne
 *
 ******************************************************************************/
func (impl *ReplenishOrderServiceImplement) UpdateOne(user *models.Usertab, entity *model.ReplenishOrder) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

var ReplenishOrderArtStatus = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ReplenishOrder_ArtId.UrlParameterName:         grmi.NewConditionItem(meta.ReplenishOrder_ArtId, grmi.Equal, false),
		meta.ReplenishOrder_Status.UrlParameterName:        grmi.NewConditionItem(meta.ReplenishOrder_Status, grmi.Equal, false),
	},
	nil,
)

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

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

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