// 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              : Printer的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
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_PrinterName.UrlParameterName:   grmi.NewConditionItem(meta.Printer_PrinterName, grmi.Equal, false),
		meta.Printer_IpAddress.UrlParameterName:     grmi.NewConditionItem(meta.Printer_IpAddress, grmi.Equal, false),
		meta.Printer_Active.UrlParameterName:        grmi.NewConditionItem(meta.Printer_Active, grmi.Equal, false),
		meta.Printer_PrinterSpec.UrlParameterName:   grmi.NewConditionItem(meta.Printer_PrinterSpec, grmi.Equal, false),
		meta.Printer_PageSize.UrlParameterName:      grmi.NewConditionItem(meta.Printer_PageSize, 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_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-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfPrinterAndPaging = grmi.NewCondition(
	DefaultConditionOfPrinter.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : PrinterServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Printer的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
type PrinterServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewPrinterDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个PrinterDAOImplement实例
 *
 * @Return Value             : PrinterDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
func NewPrinterServiceImplement() *PrinterServiceImplement {
	return &PrinterServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrinterService.InsertOne
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) InsertOne(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/base/PrinterService.DeleteOne
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) DeleteOne(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/base/PrinterService.SelectOne
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) SelectOne(user *models.Usertab, 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.Pid, user.Userid)
	result, err := dao.SelectOne(printerId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrinterService.UpdateOne
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) UpdateOne(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/base/PrinterService.Insert
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) Insert(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/base/PrinterService.Delete
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) Delete(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/base/PrinterService.Select
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, 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.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/PrinterService.Update
 *
 ******************************************************************************/
func (impl *PrinterServiceImplement) Update(user *models.Usertab, 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.Pid, 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
}
