// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/jit"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/jit"
	model "LAPP_ACURA_MOM_BACKEND/models/jit"
)

/******************************************************************************
 *
 * @Description              : PackTemplate的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
var DefaultConditionOfPackTemplate = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PackTemplate_PackTemplateId.UrlParameterName:       grmi.NewConditionItem(meta.PackTemplate_PackTemplateId, grmi.Equal, true),
		meta.PackTemplate_EnabledToggle.UrlParameterName:        grmi.NewConditionItem(meta.PackTemplate_EnabledToggle, grmi.Equal, false),
		meta.PackTemplate_Descr.UrlParameterName:                grmi.NewConditionItem(meta.PackTemplate_Descr, grmi.Equal, false),
		meta.PackTemplate_ProjectId.UrlParameterName:            grmi.NewConditionItem(meta.PackTemplate_ProjectId, grmi.Equal, false),
		meta.PackTemplate_PackTypeId.UrlParameterName:           grmi.NewConditionItem(meta.PackTemplate_PackTypeId, grmi.Equal, false),
		meta.PackTemplate_UnfullfillableToggle.UrlParameterName: grmi.NewConditionItem(meta.PackTemplate_UnfullfillableToggle, grmi.Equal, false),
		meta.PackTemplate_PackOrderSnrId.UrlParameterName:       grmi.NewConditionItem(meta.PackTemplate_PackOrderSnrId, grmi.Equal, false),
		meta.PackTemplate_PrintQty.UrlParameterName:             grmi.NewConditionItem(meta.PackTemplate_PrintQty, grmi.Equal, false),
		meta.PackTemplate_PlanQty.UrlParameterName:              grmi.NewConditionItem(meta.PackTemplate_PlanQty, grmi.Equal, false),
		meta.PackTemplate_PrinterId.UrlParameterName:            grmi.NewConditionItem(meta.PackTemplate_PrinterId, grmi.Equal, false),
		meta.PackTemplate_TemplateFile.UrlParameterName:         grmi.NewConditionItem(meta.PackTemplate_TemplateFile, grmi.Equal, false),
		meta.PackTemplate_SyncNr.UrlParameterName:               grmi.NewConditionItem(meta.PackTemplate_SyncNr, grmi.Equal, false),
		meta.PackTemplate_Orientation.UrlParameterName:          grmi.NewConditionItem(meta.PackTemplate_Orientation, grmi.Equal, false),
		meta.PackTemplate_SyncFlag.UrlParameterName:             grmi.NewConditionItem(meta.PackTemplate_SyncFlag, grmi.Equal, false),
		meta.PackTemplate_BoxSnrId.UrlParameterName:             grmi.NewConditionItem(meta.PackTemplate_BoxSnrId, grmi.Equal, false),
		meta.PackTemplate_CheckSyncKey.UrlParameterName:         grmi.NewConditionItem(meta.PackTemplate_CheckSyncKey, grmi.Equal, false),
		meta.PackTemplate_CtrlPara1.UrlParameterName:            grmi.NewConditionItem(meta.PackTemplate_CtrlPara1, grmi.Equal, false),
		meta.PackTemplate_CtrlPara2.UrlParameterName:            grmi.NewConditionItem(meta.PackTemplate_CtrlPara2, grmi.Equal, false),
		meta.PackTemplate_CtrlStr1.UrlParameterName:             grmi.NewConditionItem(meta.PackTemplate_CtrlStr1, grmi.Equal, false),
		meta.PackTemplate_CtrlStr2.UrlParameterName:             grmi.NewConditionItem(meta.PackTemplate_CtrlStr2, grmi.Equal, false),
		meta.PackTemplate_CtrlTime1.UrlParameterName:            grmi.NewConditionItem(meta.PackTemplate_CtrlTime1, grmi.Approximate, false),
		meta.PackTemplate_CtrlTime2.UrlParameterName:            grmi.NewConditionItem(meta.PackTemplate_CtrlTime2, grmi.Approximate, false),
		meta.PackTemplate_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.PackTemplate_LastModify, grmi.Approximate, false),
		meta.PackTemplate_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.PackTemplate_LastUser, grmi.Equal, false),
		meta.PackTemplate_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.PackTemplate_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : PackTemplate的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
var DefaultConditionOfPackTemplateAndPaging = grmi.NewCondition(
	DefaultConditionOfPackTemplate.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : PackTemplateServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : PackTemplate的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
type PackTemplateServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewPackTemplateDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个PackTemplateDAOImplement实例
 *
 * @Return Value             : PackTemplateDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
func NewPackTemplateServiceImplement() *PackTemplateServiceImplement {
	return &PackTemplateServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackTemplateService.InsertOne
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) InsertOne(user *global.User, entity *model.PackTemplate) error {

	grmi.Log(user, "/services/jit/implments/PackTemplate.service.impl.go", "InsertOnePackTemplate", "插入一个PackTemplate")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackTemplateDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.PackTemplateId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackTemplateService.DeleteOne
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) DeleteOne(user *global.User, packTemplateId string) error {

	grmi.Log(user, "/services/jit/implments/PackTemplate.service.impl.go", "DeleteOnePackTemplate", "删除一个PackTemplate")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewPackTemplateDAO(session, user.PlantNr, user.UserId)
	detailDao := dal.NewPackTemplateItemLstDAO(session, user.PlantNr, user.UserId)
	templateLstDao := dal.NewPackTemplateItemLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(packTemplateId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(packTemplateId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = detailDao.DeleteWhere([]grmi.Predicate{meta.PackTemplateItemLst_PackTemplateId.NewPredicate(grmi.Equal, packTemplateId)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除包装模板子项失败, error:" + err.Error())
	}
	// 更新全局包装模板数据
	err = templateLstDao.InitGlobalPackTemplate()
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("初始化全局模板数据失败, error:" + err.Error())
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackTemplateService.SelectOne
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) SelectOne(user *global.User, packTemplateId string) (*model.PackTemplate, error) {

	grmi.Log(user, "/services/jit/implments/PackTemplate.service.impl.go", "SelectOnePackTemplate", "查询一个PackTemplate")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackTemplateDAO(session, user.PlantNr, user.UserId)
	detailDao := dal.NewPackTemplateItemLstDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(packTemplateId)
	if err != nil {
		return nil, err
	}
	detailLi, err := detailDao.Select([]grmi.Predicate{meta.PackTemplateItemLst_PackTemplateId.NewPredicate(grmi.Equal, packTemplateId)}, []grmi.Field{meta.PackTemplateItemLst_Pos})
	if err != nil {
		return nil, grmi.NewBusinessError("查询包装模板子项失败, error:" + err.Error())
	}
	result.PackTemplateItemLstLi = detailLi
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackTemplateService.UpdateOne
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) UpdateOne(user *global.User, entity *model.PackTemplate) error {

	grmi.Log(user, "/services/jit/implments/PackTemplate.service.impl.go", "UpdateOnePackTemplate", "修改一个PackTemplate")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackTemplateDAO(session, user.PlantNr, user.UserId)
	templateLstDao := dal.NewPackTemplateItemLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.PackTemplateId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	if err = session.Begin(); err != nil {
		return grmi.NewBusinessError("开启事务失败, 错误:" + err.Error())
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	// 更新全局包装模板数据
	err = templateLstDao.InitGlobalPackTemplate()
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("初始化全局模板数据失败, error:" + err.Error())
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackTemplateService.Insert
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) Insert(user *global.User, entities *[]model.PackTemplate) error {

	grmi.Log(user, "/services/jit/implments/PackTemplate.service.impl.go", "InsertPackTemplate", "插入多个PackTemplate")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.PackTemplateId)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackTemplateService.Delete
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) Delete(user *global.User, entities *[]model.PackTemplate) error {

	grmi.Log(user, "/services/jit/implments/PackTemplate.service.impl.go", "DeletePackTemplate", "删除多个PackTemplate")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.PackTemplateId)
		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/jit/PackTemplateService.Select
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.PackTemplate, error) {

	grmi.Log(user, "/services/jit/implments/PackTemplate.service.impl.go", "SelectPackTemplate", "查询PackTemplate")

	condition := DefaultConditionOfPackTemplateAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewPackTemplateDAO(session, user.PlantNr, user.UserId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackTemplateService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

	grmi.Log(user, "/services/jit/implments/PackTemplate.service.impl.go", "SelectPackTemplate", "查询PackTemplate")

	condition := DefaultConditionOfPackTemplateAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewPackTemplateDAO(session, user.PlantNr, user.UserId)
	condition.Fill(urlParameters)
	result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/PackTemplateService.Update
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) Update(user *global.User, entities *[]model.PackTemplate) error {

	grmi.Log(user, "/services/jit/implments/PackTemplate.service.impl.go", "UpdatePackTemplate", "修改多个PackTemplate")

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

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

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