// 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              : ProductFamilyWorkLineLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-04-11 13:56:25
 *
 ******************************************************************************/
var DefaultConditionOfProductFamilyWorkLineLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ProductFamilyWorkLineLst_ProductFamilyId.UrlParameterName: grmi.NewConditionItem(meta.ProductFamilyWorkLineLst_ProductFamilyId, grmi.Equal, true),
		meta.ProductFamilyWorkLineLst_WorkLineId.UrlParameterName:      grmi.NewConditionItem(meta.ProductFamilyWorkLineLst_WorkLineId, grmi.Equal, true),
		meta.ProductFamilyWorkLineLst_WorkLineDescr.UrlParameterName:   grmi.NewConditionItem(meta.ProductFamilyWorkLineLst_WorkLineDescr, grmi.Equal, false),
		meta.ProductFamilyWorkLineLst_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.ProductFamilyWorkLineLst_LastModify, grmi.Approximate, false),
		meta.ProductFamilyWorkLineLst_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.ProductFamilyWorkLineLst_LastUser, grmi.Equal, false),
		meta.ProductFamilyWorkLineLst_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.ProductFamilyWorkLineLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ProductFamilyWorkLineLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-04-11 13:56:25
 *
 ******************************************************************************/
var DefaultConditionOfProductFamilyWorkLineLstAndPaging = grmi.NewCondition(
	DefaultConditionOfProductFamilyWorkLineLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ProductFamilyWorkLineLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ProductFamilyWorkLineLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-04-11 13:56:25
 *
 ******************************************************************************/
type ProductFamilyWorkLineLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewProductFamilyWorkLineLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProductFamilyWorkLineLstDAOImplement实例
 *
 * @Return Value             : ProductFamilyWorkLineLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-04-11 13:56:25
 *
 ******************************************************************************/
func NewProductFamilyWorkLineLstServiceImplement() *ProductFamilyWorkLineLstServiceImplement {
	return &ProductFamilyWorkLineLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/ProductFamilyWorkLineLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ProductFamilyWorkLineLstServiceImplement) InsertOne(user *global.User, entity *model.ProductFamilyWorkLineLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProductFamilyWorkLineLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ProductFamilyId, 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/ProductFamilyWorkLineLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProductFamilyWorkLineLstServiceImplement) DeleteOne(user *global.User, productFamilyId string, workLineId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/ProductFamilyWorkLineLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ProductFamilyWorkLineLstServiceImplement) SelectOne(user *global.User, productFamilyId string, workLineId string) (*model.ProductFamilyWorkLineLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/ProductFamilyWorkLineLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProductFamilyWorkLineLstServiceImplement) UpdateOne(user *global.User, entity *model.ProductFamilyWorkLineLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProductFamilyWorkLineLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ProductFamilyId, 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/ProductFamilyWorkLineLstService.Insert
 *
 ******************************************************************************/
func (impl *ProductFamilyWorkLineLstServiceImplement) Insert(user *global.User, entities *[]model.ProductFamilyWorkLineLst) error {

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

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

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

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

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

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

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

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

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

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

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ProductFamilyId, 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 *ProductFamilyWorkLineLstServiceImplement) Save(user *global.User, entity *model.ProductFamilyWorkLineData) 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.NewProductFamilyWorkLineLstDAO(session, user.PlantNr, user.UserId)
	productFamilyDao := dal.NewProductFamilyDAO(session, user.PlantNr, user.UserId)
	workLineDao := baseDal.NewWorkLineDAO(session, user.PlantNr, user.UserId)
	productFamilyId := entity.ProductFamilyId
	li := entity.WorkLineIdLi
	productFamily, err := productFamilyDao.SelectOne(productFamilyId)
	if err != nil {
		return grmi.NewBusinessError("查询派生数据失败, error:" + err.Error())
	}
	if productFamily == nil {
		return grmi.NewBusinessError("不存在对应的派生")
	}
	if !productFamily.MultiWorkLineToggle {
		return grmi.NewBusinessError("该派生未打开多产线开关!")
	}
	if len(li) <= 1 {
		return grmi.NewBusinessError("产线数量小于两条")
	}
	var dataLi []model.ProductFamilyWorkLineLst
	for _, workLineId := range li {
		var data model.ProductFamilyWorkLineLst
		data.ProductFamilyId = productFamilyId
		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_GROUP {
			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.ProductFamilyWorkLineLst_ProductFamilyId.NewPredicate(grmi.Equal, productFamilyId)})
	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
}

