// 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              : WorkOrderStatus的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrderStatus = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkOrderStatus_WorkOrderRecNr.UrlParameterName:   grmi.NewConditionItem(meta.WorkOrderStatus_WorkOrderRecNr, grmi.Equal, true),
		meta.WorkOrderStatus_MaintWorkOrderId.UrlParameterName: grmi.NewConditionItem(meta.WorkOrderStatus_MaintWorkOrderId, grmi.Equal, false),
		meta.WorkOrderStatus_Operator.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderStatus_Operator, grmi.Equal, false),
		meta.WorkOrderStatus_WorkOrderBagType.UrlParameterName: grmi.NewConditionItem(meta.WorkOrderStatus_WorkOrderBagType, grmi.Equal, false),
		meta.WorkOrderStatus_StatusFrom.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrderStatus_StatusFrom, grmi.Equal, false),
		meta.WorkOrderStatus_StatusTo.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderStatus_StatusTo, grmi.Equal, false),
		meta.WorkOrderStatus_OperateTime.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrderStatus_OperateTime, grmi.Approximate, false),
		meta.WorkOrderStatus_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrderStatus_LastModify, grmi.Approximate, false),
		meta.WorkOrderStatus_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderStatus_LastUser, grmi.Equal, false),
		meta.WorkOrderStatus_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrderStatus_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/WorkOrderStatusService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkOrderStatusServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkOrderStatus) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderStatus.service.impl.go", "InsertOneWorkOrderStatus", "插入一个WorkOrderStatus")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderStatusDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkOrderRecNr)
	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/WorkOrderStatusService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkOrderStatusServiceImplement) DeleteOne(user *models.Usertab, workOrderRecNr int) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderStatus.service.impl.go", "DeleteOneWorkOrderStatus", "删除一个WorkOrderStatus")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/WorkOrderStatusService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkOrderStatusServiceImplement) SelectOne(user *models.Usertab, workOrderRecNr int) (*model.WorkOrderStatus, error) {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderStatus.service.impl.go", "SelectOneWorkOrderStatus", "查询一个WorkOrderStatus")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/WorkOrderStatusService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkOrderStatusServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkOrderStatus) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderStatus.service.impl.go", "UpdateOneWorkOrderStatus", "修改一个WorkOrderStatus")

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderStatus.service.impl.go", "InsertWorkOrderStatus", "插入多个WorkOrderStatus")

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

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderStatus.service.impl.go", "DeleteWorkOrderStatus", "删除多个WorkOrderStatus")

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

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderStatus.service.impl.go", "SelectWorkOrderStatus", "查询WorkOrderStatus")

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderStatus.service.impl.go", "UpdateWorkOrderStatus", "修改多个WorkOrderStatus")

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

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

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