// 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              : WorkLineWorkPlaceLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
var DefaultConditionOfWorkLineWorkPlaceLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkLineWorkPlaceLst_WorkLineId.UrlParameterName:  grmi.NewConditionItem(meta.WorkLineWorkPlaceLst_WorkLineId, grmi.Equal, true),
		meta.WorkLineWorkPlaceLst_WorkPlaceNr.UrlParameterName: grmi.NewConditionItem(meta.WorkLineWorkPlaceLst_WorkPlaceNr, grmi.Equal, true),
		meta.WorkLineWorkPlaceLst_Pos.UrlParameterName:         grmi.NewConditionItem(meta.WorkLineWorkPlaceLst_Pos, grmi.Equal, false),
		meta.WorkLineWorkPlaceLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.WorkLineWorkPlaceLst_LastModify, grmi.Approximate, false),
		meta.WorkLineWorkPlaceLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.WorkLineWorkPlaceLst_LastUser, grmi.Equal, false),
		meta.WorkLineWorkPlaceLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.WorkLineWorkPlaceLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkLineWorkPlaceLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
var DefaultConditionOfWorkLineWorkPlaceLstAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkLineWorkPlaceLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkLineWorkPlaceLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkLineWorkPlaceLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
type WorkLineWorkPlaceLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkLineWorkPlaceLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkLineWorkPlaceLstDAOImplement实例
 *
 * @Return Value             : WorkLineWorkPlaceLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
func NewWorkLineWorkPlaceLstServiceImplement() *WorkLineWorkPlaceLstServiceImplement {
	return &WorkLineWorkPlaceLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkLineWorkPlaceLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkPlaceLstServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkLineWorkPlaceLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineWorkPlaceLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkLineId, entity.WorkPlaceNr)
	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/base/WorkLineWorkPlaceLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkPlaceLstServiceImplement) DeleteOne(user *models.Usertab, workLineId string, workPlaceNr int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkLineWorkPlaceLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkPlaceLstServiceImplement) SelectOne(user *models.Usertab, workLineId string, workPlaceNr int) (*model.WorkLineWorkPlaceLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkLineWorkPlaceLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkPlaceLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkLineWorkPlaceLst) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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