// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	baseDal "LAPP_ACURA_MOM_BACKEND/dao/base"
	dal "LAPP_ACURA_MOM_BACKEND/dao/me"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/me"
	baseModel "LAPP_ACURA_MOM_BACKEND/models/base"
	model "LAPP_ACURA_MOM_BACKEND/models/me"
)

/******************************************************************************
 *
 * @Description              : ProductWorkLineLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-28 13:12:09
 *
 ******************************************************************************/
var DefaultConditionOfProductWorkLineLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ProductWorkLineLst_ProductId.UrlParameterName:     grmi.NewConditionItem(meta.ProductWorkLineLst_ProductId, grmi.Equal, true),
		meta.ProductWorkLineLst_ProjectId.UrlParameterName:     grmi.NewConditionItem(meta.ProductWorkLineLst_ProjectId, grmi.Equal, true),
		meta.ProductWorkLineLst_WorkLineId.UrlParameterName:    grmi.NewConditionItem(meta.ProductWorkLineLst_WorkLineId, grmi.Equal, true),
		meta.ProductWorkLineLst_WorkLineDescr.UrlParameterName: grmi.NewConditionItem(meta.ProductWorkLineLst_WorkLineDescr, grmi.Equal, false),
		meta.ProductWorkLineLst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.ProductWorkLineLst_LastModify, grmi.Approximate, false),
		meta.ProductWorkLineLst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.ProductWorkLineLst_LastUser, grmi.Equal, false),
		meta.ProductWorkLineLst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.ProductWorkLineLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ProductWorkLineLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-28 13:12:09
 *
 ******************************************************************************/
var DefaultConditionOfProductWorkLineLstAndPaging = grmi.NewCondition(
	DefaultConditionOfProductWorkLineLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ProductWorkLineLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ProductWorkLineLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-28 13:12:09
 *
 ******************************************************************************/
type ProductWorkLineLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewProductWorkLineLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProductWorkLineLstDAOImplement实例
 *
 * @Return Value             : ProductWorkLineLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-28 13:12:09
 *
 ******************************************************************************/
func NewProductWorkLineLstServiceImplement() *ProductWorkLineLstServiceImplement {
	return &ProductWorkLineLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/ProductWorkLineLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ProductWorkLineLstServiceImplement) InsertOne(user *global.User, entity *model.ProductWorkLineLst) error {

	grmi.Log(user, "/services/me/implments/ProductWorkLineLst.service.impl.go", "InsertOneProductWorkLineLst", "插入一个ProductWorkLineLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProductWorkLineLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ProductId, entity.WorkLineId)
	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/me/ProductWorkLineLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProductWorkLineLstServiceImplement) DeleteOne(user *global.User, productId string, workLineId string) error {

	grmi.Log(user, "/services/me/implments/ProductWorkLineLst.service.impl.go", "DeleteOneProductWorkLineLst", "删除一个ProductWorkLineLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/ProductWorkLineLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ProductWorkLineLstServiceImplement) SelectOne(user *global.User, productId string, workLineId string) (*model.ProductWorkLineLst, error) {

	grmi.Log(user, "/services/me/implments/ProductWorkLineLst.service.impl.go", "SelectOneProductWorkLineLst", "查询一个ProductWorkLineLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/ProductWorkLineLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProductWorkLineLstServiceImplement) UpdateOne(user *global.User, entity *model.ProductWorkLineLst) error {

	grmi.Log(user, "/services/me/implments/ProductWorkLineLst.service.impl.go", "UpdateOneProductWorkLineLst", "修改一个ProductWorkLineLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProductWorkLineLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ProductId, entity.WorkLineId)
	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/me/ProductWorkLineLstService.Insert
 *
 ******************************************************************************/
func (impl *ProductWorkLineLstServiceImplement) Insert(user *global.User, entities *[]model.ProductWorkLineLst) error {

	grmi.Log(user, "/services/me/implments/ProductWorkLineLst.service.impl.go", "InsertProductWorkLineLst", "插入多个ProductWorkLineLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ProductId, entity.WorkLineId)
		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/me/ProductWorkLineLstService.Delete
 *
 ******************************************************************************/
func (impl *ProductWorkLineLstServiceImplement) Delete(user *global.User, entities *[]model.ProductWorkLineLst) error {

	grmi.Log(user, "/services/me/implments/ProductWorkLineLst.service.impl.go", "DeleteProductWorkLineLst", "删除多个ProductWorkLineLst")

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

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

	grmi.Log(user, "/services/me/implments/ProductWorkLineLst.service.impl.go", "SelectProductWorkLineLst", "查询ProductWorkLineLst")

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

	grmi.Log(user, "/services/me/implments/ProductWorkLineLst.service.impl.go", "SelectProductWorkLineLst", "查询ProductWorkLineLst")

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

	grmi.Log(user, "/services/me/implments/ProductWorkLineLst.service.impl.go", "UpdateProductWorkLineLst", "修改多个ProductWorkLineLst")

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProductWorkLineLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ProductWorkLineLstServiceImplement) Save(user *global.User, entity *model.ProductWorkLineData) error {

	grmi.Log(user, "/services/base/implments/ArtWorkLineLstService.service.impl.go", "Save", "bao ")

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

	dao := dal.NewProductWorkLineLstDAO(session, user.PlantNr, user.UserId)
	productDao := dal.NewProductDAO(session, user.PlantNr, user.UserId)
	workLineDao := baseDal.NewWorkLineDAO(session, user.PlantNr, user.UserId)
	productId := entity.ProductId
	projectId := entity.ProjectId
	li := entity.WorkLineIdLi
	product, err := productDao.SelectOne(productId, projectId)
	if err != nil {
		return grmi.NewBusinessError("查询产品数据失败, error:" + err.Error())
	}
	if product == nil {
		return grmi.NewBusinessError("不存在对应的产品, 产品ID:" + productId)
	}
	if !product.MultiWorkLineToggle {
		return grmi.NewBusinessError("该总成未打开多产线开关!")
	}
	if len(li) <= 1 {
		return grmi.NewBusinessError("产线数量小于两条")
	}
	var dataLi []model.ProductWorkLineLst
	for _, workLineId := range li {
		var data model.ProductWorkLineLst
		data.ProductId = productId
		data.ProjectId = projectId
		data.WorkLineId = workLineId
		workLine, err := workLineDao.SelectOne(data.WorkLineId)
		if err != nil {
			return grmi.NewBusinessError("查询产线数据失败, error:" + err.Error())
		}
		if workLine == nil {
			return grmi.NewBusinessError("不存在对应的产线, 产线ID:" + data.WorkLineId)
		}
		if workLine.GroupType != baseModel.WORK_LINE_TYPE_REAL {
			return grmi.NewBusinessError("关联的产线不是实体产线, 产线ID:" + data.WorkLineId)
		}
		data.WorkLineDescr = workLine.Descr
		dataLi = append(dataLi, data)
	}
	if err = session.Begin(); err != nil {
		return err
	}
	err = dao.DeleteWhere([]grmi.Predicate{
		meta.ProductWorkLineLst_ProductId.NewPredicate(grmi.Equal, productId),
		meta.ProductWorkLineLst_ProjectId.NewPredicate(grmi.Equal, projectId),
	})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除原有数据失败, error:" + err.Error())
	}
	err = dao.Insert(&dataLi)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("保存失败, error:" + err.Error())
	}
	_ = session.Commit()
	return nil
}
