// 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"
	baseMeta "LAPP_ACURA_MOM_BACKEND/meta/base"
	meta "LAPP_ACURA_MOM_BACKEND/meta/me"
	baseModel "LAPP_ACURA_MOM_BACKEND/models/base"
	model "LAPP_ACURA_MOM_BACKEND/models/me"
)

/******************************************************************************
 *
 * @Description              : ShipableAUConfig的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-24 09:49:31
 *
 ******************************************************************************/
var DefaultConditionOfShipableAUConfig = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ShipableAUConfig_ArtId.UrlParameterName:       grmi.NewConditionItem(meta.ShipableAUConfig_ArtId, grmi.Equal, true),
		meta.ShipableAUConfig_AuPo.UrlParameterName:        grmi.NewConditionItem(meta.ShipableAUConfig_AuPo, grmi.Equal, false),
		meta.ShipableAUConfig_BarCodeRule.UrlParameterName: grmi.NewConditionItem(meta.ShipableAUConfig_BarCodeRule, grmi.Equal, false),
		meta.ShipableAUConfig_StrPara1.UrlParameterName:    grmi.NewConditionItem(meta.ShipableAUConfig_StrPara1, grmi.Equal, false),
		meta.ShipableAUConfig_StrPara2.UrlParameterName:    grmi.NewConditionItem(meta.ShipableAUConfig_StrPara2, grmi.Equal, false),
		meta.ShipableAUConfig_StrPara3.UrlParameterName:    grmi.NewConditionItem(meta.ShipableAUConfig_StrPara3, grmi.Equal, false),
		meta.ShipableAUConfig_IntPara1.UrlParameterName:    grmi.NewConditionItem(meta.ShipableAUConfig_IntPara1, grmi.Equal, false),
		meta.ShipableAUConfig_IntPara2.UrlParameterName:    grmi.NewConditionItem(meta.ShipableAUConfig_IntPara2, grmi.Equal, false),
		meta.ShipableAUConfig_IntPara3.UrlParameterName:    grmi.NewConditionItem(meta.ShipableAUConfig_IntPara3, grmi.Equal, false),
		meta.ShipableAUConfig_Toggle1.UrlParameterName:     grmi.NewConditionItem(meta.ShipableAUConfig_Toggle1, grmi.Equal, false),
		meta.ShipableAUConfig_Toggle2.UrlParameterName:     grmi.NewConditionItem(meta.ShipableAUConfig_Toggle2, grmi.Equal, false),
		meta.ShipableAUConfig_Toggle3.UrlParameterName:     grmi.NewConditionItem(meta.ShipableAUConfig_Toggle3, grmi.Equal, false),
		meta.ShipableAUConfig_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.ShipableAUConfig_LastModify, grmi.Approximate, false),
		meta.ShipableAUConfig_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.ShipableAUConfig_LastUser, grmi.Equal, false),
		meta.ShipableAUConfig_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.ShipableAUConfig_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ShipableAUConfig的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-24 09:49:31
 *
 ******************************************************************************/
var DefaultConditionOfShipableAUConfigAndPaging = grmi.NewCondition(
	DefaultConditionOfShipableAUConfig.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ShipableAUConfigServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ShipableAUConfig的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-24 09:49:31
 *
 ******************************************************************************/
type ShipableAUConfigServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewShipableAUConfigDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ShipableAUConfigDAOImplement实例
 *
 * @Return Value             : ShipableAUConfigDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-02-24 09:49:31
 *
 ******************************************************************************/
func NewShipableAUConfigServiceImplement() *ShipableAUConfigServiceImplement {
	return &ShipableAUConfigServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/ShipableAUConfigService.InsertOne
 *
 ******************************************************************************/
func (impl *ShipableAUConfigServiceImplement) InsertOne(user *global.User, entity *model.ShipableAUConfig) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewShipableAUConfigDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ArtId)
	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/ShipableAUConfigService.DeleteOne
 *
 ******************************************************************************/
func (impl *ShipableAUConfigServiceImplement) DeleteOne(user *global.User, artId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/ShipableAUConfigService.SelectOne
 *
 ******************************************************************************/
func (impl *ShipableAUConfigServiceImplement) SelectOne(user *global.User, artId string) (*model.ShipableAUConfig, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/me/ShipableAUConfigService.UpdateOne
 *
 ******************************************************************************/
func (impl *ShipableAUConfigServiceImplement) UpdateOne(user *global.User, entity *model.ShipableAUConfig) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// Save 保存
func (impl *ShipableAUConfigServiceImplement) Save(user *global.User, entity *model.ShipableAUConfig) error {
	grmi.Log(user, "/services/me/implments/ShipableAUConfig.service.impl.go", "Save", "保存ShipableAUConfig")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewShipableAUConfigDAO(session, user.PlantNr, user.UserId)
	articleDao := baseDal.NewArticleDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ArtId)
	if err != nil {
		return grmi.NewBusinessError("查询配置数据失败, 错误:" + err.Error())
	}
	processDao := baseDal.NewProcessDAO(session, user.PlantNr, user.UserId)
	if record == nil {
		articleLi, err := articleDao.Select([]grmi.Predicate{
			baseMeta.Article_ArtId.NewPredicate(grmi.Equal, entity.ArtId),
		}, nil)
		if err != nil {
			return grmi.NewBusinessError("查询物料失败, 错误:" + err.Error())
		}
		if len(articleLi) == 0 {
			return grmi.NewBusinessError("物料不存在")
		}
		if articleLi[0].ArtSpec1 != baseModel.ART_TYPE_SEMI_FINISHED {
			return grmi.NewBusinessError("物料不是半成品, 不能执行该操作")
		}
		process, err := processDao.SelectOne(entity.AuPo)
		if err != nil {
			return grmi.NewBusinessError("查询工序失败，错误：" + err.Error())
		}
		if process == nil {
			return grmi.NewBusinessError("指定工序不存在")
		}
		err = dao.InsertOne(entity)
		if err != nil {
			return grmi.NewBusinessError("保存配置数据失败, 错误:" + err.Error())
		}
		return nil
	} else {
		if record.AuPo != entity.AuPo {
			process, err := processDao.SelectOne(entity.AuPo)
			if err != nil {
				return grmi.NewBusinessError("查询工序失败，错误：" + err.Error())
			}
			if process == nil {
				return grmi.NewBusinessError("指定工序不存在")
			}
		}
		err = dao.UpdateOne(entity)
		if err != nil {
			return grmi.NewBusinessError("更新配置数据失败, 错误:" + err.Error())
		}
		return nil
	}

}