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

package implments

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

/******************************************************************************
 *
 * @Description              : CustOrder的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-20 14:37:20
 *
 ******************************************************************************/
var DefaultConditionOfCustOrder = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.CustOrder_CustOrderId.UrlParameterName: grmi.NewConditionItem(meta.CustOrder_CustOrderId, grmi.Equal, true),
		meta.CustOrder_OemOrderId.UrlParameterName:  grmi.NewConditionItem(meta.CustOrder_OemOrderId, grmi.Equal, false),
		meta.CustOrder_OrderInfo.UrlParameterName:   grmi.NewConditionItem(meta.CustOrder_OrderInfo, grmi.Equal, false),
		meta.CustOrder_ArtId.UrlParameterName:       grmi.NewConditionItem(meta.CustOrder_ArtId, grmi.Equal, false),
		meta.CustOrder_ArtDesc.UrlParameterName:     grmi.NewConditionItem(meta.CustOrder_ArtDesc, grmi.Equal, false),
		meta.CustOrder_OrderTime.UrlParameterName:   grmi.NewConditionItem(meta.CustOrder_OrderTime, grmi.Approximate, false),
		meta.CustOrder_DDT.UrlParameterName:         grmi.NewConditionItem(meta.CustOrder_DDT, grmi.Approximate, false),
		meta.CustOrder_ProjectId.UrlParameterName:   grmi.NewConditionItem(meta.CustOrder_ProjectId, grmi.Equal, false),
		meta.CustOrder_ArtTypeId.UrlParameterName:   grmi.NewConditionItem(meta.CustOrder_ArtTypeId, grmi.Equal, false),
		meta.CustOrder_ArtTypeDesc.UrlParameterName: grmi.NewConditionItem(meta.CustOrder_ArtTypeDesc, grmi.Equal, false),
		meta.CustOrder_SEQ.UrlParameterName:         grmi.NewConditionItem(meta.CustOrder_SEQ, grmi.Equal, false),
		meta.CustOrder_Status.UrlParameterName:      grmi.NewConditionItem(meta.CustOrder_Status, grmi.Equal, false),
		meta.CustOrder_ParseStatus.UrlParameterName: grmi.NewConditionItem(meta.CustOrder_ParseStatus, grmi.Equal, false),
		meta.CustOrder_ErrNum.UrlParameterName:      grmi.NewConditionItem(meta.CustOrder_ErrNum, grmi.Equal, false),
		meta.CustOrder_ErrInfo.UrlParameterName:     grmi.NewConditionItem(meta.CustOrder_ErrInfo, grmi.Equal, false),
		meta.CustOrder_CustomerId.UrlParameterName:  grmi.NewConditionItem(meta.CustOrder_CustomerId, grmi.Equal, false),
		meta.CustOrder_VIN.UrlParameterName:         grmi.NewConditionItem(meta.CustOrder_VIN, grmi.Equal, false),
		meta.CustOrder_CalloffNr.UrlParameterName:   grmi.NewConditionItem(meta.CustOrder_CalloffNr, grmi.Equal, false),
		meta.CustOrder_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.CustOrder_LastModify, grmi.Approximate, false),
		meta.CustOrder_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.CustOrder_LastUser, grmi.Equal, false),
		meta.CustOrder_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.CustOrder_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : CustOrder的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-20 14:37:20
 *
 ******************************************************************************/
var DefaultConditionOfCustOrderAndPaging = grmi.NewCondition(
	DefaultConditionOfCustOrder.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : CustOrderServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : CustOrder的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-20 14:37:20
 *
 ******************************************************************************/
type CustOrderServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewCustOrderDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个CustOrderDAOImplement实例
 *
 * @Return Value             : CustOrderDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-20 14:37:20
 *
 ******************************************************************************/
func NewCustOrderServiceImplement() *CustOrderServiceImplement {
	return &CustOrderServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/CustOrderService.InsertOne
 *
 ******************************************************************************/
func (impl *CustOrderServiceImplement) InsertOne(user *global.User, entity *model.CustOrder) error {

	grmi.Log(user, "/services/om/implments/CustOrder.service.impl.go", "InsertOneCustOrder", "插入一个CustOrder")

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

	grmi.Log(user, "/services/om/implments/CustOrder.service.impl.go", "DeleteOneCustOrder", "删除一个CustOrder")

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

	grmi.Log(user, "/services/om/implments/CustOrder.service.impl.go", "SelectOneCustOrder", "查询一个CustOrder")

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

	grmi.Log(user, "/services/om/implments/CustOrder.service.impl.go", "UpdateOneCustOrder", "修改一个CustOrder")

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

	grmi.Log(user, "/services/om/implments/CustOrder.service.impl.go", "InsertCustOrder", "插入多个CustOrder")

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

	grmi.Log(user, "/services/om/implments/CustOrder.service.impl.go", "DeleteCustOrder", "删除多个CustOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderDAO(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/om/CustOrderService.Select
 *
 ******************************************************************************/
func (impl *CustOrderServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/om/implments/CustOrder.service.impl.go", "SelectCustOrder", "查询CustOrder")

	condition := DefaultConditionOfCustOrderAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewCustOrderDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/om/CustOrderService.Update
 *
 ******************************************************************************/
func (impl *CustOrderServiceImplement) Update(user *global.User, entities *[]model.CustOrder) error {

	grmi.Log(user, "/services/om/implments/CustOrder.service.impl.go", "UpdateCustOrder", "修改多个CustOrder")

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