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

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/om"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/om"
	model "LAPP_ACURA_MOM_BACKEND/models/om"
)

/******************************************************************************
 *
 * @Description              : SerialOrderDisassemble的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-03-28 09:55:48
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderDisassemble = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SerialOrderDisassemble_SerialOrderId.UrlParameterName: grmi.NewConditionItem(meta.SerialOrderDisassemble_SerialOrderId, grmi.Equal, true),
		meta.SerialOrderDisassemble_Pos.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderDisassemble_Pos, grmi.Equal, true),
		meta.SerialOrderDisassemble_CtrlPara1.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderDisassemble_CtrlPara1, grmi.Equal, false),
		meta.SerialOrderDisassemble_CtrlPara2.UrlParameterName:     grmi.NewConditionItem(meta.SerialOrderDisassemble_CtrlPara2, grmi.Equal, false),
		meta.SerialOrderDisassemble_IntPara1.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderDisassemble_IntPara1, grmi.Equal, false),
		meta.SerialOrderDisassemble_IntPara2.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderDisassemble_IntPara2, grmi.Equal, false),
		meta.SerialOrderDisassemble_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderDisassemble_LastModify, grmi.Approximate, false),
		meta.SerialOrderDisassemble_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderDisassemble_LastUser, grmi.Equal, false),
		meta.SerialOrderDisassemble_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderDisassemble_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : SerialOrderDisassemble的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-03-28 09:55:48
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderDisassembleAndPaging = grmi.NewCondition(
	DefaultConditionOfSerialOrderDisassemble.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : SerialOrderDisassembleServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : SerialOrderDisassemble的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-03-28 09:55:48
 *
 ******************************************************************************/
type SerialOrderDisassembleServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewSerialOrderDisassembleDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SerialOrderDisassembleDAOImplement实例
 *
 * @Return Value             : SerialOrderDisassembleDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-03-28 09:55:48
 *
 ******************************************************************************/
func NewSerialOrderDisassembleServiceImplement() *SerialOrderDisassembleServiceImplement {
	return &SerialOrderDisassembleServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderDisassembleService.InsertOne
 *
 ******************************************************************************/
func (impl *SerialOrderDisassembleServiceImplement) InsertOne(user *global.User, entity *model.SerialOrderDisassemble) error {

	grmi.Log(user, "/services/om/implments/SerialOrderDisassemble.service.impl.go", "InsertOneSerialOrderDisassemble", "插入一个SerialOrderDisassemble")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderDisassembleDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.SerialOrderId, entity.Pos)
	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/om/SerialOrderDisassembleService.DeleteOne
 *
 ******************************************************************************/
func (impl *SerialOrderDisassembleServiceImplement) DeleteOne(user *global.User, serialOrderId string, pos int) error {

	grmi.Log(user, "/services/om/implments/SerialOrderDisassemble.service.impl.go", "DeleteOneSerialOrderDisassemble", "删除一个SerialOrderDisassemble")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderDisassembleService.SelectOne
 *
 ******************************************************************************/
func (impl *SerialOrderDisassembleServiceImplement) SelectOne(user *global.User, serialOrderId string, pos int) (*model.SerialOrderDisassemble, error) {

	grmi.Log(user, "/services/om/implments/SerialOrderDisassemble.service.impl.go", "SelectOneSerialOrderDisassemble", "查询一个SerialOrderDisassemble")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderDisassembleService.UpdateOne
 *
 ******************************************************************************/
func (impl *SerialOrderDisassembleServiceImplement) UpdateOne(user *global.User, entity *model.SerialOrderDisassemble) error {

	grmi.Log(user, "/services/om/implments/SerialOrderDisassemble.service.impl.go", "UpdateOneSerialOrderDisassemble", "修改一个SerialOrderDisassemble")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderDisassembleDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.SerialOrderId, entity.Pos)
	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/om/SerialOrderDisassembleService.Insert
 *
 ******************************************************************************/
func (impl *SerialOrderDisassembleServiceImplement) Insert(user *global.User, entities *[]model.SerialOrderDisassemble) error {

	grmi.Log(user, "/services/om/implments/SerialOrderDisassemble.service.impl.go", "InsertSerialOrderDisassemble", "插入多个SerialOrderDisassemble")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.SerialOrderId, entity.Pos)
		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/om/SerialOrderDisassembleService.Delete
 *
 ******************************************************************************/
func (impl *SerialOrderDisassembleServiceImplement) Delete(user *global.User, entities *[]model.SerialOrderDisassemble) error {

	grmi.Log(user, "/services/om/implments/SerialOrderDisassemble.service.impl.go", "DeleteSerialOrderDisassemble", "删除多个SerialOrderDisassemble")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.SerialOrderId, entity.Pos)
		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/om/SerialOrderDisassembleService.Select
 *
 ******************************************************************************/
func (impl *SerialOrderDisassembleServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.SerialOrderDisassemble, error) {

	grmi.Log(user, "/services/om/implments/SerialOrderDisassemble.service.impl.go", "SelectSerialOrderDisassemble", "查询SerialOrderDisassemble")

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

	grmi.Log(user, "/services/om/implments/SerialOrderDisassemble.service.impl.go", "SelectSerialOrderDisassemble", "查询SerialOrderDisassemble")

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

	grmi.Log(user, "/services/om/implments/SerialOrderDisassemble.service.impl.go", "UpdateSerialOrderDisassemble", "修改多个SerialOrderDisassemble")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.SerialOrderId, entity.Pos)
		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/om/SerialOrderDisassembleService.InsertSerialOrderRecord
 *
 ******************************************************************************/
func (impl *SerialOrderDisassembleServiceImplement) InsertSerialOrderRecord(user *global.User, serialOrderId string) error {

	grmi.Log(user, "/services/om/implments/SerialOrderDisassemble.service.impl.go", "InsertSerialOrderRecord", "查询返修拆解记录")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderDisassembleDAO(session, user.PlantNr, user.UserId)
	serialOrderDao := dal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	serialOrder, err := serialOrderDao.SelectOne(serialOrderId)
	if err != nil {
		return grmi.NewBusinessError("查询工单失败, 错误:" + err.Error())
	}
	if serialOrder == nil {
		return grmi.NewBusinessError("工单状态不存在")
	}
	maxPos, err := dao.GetMaxPos(serialOrderId)
	if err != nil {
		return grmi.NewBusinessError("查询工单记录失败, 错误：" + err.Error())
	}
	data := model.SerialOrderDisassemble{
		SerialOrderId: serialOrderId,
		Pos: maxPos+1,
	}
	err = dao.InsertOne(&data)
	if err != nil {
		return grmi.NewBusinessError("写入工单拆解记录失败, 错误:" + err.Error())
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderDisassembleService.SelectSerialOrderRecord
 *
 ******************************************************************************/
func (impl *SerialOrderDisassembleServiceImplement) SelectSerialOrderRecord(user *global.User, serialOrderId string) ([]model.SerialOrderDisassemble, error) {

	grmi.Log(user, "/services/om/implments/SerialOrderDisassemble.service.impl.go", "SelectSerialOrderRecord", "查询返修拆解记录")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderDisassembleDAO(session, user.PlantNr, user.UserId)
	serialOrderDao := dal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	serialOrder, err := serialOrderDao.SelectOne(serialOrderId)
	if err != nil {
		return nil, grmi.NewBusinessError("查询工单失败, 错误:" + err.Error())
	}
	if serialOrder == nil {
		return nil, grmi.NewBusinessError("工单状态不存在")
	}
	//orderIdLi := make([]interface{}, 0)
	//if serialOrder.OriginSerialOrderId != "" {
	//	serialOrderLi, err := serialOrderDao.Select([]grmi.Predicate{
	//		meta.SerialOrder_SerialOrderId.NewPredicate(grmi.Equal, serialOrder.OriginSerialOrderId),
	//	}, nil)
	//	if err != nil {
	//		return nil, grmi.NewBusinessError("查询原始工单数据失败, 错误:" + err.Error())
	//	}
	//	orderIdLi = append(orderIdLi, serialOrder.OriginSerialOrderId)
	//	for _, innerSerialOrder := range serialOrderLi {
	//		orderIdLi = append(orderIdLi, innerSerialOrder.SerialOrderId)
	//	}
	//} else {
	//	orderIdLi = append(orderIdLi, serialOrderId)
	//}
	disassembleLi, err := dao.Select([]grmi.Predicate{
		meta.SerialOrderDisassemble_SerialOrderId.NewPredicate(grmi.Equal, serialOrderId),
	}, []grmi.Field{meta.SerialOrderDisassemble_Pos})
	if err != nil {
		return nil, grmi.NewBusinessError("查询拆解记录失败，错误:" + err.Error())
	}
	return disassembleLi, nil
}