// 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/me"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	baseMeta "LAPP_ACURA_MOM_BACKEND/meta/base"
	meta "LAPP_ACURA_MOM_BACKEND/meta/me"
	model "LAPP_ACURA_MOM_BACKEND/models/me"
)

/******************************************************************************
 *
 * @Description              : Project的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 11:07:11
 *
 ******************************************************************************/
var DefaultConditionOfProject = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Project_ProjectId.UrlParameterName:                        grmi.NewConditionItem(meta.Project_ProjectId, grmi.Equal, true),
		meta.Project_Descr.UrlParameterName:                            grmi.NewConditionItem(meta.Project_Descr, grmi.Equal, false),
		meta.Project_CustomerId.UrlParameterName:                       grmi.NewConditionItem(meta.Project_CustomerId, grmi.Equal, false),
		meta.Project_PlantSiteCode.UrlParameterName:                    grmi.NewConditionItem(meta.Project_PlantSiteCode, grmi.Equal, false),
		meta.Project_EdiType.UrlParameterName:                          grmi.NewConditionItem(meta.Project_EdiType, grmi.Equal, false),
		meta.Project_EnabledToggle.UrlParameterName:                    grmi.NewConditionItem(meta.Project_EnabledToggle, grmi.Equal, false),
		meta.Project_EmailServerHost.UrlParameterName:                  grmi.NewConditionItem(meta.Project_EmailServerHost, grmi.Equal, false),
		meta.Project_EmailServerPort.UrlParameterName:                  grmi.NewConditionItem(meta.Project_EmailServerPort, grmi.Equal, false),
		meta.Project_FromEmail.UrlParameterName:                        grmi.NewConditionItem(meta.Project_FromEmail, grmi.Equal, false),
		meta.Project_FromPwd.UrlParameterName:                          grmi.NewConditionItem(meta.Project_FromPwd, grmi.Equal, false),
		meta.Project_CustOrderSnr.UrlParameterName:                     grmi.NewConditionItem(meta.Project_CustOrderSnr, grmi.Equal, false),
		meta.Project_WorkOrderSnr.UrlParameterName:                     grmi.NewConditionItem(meta.Project_WorkOrderSnr, grmi.Equal, false),
		meta.Project_ManualWorkOrderSnr.UrlParameterName:               grmi.NewConditionItem(meta.Project_ManualWorkOrderSnr, grmi.Equal, false),
		meta.Project_ReorderSnr.UrlParameterName:                       grmi.NewConditionItem(meta.Project_ReorderSnr, grmi.Equal, false),
		meta.Project_PackOrderSnr.UrlParameterName:                     grmi.NewConditionItem(meta.Project_PackOrderSnr, grmi.Equal, false),
		meta.Project_PackSyncSnr.UrlParameterName:                      grmi.NewConditionItem(meta.Project_PackSyncSnr, grmi.Equal, false),
		meta.Project_ShipOrderSnr.UrlParameterName:                     grmi.NewConditionItem(meta.Project_ShipOrderSnr, grmi.Equal, false),
		meta.Project_ShipCarSnr.UrlParameterName:                       grmi.NewConditionItem(meta.Project_ShipCarSnr, grmi.Equal, false),
		meta.Project_ShipFileSnr.UrlParameterName:                      grmi.NewConditionItem(meta.Project_ShipFileSnr, grmi.Equal, false),
		meta.Project_ShipFileFolder.UrlParameterName:                   grmi.NewConditionItem(meta.Project_ShipFileFolder, grmi.Equal, false),
		meta.Project_AsnSnr.UrlParameterName:                           grmi.NewConditionItem(meta.Project_AsnSnr, grmi.Equal, false),
		meta.Project_AsnFileType.UrlParameterName:                      grmi.NewConditionItem(meta.Project_AsnFileType, grmi.Equal, false),
		meta.Project_AsnFolder.UrlParameterName:                        grmi.NewConditionItem(meta.Project_AsnFolder, grmi.Equal, false),
		meta.Project_BackFlushObject.UrlParameterName:                  grmi.NewConditionItem(meta.Project_BackFlushObject, grmi.Equal, false),
		meta.Project_BackFlushMode.UrlParameterName:                    grmi.NewConditionItem(meta.Project_BackFlushMode, grmi.Equal, false),
		meta.Project_BackFlushRule.UrlParameterName:                    grmi.NewConditionItem(meta.Project_BackFlushRule, grmi.Equal, false),
		meta.Project_BackFlushFolder.UrlParameterName:                  grmi.NewConditionItem(meta.Project_BackFlushFolder, grmi.Equal, false),
		meta.Project_SerialOrderSnr.UrlParameterName:                   grmi.NewConditionItem(meta.Project_SerialOrderSnr, grmi.Equal, false),
		meta.Project_INTSerialOrderSnr.UrlParameterName:                grmi.NewConditionItem(meta.Project_INTSerialOrderSnr, grmi.Equal, false),
		meta.Project_PackOrderRFIDSnr.UrlParameterName:                 grmi.NewConditionItem(meta.Project_PackOrderRFIDSnr, grmi.Equal, false),
		meta.Project_PackOrderOutLineRFIDSnr.UrlParameterName:          grmi.NewConditionItem(meta.Project_PackOrderOutLineRFIDSnr, grmi.Equal, false),
		meta.Project_PreOrderSnr.UrlParameterName:                      grmi.NewConditionItem(meta.Project_PreOrderSnr, grmi.Equal, false),
		meta.Project_FileServerHost.UrlParameterName:                   grmi.NewConditionItem(meta.Project_FileServerHost, grmi.Equal, false),
		meta.Project_CalloffFolder.UrlParameterName:                    grmi.NewConditionItem(meta.Project_CalloffFolder, grmi.Equal, false),
		meta.Project_CalloffFileNameRegexp.UrlParameterName:            grmi.NewConditionItem(meta.Project_CalloffFileNameRegexp, grmi.Equal, false),
		meta.Project_CalloffVerifyCheckSequenceToggle.UrlParameterName: grmi.NewConditionItem(meta.Project_CalloffVerifyCheckSequenceToggle, grmi.Equal, false),
		meta.Project_CalloffVerifyProductFamilyToggle.UrlParameterName: grmi.NewConditionItem(meta.Project_CalloffVerifyProductFamilyToggle, grmi.Equal, false),
		meta.Project_PlcAddress.UrlParameterName:                       grmi.NewConditionItem(meta.Project_PlcAddress, grmi.Equal, false),
		meta.Project_CtrlPara1.UrlParameterName:                        grmi.NewConditionItem(meta.Project_CtrlPara1, grmi.Equal, false),
		meta.Project_CtrlPara2.UrlParameterName:                        grmi.NewConditionItem(meta.Project_CtrlPara2, grmi.Equal, false),
		meta.Project_CtrlStr1.UrlParameterName:                         grmi.NewConditionItem(meta.Project_CtrlStr1, grmi.Equal, false),
		meta.Project_CtrlStr2.UrlParameterName:                         grmi.NewConditionItem(meta.Project_CtrlStr2, grmi.Equal, false),
		meta.Project_CtrlTime1.UrlParameterName:                        grmi.NewConditionItem(meta.Project_CtrlTime1, grmi.Approximate, false),
		meta.Project_CtrlTime2.UrlParameterName:                        grmi.NewConditionItem(meta.Project_CtrlTime2, grmi.Approximate, false),
		meta.Project_LastModify.UrlParameterName:                       grmi.NewConditionItem(meta.Project_LastModify, grmi.Approximate, false),
		meta.Project_LastUser.UrlParameterName:                         grmi.NewConditionItem(meta.Project_LastUser, grmi.Equal, false),
		meta.Project_CreateTime.UrlParameterName:                       grmi.NewConditionItem(meta.Project_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

/******************************************************************************
 *
 * @Function Name            : NewProjectDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProjectDAOImplement实例
 *
 * @Return Value             : ProjectDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 11:07:11
 *
 ******************************************************************************/
func NewProjectServiceImplement() *ProjectServiceImplement {
	return &ProjectServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/ProjectService.InsertOne
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) InsertOne(user *global.User, entity *model.Project) error {

	grmi.Log(user, "/services/me/implments/Project.service.impl.go", "InsertOneProject", "插入一个Project")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProjectDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ProjectId)
	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/me/ProjectService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) DeleteOne(user *global.User, projectId string) error {

	grmi.Log(user, "/services/me/implments/Project.service.impl.go", "DeleteOneProject", "删除一个Project")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProjectDAO(session, user.PlantNr, user.UserId)
	productFamilyDao := dal.NewProductFamilyDAO(session, user.PlantNr, user.UserId)
	productDao := dal.NewProductDAO(session, user.PlantNr, user.UserId)
	productFamilyRelateDao := dal.NewProductFamilyRelateDAO(session, user.PlantNr, user.UserId)
	articleDao := baseDal.NewArticleDAO(session, user.PlantNr, user.UserId)
	articleAtcodDao := baseDal.NewArticleAtcodLstDAO(session, user.PlantNr, user.UserId)
	artCheckRuleDao := baseDal.NewArtCheckRuleDAO(session, user.PlantNr, user.UserId)
	artQtyAllocateDao := baseDal.NewArtQtyAllocateDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(projectId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	if err = session.Begin(); err != nil {
		return err
	}
	err = productFamilyDao.DeleteWhere([]grmi.Predicate{meta.ProductFamily_ProjectId.NewPredicate(grmi.Equal, projectId)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除派生数据失败, error:" + err.Error())
	}
	err = productFamilyRelateDao.DeleteWhere([]grmi.Predicate{meta.ProductFamilyRelate_ProjectId.NewPredicate(grmi.Equal, projectId)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除派生总成关联数据失败, error:" + err.Error())
	}
	err = productDao.DeleteWhere([]grmi.Predicate{meta.Product_ProjectId.NewPredicate(grmi.Equal, projectId)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除总成数据失败, error:" + err.Error())
	}
	err = articleDao.DeleteWhere([]grmi.Predicate{baseMeta.Article_ProjectId.NewPredicate(grmi.Equal, projectId)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除物料数据失败, error:" + err.Error())
	}
	err = articleAtcodDao.DeleteWhere([]grmi.Predicate{baseMeta.ArticleAtcodLst_ProjectId.NewPredicate(grmi.Equal, projectId)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除物料属性分配数据失败, error:" + err.Error())
	}
	err = artCheckRuleDao.DeleteWhere([]grmi.Predicate{baseMeta.ArtCheckRule_ProjectId.NewPredicate(grmi.Equal, projectId)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除零件校验规则数据失败, error:" + err.Error())
	}
	err = artQtyAllocateDao.DeleteWhere([]grmi.Predicate{baseMeta.ArtQtyAllocate_ProjectId.NewPredicate(grmi.Equal, projectId)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除零件数量分配规则数据失败, error:" + err.Error())
	}
	err = dao.DeleteOne(projectId)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除项目数据失败, error:" + err.Error())
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/ProjectService.SelectOne
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) SelectOne(user *global.User, projectId string) (*model.Project, error) {

	grmi.Log(user, "/services/me/implments/Project.service.impl.go", "SelectOneProject", "查询一个Project")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/ProjectService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) UpdateOne(user *global.User, entity *model.Project) error {

	grmi.Log(user, "/services/me/implments/Project.service.impl.go", "UpdateOneProject", "修改一个Project")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProjectDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ProjectId)
	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/me/ProjectService.Insert
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) Insert(user *global.User, entities *[]model.Project) error {

	grmi.Log(user, "/services/me/implments/Project.service.impl.go", "InsertProject", "插入多个Project")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ProjectId)
		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/me/ProjectService.Delete
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) Delete(user *global.User, entities *[]model.Project) error {

	grmi.Log(user, "/services/me/implments/Project.service.impl.go", "DeleteProject", "删除多个Project")

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

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

	grmi.Log(user, "/services/me/implments/Project.service.impl.go", "SelectProject", "查询Project")

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

	grmi.Log(user, "/services/me/implments/Project.service.impl.go", "SelectProject", "查询Project")

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

	grmi.Log(user, "/services/me/implments/Project.service.impl.go", "UpdateProject", "修改多个Project")

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

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

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