// 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              : WeekModelLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-16 10:03:06
 *
 ******************************************************************************/
var DefaultConditionOfWeekModelLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WeekModelLst_WeekModelNr.UrlParameterName: grmi.NewConditionItem(meta.WeekModelLst_WeekModelNr, grmi.Equal, true),
		meta.WeekModelLst_WorkDate.UrlParameterName:    grmi.NewConditionItem(meta.WeekModelLst_WorkDate, grmi.Approximate, true),
		meta.WeekModelLst_DayModelNr.UrlParameterName:  grmi.NewConditionItem(meta.WeekModelLst_DayModelNr, grmi.Equal, false),
		meta.WeekModelLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.WeekModelLst_LastModify, grmi.Approximate, false),
		meta.WeekModelLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.WeekModelLst_LastUser, grmi.Equal, false),
		meta.WeekModelLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.WeekModelLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WeekModelLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WeekModelLstServiceImplement) InsertOne(user *global.User, entity *model.WeekModelLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWeekModelLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.WeekModelNr, entity.WorkDate)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	weekModelDao := dal.NewWeekModelDAO(session, user.PlantNr, user.UserId)
	weekModel, err := weekModelDao.SelectOne(entity.WeekModelNr)
	if err != nil {
		return err
	}
	if weekModel == nil {
		return grmi.NewBusinessError("不存在对应的周模型，请检查")
	}
	dayModelDao := dal.NewDayModelDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/base/WeekModelLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WeekModelLstServiceImplement) DeleteOne(user *global.User, weekModelNr int, workDate grmi.Date) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WeekModelLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WeekModelLstServiceImplement) SelectOne(user *global.User, weekModelNr int, workDate grmi.Date) (*model.WeekModelLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WeekModelLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WeekModelLstServiceImplement) UpdateOne(user *global.User, entity *model.WeekModelLst) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WeekModelLstService.Insert
 *
 ******************************************************************************/
func (impl *WeekModelLstServiceImplement) Insert(user *global.User, entities *model.WeekModelLstInsertReq) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	weekModelNr := entities.WeekModelNr
	weekModelLstLi := entities.Records
	dao := dal.NewWeekModelLstDAO(session, user.PlantNr, user.UserId)
	weekModelDao := dal.NewWeekModelDAO(session, user.PlantNr, user.UserId)
	weekModel, err := weekModelDao.SelectOne(weekModelNr)
	if err != nil {
		return err
	}
	if weekModel == nil {
		return grmi.NewBusinessError("不存在对应的周模型")
	}
	for _, weekModelLst := range weekModelLstLi {
		if weekModelLst.WeekModelNr != weekModelNr {
			return grmi.NewBusinessError("传入的weekModelLst对应WeekModelNr和设置的不符，请检查")
		}
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.WeekModelLst_WeekModelNr.NewPredicate(grmi.Equal, weekModelNr)})
	if err != nil {
		session.Rollback()
		return err
	}

	err = dao.Insert(&weekModelLstLi)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WeekModelLstService.Delete
 *
 ******************************************************************************/
func (impl *WeekModelLstServiceImplement) Delete(user *global.User, entities *[]model.WeekModelLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WeekModelNr, 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_ACURA_MOM_BACKEND/services/base/WeekModelLstService.Select
 *
 ******************************************************************************/
func (impl *WeekModelLstServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfWeekModelLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewWeekModelLstDAO(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/WeekModelLstService.Update
 *
 ******************************************************************************/
func (impl *WeekModelLstServiceImplement) Update(user *global.User, entities *[]model.WeekModelLst) error {

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

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

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

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