// 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              : Printer的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
var DefaultConditionOfPrinter = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Printer_PrinterId.UrlParameterName:     grmi.NewConditionItem(meta.Printer_PrinterId, grmi.Equal, true),
		meta.Printer_Descr.UrlParameterName:         grmi.NewConditionItem(meta.Printer_Descr, grmi.Equal, false),
		meta.Printer_PrinterType.UrlParameterName:   grmi.NewConditionItem(meta.Printer_PrinterType, grmi.Equal, false),
		meta.Printer_PrintMode.UrlParameterName:     grmi.NewConditionItem(meta.Printer_PrintMode, grmi.Equal, false),
		meta.Printer_PrinterSpec.UrlParameterName:   grmi.NewConditionItem(meta.Printer_PrinterSpec, grmi.Equal, false),
		meta.Printer_PrinterName.UrlParameterName:   grmi.NewConditionItem(meta.Printer_PrinterName, grmi.Equal, false),
		meta.Printer_Port.UrlParameterName:          grmi.NewConditionItem(meta.Printer_Port, grmi.Equal, false),
		meta.Printer_PrintInterval.UrlParameterName: grmi.NewConditionItem(meta.Printer_PrintInterval, grmi.Equal, false),
		meta.Printer_IP.UrlParameterName:            grmi.NewConditionItem(meta.Printer_IP, grmi.Equal, false),
		meta.Printer_PrinterPara1.UrlParameterName:  grmi.NewConditionItem(meta.Printer_PrinterPara1, grmi.Equal, false),
		meta.Printer_PrinterPara2.UrlParameterName:  grmi.NewConditionItem(meta.Printer_PrinterPara2, grmi.Equal, false),
		meta.Printer_PrinterSpec1.UrlParameterName:  grmi.NewConditionItem(meta.Printer_PrinterSpec1, grmi.Equal, false),
		meta.Printer_PrinterSpec2.UrlParameterName:  grmi.NewConditionItem(meta.Printer_PrinterSpec2, grmi.Equal, false),
		meta.Printer_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.Printer_LastModify, grmi.Approximate, false),
		meta.Printer_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.Printer_LastUser, grmi.Equal, false),
		meta.Printer_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.Printer_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : Printer的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
var DefaultConditionOfPrinterAndPaging = grmi.NewCondition(
	DefaultConditionOfPrinter.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : PrinterServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Printer的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
type PrinterServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewPrinterDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个PrinterDAOImplement实例
 *
 * @Return Value             : PrinterDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
func NewPrinterServiceImplement() *PrinterServiceImplement {
	return &PrinterServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/PrinterService.InsertOne
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) InsertOne(user *global.User, entity *model.Printer) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPrinterDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.PrinterId)
	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/base/PrinterService.DeleteOne
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) DeleteOne(user *global.User, printerId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/PrinterService.SelectOne
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) SelectOne(user *global.User, printerId string) (*model.Printer, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/PrinterService.UpdateOne
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) UpdateOne(user *global.User, entity *model.Printer) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/PrinterService.Insert
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) Insert(user *global.User, entities *[]model.Printer) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.PrinterId)
		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/base/PrinterService.Delete
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) Delete(user *global.User, entities *[]model.Printer) error {

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

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

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

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

	condition := DefaultConditionOfPrinterAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewPrinterDAO(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/base/PrinterService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

	condition := DefaultConditionOfPrinterAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewPrinterDAO(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/base/PrinterService.Update
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) Update(user *global.User, entities *[]model.Printer) error {

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

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

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

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