// 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              : ArtCheckRule的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-24 11:19:07
 *
 ******************************************************************************/
var DefaultConditionOfArtCheckRule = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArtCheckRule_AttriCode.UrlParameterName:           grmi.NewConditionItem(meta.ArtCheckRule_AttriCode, grmi.Equal, true),
		meta.ArtCheckRule_WorkLineId.UrlParameterName:          grmi.NewConditionItem(meta.ArtCheckRule_WorkLineId, grmi.Equal, true),
		meta.ArtCheckRule_ProjectId.UrlParameterName:           grmi.NewConditionItem(meta.ArtCheckRule_ProjectId, grmi.Equal, true),
		meta.ArtCheckRule_SubObject.UrlParameterName:           grmi.NewConditionItem(meta.ArtCheckRule_SubObject, grmi.Equal, true),
		meta.ArtCheckRule_AttriCodeDesc.UrlParameterName:       grmi.NewConditionItem(meta.ArtCheckRule_AttriCodeDesc, grmi.Equal, false),
		meta.ArtCheckRule_CodeLength.UrlParameterName:          grmi.NewConditionItem(meta.ArtCheckRule_CodeLength, grmi.Equal, false),
		meta.ArtCheckRule_CheckStartPos.UrlParameterName:       grmi.NewConditionItem(meta.ArtCheckRule_CheckStartPos, grmi.Equal, false),
		meta.ArtCheckRule_CheckLength.UrlParameterName:         grmi.NewConditionItem(meta.ArtCheckRule_CheckLength, grmi.Equal, false),
		meta.ArtCheckRule_CheckObject.UrlParameterName:         grmi.NewConditionItem(meta.ArtCheckRule_CheckObject, grmi.Equal, false),
		meta.ArtCheckRule_OnlyBarCodeFlag.UrlParameterName:     grmi.NewConditionItem(meta.ArtCheckRule_OnlyBarCodeFlag, grmi.Equal, false),
		meta.ArtCheckRule_CodeType.UrlParameterName:            grmi.NewConditionItem(meta.ArtCheckRule_CodeType, grmi.Equal, false),
		meta.ArtCheckRule_CodeTypeCheckToggle.UrlParameterName: grmi.NewConditionItem(meta.ArtCheckRule_CodeTypeCheckToggle, grmi.Equal, false),
		meta.ArtCheckRule_CheckValue.UrlParameterName:          grmi.NewConditionItem(meta.ArtCheckRule_CheckValue, grmi.Equal, false),
		meta.ArtCheckRule_LastModify.UrlParameterName:          grmi.NewConditionItem(meta.ArtCheckRule_LastModify, grmi.Approximate, false),
		meta.ArtCheckRule_LastUser.UrlParameterName:            grmi.NewConditionItem(meta.ArtCheckRule_LastUser, grmi.Equal, false),
		meta.ArtCheckRule_CreateTime.UrlParameterName:          grmi.NewConditionItem(meta.ArtCheckRule_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArtCheckRule的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-24 11:19:07
 *
 ******************************************************************************/
var DefaultConditionOfArtCheckRuleAndPaging = grmi.NewCondition(
	DefaultConditionOfArtCheckRule.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArtCheckRuleServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArtCheckRule的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-24 11:19:07
 *
 ******************************************************************************/
type ArtCheckRuleServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArtCheckRuleDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArtCheckRuleDAOImplement实例
 *
 * @Return Value             : ArtCheckRuleDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-24 11:19:07
 *
 ******************************************************************************/
func NewArtCheckRuleServiceImplement() *ArtCheckRuleServiceImplement {
	return &ArtCheckRuleServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArtCheckRuleService.InsertOne
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) InsertOne(user *global.User, entity *model.ArtCheckRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtCheckRuleDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.AttriCode, entity.WorkLineId, entity.ProjectId, entity.SubObject)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	daoAttribute := dal.NewAttributeDAO(session, user.PlantNr, user.UserId)
	recordAttribute, err := daoAttribute.SelectOne(entity.AttriCode)
	if err != nil {
		return err
	}
	if recordAttribute == nil {
		return grmi.NewBusinessError("不存在指定零件属性!")
	}
	if entity.CodeLength == "" {
		entity.CodeLength = "可变"
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArtCheckRuleService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) DeleteOne(user *global.User, attriCode int, workLineId string, projectId string, subObject string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArtCheckRuleService.SelectOne
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) SelectOne(user *global.User, attriCode int, workLineId string, projectId string, subObject string) (*model.ArtCheckRule, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtCheckRuleDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(attriCode, workLineId, projectId, subObject)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArtCheckRuleService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) UpdateOne(user *global.User, entity *model.ArtCheckRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtCheckRuleDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.AttriCode, entity.WorkLineId, entity.ProjectId, entity.SubObject)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	daoAttribute := dal.NewAttributeDAO(session, user.PlantNr, user.UserId)
	if record.AttriCode != entity.AttriCode {
		recordAttribute, err := daoAttribute.SelectOne(entity.AttriCode)
		if err != nil {
			return err
		}
		if recordAttribute == nil {
			return grmi.NewBusinessError("不存在指定零件属性")
		}
	}
	if entity.CodeLength == "" {
		entity.CodeLength = "可变"
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArtCheckRuleService.Insert
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) Insert(user *global.User, entities *[]model.ArtCheckRule) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AttriCode, entity.WorkLineId, entity.ProjectId, entity.SubObject)
		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/ArtCheckRuleService.Delete
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) Delete(user *global.User, entities *[]model.ArtCheckRule) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AttriCode, entity.WorkLineId, entity.ProjectId, entity.SubObject)
		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/ArtCheckRuleService.Select
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.ArtCheckRule, error) {

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

	condition := DefaultConditionOfArtCheckRuleAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewArtCheckRuleDAO(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/base/ArtCheckRuleService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

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

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AttriCode, entity.WorkLineId, entity.ProjectId, entity.SubObject)
		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/ArtCheckRuleService.SelectByArt
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) SelectByArt(user *global.User, artId string, projectId string) ([]model.ArtCheckRule, error) {
	grmi.Log(user, "/services/base/implments/ArtCheckRule.service.impl.go", "SelectByArt", "通过物料ID查询匹配的校验规则")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtCheckRuleDAO(session, user.PlantNr, user.UserId)
	articleAtcodDao := dal.NewArticleAtcodLstDAO(session, user.PlantNr, user.UserId)
	articleAtcodLi, err := articleAtcodDao.Select([]grmi.Predicate{meta.ArticleAtcodLst_ArtId.NewPredicate(grmi.Equal, artId)}, []grmi.Field{meta.ArticleAtcodLst_AttrCode})
	if err != nil {
		return nil, grmi.NewBusinessError("查询物料关联属性失败, error:" + err.Error())
	}
	if len(articleAtcodLi) == 0 {
		return make([]model.ArtCheckRule, 0), nil
	}
	attrCodeMap := make(map[int]interface{})
	attrCodeLi := make([]int, 0)
	for _, articleAtcod := range articleAtcodLi {
		_, exist := attrCodeMap[articleAtcod.AttrCode]
		if !exist {
			attrCodeLi = append(attrCodeLi, articleAtcod.AttrCode)
			attrCodeMap[articleAtcod.AttrCode] = nil
		}
	}
	result, err := dao.SelectByArtId(artId, projectId, attrCodeLi)
	return result, err

}
