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

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/pln"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/pln"
	model "LAPP_ACURA_MOM_BACKEND/models/pln"
)

/******************************************************************************
 *
 * @Description              : CustOrderQty的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-12-03 16:35:12
 *
 ******************************************************************************/
var DefaultConditionOfCustOrderQty = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.CustOrderQty_CustOrderId.UrlParameterName:       grmi.NewConditionItem(meta.CustOrderQty_CustOrderId, grmi.Equal, true),
		meta.CustOrderQty_PlanQty.UrlParameterName:           grmi.NewConditionItem(meta.CustOrderQty_PlanQty, grmi.Equal, false),
		meta.CustOrderQty_ReleasedQty.UrlParameterName:       grmi.NewConditionItem(meta.CustOrderQty_ReleasedQty, grmi.Equal, false),
		meta.CustOrderQty_SerialOrderQty.UrlParameterName:    grmi.NewConditionItem(meta.CustOrderQty_SerialOrderQty, grmi.Equal, false),
		meta.CustOrderQty_SerialOrderActQty.UrlParameterName: grmi.NewConditionItem(meta.CustOrderQty_SerialOrderActQty, grmi.Equal, false),
		meta.CustOrderQty_ActQty.UrlParameterName:            grmi.NewConditionItem(meta.CustOrderQty_ActQty, grmi.Equal, false),
		meta.CustOrderQty_ActGoodQty.UrlParameterName:        grmi.NewConditionItem(meta.CustOrderQty_ActGoodQty, grmi.Equal, false),
		meta.CustOrderQty_ActScrapQty.UrlParameterName:       grmi.NewConditionItem(meta.CustOrderQty_ActScrapQty, grmi.Equal, false),
		meta.CustOrderQty_ActReworkQty.UrlParameterName:      grmi.NewConditionItem(meta.CustOrderQty_ActReworkQty, grmi.Equal, false),
		meta.CustOrderQty_CancelQty.UrlParameterName:         grmi.NewConditionItem(meta.CustOrderQty_CancelQty, grmi.Equal, false),
		meta.CustOrderQty_ActQty1.UrlParameterName:           grmi.NewConditionItem(meta.CustOrderQty_ActQty1, grmi.Equal, false),
		meta.CustOrderQty_ActQty2.UrlParameterName:           grmi.NewConditionItem(meta.CustOrderQty_ActQty2, grmi.Equal, false),
		meta.CustOrderQty_ActQty3.UrlParameterName:           grmi.NewConditionItem(meta.CustOrderQty_ActQty3, grmi.Equal, false),
		meta.CustOrderQty_ActQty4.UrlParameterName:           grmi.NewConditionItem(meta.CustOrderQty_ActQty4, grmi.Equal, false),
		meta.CustOrderQty_LastModify.UrlParameterName:        grmi.NewConditionItem(meta.CustOrderQty_LastModify, grmi.Approximate, false),
		meta.CustOrderQty_LastUser.UrlParameterName:          grmi.NewConditionItem(meta.CustOrderQty_LastUser, grmi.Equal, false),
		meta.CustOrderQty_CreateTime.UrlParameterName:        grmi.NewConditionItem(meta.CustOrderQty_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : CustOrderQty的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-12-03 16:35:12
 *
 ******************************************************************************/
var DefaultConditionOfCustOrderQtyAndPaging = grmi.NewCondition(
	DefaultConditionOfCustOrderQty.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : CustOrderQtyServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : CustOrderQty的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-12-03 16:35:12
 *
 ******************************************************************************/
type CustOrderQtyServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewCustOrderQtyDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个CustOrderQtyDAOImplement实例
 *
 * @Return Value             : CustOrderQtyDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-12-03 16:35:12
 *
 ******************************************************************************/
func NewCustOrderQtyServiceImplement() *CustOrderQtyServiceImplement {
	return &CustOrderQtyServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderQtyService.InsertOne
 *
 ******************************************************************************/
func (impl *CustOrderQtyServiceImplement) InsertOne(user *global.User, entity *model.CustOrderQty) error {

	grmi.Log(user, "/services/pln/implments/CustOrderQty.service.impl.go", "InsertOneCustOrderQty", "插入一个CustOrderQty")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderQtyDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.CustOrderId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderQtyService.DeleteOne
 *
 ******************************************************************************/
func (impl *CustOrderQtyServiceImplement) DeleteOne(user *global.User, custOrderId string) error {

	grmi.Log(user, "/services/pln/implments/CustOrderQty.service.impl.go", "DeleteOneCustOrderQty", "删除一个CustOrderQty")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderQtyService.SelectOne
 *
 ******************************************************************************/
func (impl *CustOrderQtyServiceImplement) SelectOne(user *global.User, custOrderId string) (*model.CustOrderQty, error) {

	grmi.Log(user, "/services/pln/implments/CustOrderQty.service.impl.go", "SelectOneCustOrderQty", "查询一个CustOrderQty")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderQtyDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(custOrderId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderQtyService.UpdateOne
 *
 ******************************************************************************/
func (impl *CustOrderQtyServiceImplement) UpdateOne(user *global.User, entity *model.CustOrderQty) error {

	grmi.Log(user, "/services/pln/implments/CustOrderQty.service.impl.go", "UpdateOneCustOrderQty", "修改一个CustOrderQty")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderQtyDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.CustOrderId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderQtyService.Insert
 *
 ******************************************************************************/
func (impl *CustOrderQtyServiceImplement) Insert(user *global.User, entities *[]model.CustOrderQty) error {

	grmi.Log(user, "/services/pln/implments/CustOrderQty.service.impl.go", "InsertCustOrderQty", "插入多个CustOrderQty")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderQtyDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.CustOrderId)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderQtyService.Delete
 *
 ******************************************************************************/
func (impl *CustOrderQtyServiceImplement) Delete(user *global.User, entities *[]model.CustOrderQty) error {

	grmi.Log(user, "/services/pln/implments/CustOrderQty.service.impl.go", "DeleteCustOrderQty", "删除多个CustOrderQty")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderQtyDAO(session, user.PlantNr, user.UserId)

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderQtyService.Select
 *
 ******************************************************************************/
func (impl *CustOrderQtyServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.CustOrderQty, error) {

	grmi.Log(user, "/services/pln/implments/CustOrderQty.service.impl.go", "SelectCustOrderQty", "查询CustOrderQty")

	condition := DefaultConditionOfCustOrderQtyAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewCustOrderQtyDAO(session, user.PlantNr, user.UserId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderQtyService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *CustOrderQtyServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

	grmi.Log(user, "/services/pln/implments/CustOrderQty.service.impl.go", "SelectCustOrderQty", "查询CustOrderQty")

	condition := DefaultConditionOfCustOrderQtyAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewCustOrderQtyDAO(session, user.PlantNr, user.UserId)
	condition.Fill(urlParameters)
	result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderQtyService.Update
 *
 ******************************************************************************/
func (impl *CustOrderQtyServiceImplement) Update(user *global.User, entities *[]model.CustOrderQty) error {

	grmi.Log(user, "/services/pln/implments/CustOrderQty.service.impl.go", "UpdateCustOrderQty", "修改多个CustOrderQty")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderQtyDAO(session, user.PlantNr, user.UserId)

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

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