// 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              : PrintHead的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfPrintHead = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PrintHead_PrintHeadId.UrlParameterName:    grmi.NewConditionItem(meta.PrintHead_PrintHeadId, grmi.Equal, true),
		meta.PrintHead_PrinterId.UrlParameterName:      grmi.NewConditionItem(meta.PrintHead_PrinterId, grmi.Equal, false),
		meta.PrintHead_PrintFileType.UrlParameterName:  grmi.NewConditionItem(meta.PrintHead_PrintFileType, grmi.Equal, false),
		meta.PrintHead_PrintEventType.UrlParameterName: grmi.NewConditionItem(meta.PrintHead_PrintEventType, grmi.Equal, false),
		meta.PrintHead_PrintObjId.UrlParameterName:     grmi.NewConditionItem(meta.PrintHead_PrintObjId, grmi.Equal, false),
		meta.PrintHead_PrintObjType.UrlParameterName:   grmi.NewConditionItem(meta.PrintHead_PrintObjType, grmi.Equal, false),
		meta.PrintHead_Status.UrlParameterName:         grmi.NewConditionItem(meta.PrintHead_Status, grmi.Equal, false),
		meta.PrintHead_TemplateFile.UrlParameterName:   grmi.NewConditionItem(meta.PrintHead_TemplateFile, grmi.Equal, false),
		meta.PrintHead_TermId.UrlParameterName:         grmi.NewConditionItem(meta.PrintHead_TermId, grmi.Equal, false),
		meta.PrintHead_WebAddress.UrlParameterName:     grmi.NewConditionItem(meta.PrintHead_WebAddress, grmi.Equal, false),
		meta.PrintHead_PrintCopies.UrlParameterName:    grmi.NewConditionItem(meta.PrintHead_PrintCopies, grmi.Equal, false),
		meta.PrintHead_PrintedTime.UrlParameterName:    grmi.NewConditionItem(meta.PrintHead_PrintedTime, grmi.Equal, false),
		meta.PrintHead_PrintedCopies.UrlParameterName:  grmi.NewConditionItem(meta.PrintHead_PrintedCopies, grmi.Equal, false),
		meta.PrintHead_Ival1.UrlParameterName:          grmi.NewConditionItem(meta.PrintHead_Ival1, grmi.Equal, false),
		meta.PrintHead_Ival2.UrlParameterName:          grmi.NewConditionItem(meta.PrintHead_Ival2, grmi.Equal, false),
		meta.PrintHead_Cval1.UrlParameterName:          grmi.NewConditionItem(meta.PrintHead_Cval1, grmi.Equal, false),
		meta.PrintHead_Cval2.UrlParameterName:          grmi.NewConditionItem(meta.PrintHead_Cval2, grmi.Equal, false),
		meta.PrintHead_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.PrintHead_LastModify, grmi.Approximate, false),
		meta.PrintHead_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.PrintHead_LastUser, grmi.Equal, false),
		meta.PrintHead_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.PrintHead_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : PrintHead的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfPrintHeadAndPaging = grmi.NewCondition(
	DefaultConditionOfPrintHead.Items,
	grmi.NewPaging(),
)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrintHeadService.InsertOne
 *
 ******************************************************************************/
func (impl *PrintHeadServiceImplement) InsertOne(user *models.Usertab, entity *model.PrintHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPrintHeadDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PrintHeadId)
	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/PrintHeadService.DeleteOne
 *
 ******************************************************************************/
func (impl *PrintHeadServiceImplement) DeleteOne(user *models.Usertab, printHeadId string) error {

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

	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.NewPrintHeadDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(printHeadId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(printHeadId)
	if err != nil {
		session.Rollback()
		return err
	}
	//删除对应的记录
	lstdao := dal.NewPrintDetailDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.PrintDetail_PrintHeadId.NewPredicate(grmi.Equal, printHeadId),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrintHeadService.SelectOne
 *
 ******************************************************************************/
func (impl *PrintHeadServiceImplement) SelectOne(user *models.Usertab, printHeadId string) (*model.PrintHead, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPrintHeadDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(printHeadId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrintHeadService.UpdateOne
 *
 ******************************************************************************/
func (impl *PrintHeadServiceImplement) UpdateOne(user *models.Usertab, entity *model.PrintHead) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrintHeadService.Insert
 *
 ******************************************************************************/
func (impl *PrintHeadServiceImplement) Insert(user *models.Usertab, entities *[]model.PrintHead) error {

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

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

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

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

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

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

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

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

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

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

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

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