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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/log"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/log"
	model "LAPP_LF_MOM_BACKEND/models/log"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : PackTemplate的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfPackTemplate = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PackTemplate_PackTemplateId.UrlParameterName: grmi.NewConditionItem(meta.PackTemplate_PackTemplateId, grmi.Equal, true),
		meta.PackTemplate_Descr.UrlParameterName:          grmi.NewConditionItem(meta.PackTemplate_Descr, grmi.Equal, false),
		meta.PackTemplate_ActiveToggle.UrlParameterName:   grmi.NewConditionItem(meta.PackTemplate_ActiveToggle, grmi.Equal, false),
		meta.PackTemplate_PackTypeId.UrlParameterName:     grmi.NewConditionItem(meta.PackTemplate_PackTypeId, 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_PrintQty.UrlParameterName:       grmi.NewConditionItem(meta.PackTemplate_PrintQty, grmi.Equal, false),
		meta.PackTemplate_PackOrderSn.UrlParameterName:    grmi.NewConditionItem(meta.PackTemplate_PackOrderSn, grmi.Equal, false),
		meta.PackTemplate_BoxSn.UrlParameterName:          grmi.NewConditionItem(meta.PackTemplate_BoxSn, grmi.Equal, false),
		meta.PackTemplate_SyncToggle.UrlParameterName:     grmi.NewConditionItem(meta.PackTemplate_SyncToggle, grmi.Equal, false),
		meta.PackTemplate_SyncSn.UrlParameterName:         grmi.NewConditionItem(meta.PackTemplate_SyncSn, grmi.Equal, false),
		meta.PackTemplate_TwoDSheet.UrlParameterName:      grmi.NewConditionItem(meta.PackTemplate_TwoDSheet, grmi.Equal, false),
		meta.PackTemplate_FullFillToggle.UrlParameterName: grmi.NewConditionItem(meta.PackTemplate_FullFillToggle, grmi.Equal, false),
		meta.PackTemplate_MaterialId.UrlParameterName:     grmi.NewConditionItem(meta.PackTemplate_MaterialId, grmi.Equal, false),
		meta.PackTemplate_PlanQty.UrlParameterName:        grmi.NewConditionItem(meta.PackTemplate_PlanQty, grmi.Equal, false),
		meta.PackTemplate_PKTMPCustC1.UrlParameterName:    grmi.NewConditionItem(meta.PackTemplate_PKTMPCustC1, grmi.Equal, false),
		meta.PackTemplate_PKTMPCustC2.UrlParameterName:    grmi.NewConditionItem(meta.PackTemplate_PKTMPCustC2, grmi.Equal, false),
		meta.PackTemplate_PKTMPCustI1.UrlParameterName:    grmi.NewConditionItem(meta.PackTemplate_PKTMPCustI1, grmi.Equal, false),
		meta.PackTemplate_PKTMPCustI2.UrlParameterName:    grmi.NewConditionItem(meta.PackTemplate_PKTMPCustI2, grmi.Equal, false),
		meta.PackTemplate_ShipTo.UrlParameterName:         grmi.NewConditionItem(meta.PackTemplate_ShipTo, grmi.Equal, false),
		meta.PackTemplate_CustArtId.UrlParameterName:      grmi.NewConditionItem(meta.PackTemplate_CustArtId, grmi.Equal, false),
		meta.PackTemplate_Descr2.UrlParameterName:         grmi.NewConditionItem(meta.PackTemplate_Descr2, grmi.Equal, false),
		meta.PackTemplate_SiteCode.UrlParameterName:       grmi.NewConditionItem(meta.PackTemplate_SiteCode, grmi.Equal, 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-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfPackTemplateAndPaging = grmi.NewCondition(
	DefaultConditionOfPackTemplate.Items,
	grmi.NewPaging(),
)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackTemplateService.InsertOne
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) InsertOne(user *models.Usertab, entity *model.PackTemplate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewPackTemplateDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PackTemplateId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	packTypeDAO := dal.NewPackTypeDAO(session, user.Pid, user.Userid)
	packType, err := packTypeDAO.SelectOne(entity.PackTypeId)
	if err != nil {
		return err
	}
	if packType == nil {
		return grmi.NewBusinessError("不存在包装类型!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	packTemplateItemDAO := dal.NewPackTemplateItemlstDAO(session, user.Pid, user.Userid)
	if entity.Items != nil {
		for i := 0; i < len(entity.Items); i++ {
			item := &(entity.Items[i])
			item.PackTemplateId = entity.PackTemplateId
			item.Pos = i + 1
		}
		err = packTemplateItemDAO.Insert(&entity.Items)
		if err != nil {
			return err
		}
	}
	err = session.Commit()
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackTemplateService.DeleteOne
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) DeleteOne(user *models.Usertab, packTemplateId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackTemplateService.SelectOne
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) SelectOne(user *models.Usertab, packTemplateId string) (*model.PackTemplate, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackTemplateDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(packTemplateId)
	if err != nil {
		return nil, err
	}
	packTemplateItemlstDAO := dal.NewPackTemplateItemlstDAO(session, user.Pid, user.Userid)
	items, err := packTemplateItemlstDAO.Select([]grmi.Predicate{meta.PackTemplateItemlst_PackTemplateId.NewPredicate(grmi.Equal, packTemplateId)}, nil)
	if err != nil {
		return nil, err
	}
	result.Items = items
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackTemplateService.UpdateOne
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) UpdateOne(user *models.Usertab, entity *model.PackTemplate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewPackTemplateDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PackTemplateId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	packTypeDAO := dal.NewPackTypeDAO(session, user.Pid, user.Userid)
	packType, err := packTypeDAO.SelectOne(entity.PackTypeId)
	if err != nil {
		return err
	}
	if packType == nil {
		return grmi.NewBusinessError("不存在包装类型!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	packTemplateItemDAO := dal.NewPackTemplateItemlstDAO(session, user.Pid, user.Userid)
	err = packTemplateItemDAO.DeleteWhere([]grmi.Predicate{meta.PackTemplateItemlst_PackTemplateId.NewPredicate(grmi.Equal, entity.PackTemplateId)})
	if err != nil {
		return err
	}
	if entity.Items != nil {
		for i := 0; i < len(entity.Items); i++ {
			item := &(entity.Items[i])
			item.PackTemplateId = entity.PackTemplateId
			item.Pos = i + 1
		}
		err = packTemplateItemDAO.Insert(&entity.Items)
		if err != nil {
			return err
		}
	}
	err = session.Commit()
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackTemplateService.Insert
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) Insert(user *models.Usertab, entities *[]model.PackTemplate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackTemplateDAO(session, user.Pid, 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_LF_MOM_BACKEND/services/log/PackTemplateService.Delete
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) Delete(user *models.Usertab, entities *[]model.PackTemplate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackTemplateDAO(session, user.Pid, 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_LF_MOM_BACKEND/services/log/PackTemplateService.Select
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/log/implments/LOG_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.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/log/PackTemplateService.Update
 *
 ******************************************************************************/
func (impl *PackTemplateServiceImplement) Update(user *models.Usertab, entities *[]model.PackTemplate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackTemplateDAO(session, user.Pid, 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
}
