// 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              : WorkOrderAttrLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrderAttrLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkOrderAttrLst_MaintWorkOrderId.UrlParameterName: grmi.NewConditionItem(meta.WorkOrderAttrLst_MaintWorkOrderId, grmi.Equal, true),
		meta.WorkOrderAttrLst_MainServiceNr.UrlParameterName:    grmi.NewConditionItem(meta.WorkOrderAttrLst_MainServiceNr, grmi.Equal, true),
		meta.WorkOrderAttrLst_AttrNr.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrderAttrLst_AttrNr, grmi.Equal, true),
		meta.WorkOrderAttrLst_Pos.UrlParameterName:              grmi.NewConditionItem(meta.WorkOrderAttrLst_Pos, grmi.Equal, false),
		meta.WorkOrderAttrLst_Descr.UrlParameterName:            grmi.NewConditionItem(meta.WorkOrderAttrLst_Descr, grmi.Equal, false),
		meta.WorkOrderAttrLst_Bitmap.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrderAttrLst_Bitmap, grmi.Equal, false),
		meta.WorkOrderAttrLst_Parameter.UrlParameterName:        grmi.NewConditionItem(meta.WorkOrderAttrLst_Parameter, grmi.Equal, false),
		meta.WorkOrderAttrLst_ParaValue.UrlParameterName:        grmi.NewConditionItem(meta.WorkOrderAttrLst_ParaValue, grmi.Equal, false),
		meta.WorkOrderAttrLst_Operator.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderAttrLst_Operator, grmi.Equal, false),
		meta.WorkOrderAttrLst_ActValue.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderAttrLst_ActValue, grmi.Equal, false),
		meta.WorkOrderAttrLst_OperateTime.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrderAttrLst_OperateTime, grmi.Approximate, false),
		meta.WorkOrderAttrLst_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrderAttrLst_LastModify, grmi.Approximate, false),
		meta.WorkOrderAttrLst_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderAttrLst_LastUser, grmi.Equal, false),
		meta.WorkOrderAttrLst_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrderAttrLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/WorkOrderAttrLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkOrderAttrLstServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkOrderAttrLst) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderAttrLst.service.impl.go", "InsertOneWorkOrderAttrLst", "插入一个WorkOrderAttrLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderAttrLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.MaintWorkOrderId, entity.MainServiceNr, entity.AttrNr)
	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/WorkOrderAttrLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkOrderAttrLstServiceImplement) DeleteOne(user *models.Usertab, maintWorkOrderId string, mainServiceNr int, attrNr int) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderAttrLst.service.impl.go", "DeleteOneWorkOrderAttrLst", "删除一个WorkOrderAttrLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/WorkOrderAttrLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkOrderAttrLstServiceImplement) SelectOne(user *models.Usertab, maintWorkOrderId string, mainServiceNr int, attrNr int) (*model.WorkOrderAttrLst, error) {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderAttrLst.service.impl.go", "SelectOneWorkOrderAttrLst", "查询一个WorkOrderAttrLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/WorkOrderAttrLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkOrderAttrLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkOrderAttrLst) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderAttrLst.service.impl.go", "UpdateOneWorkOrderAttrLst", "修改一个WorkOrderAttrLst")

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderAttrLst.service.impl.go", "InsertWorkOrderAttrLst", "插入多个WorkOrderAttrLst")

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

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderAttrLst.service.impl.go", "DeleteWorkOrderAttrLst", "删除多个WorkOrderAttrLst")

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

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderAttrLst.service.impl.go", "SelectWorkOrderAttrLst", "查询WorkOrderAttrLst")

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderAttrLst.service.impl.go", "UpdateWorkOrderAttrLst", "修改多个WorkOrderAttrLst")

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

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

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