// 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              : WorkLineWorkShiftSpecialTime的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfWorkLineWorkShiftSpecialTime = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkLineWorkShiftSpecialTime_WorkLineId.UrlParameterName: grmi.NewConditionItem(meta.WorkLineWorkShiftSpecialTime_WorkLineId, grmi.Equal, true),
		meta.WorkLineWorkShiftSpecialTime_WorkDay.UrlParameterName:    grmi.NewConditionItem(meta.WorkLineWorkShiftSpecialTime_WorkDay, grmi.Equal, true),
		meta.WorkLineWorkShiftSpecialTime_Pos.UrlParameterName:        grmi.NewConditionItem(meta.WorkLineWorkShiftSpecialTime_Pos, grmi.Equal, true),
		meta.WorkLineWorkShiftSpecialTime_BegTime.UrlParameterName:    grmi.NewConditionItem(meta.WorkLineWorkShiftSpecialTime_BegTime, grmi.Approximate, true),
		meta.WorkLineWorkShiftSpecialTime_AddMinus.UrlParameterName:   grmi.NewConditionItem(meta.WorkLineWorkShiftSpecialTime_AddMinus, grmi.Equal, false),
		meta.WorkLineWorkShiftSpecialTime_EndTime.UrlParameterName:    grmi.NewConditionItem(meta.WorkLineWorkShiftSpecialTime_EndTime, grmi.Approximate, false),
		meta.WorkLineWorkShiftSpecialTime_TimeObjId.UrlParameterName:  grmi.NewConditionItem(meta.WorkLineWorkShiftSpecialTime_TimeObjId, grmi.Equal, false),
		meta.WorkLineWorkShiftSpecialTime_LastModify.UrlParameterName: grmi.NewConditionItem(meta.WorkLineWorkShiftSpecialTime_LastModify, grmi.Approximate, false),
		meta.WorkLineWorkShiftSpecialTime_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.WorkLineWorkShiftSpecialTime_LastUser, grmi.Equal, false),
		meta.WorkLineWorkShiftSpecialTime_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.WorkLineWorkShiftSpecialTime_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkLineWorkShiftSpecialTime的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfWorkLineWorkShiftSpecialTimeAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkLineWorkShiftSpecialTime.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : WorkLineWorkShiftSpecialTimeServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : WorkLineWorkShiftSpecialTime的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type WorkLineWorkShiftSpecialTimeServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkLineWorkShiftSpecialTimeDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkLineWorkShiftSpecialTimeDAOImplement实例
 *
 * @Return Value             : WorkLineWorkShiftSpecialTimeDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewWorkLineWorkShiftSpecialTimeServiceImplement() *WorkLineWorkShiftSpecialTimeServiceImplement {
	return &WorkLineWorkShiftSpecialTimeServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkLineWorkShiftSpecialTimeService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftSpecialTimeServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkLineWorkShiftSpecialTime) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineWorkShiftSpecialTimeDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkLineId, entity.WorkDay, entity.Pos, entity.BegTime)
	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/WorkLineWorkShiftSpecialTimeService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftSpecialTimeServiceImplement) DeleteOne(user *models.Usertab, workLineId string, workDay string, pos int, begTime grmi.DateTime) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkLineWorkShiftSpecialTimeService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftSpecialTimeServiceImplement) SelectOne(user *models.Usertab, workLineId string, workDay string, pos int, begTime grmi.DateTime) (*model.WorkLineWorkShiftSpecialTime, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkLineWorkShiftSpecialTimeService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftSpecialTimeServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkLineWorkShiftSpecialTime) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineWorkShiftSpecialTimeDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkLineId, entity.WorkDay, entity.Pos, entity.BegTime)
	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/WorkLineWorkShiftSpecialTimeService.Insert
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftSpecialTimeServiceImplement) Insert(user *models.Usertab, entities *[]model.WorkLineWorkShiftSpecialTime) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkLineId, entity.WorkDay, entity.Pos, entity.BegTime)
		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/WorkLineWorkShiftSpecialTimeService.Delete
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftSpecialTimeServiceImplement) Delete(user *models.Usertab, entities *[]model.WorkLineWorkShiftSpecialTime) error {

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

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

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

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

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

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

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

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

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