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

package implments

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

/******************************************************************************
 *
 * @Description              : Asset的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfAsset = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Asset_AssetNr.UrlParameterName:        grmi.NewConditionItem(meta.Asset_AssetNr, grmi.Equal, true),
		meta.Asset_Descr.UrlParameterName:          grmi.NewConditionItem(meta.Asset_Descr, grmi.Equal, false),
		meta.Asset_AssetName.UrlParameterName:      grmi.NewConditionItem(meta.Asset_AssetName, grmi.Equal, false),
		meta.Asset_AssetType.UrlParameterName:      grmi.NewConditionItem(meta.Asset_AssetType, grmi.Equal, false),
		meta.Asset_Model.UrlParameterName:          grmi.NewConditionItem(meta.Asset_Model, grmi.Equal, false),
		meta.Asset_Year.UrlParameterName:           grmi.NewConditionItem(meta.Asset_Year, grmi.Equal, false),
		meta.Asset_SerialNo.UrlParameterName:       grmi.NewConditionItem(meta.Asset_SerialNo, grmi.Equal, false),
		meta.Asset_TagNo.UrlParameterName:          grmi.NewConditionItem(meta.Asset_TagNo, grmi.Equal, false),
		meta.Asset_Maker.UrlParameterName:          grmi.NewConditionItem(meta.Asset_Maker, grmi.Equal, false),
		meta.Asset_LocaNr.UrlParameterName:         grmi.NewConditionItem(meta.Asset_LocaNr, grmi.Equal, false),
		meta.Asset_AssetStatus.UrlParameterName:    grmi.NewConditionItem(meta.Asset_AssetStatus, grmi.Equal, false),
		meta.Asset_Operator.UrlParameterName:       grmi.NewConditionItem(meta.Asset_Operator, grmi.Equal, false),
		meta.Asset_WeekModelId.UrlParameterName:    grmi.NewConditionItem(meta.Asset_WeekModelId, grmi.Equal, false),
		meta.Asset_WorkCalEndarId.UrlParameterName: grmi.NewConditionItem(meta.Asset_WorkCalEndarId, grmi.Equal, false),
		meta.Asset_CostCenterId.UrlParameterName:   grmi.NewConditionItem(meta.Asset_CostCenterId, grmi.Equal, false),
		meta.Asset_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.Asset_LastModify, grmi.Approximate, false),
		meta.Asset_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.Asset_LastUser, grmi.Equal, false),
		meta.Asset_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.Asset_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : Asset的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfAssetAndPaging = grmi.NewCondition(
	DefaultConditionOfAsset.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : AssetServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Asset的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
type AssetServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewAssetDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个AssetDAOImplement实例
 *
 * @Return Value             : AssetDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
func NewAssetServiceImplement() *AssetServiceImplement {
	return &AssetServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetService.InsertOne
 *
 ******************************************************************************/
func (impl *AssetServiceImplement) InsertOne(user *models.Usertab, entity *model.Asset) error {

	grmi.Log(user, "/services/pm/implments/PM_Asset.service.impl.go", "InsertOneAsset", "插入一个Asset")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssetDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AssetNr)
	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/pm/AssetService.DeleteOne
 *
 ******************************************************************************/
func (impl *AssetServiceImplement) DeleteOne(user *models.Usertab, assetNr int) error {

	grmi.Log(user, "/services/pm/implments/PM_Asset.service.impl.go", "DeleteOneAsset", "删除一个Asset")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetService.SelectOne
 *
 ******************************************************************************/
func (impl *AssetServiceImplement) SelectOne(user *models.Usertab, assetNr int) (*model.Asset, error) {

	grmi.Log(user, "/services/pm/implments/PM_Asset.service.impl.go", "SelectOneAsset", "查询一个Asset")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/AssetService.UpdateOne
 *
 ******************************************************************************/
func (impl *AssetServiceImplement) UpdateOne(user *models.Usertab, entity *model.Asset) error {

	grmi.Log(user, "/services/pm/implments/PM_Asset.service.impl.go", "UpdateOneAsset", "修改一个Asset")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssetDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AssetNr)
	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/pm/AssetService.Insert
 *
 ******************************************************************************/
func (impl *AssetServiceImplement) Insert(user *models.Usertab, entities *[]model.Asset) error {

	grmi.Log(user, "/services/pm/implments/PM_Asset.service.impl.go", "InsertAsset", "插入多个Asset")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AssetNr)
		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/pm/AssetService.Delete
 *
 ******************************************************************************/
func (impl *AssetServiceImplement) Delete(user *models.Usertab, entities *[]model.Asset) error {

	grmi.Log(user, "/services/pm/implments/PM_Asset.service.impl.go", "DeleteAsset", "删除多个Asset")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AssetNr)
		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/pm/AssetService.Select
 *
 ******************************************************************************/
func (impl *AssetServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/pm/implments/PM_Asset.service.impl.go", "SelectAsset", "查询Asset")

	condition := DefaultConditionOfAssetAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewAssetDAO(session, user.Pid, 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/pm/AssetService.Update
 *
 ******************************************************************************/
func (impl *AssetServiceImplement) Update(user *models.Usertab, entities *[]model.Asset) error {

	grmi.Log(user, "/services/pm/implments/PM_Asset.service.impl.go", "UpdateAsset", "修改多个Asset")

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

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

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