// 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/web/models"
)

/******************************************************************************
 *
 * @Description              : Process的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
var DefaultConditionOfProcess = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Process_PO.UrlParameterName:          grmi.NewConditionItem(meta.Process_PO, grmi.Equal, true),
		meta.Process_WorkLineId.UrlParameterName:  grmi.NewConditionItem(meta.Process_WorkLineId, grmi.Equal, true),
		meta.Process_Name.UrlParameterName:        grmi.NewConditionItem(meta.Process_Name, grmi.Equal, false),
		meta.Process_Descr.UrlParameterName:       grmi.NewConditionItem(meta.Process_Descr, grmi.Equal, false),
		meta.Process_PrePO.UrlParameterName:       grmi.NewConditionItem(meta.Process_PrePO, grmi.Equal, false),
		meta.Process_NextPO.UrlParameterName:      grmi.NewConditionItem(meta.Process_NextPO, grmi.Equal, false),
		meta.Process_IsFirstPO.UrlParameterName:   grmi.NewConditionItem(meta.Process_IsFirstPO, grmi.Equal, false),
		meta.Process_IsLastPO.UrlParameterName:    grmi.NewConditionItem(meta.Process_IsLastPO, grmi.Equal, false),
		meta.Process_HasDependOn.UrlParameterName: grmi.NewConditionItem(meta.Process_HasDependOn, grmi.Equal, false),
		meta.Process_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.Process_LastModify, grmi.Approximate, false),
		meta.Process_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.Process_LastUser, grmi.Equal, false),
		meta.Process_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.Process_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : Process的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 13:55:54
 *
 ******************************************************************************/
var DefaultConditionOfProcessAndPaging = grmi.NewCondition(
	DefaultConditionOfProcess.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ProcessServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Process的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 13:55:54
 *
 ******************************************************************************/
type ProcessServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewProcessDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProcessDAOImplement实例
 *
 * @Return Value             : ProcessDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 13:55:54
 *
 ******************************************************************************/
func NewProcessServiceImplement() *ProcessServiceImplement {
	return &ProcessServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ProcessService.InsertOne
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) InsertOne(user *models.Usertab, entity *model.Process) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessDAO(session, user.Pid, user.Userid)
	workLineDao := dal.NewWorkLineDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PO, entity.WorkLineId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	workLine, err := workLineDao.SelectOne(entity.WorkLineId)
	if err != nil {
		return err
	}
	if workLine == nil {
		return grmi.NewBusinessError("不存在对应的产线")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ProcessService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) DeleteOne(user *models.Usertab, pO int, workLineId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewProcessDAO(session, user.Pid, user.Userid)
	poRelDao := dal.NewProcessWorkPlaceLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(pO, workLineId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = poRelDao.DeleteWhere([]grmi.Predicate{meta.ProcessWorkPlaceLst_PO.NewPredicate(grmi.Equal, pO), meta.ProcessWorkPlaceLst_WorkLineId.NewPredicate(grmi.Equal, workLineId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.DeleteOne(pO, workLineId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ProcessService.SelectOne
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) SelectOne(user *models.Usertab, pO int, workLineId string) (*model.Process, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(pO, workLineId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ProcessService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) UpdateOne(user *models.Usertab, entity *model.Process) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PO, entity.WorkLineId)
	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/base/ProcessService.Insert
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) Insert(user *models.Usertab, entities *[]model.Process) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.PO, entity.WorkLineId)
		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/ProcessService.Delete
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) Delete(user *models.Usertab, entities *[]model.Process) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.PO, entity.WorkLineId)
		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/ProcessService.Select
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfProcessAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewProcessDAO(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/ProcessService.Update
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) Update(user *models.Usertab, entities *[]model.Process) error {

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

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

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

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