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

package implments

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

/******************************************************************************
 *
 * @Description              : Attribute的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfAttribute = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Attribute_AttrCode.UrlParameterName:     grmi.NewConditionItem(meta.Attribute_AttrCode, grmi.Equal, true),
		meta.Attribute_AttrName.UrlParameterName:     grmi.NewConditionItem(meta.Attribute_AttrName, grmi.Equal, false),
		meta.Attribute_Descr.UrlParameterName:        grmi.NewConditionItem(meta.Attribute_Descr, grmi.Equal, false),
		meta.Attribute_ShortCode.UrlParameterName:    grmi.NewConditionItem(meta.Attribute_ShortCode, grmi.Equal, false),
		meta.Attribute_AttrType.UrlParameterName:     grmi.NewConditionItem(meta.Attribute_AttrType, grmi.Equal, false),
		meta.Attribute_AttrDataType.UrlParameterName: grmi.NewConditionItem(meta.Attribute_AttrDataType, grmi.Equal, false),
		meta.Attribute_AttrValType.UrlParameterName:  grmi.NewConditionItem(meta.Attribute_AttrValType, grmi.Equal, false),
		meta.Attribute_ValFormat.UrlParameterName:    grmi.NewConditionItem(meta.Attribute_ValFormat, grmi.Equal, false),
		meta.Attribute_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.Attribute_LastModify, grmi.Approximate, false),
		meta.Attribute_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.Attribute_LastUser, grmi.Equal, false),
		meta.Attribute_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.Attribute_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/AttributeService.InsertOne
 *
 ******************************************************************************/
func (impl *AttributeServiceImplement) InsertOne(user *global.User, entity *model.Attribute) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewAttributeDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.AttrCode)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}

	/**处理数据**/
	for k, _ := range entity.Valst {
		entity.Valst[k].AttrCode = entity.AttrCode
		entity.Valst[k].Pos = k + 1
	}
	lstdao := dal.NewAttributeValstDAO(session, user.PlantNr, user.UserId)
	err = lstdao.Insert(&entity.Valst)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/AttributeService.DeleteOne
 *
 ******************************************************************************/
func (impl *AttributeServiceImplement) DeleteOne(user *global.User, attrCode int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewAttributeDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(attrCode)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(attrCode)
	if err != nil {
		session.Rollback()
		return err
	}
	lstdao := dal.NewAttributeValstDAO(session, user.PlantNr, user.UserId)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.AttributeValst_AttrCode.NewPredicate(grmi.Equal, attrCode),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/AttributeService.SelectOne
 *
 ******************************************************************************/
func (impl *AttributeServiceImplement) SelectOne(user *global.User, attrCode int) (*model.Attribute, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAttributeDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(attrCode)
	if err != nil {
		return nil, err
	}
	lstdao := dal.NewAttributeValstDAO(session, user.PlantNr, user.UserId)
	list, err := lstdao.Select([]grmi.Predicate{
		meta.AttributeValst_AttrCode.NewPredicate(grmi.Equal, attrCode),
	}, nil)
	if err != nil {
		return nil, err
	}
	result.Valst = list
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/AttributeService.UpdateOne
 *
 ******************************************************************************/
func (impl *AttributeServiceImplement) UpdateOne(user *global.User, entity *model.Attribute) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewAttributeDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.AttrCode)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	/**处理数据**/
	for k, _ := range entity.Valst {
		entity.Valst[k].AttrCode = entity.AttrCode
		entity.Valst[k].Pos = k + 1
	}
	lstdao := dal.NewAttributeValstDAO(session, user.PlantNr, user.UserId)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.AttributeValst_AttrCode.NewPredicate(grmi.Equal, entity.AttrCode),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	err = lstdao.Insert(&entity.Valst)
	if err != nil {
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/AttributeService.Insert
 *
 ******************************************************************************/
func (impl *AttributeServiceImplement) Insert(user *global.User, entities *[]model.Attribute) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AttrCode)
		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/base/AttributeService.Delete
 *
 ******************************************************************************/
func (impl *AttributeServiceImplement) Delete(user *global.User, entities *[]model.Attribute) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AttrCode)
		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/base/AttributeService.Select
 *
 ******************************************************************************/
func (impl *AttributeServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfAttributeAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewAttributeDAO(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/base/AttributeService.Update
 *
 ******************************************************************************/
func (impl *AttributeServiceImplement) Update(user *global.User, entities *[]model.Attribute) error {

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

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

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

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