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

package implments

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

/******************************************************************************
 *
 * @Description              : Project的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-14 08:56:34
 *
 ******************************************************************************/
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_EnableToggle.UrlParameterName:           grmi.NewConditionItem(meta.Project_EnableToggle, grmi.Equal, false),
		meta.Project_PlantSiteCode.UrlParameterName:          grmi.NewConditionItem(meta.Project_PlantSiteCode, grmi.Equal, false),
		meta.Project_CustName1.UrlParameterName:              grmi.NewConditionItem(meta.Project_CustName1, grmi.Equal, false),
		meta.Project_CustName2.UrlParameterName:              grmi.NewConditionItem(meta.Project_CustName2, grmi.Equal, false),
		meta.Project_FileServerHost.UrlParameterName:         grmi.NewConditionItem(meta.Project_FileServerHost, grmi.Equal, false),
		meta.Project_FileFolder1.UrlParameterName:            grmi.NewConditionItem(meta.Project_FileFolder1, grmi.Equal, false),
		meta.Project_FileFolder2.UrlParameterName:            grmi.NewConditionItem(meta.Project_FileFolder2, grmi.Equal, false),
		meta.Project_FileFolder3.UrlParameterName:            grmi.NewConditionItem(meta.Project_FileFolder3, grmi.Equal, false),
		meta.Project_FileFolder4.UrlParameterName:            grmi.NewConditionItem(meta.Project_FileFolder4, grmi.Equal, false),
		meta.Project_FileFolder5.UrlParameterName:            grmi.NewConditionItem(meta.Project_FileFolder5, grmi.Equal, false),
		meta.Project_FileFolder6.UrlParameterName:            grmi.NewConditionItem(meta.Project_FileFolder6, grmi.Equal, false),
		meta.Project_File1NameRegexp.UrlParameterName:        grmi.NewConditionItem(meta.Project_File1NameRegexp, grmi.Equal, false),
		meta.Project_File2NameRegexp.UrlParameterName:        grmi.NewConditionItem(meta.Project_File2NameRegexp, grmi.Equal, false),
		meta.Project_File3NameRegexp.UrlParameterName:        grmi.NewConditionItem(meta.Project_File3NameRegexp, grmi.Equal, false),
		meta.Project_File4NameRegexp.UrlParameterName:        grmi.NewConditionItem(meta.Project_File4NameRegexp, grmi.Equal, false),
		meta.Project_File5NameRegexp.UrlParameterName:        grmi.NewConditionItem(meta.Project_File5NameRegexp, grmi.Equal, false),
		meta.Project_File6NameRegexp.UrlParameterName:        grmi.NewConditionItem(meta.Project_File6NameRegexp, grmi.Equal, false),
		meta.Project_OrderVerifyRuleToggle1.UrlParameterName: grmi.NewConditionItem(meta.Project_OrderVerifyRuleToggle1, grmi.Equal, false),
		meta.Project_OrderVerifyRuleToggle2.UrlParameterName: grmi.NewConditionItem(meta.Project_OrderVerifyRuleToggle2, grmi.Equal, false),
		meta.Project_OrderVerifyRuleToggle3.UrlParameterName: grmi.NewConditionItem(meta.Project_OrderVerifyRuleToggle3, grmi.Equal, false),
		meta.Project_OrderVerifyRuleToggle4.UrlParameterName: grmi.NewConditionItem(meta.Project_OrderVerifyRuleToggle4, grmi.Equal, false),
		meta.Project_OrderVerifyRuleToggle5.UrlParameterName: grmi.NewConditionItem(meta.Project_OrderVerifyRuleToggle5, grmi.Equal, false),
		meta.Project_OrderVerifyRuleToggle6.UrlParameterName: grmi.NewConditionItem(meta.Project_OrderVerifyRuleToggle6, grmi.Equal, false),
		meta.Project_OrderVerifyRule1.UrlParameterName:       grmi.NewConditionItem(meta.Project_OrderVerifyRule1, grmi.Equal, false),
		meta.Project_OrderVerifyRule2.UrlParameterName:       grmi.NewConditionItem(meta.Project_OrderVerifyRule2, grmi.Equal, false),
		meta.Project_OrderVerifyRule3.UrlParameterName:       grmi.NewConditionItem(meta.Project_OrderVerifyRule3, grmi.Equal, false),
		meta.Project_OrderVerifyRule4.UrlParameterName:       grmi.NewConditionItem(meta.Project_OrderVerifyRule4, grmi.Equal, false),
		meta.Project_OrderVerifyRule5.UrlParameterName:       grmi.NewConditionItem(meta.Project_OrderVerifyRule5, grmi.Equal, false),
		meta.Project_OrderVerifyRule6.UrlParameterName:       grmi.NewConditionItem(meta.Project_OrderVerifyRule6, 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_ErrorRecievers.UrlParameterName:         grmi.NewConditionItem(meta.Project_ErrorRecievers, grmi.Equal, false),
		meta.Project_ErrorCcRecievers.UrlParameterName:       grmi.NewConditionItem(meta.Project_ErrorCcRecievers, grmi.Equal, false),
		meta.Project_LogUrl.UrlParameterName:                 grmi.NewConditionItem(meta.Project_LogUrl, grmi.Equal, false),
		meta.Project_LogTable.UrlParameterName:               grmi.NewConditionItem(meta.Project_LogTable, 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_SerialOrderSnr.UrlParameterName:         grmi.NewConditionItem(meta.Project_SerialOrderSnr, grmi.Equal, false),
		meta.Project_ManualOrderSnr.UrlParameterName:         grmi.NewConditionItem(meta.Project_ManualOrderSnr, grmi.Equal, false),
		meta.Project_OrderMsgSnr.UrlParameterName:            grmi.NewConditionItem(meta.Project_OrderMsgSnr, grmi.Equal, false),
		meta.Project_ReworkOrderSnr.UrlParameterName:         grmi.NewConditionItem(meta.Project_ReworkOrderSnr, grmi.Equal, false),
		meta.Project_PackOrderSnr.UrlParameterName:           grmi.NewConditionItem(meta.Project_PackOrderSnr, grmi.Equal, false),
		meta.Project_PackOrderSyncSnr.UrlParameterName:       grmi.NewConditionItem(meta.Project_PackOrderSyncSnr, grmi.Equal, false),
		meta.Project_ShipOrderSnr.UrlParameterName:           grmi.NewConditionItem(meta.Project_ShipOrderSnr, grmi.Equal, false),
		meta.Project_ShipFileSnr.UrlParameterName:            grmi.NewConditionItem(meta.Project_ShipFileSnr, grmi.Equal, false),
		meta.Project_AsnFileSnr.UrlParameterName:             grmi.NewConditionItem(meta.Project_AsnFileSnr, grmi.Equal, 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-04-14 08:56:34
 *
 ******************************************************************************/
var DefaultConditionOfProjectAndPaging = grmi.NewCondition(
	DefaultConditionOfProject.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ProjectServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Project的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-14 08:56:34
 *
 ******************************************************************************/
type ProjectServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewProjectDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProjectDAOImplement实例
 *
 * @Return Value             : ProjectDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-14 08:56:34
 *
 ******************************************************************************/
func NewProjectServiceImplement() *ProjectServiceImplement {
	return &ProjectServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/me/ProjectService.InsertOne
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) InsertOne(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/me/ProjectService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) DeleteOne(user *models.Usertab, 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.Pid, user.Userid)
	record, err := dao.SelectOne(projectId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(projectId)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/me/ProjectService.SelectOne
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) SelectOne(user *models.Usertab, 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.Pid, user.Userid)
	result, err := dao.SelectOne(projectId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/me/ProjectService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) UpdateOne(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/me/ProjectService.Insert
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) Insert(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/me/ProjectService.Delete
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) Delete(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/me/ProjectService.Select
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) Select(user *models.Usertab, 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.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/me/ProjectService.Update
 *
 ******************************************************************************/
func (impl *ProjectServiceImplement) Update(user *models.Usertab, 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.Pid, 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
}
