// 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              : PrinterTemplate的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-20 16:59:51
 *
 ******************************************************************************/
var DefaultConditionOfPrinterTemplate = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PrinterTemplate_TemplateId.UrlParameterName:   grmi.NewConditionItem(meta.PrinterTemplate_TemplateId, grmi.Equal, true),
		meta.PrinterTemplate_TemplateTye.UrlParameterName:  grmi.NewConditionItem(meta.PrinterTemplate_TemplateTye, grmi.Equal, false),
		meta.PrinterTemplate_Descr.UrlParameterName:        grmi.NewConditionItem(meta.PrinterTemplate_Descr, grmi.Equal, false),
		meta.PrinterTemplate_Active.UrlParameterName:       grmi.NewConditionItem(meta.PrinterTemplate_Active, grmi.Equal, false),
		meta.PrinterTemplate_PrintQty.UrlParameterName:     grmi.NewConditionItem(meta.PrinterTemplate_PrintQty, grmi.Equal, false),
		meta.PrinterTemplate_PlanQty.UrlParameterName:      grmi.NewConditionItem(meta.PrinterTemplate_PlanQty, grmi.Equal, false),
		meta.PrinterTemplate_PrinterId.UrlParameterName:    grmi.NewConditionItem(meta.PrinterTemplate_PrinterId, grmi.Equal, false),
		meta.PrinterTemplate_TemplateFile.UrlParameterName: grmi.NewConditionItem(meta.PrinterTemplate_TemplateFile, grmi.Equal, false),
		meta.PrinterTemplate_Address.UrlParameterName:      grmi.NewConditionItem(meta.PrinterTemplate_Address, grmi.Equal, false),
		meta.PrinterTemplate_Cpara1.UrlParameterName:       grmi.NewConditionItem(meta.PrinterTemplate_Cpara1, grmi.Equal, false),
		meta.PrinterTemplate_Cpara2.UrlParameterName:       grmi.NewConditionItem(meta.PrinterTemplate_Cpara2, grmi.Equal, false),
		meta.PrinterTemplate_Cpara3.UrlParameterName:       grmi.NewConditionItem(meta.PrinterTemplate_Cpara3, grmi.Equal, false),
		meta.PrinterTemplate_Cpara4.UrlParameterName:       grmi.NewConditionItem(meta.PrinterTemplate_Cpara4, grmi.Equal, false),
		meta.PrinterTemplate_Cpara5.UrlParameterName:       grmi.NewConditionItem(meta.PrinterTemplate_Cpara5, grmi.Equal, false),
		meta.PrinterTemplate_Ipara1.UrlParameterName:       grmi.NewConditionItem(meta.PrinterTemplate_Ipara1, grmi.Equal, false),
		meta.PrinterTemplate_Ipara2.UrlParameterName:       grmi.NewConditionItem(meta.PrinterTemplate_Ipara2, grmi.Equal, false),
		meta.PrinterTemplate_Ipara3.UrlParameterName:       grmi.NewConditionItem(meta.PrinterTemplate_Ipara3, grmi.Equal, false),
		meta.PrinterTemplate_Ipara4.UrlParameterName:       grmi.NewConditionItem(meta.PrinterTemplate_Ipara4, grmi.Equal, false),
		meta.PrinterTemplate_Ipara5.UrlParameterName:       grmi.NewConditionItem(meta.PrinterTemplate_Ipara5, grmi.Equal, false),
		meta.PrinterTemplate_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.PrinterTemplate_LastModify, grmi.Approximate, false),
		meta.PrinterTemplate_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.PrinterTemplate_LastUser, grmi.Equal, false),
		meta.PrinterTemplate_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.PrinterTemplate_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : PrinterTemplate的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-20 16:59:51
 *
 ******************************************************************************/
var DefaultConditionOfPrinterTemplateAndPaging = grmi.NewCondition(
	DefaultConditionOfPrinterTemplate.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : PrinterTemplateServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : PrinterTemplate的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-20 16:59:51
 *
 ******************************************************************************/
type PrinterTemplateServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewPrinterTemplateDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个PrinterTemplateDAOImplement实例
 *
 * @Return Value             : PrinterTemplateDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-20 16:59:51
 *
 ******************************************************************************/
func NewPrinterTemplateServiceImplement() *PrinterTemplateServiceImplement {
	return &PrinterTemplateServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrinterTemplateService.InsertOne
 *
 ******************************************************************************/
func (impl *PrinterTemplateServiceImplement) InsertOne(user *models.Usertab, entity *model.PrinterTemplate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewPrinterTemplateDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.TemplateId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	/**处理数据**/
	for k, _ := range entity.PrinterTemplateLst {
		entity.PrinterTemplateLst[k].TemplateId = entity.TemplateId
		entity.PrinterTemplateLst[k].Pos = k + 1
	}
	lstdao := dal.NewPrinterTemplateLstDAO(session, user.Pid, user.Userid)
	err = lstdao.Insert(&entity.PrinterTemplateLst)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()

	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrinterTemplateService.DeleteOne
 *
 ******************************************************************************/
func (impl *PrinterTemplateServiceImplement) DeleteOne(user *models.Usertab, templateId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewPrinterTemplateDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(templateId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(templateId)
	if err != nil {
		session.Rollback()
		return err
	}
	lstdao := dal.NewPrinterTemplateLstDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.PrinterTemplateLst_TemplateId.NewPredicate(grmi.Equal, templateId),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()

	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrinterTemplateService.SelectOne
 *
 ******************************************************************************/
func (impl *PrinterTemplateServiceImplement) SelectOne(user *models.Usertab, templateId string) (*model.PrinterTemplate, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPrinterTemplateDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(templateId)
	if err != nil {
		return nil, err
	}
	lstdao := dal.NewPrinterTemplateLstDAO(session, user.Pid, user.Userid)
	list, err := lstdao.Select([]grmi.Predicate{
		meta.PrinterTemplateLst_TemplateId.NewPredicate(grmi.Equal, templateId),
	}, nil)
	if err != nil {
		return nil, err
	}
	result.PrinterTemplateLst = list
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrinterTemplateService.UpdateOne
 *
 ******************************************************************************/
func (impl *PrinterTemplateServiceImplement) UpdateOne(user *models.Usertab, entity *model.PrinterTemplate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewPrinterTemplateDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.TemplateId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}

	/**处理数据**/
	for k, _ := range entity.PrinterTemplateLst {
		entity.PrinterTemplateLst[k].TemplateId = entity.TemplateId
		entity.PrinterTemplateLst[k].Pos = k+1
	}
	lstdao := dal.NewPrinterTemplateLstDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.PrinterTemplateLst_TemplateId.NewPredicate(grmi.Equal, entity.TemplateId),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	err = lstdao.Insert(&entity.PrinterTemplateLst)
	if err != nil {
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrinterTemplateService.Insert
 *
 ******************************************************************************/
func (impl *PrinterTemplateServiceImplement) Insert(user *models.Usertab, entities *[]model.PrinterTemplate) error {

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

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

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

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

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

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

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

	condition := DefaultConditionOfPrinterTemplateAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewPrinterTemplateDAO(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/base/PrinterTemplateService.Update
 *
 ******************************************************************************/
func (impl *PrinterTemplateServiceImplement) Update(user *models.Usertab, entities *[]model.PrinterTemplate) error {

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

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

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

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