// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/base"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/base"
	model "LAPP_ACURA_MOM_BACKEND/models/base"
)

/******************************************************************************
 *
 * @Description              : WorkLineWorkShiftLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:25:20
 *
 ******************************************************************************/
var DefaultConditionOfWorkLineWorkShiftLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkLineWorkShiftLst_WorkLineId.UrlParameterName:  grmi.NewConditionItem(meta.WorkLineWorkShiftLst_WorkLineId, grmi.Equal, true),
		meta.WorkLineWorkShiftLst_WorkDay.UrlParameterName:     grmi.NewConditionItem(meta.WorkLineWorkShiftLst_WorkDay, grmi.Equal, true),
		meta.WorkLineWorkShiftLst_WorkShiftNr.UrlParameterName: grmi.NewConditionItem(meta.WorkLineWorkShiftLst_WorkShiftNr, grmi.Equal, true),
		meta.WorkLineWorkShiftLst_PersonQty.UrlParameterName:   grmi.NewConditionItem(meta.WorkLineWorkShiftLst_PersonQty, grmi.Equal, false),
		meta.WorkLineWorkShiftLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.WorkLineWorkShiftLst_LastModify, grmi.Approximate, false),
		meta.WorkLineWorkShiftLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.WorkLineWorkShiftLst_LastUser, grmi.Equal, false),
		meta.WorkLineWorkShiftLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.WorkLineWorkShiftLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkLineWorkShiftLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:25:20
 *
 ******************************************************************************/
var DefaultConditionOfWorkLineWorkShiftLstAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkLineWorkShiftLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkLineWorkShiftLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkLineWorkShiftLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:25:20
 *
 ******************************************************************************/
type WorkLineWorkShiftLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkLineWorkShiftLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkLineWorkShiftLstDAOImplement实例
 *
 * @Return Value             : WorkLineWorkShiftLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:25:20
 *
 ******************************************************************************/
func NewWorkLineWorkShiftLstServiceImplement() *WorkLineWorkShiftLstServiceImplement {
	return &WorkLineWorkShiftLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkShiftLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftLstServiceImplement) InsertOne(user *global.User, entity *model.WorkLineWorkShiftLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineWorkShiftLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.WorkLineId, entity.WorkDay, entity.WorkShiftNr)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	workLineDao := dal.NewWorkLineDAO(session, user.PlantNr, user.UserId)
	workLine, err := workLineDao.SelectOne(entity.WorkLineId)
	if err != nil {
		return err
	}
	if workLine == nil {
		return grmi.NewBusinessError("不存在对应的生产线，请检查")
	}
	workShiftDao := dal.NewWorkShiftDAO(session, user.PlantNr, user.UserId)
	workShift, err := workShiftDao.SelectOne(entity.WorkShiftNr)
	if err != nil {
		return err
	}
	if workShift == nil {
		return grmi.NewBusinessError("不存在对应的人员班组，请检查")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkShiftLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftLstServiceImplement) DeleteOne(user *global.User, workLineId string, workDay string, workShiftNr int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkShiftLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftLstServiceImplement) SelectOne(user *global.User, workLineId string, workDay string, workShiftNr int) (*model.WorkLineWorkShiftLst, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineWorkShiftLstDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(workLineId, workDay, workShiftNr)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkShiftLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftLstServiceImplement) UpdateOne(user *global.User, entity *model.WorkLineWorkShiftLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineWorkShiftLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.WorkLineId, entity.WorkDay, entity.WorkShiftNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	if record.WorkShiftNr != entity.WorkShiftNr {
		workShiftDao := dal.NewWorkShiftDAO(session, user.PlantNr, user.UserId)
		workShift, err := workShiftDao.SelectOne(entity.WorkShiftNr)
		if err != nil {
			return err
		}
		if workShift == nil {
			return grmi.NewBusinessError("不存在对应的人员班组，请检查")
		}
	}
	entity.WorkLineId = record.WorkLineId
	entity.WorkDay = record.WorkDay
	entity.WorkShiftNr = record.WorkShiftNr
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkShiftLstService.Insert
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftLstServiceImplement) Insert(user *global.User, entities *[]model.WorkLineWorkShiftLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineWorkShiftLstDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkLineId, entity.WorkDay, entity.WorkShiftNr)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkShiftLstService.Delete
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftLstServiceImplement) Delete(user *global.User, entities *[]model.WorkLineWorkShiftLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineWorkShiftLstDAO(session, user.PlantNr, user.UserId)

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkShiftLstService.Select
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftLstServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfWorkLineWorkShiftLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewWorkLineWorkShiftLstDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/base/WorkLineWorkShiftLstService.Update
 *
 ******************************************************************************/
func (impl *WorkLineWorkShiftLstServiceImplement) Update(user *global.User, entities *[]model.WorkLineWorkShiftLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineWorkShiftLstDAO(session, user.PlantNr, user.UserId)

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

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