// 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              : WeekModel的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-16 10:03:06
 *
 ******************************************************************************/
var DefaultConditionOfWeekModel = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WeekModel_WeekModelNr.UrlParameterName: grmi.NewConditionItem(meta.WeekModel_WeekModelNr, grmi.Equal, true),
		meta.WeekModel_DateFrom.UrlParameterName:    grmi.NewConditionItem(meta.WeekModel_DateFrom, grmi.Approximate, false),
		meta.WeekModel_Descr.UrlParameterName:       grmi.NewConditionItem(meta.WeekModel_Descr, grmi.Equal, false),
		meta.WeekModel_Abrev.UrlParameterName:       grmi.NewConditionItem(meta.WeekModel_Abrev, grmi.Equal, false),
		meta.WeekModel_CtrlToggle.UrlParameterName:  grmi.NewConditionItem(meta.WeekModel_CtrlToggle, grmi.Equal, false),
		meta.WeekModel_LoopDays.UrlParameterName:    grmi.NewConditionItem(meta.WeekModel_LoopDays, grmi.Equal, false),
		meta.WeekModel_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.WeekModel_LastModify, grmi.Approximate, false),
		meta.WeekModel_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.WeekModel_LastUser, grmi.Equal, false),
		meta.WeekModel_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.WeekModel_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WeekModel的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-14 08:56:34
 *
 ******************************************************************************/
var DefaultConditionOfWeekModelAndPaging = grmi.NewCondition(
	DefaultConditionOfWeekModel.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WeekModelServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WeekModel的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-14 08:56:34
 *
 ******************************************************************************/
type WeekModelServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWeekModelDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WeekModelDAOImplement实例
 *
 * @Return Value             : WeekModelDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-14 08:56:34
 *
 ******************************************************************************/
func NewWeekModelServiceImplement() *WeekModelServiceImplement {
	return &WeekModelServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WeekModelService.InsertOne
 *
 ******************************************************************************/
func (impl *WeekModelServiceImplement) InsertOne(user *models.Usertab, entity *model.WeekModel) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewWeekModelDAO(session, user.Pid, user.Userid)

	weekModelNr, err := dao.InsertOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	weekModelLstLi := entity.WeekModelLstLi
	for index, weekModelLst := range weekModelLstLi {
		newWeekModelLst := weekModelLst
		newWeekModelLst.WeekModelNr = weekModelNr
		weekModelLstLi[index] = newWeekModelLst
	}
	weekModelLstDao := dal.NewWeekModelLstDAO(session, user.Pid, user.Userid)
	err = weekModelLstDao.Insert(&weekModelLstLi)
	if err != nil {
		session.Rollback()
		return err
	}

	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WeekModelService.DeleteOne
 *
 ******************************************************************************/
func (impl *WeekModelServiceImplement) DeleteOne(user *models.Usertab, weekModelNr int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WeekModelService.SelectOne
 *
 ******************************************************************************/
func (impl *WeekModelServiceImplement) SelectOne(user *models.Usertab, weekModelNr int) (*model.WeekModel, error) {

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

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

	result, err := dao.SelectOne(weekModelNr)
	if err != nil  {
		return nil, err
	}
	//condition := DefaultConditionOfWeekModelLst
	weekModelLstDao := dal.NewWeekModelLstDAO(session, user.Pid, user.Userid)
	//li, err := weekModelLstDao.SelectJoinDayModel([]grmi.Predicate{meta.WeekModelLst_WeekModelNr.NewPredicate(grmi.Equal, weekModelNr)}, condition.OrderByFields)
	li, err := weekModelLstDao.SelectJoinDayModel(weekModelNr)

	if err != nil {
		return nil, err
	}
	if result == nil {
		return nil, grmi.NewBusinessError("不存在对应的周模型")
	}
	result.WeekModelLst = li
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WeekModelService.UpdateOne
 *
 ******************************************************************************/
func (impl *WeekModelServiceImplement) UpdateOne(user *models.Usertab, entity *model.WeekModel) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewWeekModelDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WeekModelNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	weekModelLstLi := entity.WeekModelLstLi
	for _, weekModelLst := range weekModelLstLi {
		if weekModelLst.WeekModelNr != entity.WeekModelNr {
			return grmi.NewBusinessError("weekModelLst的weekModelNr与weekModel的weekModelNr不一致")
		}
	}
	weekModelLstDao := dal.NewWeekModelLstDAO(session, user.Pid, user.Userid)
	err = weekModelLstDao.DeleteWhere([]grmi.Predicate{meta.WeekModelLst_WeekModelNr.NewPredicate(grmi.Equal, entity.WeekModelNr)})
	if err != nil {
		session.Rollback()
		return err
	}
	err = weekModelLstDao.Insert(&weekModelLstLi)
	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/WeekModelService.Insert
 *
 ******************************************************************************/
func (impl *WeekModelServiceImplement) Insert(user *models.Usertab, entities *[]model.WeekModel) error {

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

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

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

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

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

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

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

	condition := DefaultConditionOfWeekModelAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewWeekModelDAO(session, user.Pid, user.Userid)
	//weekModelLstDao := dal.NewWeekModelLstDAO(session, user.Pid, user.Userid)
	//lstCondition := DefaultConditionOfWeekModelAndPaging
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		//li := result.Records.([]model.WeekModel)
		//for index, weekModel := range li {
		//	weekModelLstLi, err := weekModelLstDao.SelectJoinDayModel([]grmi.Predicate{meta.WeekModelLst_WeekModelNr.NewPredicate(grmi.Equal, weekModel.WeekModelNr)}, lstCondition.OrderByFields)
		//	if err != nil {
		//		return nil, err
		//	}
		//	newWeekModel := weekModel
		//	newWeekModel.WeekModelLst = weekModelLstLi
		//	li[index] = weekModel
		//}
		//result.Records = li
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		//for index, weekModel := range result {
		//	weekModelLstLi, err := weekModelLstDao.SelectJoinDayModel([]grmi.Predicate{meta.WeekModelLst_WeekModelNr.NewPredicate(grmi.Equal, weekModel.WeekModelNr)}, lstCondition.OrderByFields)
		//	if err != nil {
		//		return nil, err
		//	}
		//	newWeekModel := weekModel
		//	newWeekModel.WeekModelLst = weekModelLstLi
		//	result[index] = weekModel
		//}
		return result, nil
	}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/WeekModelService.Update
 *
 ******************************************************************************/
func (impl *WeekModelServiceImplement) Update(user *models.Usertab, entities *[]model.WeekModel) error {

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

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

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

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