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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/base"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/base"
	model "LAPP_LF_MOM_BACKEND/models/base"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : Plant的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfPlant = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Plant_PlantNr.UrlParameterName:        grmi.NewConditionItem(meta.Plant_PlantNr, grmi.Equal, true),
		meta.Plant_Status.UrlParameterName:         grmi.NewConditionItem(meta.Plant_Status, grmi.Equal, false),
		meta.Plant_Name1.UrlParameterName:          grmi.NewConditionItem(meta.Plant_Name1, grmi.Equal, false),
		meta.Plant_Name2.UrlParameterName:          grmi.NewConditionItem(meta.Plant_Name2, grmi.Equal, false),
		meta.Plant_Logo.UrlParameterName:           grmi.NewConditionItem(meta.Plant_Logo, grmi.Equal, false),
		meta.Plant_Abrevname.UrlParameterName:      grmi.NewConditionItem(meta.Plant_Abrevname, grmi.Equal, false),
		meta.Plant_Country.UrlParameterName:        grmi.NewConditionItem(meta.Plant_Country, grmi.Equal, false),
		meta.Plant_Street.UrlParameterName:         grmi.NewConditionItem(meta.Plant_Street, grmi.Equal, false),
		meta.Plant_Address.UrlParameterName:        grmi.NewConditionItem(meta.Plant_Address, grmi.Equal, false),
		meta.Plant_PostCode.UrlParameterName:       grmi.NewConditionItem(meta.Plant_PostCode, grmi.Equal, false),
		meta.Plant_Phone.UrlParameterName:          grmi.NewConditionItem(meta.Plant_Phone, grmi.Equal, false),
		meta.Plant_Fax.UrlParameterName:            grmi.NewConditionItem(meta.Plant_Fax, grmi.Equal, false),
		meta.Plant_StartYear.UrlParameterName:      grmi.NewConditionItem(meta.Plant_StartYear, grmi.Equal, false),
		meta.Plant_EndYear.UrlParameterName:        grmi.NewConditionItem(meta.Plant_EndYear, grmi.Equal, false),
		meta.Plant_CompanyTitle.UrlParameterName:   grmi.NewConditionItem(meta.Plant_CompanyTitle, grmi.Equal, false),
		meta.Plant_CompanyAddress.UrlParameterName: grmi.NewConditionItem(meta.Plant_CompanyAddress, grmi.Equal, false),
		meta.Plant_TaxId.UrlParameterName:          grmi.NewConditionItem(meta.Plant_TaxId, grmi.Equal, false),
		meta.Plant_AccountBank.UrlParameterName:    grmi.NewConditionItem(meta.Plant_AccountBank, grmi.Equal, false),
		meta.Plant_AccountNr.UrlParameterName:      grmi.NewConditionItem(meta.Plant_AccountNr, grmi.Equal, false),
		meta.Plant_Currency.UrlParameterName:       grmi.NewConditionItem(meta.Plant_Currency, grmi.Equal, false),
		meta.Plant_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.Plant_LastModify, grmi.Approximate, false),
		meta.Plant_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.Plant_LastUser, grmi.Equal, false),
		meta.Plant_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.Plant_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : Plant的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfPlantAndPaging = grmi.NewCondition(
	DefaultConditionOfPlant.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : PlantServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : Plant的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type PlantServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewPlantDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个PlantDAOImplement实例
 *
 * @Return Value             : PlantDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewPlantServiceImplement() *PlantServiceImplement {
	return &PlantServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PlantService.InsertOne
 *
 ******************************************************************************/
func (impl *PlantServiceImplement) InsertOne(user *models.Usertab, entity *model.Plant) error {

	grmi.Log(user, "/services/base/implments/Plant.service.impl.go", "InsertOnePlant", "插入一个Plant")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PlantService.DeleteOne
 *
 ******************************************************************************/
func (impl *PlantServiceImplement) DeleteOne(user *models.Usertab, plantNr int) error {

	grmi.Log(user, "/services/base/implments/Plant.service.impl.go", "DeleteOnePlant", "删除一个Plant")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PlantService.SelectOne
 *
 ******************************************************************************/
func (impl *PlantServiceImplement) SelectOne(user *models.Usertab, plantNr int) (*model.Plant, error) {

	grmi.Log(user, "/services/base/implments/Plant.service.impl.go", "SelectOnePlant", "查询一个Plant")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPlantDAO(session, user.Userid)
	result, err := dao.SelectOne(plantNr)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PlantService.UpdateOne
 *
 ******************************************************************************/
func (impl *PlantServiceImplement) UpdateOne(user *models.Usertab, entity *model.Plant) error {

	grmi.Log(user, "/services/base/implments/Plant.service.impl.go", "UpdateOnePlant", "修改一个Plant")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PlantService.Insert
 *
 ******************************************************************************/
func (impl *PlantServiceImplement) Insert(user *models.Usertab, entities *[]model.Plant) error {

	grmi.Log(user, "/services/base/implments/Plant.service.impl.go", "InsertPlant", "插入多个Plant")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPlantDAO(session, user.Userid)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PlantService.Delete
 *
 ******************************************************************************/
func (impl *PlantServiceImplement) Delete(user *models.Usertab, entities *[]model.Plant) error {

	grmi.Log(user, "/services/base/implments/Plant.service.impl.go", "DeletePlant", "删除多个Plant")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPlantDAO(session, user.Userid)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PlantService.Select
 *
 ******************************************************************************/
func (impl *PlantServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/base/implments/Plant.service.impl.go", "SelectPlant", "查询Plant")

	condition := DefaultConditionOfPlantAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewPlantDAO(session, 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_LF_MOM_BACKEND/services/base/PlantService.Update
 *
 ******************************************************************************/
func (impl *PlantServiceImplement) Update(user *models.Usertab, entities *[]model.Plant) error {

	grmi.Log(user, "/services/base/implments/Plant.service.impl.go", "UpdatePlant", "修改多个Plant")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPlantDAO(session, user.Userid)

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

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