// 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              : WorkCalendarLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-16 10:03:06
 *
 ******************************************************************************/
var DefaultConditionOfWorkCalendarLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkCalendarLst_WorkCalendarNr.UrlParameterName: grmi.NewConditionItem(meta.WorkCalendarLst_WorkCalendarNr, grmi.Equal, true),
		meta.WorkCalendarLst_WorkDate.UrlParameterName:       grmi.NewConditionItem(meta.WorkCalendarLst_WorkDate, grmi.Approximate, true),
		meta.WorkCalendarLst_DayModelNr.UrlParameterName:     grmi.NewConditionItem(meta.WorkCalendarLst_DayModelNr, grmi.Equal, false),
		meta.WorkCalendarLst_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.WorkCalendarLst_LastModify, grmi.Approximate, false),
		meta.WorkCalendarLst_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.WorkCalendarLst_LastUser, grmi.Equal, false),
		meta.WorkCalendarLst_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.WorkCalendarLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkCalendarLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-16 10:03:06
 *
 ******************************************************************************/
var DefaultConditionOfWorkCalendarLstAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkCalendarLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkCalendarLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkCalendarLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-16 10:03:06
 *
 ******************************************************************************/
type WorkCalendarLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkCalendarLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkCalendarLstDAOImplement实例
 *
 * @Return Value             : WorkCalendarLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-16 10:03:06
 *
 ******************************************************************************/
func NewWorkCalendarLstServiceImplement() *WorkCalendarLstServiceImplement {
	return &WorkCalendarLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkCalendarLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkCalendarLstServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkCalendarLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkCalendarLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkCalendarNr, entity.WorkDate)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	calendarDao := dal.NewWorkCalendarDAO(session, user.Pid, user.Userid)
	calendar, err := calendarDao.SelectOne(entity.WorkCalendarNr)
	if err != nil {
		return err
	}
	if calendar == 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/WorkCalendarLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkCalendarLstServiceImplement) DeleteOne(user *models.Usertab, workCalendarNr int, workDate grmi.Date) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkCalendarLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkCalendarLstServiceImplement) SelectOne(user *models.Usertab, workCalendarNr int, workDate grmi.Date) (*model.WorkCalendarLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkCalendarLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkCalendarLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkCalendarLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkCalendarLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkCalendarNr, entity.WorkDate)
	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.WorkCalendarNr = record.WorkCalendarNr
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkCalendarLstService.Insert
 *
 ******************************************************************************/
func (impl *WorkCalendarLstServiceImplement) Insert(user *models.Usertab, entities *model.WorkCalendarLstInsertReq) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewWorkCalendarLstDAO(session, user.Pid, user.Userid)
	workCalendarNr := entities.WorkCalendarNr
	workCalendarLstLi := entities.Records
	for _, entity := range workCalendarLstLi {
		if workCalendarNr != entity.WorkCalendarNr {
			return grmi.NewBusinessError("与设置的workCalendarNr不一致，请检查")
		}
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.WorkCalendarLst_WorkCalendarNr.NewPredicate(grmi.Equal, workCalendarNr)})
	if err != nil {
		session.Rollback()
		return err
	}
	err = dao.Insert(&workCalendarLstLi)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkCalendarLstService.Delete
 *
 ******************************************************************************/
func (impl *WorkCalendarLstServiceImplement) Delete(user *models.Usertab, entities *[]model.WorkCalendarLst) error {

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

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

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

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

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

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

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

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

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