// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/om"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/om"
	model "LAPP_LF_MOM_BACKEND/models/om"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : ProdOrderQty的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfProdOrderQty = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ProdOrderQty_ProdOrderId.UrlParameterName:  grmi.NewConditionItem(meta.ProdOrderQty_ProdOrderId, grmi.Equal, true),
		meta.ProdOrderQty_ActQty.UrlParameterName:       grmi.NewConditionItem(meta.ProdOrderQty_ActQty, grmi.Equal, false),
		meta.ProdOrderQty_ActGoodQty.UrlParameterName:   grmi.NewConditionItem(meta.ProdOrderQty_ActGoodQty, grmi.Equal, false),
		meta.ProdOrderQty_ActScrapQty.UrlParameterName:  grmi.NewConditionItem(meta.ProdOrderQty_ActScrapQty, grmi.Equal, false),
		meta.ProdOrderQty_ActReworkQty.UrlParameterName: grmi.NewConditionItem(meta.ProdOrderQty_ActReworkQty, grmi.Equal, false),
		meta.ProdOrderQty_ActQty1.UrlParameterName:      grmi.NewConditionItem(meta.ProdOrderQty_ActQty1, grmi.Equal, false),
		meta.ProdOrderQty_ActQty2.UrlParameterName:      grmi.NewConditionItem(meta.ProdOrderQty_ActQty2, grmi.Equal, false),
		meta.ProdOrderQty_ActQty3.UrlParameterName:      grmi.NewConditionItem(meta.ProdOrderQty_ActQty3, grmi.Equal, false),
		meta.ProdOrderQty_ActQty4.UrlParameterName:      grmi.NewConditionItem(meta.ProdOrderQty_ActQty4, grmi.Equal, false),
		meta.ProdOrderQty_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.ProdOrderQty_LastModify, grmi.Approximate, false),
		meta.ProdOrderQty_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.ProdOrderQty_LastUser, grmi.Equal, false),
		meta.ProdOrderQty_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.ProdOrderQty_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ProdOrderQty的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfProdOrderQtyAndPaging = grmi.NewCondition(
	DefaultConditionOfProdOrderQty.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : ProdOrderQtyServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : ProdOrderQty的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type ProdOrderQtyServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewProdOrderQtyDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProdOrderQtyDAOImplement实例
 *
 * @Return Value             : ProdOrderQtyDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewProdOrderQtyServiceImplement() *ProdOrderQtyServiceImplement {
	return &ProdOrderQtyServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderQtyService.InsertOne
 *
 ******************************************************************************/
func (impl *ProdOrderQtyServiceImplement) InsertOne(user *models.Usertab, entity *model.ProdOrderQty) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQty.service.impl.go", "InsertOneProdOrderQty", "插入一个ProdOrderQty")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderQtyDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ProdOrderId)
	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/om/ProdOrderQtyService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProdOrderQtyServiceImplement) DeleteOne(user *models.Usertab, prodOrderId string) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQty.service.impl.go", "DeleteOneProdOrderQty", "删除一个ProdOrderQty")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderQtyService.SelectOne
 *
 ******************************************************************************/
func (impl *ProdOrderQtyServiceImplement) SelectOne(user *models.Usertab, prodOrderId string) (*model.ProdOrderQty, error) {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQty.service.impl.go", "SelectOneProdOrderQty", "查询一个ProdOrderQty")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderQtyService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProdOrderQtyServiceImplement) UpdateOne(user *models.Usertab, entity *model.ProdOrderQty) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQty.service.impl.go", "UpdateOneProdOrderQty", "修改一个ProdOrderQty")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderQtyDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ProdOrderId)
	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/om/ProdOrderQtyService.Insert
 *
 ******************************************************************************/
func (impl *ProdOrderQtyServiceImplement) Insert(user *models.Usertab, entities *[]model.ProdOrderQty) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQty.service.impl.go", "InsertProdOrderQty", "插入多个ProdOrderQty")

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

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

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQty.service.impl.go", "DeleteProdOrderQty", "删除多个ProdOrderQty")

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

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

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQty.service.impl.go", "SelectProdOrderQty", "查询ProdOrderQty")

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

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQty.service.impl.go", "UpdateProdOrderQty", "修改多个ProdOrderQty")

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

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

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