// 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              : WorkLineProcessLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
var DefaultConditionOfWorkLineProcessLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkLineProcessLst_WorkLineId.UrlParameterName: grmi.NewConditionItem(meta.WorkLineProcessLst_WorkLineId, grmi.Equal, true),
		meta.WorkLineProcessLst_PO.UrlParameterName:         grmi.NewConditionItem(meta.WorkLineProcessLst_PO, grmi.Equal, true),
		meta.WorkLineProcessLst_PODescr.UrlParameterName:    grmi.NewConditionItem(meta.WorkLineProcessLst_PODescr, grmi.Equal, false),
		meta.WorkLineProcessLst_Pos.UrlParameterName:        grmi.NewConditionItem(meta.WorkLineProcessLst_Pos, grmi.Equal, false),
		meta.WorkLineProcessLst_LastModify.UrlParameterName: grmi.NewConditionItem(meta.WorkLineProcessLst_LastModify, grmi.Approximate, false),
		meta.WorkLineProcessLst_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.WorkLineProcessLst_LastUser, grmi.Equal, false),
		meta.WorkLineProcessLst_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.WorkLineProcessLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkLineProcessLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
var DefaultConditionOfWorkLineProcessLstAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkLineProcessLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkLineProcessLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkLineProcessLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
type WorkLineProcessLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkLineProcessLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkLineProcessLstDAOImplement实例
 *
 * @Return Value             : WorkLineProcessLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
func NewWorkLineProcessLstServiceImplement() *WorkLineProcessLstServiceImplement {
	return &WorkLineProcessLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineProcessLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkLineProcessLstServiceImplement) InsertOne(user *global.User, entity *model.WorkLineProcessLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineProcessLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.WorkLineId, entity.PO)
	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/WorkLineProcessLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkLineProcessLstServiceImplement) DeleteOne(user *global.User, workLineId string, pO string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineProcessLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkLineProcessLstServiceImplement) SelectOne(user *global.User, workLineId string, pO string) (*model.WorkLineProcessLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineProcessLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkLineProcessLstServiceImplement) UpdateOne(user *global.User, entity *model.WorkLineProcessLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineProcessLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.WorkLineId, entity.PO)
	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/WorkLineProcessLstService.Insert
 *
 ******************************************************************************/
func (impl *WorkLineProcessLstServiceImplement) Insert(user *global.User, entities *model.WorkLineProcessData) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()

	workLineDao := dal.NewWorkLineDAO(session, user.PlantNr, user.UserId)
	processDao := dal.NewProcessDAO(session, user.PlantNr, user.UserId)
	dao := dal.NewWorkLineProcessLstDAO(session, user.PlantNr, user.UserId)
	workLineId := entities.WorkLineId
	li := entities.WorkLineProcessLstLi
	workLine, err := workLineDao.SelectOne(workLineId)
	if err != nil {
		return grmi.NewBusinessError("查询产线数据失败, error:" + err.Error())
	}
	if workLine == nil {
		return grmi.NewBusinessError("产线不存在, 产线ID:" + workLineId)
	}
	if workLine.GroupType != model.WORK_LINE_TYPE_REAL {
		return grmi.NewBusinessError("产线不是实体产线, 不能绑定工序")
	}

	for index, data := range li {
		process, err := processDao.SelectOne(data.PO)
		if err != nil {
			return grmi.NewBusinessError("查询工序数据失败, error:" + err.Error())
		}
		if process == nil {
			return grmi.NewBusinessError("工序不存在, 工序ID:" + data.PO)
		}
		data.PODescr = process.Descr
		data.WorkLineId = workLineId
		data.Pos = index + 1
		li[index] = data
	}
	if err = session.Begin(); err != nil {
		return err
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.WorkLineProcessLst_WorkLineId.NewPredicate(grmi.Equal, workLineId)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除原有数据失败, error:" + err.Error())
	}
	tempLi := make([]model.WorkLineProcessLst, 0, 20)
	for index, data := range li {
		if index != 0 && index % 20 == 0 {
			err = dao.Insert(&tempLi)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("写入数据失败, error:" + err.Error())
			}
			tempLi = make([]model.WorkLineProcessLst, 0, 20)
		}
		tempLi = append(tempLi, data)
	}
	if len(tempLi) != 0 {
		err = dao.Insert(&tempLi)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("写入数据失败, error:" + err.Error())
		}
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineProcessLstService.Delete
 *
 ******************************************************************************/
func (impl *WorkLineProcessLstServiceImplement) Delete(user *global.User, entities *[]model.WorkLineProcessLst) error {

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

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

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

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

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

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

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

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

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

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

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