// 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              : WorkOrderNoteLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 11:00:13
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrderNoteLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkOrderNoteLst_MaintWorkOrderId.UrlParameterName: grmi.NewConditionItem(meta.WorkOrderNoteLst_MaintWorkOrderId, grmi.Equal, true),
		meta.WorkOrderNoteLst_MainServiceNr.UrlParameterName:    grmi.NewConditionItem(meta.WorkOrderNoteLst_MainServiceNr, grmi.Equal, true),
		meta.WorkOrderNoteLst_Pos.UrlParameterName:              grmi.NewConditionItem(meta.WorkOrderNoteLst_Pos, grmi.Equal, true),
		meta.WorkOrderNoteLst_Note.UrlParameterName:             grmi.NewConditionItem(meta.WorkOrderNoteLst_Note, grmi.Equal, false),
		meta.WorkOrderNoteLst_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrderNoteLst_LastModify, grmi.Approximate, false),
		meta.WorkOrderNoteLst_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderNoteLst_LastUser, grmi.Equal, false),
		meta.WorkOrderNoteLst_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrderNoteLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/WorkOrderNoteLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkOrderNoteLstServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkOrderNoteLst) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderNoteLst.service.impl.go", "InsertOneWorkOrderNoteLst", "插入一个WorkOrderNoteLst")

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderNoteLst.service.impl.go", "DeleteOneWorkOrderNoteLst", "删除一个WorkOrderNoteLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/WorkOrderNoteLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkOrderNoteLstServiceImplement) SelectOne(user *models.Usertab, maintWorkOrderId string, mainServiceNr int, pos int) (*model.WorkOrderNoteLst, error) {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderNoteLst.service.impl.go", "SelectOneWorkOrderNoteLst", "查询一个WorkOrderNoteLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/WorkOrderNoteLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkOrderNoteLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkOrderNoteLst) error {

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderNoteLst.service.impl.go", "UpdateOneWorkOrderNoteLst", "修改一个WorkOrderNoteLst")

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderNoteLst.service.impl.go", "InsertWorkOrderNoteLst", "插入多个WorkOrderNoteLst")

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

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderNoteLst.service.impl.go", "DeleteWorkOrderNoteLst", "删除多个WorkOrderNoteLst")

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

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderNoteLst.service.impl.go", "SelectWorkOrderNoteLst", "查询WorkOrderNoteLst")

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

	grmi.Log(user, "/services/pm/implments/PM_WorkOrderNoteLst.service.impl.go", "UpdateWorkOrderNoteLst", "修改多个WorkOrderNoteLst")

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

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

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