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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/base"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/base"
	model "LAPP_LF_MOM_BACKEND/models/base"
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : Article的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-06 15:52:37
 *
 ******************************************************************************/
var DefaultConditionOfArticle = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Article_ArtId.UrlParameterName:                grmi.NewConditionItem(meta.Article_ArtId, grmi.Equal, true),
		meta.Article_Descr1.UrlParameterName:               grmi.NewConditionItem(meta.Article_Descr1, grmi.Equal, false),
		meta.Article_Descr2.UrlParameterName:               grmi.NewConditionItem(meta.Article_Descr2, grmi.Equal, false),
		meta.Article_ArtTypeId.UrlParameterName:            grmi.NewConditionItem(meta.Article_ArtTypeId, grmi.Equal, false),
		meta.Article_ProjectId.UrlParameterName:            grmi.NewConditionItem(meta.Article_ProjectId, grmi.Equal, false),
		meta.Article_CustArtId.UrlParameterName:            grmi.NewConditionItem(meta.Article_CustArtId, grmi.Equal, false),
		meta.Article_AlternativeArtId.UrlParameterName:     grmi.NewConditionItem(meta.Article_AlternativeArtId, grmi.Equal, false),
		meta.Article_PlanResourceGroupId.UrlParameterName:  grmi.NewConditionItem(meta.Article_PlanResourceGroupId, grmi.Equal, false),
		meta.Article_PlanResourceId.UrlParameterName:       grmi.NewConditionItem(meta.Article_PlanResourceId, grmi.Equal, false),
		meta.Article_UsedResourceId.UrlParameterName:       grmi.NewConditionItem(meta.Article_UsedResourceId, grmi.Equal, false),
		meta.Article_UomId.UrlParameterName:                grmi.NewConditionItem(meta.Article_UomId, grmi.Equal, false),
		meta.Article_PlanScrapRate.UrlParameterName:        grmi.NewConditionItem(meta.Article_PlanScrapRate, grmi.Equal, false),
		meta.Article_RatePerHourToggle.UrlParameterName:    grmi.NewConditionItem(meta.Article_RatePerHourToggle, grmi.Equal, false),
		meta.Article_TimePerItemToggle.UrlParameterName:    grmi.NewConditionItem(meta.Article_TimePerItemToggle, grmi.Equal, false),
		meta.Article_TimePerBatchToggle.UrlParameterName:   grmi.NewConditionItem(meta.Article_TimePerBatchToggle, grmi.Equal, false),
		meta.Article_BatchTimeFieldToggle.UrlParameterName: grmi.NewConditionItem(meta.Article_BatchTimeFieldToggle, grmi.Equal, false),
		meta.Article_OpTimePerItem.UrlParameterName:        grmi.NewConditionItem(meta.Article_OpTimePerItem, grmi.Equal, false),
		meta.Article_BatchTime.UrlParameterName:            grmi.NewConditionItem(meta.Article_BatchTime, grmi.Equal, false),
		meta.Article_QuantityPerHour.UrlParameterName:      grmi.NewConditionItem(meta.Article_QuantityPerHour, grmi.Equal, false),
		meta.Article_MidBatchQuantity.UrlParameterName:     grmi.NewConditionItem(meta.Article_MidBatchQuantity, grmi.Equal, false),
		meta.Article_MidBatchTime.UrlParameterName:         grmi.NewConditionItem(meta.Article_MidBatchTime, grmi.Approximate, false),
		meta.Article_EffectiveOpTime.UrlParameterName:      grmi.NewConditionItem(meta.Article_EffectiveOpTime, grmi.Equal, false),
		meta.Article_BatchingMethod.UrlParameterName:       grmi.NewConditionItem(meta.Article_BatchingMethod, grmi.Equal, false),
		meta.Article_DeliveryBuffer.UrlParameterName:       grmi.NewConditionItem(meta.Article_DeliveryBuffer, grmi.Equal, false),
		meta.Article_Icon.UrlParameterName:                 grmi.NewConditionItem(meta.Article_Icon, grmi.Equal, false),
		meta.Article_DisplayColor.UrlParameterName:         grmi.NewConditionItem(meta.Article_DisplayColor, grmi.Equal, false),
		meta.Article_Document.UrlParameterName:             grmi.NewConditionItem(meta.Article_Document, grmi.Equal, false),
		meta.Article_DefaultRoute.UrlParameterName:         grmi.NewConditionItem(meta.Article_DefaultRoute, grmi.Equal, false),
		meta.Article_DefaultInspectPlanId.UrlParameterName: grmi.NewConditionItem(meta.Article_DefaultInspectPlanId, grmi.Equal, false),
		meta.Article_LotSize.UrlParameterName:              grmi.NewConditionItem(meta.Article_LotSize, grmi.Equal, false),
		meta.Article_ABCClass.UrlParameterName:             grmi.NewConditionItem(meta.Article_ABCClass, grmi.Equal, false),
		meta.Article_ArtSize1.UrlParameterName:             grmi.NewConditionItem(meta.Article_ArtSize1, grmi.Equal, false),
		meta.Article_ArtSize2.UrlParameterName:             grmi.NewConditionItem(meta.Article_ArtSize2, grmi.Equal, false),
		meta.Article_ArtSize3.UrlParameterName:             grmi.NewConditionItem(meta.Article_ArtSize3, grmi.Equal, false),
		meta.Article_ArtSize4.UrlParameterName:             grmi.NewConditionItem(meta.Article_ArtSize4, grmi.Equal, false),
		meta.Article_ArtSize5.UrlParameterName:             grmi.NewConditionItem(meta.Article_ArtSize5, grmi.Equal, false),
		meta.Article_ArtSpec1.UrlParameterName:             grmi.NewConditionItem(meta.Article_ArtSpec1, grmi.Equal, false),
		meta.Article_ArtSpec2.UrlParameterName:             grmi.NewConditionItem(meta.Article_ArtSpec2, grmi.Equal, false),
		meta.Article_ArtSpec3.UrlParameterName:             grmi.NewConditionItem(meta.Article_ArtSpec3, grmi.Equal, false),
		meta.Article_ArtSpec4.UrlParameterName:             grmi.NewConditionItem(meta.Article_ArtSpec4, grmi.Equal, false),
		meta.Article_ArtSpec5.UrlParameterName:             grmi.NewConditionItem(meta.Article_ArtSpec5, grmi.Equal, false),
		meta.Article_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.Article_LastModify, grmi.Approximate, false),
		meta.Article_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.Article_LastUser, grmi.Equal, false),
		meta.Article_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.Article_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : Article的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-06 15:52:37
 *
 ******************************************************************************/
var DefaultConditionOfArticleAndPaging = grmi.NewCondition(
	DefaultConditionOfArticle.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArticleServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Article的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-06 15:52:37
 *
 ******************************************************************************/
type ArticleServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArticleDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArticleDAOImplement实例
 *
 * @Return Value             : ArticleDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-06 15:52:37
 *
 ******************************************************************************/
func NewArticleServiceImplement() *ArticleServiceImplement {
	return &ArticleServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleService.InsertOne
 *
 ******************************************************************************/
func (impl *ArticleServiceImplement) InsertOne(user *models.Usertab, entity *model.Article) error {

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

	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.NewArticleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ArtId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}

	/**处理数据**/
	AtcodLst := make([]model.ArticleAtcodLst, 0)
	for _, v := range entity.Valst {
		onedata := model.ArticleAtcodLst{}
		onedata.ArtId = entity.ArtId
		onedata.ProjectId = entity.ProjectId
		onedata.Version = 1
		onedata.AttrCode = v.AttrCode
		onedata.AttrValue = v.AttrValue
		AtcodLst = append(AtcodLst, onedata)
	}
	lstdao := dal.NewArticleAtcodLstDAO(session, user.Pid, user.Userid)
	err = lstdao.Insert(&AtcodLst)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()

	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArticleServiceImplement) DeleteOne(user *models.Usertab, artId string) error {

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

	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.NewArticleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(artId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(artId)
	if err != nil {
		session.Rollback()
		return err
	}
	lstdao := dal.NewArticleAtcodLstDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.ArticleAtcodLst_ArtId.NewPredicate(grmi.Equal, artId),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleService.SelectOne
 *
 ******************************************************************************/
func (impl *ArticleServiceImplement) SelectOne(user *models.Usertab, artId string) (*model.Article, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticleDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(artId)
	if err != nil {
		return nil, err
	}
	lstdao := dal.NewArticleAtcodLstDAO(session, user.Pid, user.Userid)
	list, err := lstdao.SelectAllot(artId)
	if err != nil {
		return nil, err
	}
	if utils.ValueIsEmpty(result) {
		return nil, err
	}
	//获取已分配的属性集合
	var attrCodes []int
	for _, v := range list {
		attrCodes = append(attrCodes, v.AttrCode)
	}
	unlist, err := lstdao.SelectUnAllot(attrCodes)
	if err != nil {
		return nil, err
	}
	//查询已分配
	result.Valst = list
	//查询未分配
	result.UnValst = unlist
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArticleServiceImplement) UpdateOne(user *models.Usertab, entity *model.Article) error {

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

	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.NewArticleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ArtId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}

	/**处理数据**/
	AtcodLst := make([]model.ArticleAtcodLst, 0)
	for _, v := range entity.Valst {
		onedata := model.ArticleAtcodLst{}
		onedata.ArtId = entity.ArtId
		onedata.ProjectId = entity.ProjectId
		onedata.Version = 1
		onedata.AttrCode = v.AttrCode
		onedata.AttrValue = v.AttrValue
		AtcodLst = append(AtcodLst, onedata)
	}
	lstdao := dal.NewArticleAtcodLstDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.ArticleAtcodLst_ArtId.NewPredicate(grmi.Equal, entity.ArtId),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	err = lstdao.Insert(&AtcodLst)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()

	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleService.Insert
 *
 ******************************************************************************/
func (impl *ArticleServiceImplement) Insert(user *models.Usertab, entities *[]model.Article) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticleDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ArtId)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleService.Delete
 *
 ******************************************************************************/
func (impl *ArticleServiceImplement) Delete(user *models.Usertab, entities *[]model.Article) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticleDAO(session, user.Pid, user.Userid)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArticleService.Select
 *
 ******************************************************************************/
func (impl *ArticleServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfArticleAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewArticleDAO(session, user.Pid, 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_LF_MOM_BACKEND/services/base/ArticleService.Update
 *
 ******************************************************************************/
func (impl *ArticleServiceImplement) Update(user *models.Usertab, entities *[]model.Article) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArticleDAO(session, user.Pid, user.Userid)

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

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