// 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              : ArticleAtcodLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 09:34:38
 *
 ******************************************************************************/
var DefaultConditionOfArticleAtcodLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArticleAtcodLst_ArtId.UrlParameterName:      grmi.NewConditionItem(meta.ArticleAtcodLst_ArtId, grmi.Equal, true),
		meta.ArticleAtcodLst_Version.UrlParameterName:    grmi.NewConditionItem(meta.ArticleAtcodLst_Version, grmi.Equal, true),
		meta.ArticleAtcodLst_ProjectId.UrlParameterName:  grmi.NewConditionItem(meta.ArticleAtcodLst_ProjectId, grmi.Equal, true),
		meta.ArticleAtcodLst_AttrCode.UrlParameterName:   grmi.NewConditionItem(meta.ArticleAtcodLst_AttrCode, grmi.Equal, true),
		meta.ArticleAtcodLst_AttrValue.UrlParameterName:  grmi.NewConditionItem(meta.ArticleAtcodLst_AttrValue, grmi.Equal, true),
		meta.ArticleAtcodLst_LastModify.UrlParameterName: grmi.NewConditionItem(meta.ArticleAtcodLst_LastModify, grmi.Approximate, false),
		meta.ArticleAtcodLst_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.ArticleAtcodLst_LastUser, grmi.Equal, false),
		meta.ArticleAtcodLst_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.ArticleAtcodLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArticleAtcodLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 09:34:38
 *
 ******************************************************************************/
var DefaultConditionOfArticleAtcodLstAndPaging = grmi.NewCondition(
	DefaultConditionOfArticleAtcodLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArticleAtcodLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArticleAtcodLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 09:34:38
 *
 ******************************************************************************/
type ArticleAtcodLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArticleAtcodLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArticleAtcodLstDAOImplement实例
 *
 * @Return Value             : ArticleAtcodLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 09:34:38
 *
 ******************************************************************************/
func NewArticleAtcodLstServiceImplement() *ArticleAtcodLstServiceImplement {
	return &ArticleAtcodLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArticleAtcodLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ArticleAtcodLstServiceImplement) InsertOne(user *global.User, entity *model.ArticleAtcodLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticleAtcodLstDAO(session, user.PlantNr, user.UserId)
	artDao := dal.NewArticleDAO(session, user.PlantNr, user.UserId)
	attriValueDao := dal.NewAttributeValstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ArtId, entity.Version, entity.ProjectId, entity.AttrCode, entity.AttrValue)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	article, err := artDao.SelectOne(entity.ArtId, entity.ProjectId)
	if err != nil {
		return err
	}
	if article == nil {
		return grmi.NewBusinessError("不存在对应的物料")
	}
	valueLi, err := attriValueDao.Select([]grmi.Predicate{meta.AttributeValst_AttrCode.NewPredicate(grmi.Equal, entity.AttrCode), meta.AttributeValst_AttrValue.NewPredicate(grmi.Equal, entity.AttrValue)}, nil)
	if err != nil {
		return err
	}
	if len(valueLi) == 0 {
		return grmi.NewBusinessError("不存在对应的属性值")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArticleAtcodLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArticleAtcodLstServiceImplement) DeleteOne(user *global.User, artId string, version int, projectId string, attrCode int, attrValue string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticleAtcodLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(artId, version, projectId, attrCode, attrValue)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(artId, version, projectId, attrCode, attrValue)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArticleAtcodLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ArticleAtcodLstServiceImplement) SelectOne(user *global.User, artId string, version int, projectId string, attrCode int, attrValue string) (*model.ArticleAtcodLst, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticleAtcodLstDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(artId, version, projectId, attrCode, attrValue)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArticleAtcodLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArticleAtcodLstServiceImplement) UpdateOne(user *global.User, entity *model.ArticleAtcodLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticleAtcodLstDAO(session, user.PlantNr, user.UserId)
	attriValueDao := dal.NewAttributeValstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ArtId, entity.Version, entity.ProjectId, entity.AttrCode, entity.AttrValue)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	valueLi, err := attriValueDao.Select([]grmi.Predicate{meta.AttributeValst_AttrCode.NewPredicate(grmi.Equal, entity.AttrCode), meta.AttributeValst_AttrValue.NewPredicate(grmi.Equal, entity.AttrValue)}, nil)
	if err != nil {
		return err
	}
	if len(valueLi) == 0 {
		return grmi.NewBusinessError("不存在对应的属性值")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArticleAtcodLstService.Insert
 *
 ******************************************************************************/
func (impl *ArticleAtcodLstServiceImplement) Insert(user *global.User, entities *[]model.ArticleAtcodLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ArtId, entity.Version, entity.ProjectId, entity.AttrCode, entity.AttrValue)
		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/ArticleAtcodLstService.Delete
 *
 ******************************************************************************/
func (impl *ArticleAtcodLstServiceImplement) Delete(user *global.User, entities *[]model.ArticleAtcodLst) error {

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

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

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

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

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

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ArtId, entity.Version, entity.ProjectId, entity.AttrCode, entity.AttrValue)
		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/base/ArticleAtcodLstService.SelectAttrCodeAndValueByArtId
 *
 ******************************************************************************/
func (impl *ArticleAtcodLstServiceImplement) SelectAttrCodeAndValueByArtId(user *global.User, artId string) (interface{}, error) {

	grmi.Log(user, "/services/base/implments/ArticleAtcodLst.service.impl.go", "SelectAttrCodeAndValueByArtId", "通过artId查询关联的所有属性集")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticleAtcodLstDAO(session, user.PlantNr, user.UserId)
	li, err := dao.SelectByArtId(artId)
	if err != nil {
		return nil, grmi.NewBusinessError("查询物料属性相关数据失败, error:" + err.Error())
	}
	//type AttrValue struct {
	//	Value string `json:"value"`
	//	ValueDesc string `json:"valueDesc"`
	//}
	//type Attribute struct {
	//	Code int `json:"code"`
	//	CodeDesc string `json:"CodeDesc"`
	//	ValueLi []AttrValue `json:"valueLi"`
	//}
	//result := make([]Attribute, 0)
	//tempMap := make(map[int][]model.ArticleRelateAttribute)
	//for _, data := range li {
	//	_, exist := tempMap[data.ArticleAtcodLst.AttrCode]
	//	if !exist {
	//		tempMap[data.ArticleAtcodLst.AttrCode] = make([]model.ArticleRelateAttribute, 0)
	//	}
	//	tempMap[data.ArticleAtcodLst.AttrCode] = append(tempMap[data.ArticleAtcodLst.AttrCode], data)
	//}
	//
	//for attrCode, attributeLi := range tempMap {
	//	attrCodeData := Attribute{
	//		Code: attrCode,
	//		CodeDesc: attributeLi[0].Attribute.Descr,
	//		ValueLi: make([]AttrValue, 0),
	//	}
	//	for _, item := range attributeLi {
	//		attrValueData := AttrValue{
	//			Value: item.ArticleAtcodLst.AttrValue,
	//			ValueDesc: item.AttributeValst.Descr,
	//		}
	//		attrCodeData.ValueLi = append(attrCodeData.ValueLi, attrValueData)
	//	}
	//	result = append(result, attrCodeData)
	//}

	type ArtAttributeData struct {
		Code int `json:"code"`
		Desc string `json:"desc"`
	}
	result := make([]ArtAttributeData, 0)
	codeMap := make(map[int]interface{})
	for _, data := range li {
		_, exist := codeMap[data.Attribute.AttrCode]
		if !exist {
			codeMap[data.Attribute.AttrCode] = nil
			codeData := ArtAttributeData{
				Code: data.Attribute.AttrCode,
				Desc: data.Attribute.Descr,
			}
			result = append(result, codeData)
		}
	}
	return result, nil
}
