// 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/pm"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/pm"
	model "LAPP_ACURA_MOM_BACKEND/models/pm"
	"LAPP_ACURA_MOM_BACKEND/utils"
)

/******************************************************************************
 *
 * @Description              : Attribute的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-08 13:20:42
 *
 ******************************************************************************/
var DefaultConditionOfAttribute = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Attribute_AttrNr.UrlParameterName:          grmi.NewConditionItem(meta.Attribute_AttrNr, 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_AttrDataType.UrlParameterName:    grmi.NewConditionItem(meta.Attribute_AttrDataType, grmi.Equal, false),
		meta.Attribute_AttrValType.UrlParameterName:     grmi.NewConditionItem(meta.Attribute_AttrValType, grmi.Equal, false),
		meta.Attribute_ValidVal.UrlParameterName:        grmi.NewConditionItem(meta.Attribute_ValidVal, grmi.Equal, false),
		meta.Attribute_ValPicture.UrlParameterName:      grmi.NewConditionItem(meta.Attribute_ValPicture, grmi.Equal, false),
		meta.Attribute_Picture1.UrlParameterName:        grmi.NewConditionItem(meta.Attribute_Picture1, grmi.Equal, false),
		meta.Attribute_Picture2.UrlParameterName:        grmi.NewConditionItem(meta.Attribute_Picture2, grmi.Equal, false),
		meta.Attribute_IsAttrGrpToggle.UrlParameterName: grmi.NewConditionItem(meta.Attribute_IsAttrGrpToggle, 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-10-08 13:20:42
 *
 ******************************************************************************/
var DefaultConditionOfAttributeAndPaging = grmi.NewCondition(
	DefaultConditionOfAttribute.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : AttributeServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Attribute的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-08 13:20:42
 *
 ******************************************************************************/
type AttributeServiceImplement struct {
}

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAttributeDAO(session, user.PlantNr, user.UserId)
	//生成ID
	snrDao := baseDal.NewSnrDAO(session, user.PlantNr, user.UserId)
	Attrnr, err := snrDao.GetNextSnr("Attrnr")
	entity.AttrNr = utils.ValueToInt(Attrnr,0)
	record, err := dao.SelectOne(entity.AttrNr)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	//批量插入
	for k, _ := range entity.Valst {
		entity.Valst[k].PlantNr = entity.PlantNr
		entity.Valst[k].AttrNr = entity.AttrNr
		entity.Valst[k].CreateTime = entity.CreateTime
		entity.Valst[k].LastUser = entity.LastUser
		entity.Valst[k].LastModify = entity.LastModify
	}
	alldata := make([]model.AttributeValst, 0)
	alldata = entity.Valst
	_, err = session.Table("PM_AttributeValst").Insert(&alldata)
	if err != nil {
		return err
	}
	return nil
}

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAttributeDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(attrNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(attrNr)
	if err != nil {
		return err
	}
	_, err = session.Table("PM_AttributeValst").Where("PlantNr = ? and AttrNr = ?", user.PlantNr, attrNr).Delete(&model.AttributeValst{})
	if err != nil {
		return err
	}
	return nil
}

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

	grmi.Log(user, "/services/pm/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(attrNr)
	if err != nil {
		return nil, err
	}

	var datalist []model.AttributeValst
	err = session.Table("PM_AttributeValst").Where("PlantNr = ? and AttrNr = ?", result.PlantNr, result.AttrNr).Find(&datalist)
	if err != nil {
		return result, err
	}
	result.Valst = datalist
	return result, nil
}

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

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

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

	if err != nil {
		return err
	}
	_, err = session.Table("PM_AttributeValst").Where("PlantNr = ? and AttrNr = ?", user.PlantNr, entity.AttrNr).Delete(&model.AttributeValst{})
	if err != nil {
		return err
	}
	//批量插入
	for k, _ := range entity.Valst {
		entity.Valst[k].PlantNr = entity.PlantNr
		entity.Valst[k].AttrNr = entity.AttrNr
		entity.Valst[k].CreateTime = entity.CreateTime
		entity.Valst[k].LastUser = entity.LastUser
		entity.Valst[k].LastModify = entity.LastModify
	}
	alldata := make([]model.AttributeValst, 0)
	alldata = entity.Valst
	_, err = session.Table("PM_AttributeValst").Insert(&alldata)
	if err != nil {
		return err
	}
	return nil
}

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

	grmi.Log(user, "/services/pm/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.AttrNr)
		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/pm/AttributeService.Delete
 *
 ******************************************************************************/
func (impl *AttributeServiceImplement) Delete(user *global.User, entities *[]model.Attribute) error {

	grmi.Log(user, "/services/pm/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.AttrNr)
		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/pm/AttributeService.Select
 *
 ******************************************************************************/
func (impl *AttributeServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.Attribute, error) {

	grmi.Log(user, "/services/pm/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)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pm/AttributeService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *AttributeServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

	grmi.Log(user, "/services/pm/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 grmi.EmptyPagingResult, err
	}
	dao := dal.NewAttributeDAO(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/pm/AttributeService.Update
 *
 ******************************************************************************/
func (impl *AttributeServiceImplement) Update(user *global.User, entities *[]model.Attribute) error {

	grmi.Log(user, "/services/pm/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.AttrNr)
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
	}

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