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

package implments

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

/******************************************************************************
 *
 * @Description              : WorkOrder的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrder = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkOrder_MaintWorkOrderId.UrlParameterName: grmi.NewConditionItem(meta.WorkOrder_MaintWorkOrderId, grmi.Equal, true),
		meta.WorkOrder_MainServiceNr.UrlParameterName:    grmi.NewConditionItem(meta.WorkOrder_MainServiceNr, grmi.Equal, false),
		meta.WorkOrder_Descr.UrlParameterName:            grmi.NewConditionItem(meta.WorkOrder_Descr, grmi.Equal, false),
		meta.WorkOrder_Status.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrder_Status, grmi.Equal, false),
		meta.WorkOrder_Priority.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrder_Priority, grmi.Equal, false),
		meta.WorkOrder_AssetNr.UrlParameterName:          grmi.NewConditionItem(meta.WorkOrder_AssetNr, grmi.Equal, false),
		meta.WorkOrder_PlBegDat.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrder_PlBegDat, grmi.Approximate, false),
		meta.WorkOrder_PlEndDat.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrder_PlEndDat, grmi.Approximate, false),
		meta.WorkOrder_NeedSchedule.UrlParameterName:     grmi.NewConditionItem(meta.WorkOrder_NeedSchedule, grmi.Equal, false),
		meta.WorkOrder_MainTimeLen.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrder_MainTimeLen, grmi.Equal, false),
		meta.WorkOrder_TimeUnit.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrder_TimeUnit, grmi.Equal, false),
		meta.WorkOrder_SchedBegTime.UrlParameterName:     grmi.NewConditionItem(meta.WorkOrder_SchedBegTime, grmi.Approximate, false),
		meta.WorkOrder_SchedEndTime.UrlParameterName:     grmi.NewConditionItem(meta.WorkOrder_SchedEndTime, grmi.Approximate, false),
		meta.WorkOrder_ActBegTime.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrder_ActBegTime, grmi.Approximate, false),
		meta.WorkOrder_ActEndTime.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrder_ActEndTime, grmi.Approximate, false),
		meta.WorkOrder_AssignedTo.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrder_AssignedTo, grmi.Equal, false),
		meta.WorkOrder_Operator.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrder_Operator, grmi.Equal, false),
		meta.WorkOrder_Vendor.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrder_Vendor, grmi.Equal, false),
		meta.WorkOrder_MaintType.UrlParameterName:        grmi.NewConditionItem(meta.WorkOrder_MaintType, grmi.Equal, false),
		meta.WorkOrder_CostCenterId.UrlParameterName:     grmi.NewConditionItem(meta.WorkOrder_CostCenterId, grmi.Equal, false),
		meta.WorkOrder_PoId.UrlParameterName:             grmi.NewConditionItem(meta.WorkOrder_PoId, grmi.Equal, false),
		meta.WorkOrder_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrder_LastModify, grmi.Approximate, false),
		meta.WorkOrder_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrder_LastUser, grmi.Equal, false),
		meta.WorkOrder_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrder_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkOrder的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrderAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkOrder.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkOrderServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkOrder的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
type WorkOrderServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkOrderDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkOrderDAOImplement实例
 *
 * @Return Value             : WorkOrderDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
func NewWorkOrderServiceImplement() *WorkOrderServiceImplement {
	return &WorkOrderServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/WorkOrderService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkOrder) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrder.service.impl.go", "InsertOneWorkOrder", "插入一个WorkOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.MaintWorkOrderId)
	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/pm/WorkOrderService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) DeleteOne(user *models.Usertab, maintWorkOrderId string) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrder.service.impl.go", "DeleteOneWorkOrder", "删除一个WorkOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(maintWorkOrderId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(maintWorkOrderId)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/WorkOrderService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) SelectOne(user *models.Usertab, maintWorkOrderId string) (*model.WorkOrder, error) {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrder.service.impl.go", "SelectOneWorkOrder", "查询一个WorkOrder")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/WorkOrderService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkOrder) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrder.service.impl.go", "UpdateOneWorkOrder", "修改一个WorkOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.MaintWorkOrderId)
	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/pm/WorkOrderService.Insert
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) Insert(user *models.Usertab, entities *[]model.WorkOrder) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrder.service.impl.go", "InsertWorkOrder", "插入多个WorkOrder")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.MaintWorkOrderId)
		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/pm/WorkOrderService.Delete
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) Delete(user *models.Usertab, entities *[]model.WorkOrder) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrder.service.impl.go", "DeleteWorkOrder", "删除多个WorkOrder")

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

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrder.service.impl.go", "SelectWorkOrder", "查询WorkOrder")

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrder.service.impl.go", "UpdateWorkOrder", "修改多个WorkOrder")

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

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

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