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

package implments

import (
	baseDal "LAPP_ACURA_MOM_BACKEND/dao/base"
	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"
	baseModel "LAPP_ACURA_MOM_BACKEND/models/base"
	model "LAPP_ACURA_MOM_BACKEND/models/pln"
)

/******************************************************************************
 *
 * @Description              : ToyotaDeliveryOrderErrorLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-16 10:14:04
 *
 ******************************************************************************/
var DefaultConditionOfToyotaDeliveryOrderErrorLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ToyotaDeliveryOrderErrorLst_DemandId.UrlParameterName:    grmi.NewConditionItem(meta.ToyotaDeliveryOrderErrorLst_DemandId, grmi.Equal, true),
		meta.ToyotaDeliveryOrderErrorLst_Pos.UrlParameterName:         grmi.NewConditionItem(meta.ToyotaDeliveryOrderErrorLst_Pos, grmi.Equal, true),
		meta.ToyotaDeliveryOrderErrorLst_ErrorType.UrlParameterName:   grmi.NewConditionItem(meta.ToyotaDeliveryOrderErrorLst_ErrorType, grmi.Equal, false),
		meta.ToyotaDeliveryOrderErrorLst_ErrorInfo.UrlParameterName:   grmi.NewConditionItem(meta.ToyotaDeliveryOrderErrorLst_ErrorInfo, grmi.Equal, false),
		meta.ToyotaDeliveryOrderErrorLst_ErrorStatus.UrlParameterName: grmi.NewConditionItem(meta.ToyotaDeliveryOrderErrorLst_ErrorStatus, grmi.Equal, false),
		meta.ToyotaDeliveryOrderErrorLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.ToyotaDeliveryOrderErrorLst_LastModify, grmi.Approximate, false),
		meta.ToyotaDeliveryOrderErrorLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.ToyotaDeliveryOrderErrorLst_LastUser, grmi.Equal, false),
		meta.ToyotaDeliveryOrderErrorLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.ToyotaDeliveryOrderErrorLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ToyotaDeliveryOrderErrorLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-16 10:14:04
 *
 ******************************************************************************/
var DefaultConditionOfToyotaDeliveryOrderErrorLstAndPaging = grmi.NewCondition(
	DefaultConditionOfToyotaDeliveryOrderErrorLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ToyotaDeliveryOrderErrorLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ToyotaDeliveryOrderErrorLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-16 10:14:04
 *
 ******************************************************************************/
type ToyotaDeliveryOrderErrorLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewToyotaDeliveryOrderErrorLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ToyotaDeliveryOrderErrorLstDAOImplement实例
 *
 * @Return Value             : ToyotaDeliveryOrderErrorLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-16 10:14:04
 *
 ******************************************************************************/
func NewToyotaDeliveryOrderErrorLstServiceImplement() *ToyotaDeliveryOrderErrorLstServiceImplement {
	return &ToyotaDeliveryOrderErrorLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/ToyotaDeliveryOrderErrorLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ToyotaDeliveryOrderErrorLstServiceImplement) InsertOne(user *global.User, entity *model.ToyotaDeliveryOrderErrorLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewToyotaDeliveryOrderErrorLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DemandId, entity.Pos)
	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/ToyotaDeliveryOrderErrorLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ToyotaDeliveryOrderErrorLstServiceImplement) DeleteOne(user *global.User, demandId string, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/ToyotaDeliveryOrderErrorLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ToyotaDeliveryOrderErrorLstServiceImplement) SelectOne(user *global.User, demandId string, pos int) (*model.ToyotaDeliveryOrderErrorLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/ToyotaDeliveryOrderErrorLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ToyotaDeliveryOrderErrorLstServiceImplement) UpdateOne(user *global.User, entity *model.ToyotaDeliveryOrderErrorLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewToyotaDeliveryOrderErrorLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DemandId, entity.Pos)
	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/ToyotaDeliveryOrderErrorLstService.Insert
 *
 ******************************************************************************/
func (impl *ToyotaDeliveryOrderErrorLstServiceImplement) Insert(user *global.User, entities *[]model.ToyotaDeliveryOrderErrorLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DemandId, entity.Pos)
		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/ToyotaDeliveryOrderErrorLstService.Delete
 *
 ******************************************************************************/
func (impl *ToyotaDeliveryOrderErrorLstServiceImplement) Delete(user *global.User, entities *[]model.ToyotaDeliveryOrderErrorLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DemandId, entity.Pos)
		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/ToyotaDeliveryOrderErrorLstService.Select
 *
 ******************************************************************************/
func (impl *ToyotaDeliveryOrderErrorLstServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.ToyotaDeliveryOrderErrorLst, error) {

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

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

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

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

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/ToyotaDeliveryOrderErrorLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ToyotaDeliveryOrderErrorLstServiceImplement) SelectError(user *global.User, demandId string) (*model.ToyotaDeliveryOrderErrorLst, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewToyotaDeliveryOrderErrorLstDAO(session, user.PlantNr, user.UserId)
	plcErrorDao := baseDal.NewPLCErrorInfoDAO(session, user.PlantNr, user.UserId)
	errorLi, err := dao.Select([]grmi.Predicate{meta.ToyotaDeliveryOrderErrorLst_DemandId.NewPredicate(grmi.Equal, demandId)}, []grmi.Field{meta.ToyotaDeliveryOrderErrorLst_Pos})
	if err != nil {
		return nil, grmi.NewBusinessError("查询错误项失败, 错误:" + err.Error())
	}
	if len(errorLi) == 0 {
		return nil, grmi.NewBusinessError("该发运数据不存在错误项")
	}
	errorInfo := errorLi[0]
	if errorInfo.ErrorType == model.ERROR_TYTPE_PLC {
		plcError, err := plcErrorDao.SelectOne(errorInfo.DemandId, baseModel.PLC_ERROR_SOURCE_CALLOFF)
		if err != nil {
			return nil, grmi.NewBusinessError("查询PLC错误项失败, 错误:" + err.Error())
		}
		if plcError == nil {
			return nil, grmi.NewBusinessError("错误项为PLC错误, 但不存在PLC错误项")
		}
		errorInfo.PLCErrorInfo = *plcError
	}
	return &errorInfo, nil
}