// 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              : ToyotaCallOffErrorLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-26 09:51:03
 *
 ******************************************************************************/
var DefaultConditionOfToyotaCallOffErrorLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ToyotaCallOffErrorLst_DemandId.UrlParameterName:    grmi.NewConditionItem(meta.ToyotaCallOffErrorLst_DemandId, grmi.Equal, true),
		meta.ToyotaCallOffErrorLst_Pos.UrlParameterName:         grmi.NewConditionItem(meta.ToyotaCallOffErrorLst_Pos, grmi.Equal, true),
		meta.ToyotaCallOffErrorLst_ErrorType.UrlParameterName:   grmi.NewConditionItem(meta.ToyotaCallOffErrorLst_ErrorType, grmi.Equal, false),
		meta.ToyotaCallOffErrorLst_ErrorInfo.UrlParameterName:   grmi.NewConditionItem(meta.ToyotaCallOffErrorLst_ErrorInfo, grmi.Equal, false),
		meta.ToyotaCallOffErrorLst_ErrorStatus.UrlParameterName: grmi.NewConditionItem(meta.ToyotaCallOffErrorLst_ErrorStatus, grmi.Equal, false),
		meta.ToyotaCallOffErrorLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.ToyotaCallOffErrorLst_LastModify, grmi.Approximate, false),
		meta.ToyotaCallOffErrorLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.ToyotaCallOffErrorLst_LastUser, grmi.Equal, false),
		meta.ToyotaCallOffErrorLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.ToyotaCallOffErrorLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ToyotaCallOffErrorLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-26 09:51:03
 *
 ******************************************************************************/
var DefaultConditionOfToyotaCallOffErrorLstAndPaging = grmi.NewCondition(
	DefaultConditionOfToyotaCallOffErrorLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ToyotaCallOffErrorLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ToyotaCallOffErrorLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-26 09:51:03
 *
 ******************************************************************************/
type ToyotaCallOffErrorLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewToyotaCallOffErrorLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ToyotaCallOffErrorLstDAOImplement实例
 *
 * @Return Value             : ToyotaCallOffErrorLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-26 09:51:03
 *
 ******************************************************************************/
func NewToyotaCallOffErrorLstServiceImplement() *ToyotaCallOffErrorLstServiceImplement {
	return &ToyotaCallOffErrorLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/ToyotaCallOffErrorLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ToyotaCallOffErrorLstServiceImplement) InsertOne(user *global.User, entity *model.ToyotaCallOffErrorLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewToyotaCallOffErrorLstDAO(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/ToyotaCallOffErrorLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ToyotaCallOffErrorLstServiceImplement) DeleteOne(user *global.User, demandId string, pos int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewToyotaCallOffErrorLstDAO(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/ToyotaCallOffErrorLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ToyotaCallOffErrorLstServiceImplement) SelectOne(user *global.User, demandId string, pos int) (*model.ToyotaCallOffErrorLst, error) {

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

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

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

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

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

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

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

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

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

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

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewToyotaCallOffErrorLstDAO(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/ToyotaCallOffErrorLstService.RepairError
 *
 ******************************************************************************/
func (impl *ToyotaCallOffErrorLstServiceImplement) RepairError(user *global.User, demandId string, pos int) error {

	grmi.Log(user, "/services/pln/implments/ToyotaCallOffErrorLst.service.impl.go", "RepairError", "修复一个ToyotaCallOffErrorLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewToyotaCallOffErrorLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(demandId, pos)
	if err != nil {
		return grmi.NewBusinessError("查询数据项失败, 错误:" + err.Error())
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	if record.ErrorStatus == model.ERROR_STATUS_OFF {
		return grmi.NewBusinessError("该错误项已修复")
	}
	record.ErrorStatus = model.ERROR_STATUS_OFF
	err = dao.UpdateOne(record)
	if err != nil {
		return grmi.NewBusinessError("更新错误项状态失败，错误:" + err.Error())
	}
	return nil
}