// 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              : ArtQtyAllocate的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-24 14:18:42
 *
 ******************************************************************************/
var DefaultConditionOfArtQtyAllocate = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArtQtyAllocate_AttriCode.UrlParameterName:     grmi.NewConditionItem(meta.ArtQtyAllocate_AttriCode, grmi.Equal, true),
		meta.ArtQtyAllocate_WorkLineId.UrlParameterName:    grmi.NewConditionItem(meta.ArtQtyAllocate_WorkLineId, grmi.Equal, true),
		meta.ArtQtyAllocate_SubObject.UrlParameterName:     grmi.NewConditionItem(meta.ArtQtyAllocate_SubObject, grmi.Equal, true),
		meta.ArtQtyAllocate_ProjectId.UrlParameterName:     grmi.NewConditionItem(meta.ArtQtyAllocate_ProjectId, grmi.Equal, true),
		meta.ArtQtyAllocate_TotalPlanty.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_TotalPlanty, grmi.Equal, true),
		meta.ArtQtyAllocate_PO.UrlParameterName:            grmi.NewConditionItem(meta.ArtQtyAllocate_PO, grmi.Equal, true),
		meta.ArtQtyAllocate_AssignPlanty.UrlParameterName:  grmi.NewConditionItem(meta.ArtQtyAllocate_AssignPlanty, grmi.Equal, true),
		meta.ArtQtyAllocate_AttriCodeDesc.UrlParameterName: grmi.NewConditionItem(meta.ArtQtyAllocate_AttriCodeDesc, grmi.Equal, false),
		meta.ArtQtyAllocate_AQCtrlPara1.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_AQCtrlPara1, grmi.Equal, false),
		meta.ArtQtyAllocate_AQCtrlPara2.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_AQCtrlPara2, grmi.Equal, false),
		meta.ArtQtyAllocate_AQCtrlPara3.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_AQCtrlPara3, grmi.Equal, false),
		meta.ArtQtyAllocate_AQCtrlPara4.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_AQCtrlPara4, grmi.Equal, false),
		meta.ArtQtyAllocate_AQCtrlPara5.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_AQCtrlPara5, grmi.Equal, false),
		meta.ArtQtyAllocate_AQCtrlPara6.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_AQCtrlPara6, grmi.Equal, false),
		meta.ArtQtyAllocate_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.ArtQtyAllocate_LastModify, grmi.Approximate, false),
		meta.ArtQtyAllocate_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.ArtQtyAllocate_LastUser, grmi.Equal, false),
		meta.ArtQtyAllocate_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.ArtQtyAllocate_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArtQtyAllocate的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-24 14:18:42
 *
 ******************************************************************************/
var DefaultConditionOfArtQtyAllocateAndPaging = grmi.NewCondition(
	DefaultConditionOfArtQtyAllocate.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArtQtyAllocateServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArtQtyAllocate的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-24 14:18:42
 *
 ******************************************************************************/
type ArtQtyAllocateServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArtQtyAllocateDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArtQtyAllocateDAOImplement实例
 *
 * @Return Value             : ArtQtyAllocateDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-24 14:18:42
 *
 ******************************************************************************/
func NewArtQtyAllocateServiceImplement() *ArtQtyAllocateServiceImplement {
	return &ArtQtyAllocateServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArtQtyAllocateService.InsertOne
 *
 ******************************************************************************/
func (impl *ArtQtyAllocateServiceImplement) InsertOne(user *global.User, entity *model.ArtQtyAllocate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtQtyAllocateDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.AttriCode, entity.WorkLineId, entity.SubObject, entity.ProjectId, entity.TotalPlanty, entity.PO, entity.AssignPlanty)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArtQtyAllocateService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArtQtyAllocateServiceImplement) DeleteOne(user *global.User, attriCode int, workLineId string, subObject string, projectId string, totalPlanty int, pO string, assignPlanty int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArtQtyAllocateService.SelectOne
 *
 ******************************************************************************/
func (impl *ArtQtyAllocateServiceImplement) SelectOne(user *global.User, attriCode int, workLineId string, subObject string, projectId string, totalPlanty int, pO string, assignPlanty int) (*model.ArtQtyAllocate, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArtQtyAllocateService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArtQtyAllocateServiceImplement) UpdateOne(user *global.User, entity *model.ArtQtyAllocate) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ArtQtyAllocateService.Insert
 *
 ******************************************************************************/
func (impl *ArtQtyAllocateServiceImplement) Insert(user *global.User, entities *[]model.ArtQtyAllocate) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtQtyAllocateDAO(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.ArtQtyAllocate, 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

}