// 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              : CustOrderStatus的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-22 13:50:40
 *
 ******************************************************************************/
var DefaultConditionOfCustOrderStatus = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.CustOrderStatus_CustOrderId.UrlParameterName:     grmi.NewConditionItem(meta.CustOrderStatus_CustOrderId, grmi.Equal, true),
		meta.CustOrderStatus_Status.UrlParameterName:          grmi.NewConditionItem(meta.CustOrderStatus_Status, grmi.Equal, false),
		meta.CustOrderStatus_BackFlushStatus.UrlParameterName: grmi.NewConditionItem(meta.CustOrderStatus_BackFlushStatus, grmi.Equal, false),
		meta.CustOrderStatus_PackStatus.UrlParameterName:      grmi.NewConditionItem(meta.CustOrderStatus_PackStatus, grmi.Equal, false),
		meta.CustOrderStatus_ShipStatus.UrlParameterName:      grmi.NewConditionItem(meta.CustOrderStatus_ShipStatus, grmi.Equal, false),
		meta.CustOrderStatus_Status1.UrlParameterName:         grmi.NewConditionItem(meta.CustOrderStatus_Status1, grmi.Equal, false),
		meta.CustOrderStatus_Status2.UrlParameterName:         grmi.NewConditionItem(meta.CustOrderStatus_Status2, grmi.Equal, false),
		meta.CustOrderStatus_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.CustOrderStatus_LastModify, grmi.Approximate, false),
		meta.CustOrderStatus_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.CustOrderStatus_LastUser, grmi.Equal, false),
		meta.CustOrderStatus_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.CustOrderStatus_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : CustOrderStatus的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-22 13:50:40
 *
 ******************************************************************************/
var DefaultConditionOfCustOrderStatusAndPaging = grmi.NewCondition(
	DefaultConditionOfCustOrderStatus.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : CustOrderStatusServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : CustOrderStatus的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-22 13:50:40
 *
 ******************************************************************************/
type CustOrderStatusServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewCustOrderStatusDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个CustOrderStatusDAOImplement实例
 *
 * @Return Value             : CustOrderStatusDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-22 13:50:40
 *
 ******************************************************************************/
func NewCustOrderStatusServiceImplement() *CustOrderStatusServiceImplement {
	return &CustOrderStatusServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderStatusService.InsertOne
 *
 ******************************************************************************/
func (impl *CustOrderStatusServiceImplement) InsertOne(user *global.User, entity *model.CustOrderStatus) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderStatusDAO(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/CustOrderStatusService.DeleteOne
 *
 ******************************************************************************/
func (impl *CustOrderStatusServiceImplement) DeleteOne(user *global.User, custOrderId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderStatusDAO(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/CustOrderStatusService.SelectOne
 *
 ******************************************************************************/
func (impl *CustOrderStatusServiceImplement) SelectOne(user *global.User, custOrderId string) (*model.CustOrderStatus, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderStatusDAO(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/CustOrderStatusService.UpdateOne
 *
 ******************************************************************************/
func (impl *CustOrderStatusServiceImplement) UpdateOne(user *global.User, entity *model.CustOrderStatus) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderStatusDAO(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/CustOrderStatusService.Insert
 *
 ******************************************************************************/
func (impl *CustOrderStatusServiceImplement) Insert(user *global.User, entities *[]model.CustOrderStatus) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderStatusDAO(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/CustOrderStatusService.Delete
 *
 ******************************************************************************/
func (impl *CustOrderStatusServiceImplement) Delete(user *global.User, entities *[]model.CustOrderStatus) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderStatusDAO(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/CustOrderStatusService.Select
 *
 ******************************************************************************/
func (impl *CustOrderStatusServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.CustOrderStatus, error) {

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

	condition := DefaultConditionOfCustOrderStatusAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewCustOrderStatusDAO(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/CustOrderStatusService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *CustOrderStatusServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

	condition := DefaultConditionOfCustOrderStatusAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewCustOrderStatusDAO(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/CustOrderStatusService.Update
 *
 ******************************************************************************/
func (impl *CustOrderStatusServiceImplement) Update(user *global.User, entities *[]model.CustOrderStatus) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderStatusDAO(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
}
