// 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              : WorkLineSpecialDay的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfWorkLineSpecialDay = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkLineSpecialDay_WorkLineid.UrlParameterName: grmi.NewConditionItem(meta.WorkLineSpecialDay_WorkLineid, grmi.Equal, true),
		meta.WorkLineSpecialDay_SpecialDay.UrlParameterName: grmi.NewConditionItem(meta.WorkLineSpecialDay_SpecialDay, grmi.Equal, true),
		meta.WorkLineSpecialDay_DayModelNr.UrlParameterName: grmi.NewConditionItem(meta.WorkLineSpecialDay_DayModelNr, grmi.Equal, false),
		meta.WorkLineSpecialDay_Descr.UrlParameterName:      grmi.NewConditionItem(meta.WorkLineSpecialDay_Descr, grmi.Equal, false),
		meta.WorkLineSpecialDay_LastModify.UrlParameterName: grmi.NewConditionItem(meta.WorkLineSpecialDay_LastModify, grmi.Approximate, false),
		meta.WorkLineSpecialDay_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.WorkLineSpecialDay_LastUser, grmi.Equal, false),
		meta.WorkLineSpecialDay_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.WorkLineSpecialDay_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkLineSpecialDayService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkLineSpecialDayServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkLineSpecialDay) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineSpecialDayDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkLineid, entity.SpecialDay)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	workLineDao := dal.NewWorkLineDAO(session, user.Pid, user.Userid)
	workLine, err := workLineDao.SelectOne(entity.WorkLineid)
	if err != nil {
		return err
	}
	if workLine == nil {
		return grmi.NewBusinessError("不存在对应的生产线，请检查")
	}
	dayModelDao := dal.NewDayModelDAO(session, user.Pid, user.Userid)
	dayModel, err := dayModelDao.SelectOne(entity.DayModelNr)
	if err != nil {
		return err
	}
	if dayModel == nil {
		return grmi.NewBusinessError("不存在对应的日模型")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkLineSpecialDayService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkLineSpecialDayServiceImplement) DeleteOne(user *models.Usertab, workLineid string, specialDay string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkLineSpecialDayService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkLineSpecialDayServiceImplement) SelectOne(user *models.Usertab, workLineid string, specialDay string) (*model.WorkLineSpecialDay, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkLineSpecialDayService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkLineSpecialDayServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkLineSpecialDay) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineSpecialDayDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkLineid, entity.SpecialDay)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	if record.DayModelNr != entity.DayModelNr {
		dayModelDao := dal.NewDayModelDAO(session, user.Pid, user.Userid)
		dayModel, err := dayModelDao.SelectOne(entity.DayModelNr)
		if err != nil {
			return err
		}
		if dayModel == nil {
			return grmi.NewBusinessError("不存在对应的日模型，请检查")
		}
	}
	entity.WorkLineid = record.WorkLineid
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkLineSpecialDayService.Insert
 *
 ******************************************************************************/
func (impl *WorkLineSpecialDayServiceImplement) Insert(user *models.Usertab, entities *[]model.WorkLineSpecialDay) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkLineid, entity.SpecialDay)
		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/WorkLineSpecialDayService.Delete
 *
 ******************************************************************************/
func (impl *WorkLineSpecialDayServiceImplement) Delete(user *models.Usertab, entities *[]model.WorkLineSpecialDay) error {

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

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

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

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

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

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

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

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

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