// 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              : WorkCalendar的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfWorkCalendar = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkCalendar_WorkCalendarNr.UrlParameterName: grmi.NewConditionItem(meta.WorkCalendar_WorkCalendarNr, grmi.Equal, true),
		meta.WorkCalendar_Descr.UrlParameterName:          grmi.NewConditionItem(meta.WorkCalendar_Descr, grmi.Equal, false),
		meta.WorkCalendar_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.WorkCalendar_LastModify, grmi.Approximate, false),
		meta.WorkCalendar_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.WorkCalendar_LastUser, grmi.Equal, false),
		meta.WorkCalendar_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.WorkCalendar_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkCalendarService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkCalendarServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkCalendar) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewWorkCalendarDAO(session, user.Pid, user.Userid)
	workCalendarNr, err := dao.InsertOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	workCalendarLstLi := entity.WorkCalendarLstLi
	workCalendarLstDao := dal.NewWorkCalendarLstDAO(session, user.Pid, user.Userid)
	for index, workCalendarLst := range workCalendarLstLi {
		newWorkCalendarLst := workCalendarLst
		newWorkCalendarLst.WorkCalendarNr = workCalendarNr
		workCalendarLstLi[index] = newWorkCalendarLst
	}
	err = workCalendarLstDao.Insert(&workCalendarLstLi)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkCalendarService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkCalendarServiceImplement) DeleteOne(user *models.Usertab, workCalendarNr int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewWorkCalendarDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(workCalendarNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	calendarLstDao := dal.NewWorkCalendarLstDAO(session, user.Pid, user.Userid)
	err = calendarLstDao.DeleteWhere([]grmi.Predicate{meta.WorkCalendarLst_WorkCalendarNr.NewPredicate(grmi.Equal, workCalendarNr)})
	if err != nil {
		session.Rollback()
		return err
	}
	err = dao.DeleteOne(workCalendarNr)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkCalendarService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkCalendarServiceImplement) SelectOne(user *models.Usertab, workCalendarNr int) (*model.WorkCalendar, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkCalendarDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(workCalendarNr)
	if err != nil {
		return nil, err
	}
	if result == nil {
		return nil, grmi.NewBusinessError("不存在该条日历，请检查")
	}
	workCalendarLstDao := dal.NewWorkCalendarLstDAO(session, user.Pid, user.Userid)
	li, err := workCalendarLstDao.SelectJoinDayModel(workCalendarNr)
	if err != nil {
		return nil, err
	}
	result.WorkCalendarLst = li
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkCalendarService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkCalendarServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkCalendar) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewWorkCalendarDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkCalendarNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	workCalendarLstDao := dal.NewWorkCalendarLstDAO(session, user.Pid, user.Userid)
	workCalendarLstLi := entity.WorkCalendarLstLi
	for _, workCalendarLst := range workCalendarLstLi {
		if workCalendarLst.WorkCalendarNr != entity.WorkCalendarNr {
			return grmi.NewBusinessError("插入的workCalendarLst的WorkCalendarNr与workCalendar的WorkCalendarNr不一致")
		}
	}
	err = workCalendarLstDao.DeleteWhere([]grmi.Predicate{meta.WorkCalendarLst_WorkCalendarNr.NewPredicate(grmi.Equal, entity.WorkCalendarNr)})
	if err != nil {
		session.Rollback()
		return err
	}
	err = workCalendarLstDao.Insert(&workCalendarLstLi)
	if err != nil {
		session.Rollback()
		return err
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkCalendarService.Insert
 *
 ******************************************************************************/
func (impl *WorkCalendarServiceImplement) Insert(user *models.Usertab, entities *[]model.WorkCalendar) error {

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

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

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

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

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

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

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

	condition := DefaultConditionOfWorkCalendarAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewWorkCalendarDAO(session, user.Pid, user.Userid)
	//calendarLstDao := dal.NewWorkCalendarLstDAO(session, user.Pid, user.Userid)
	//lstCondition := DefaultConditionOfWorkCalendarLst
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		//li := result.Records.([]model.WorkCalendar)
		//for index, calendar := range li {
		//	newCalendar := calendar
		//	workCalendarLstLi, err := calendarLstDao.SelectJoinDayModel([]grmi.Predicate{meta.WorkCalendarLst_WorkCalendarNr.NewPredicate(grmi.Equal, calendar.WorkCalendarNr)}, lstCondition.OrderByFields)
		//	if err != nil {
		//		return nil, err
		//	}
		//	newCalendar.WorkCalendarLst = workCalendarLstLi
		//	li[index] = newCalendar
		//}
		//result.Records = li
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		//for index, calendar := range result {
		//	newCalendar := calendar
		//	workCalendarLstLi, err := calendarLstDao.SelectJoinDayModel([]grmi.Predicate{meta.WorkCalendarLst_WorkCalendarNr.NewPredicate(grmi.Equal, calendar.WorkCalendarNr)}, lstCondition.OrderByFields)
		//	if err != nil {
		//		return nil, err
		//	}
		//	newCalendar.WorkCalendarLst = workCalendarLstLi
		//	result[index] = newCalendar
		//}
		return result, nil
	}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WorkCalendarService.Update
 *
 ******************************************************************************/
func (impl *WorkCalendarServiceImplement) Update(user *models.Usertab, entities *[]model.WorkCalendar) error {

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

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

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

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