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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/ods"
	basedal "LAPP_LF_MOM_BACKEND/dao/base"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	bmeta "LAPP_LF_MOM_BACKEND/meta/base"
	meta "LAPP_LF_MOM_BACKEND/meta/ods"
	model "LAPP_LF_MOM_BACKEND/models/ods"
	bmodel "LAPP_LF_MOM_BACKEND/models/base"
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/models"
	"strings"
)

/******************************************************************************
 *
 * @Description              : ArtRouteOdsLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
var DefaultConditionOfArtRouteOdsLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArtRouteOdsLst_ArtId.UrlParameterName:         grmi.NewConditionItem(meta.ArtRouteOdsLst_ArtId, grmi.Equal, true),
		meta.ArtRouteOdsLst_OperationNr.UrlParameterName:   grmi.NewConditionItem(meta.ArtRouteOdsLst_OperationNr, grmi.Equal, true),
		meta.ArtRouteOdsLst_Pos.UrlParameterName:           grmi.NewConditionItem(meta.ArtRouteOdsLst_Pos, grmi.Equal, true),
		meta.ArtRouteOdsLst_Descr.UrlParameterName:         grmi.NewConditionItem(meta.ArtRouteOdsLst_Descr, grmi.Equal, false),
		meta.ArtRouteOdsLst_OdsId.UrlParameterName:         grmi.NewConditionItem(meta.ArtRouteOdsLst_OdsId, grmi.Equal, false),
		meta.ArtRouteOdsLst_OdsTypeId.UrlParameterName:     grmi.NewConditionItem(meta.ArtRouteOdsLst_OdsTypeId, grmi.Equal, false),
		meta.ArtRouteOdsLst_OdsContentIdx.UrlParameterName: grmi.NewConditionItem(meta.ArtRouteOdsLst_OdsContentIdx, grmi.Equal, false),
		meta.ArtRouteOdsLst_CtrlPara1.UrlParameterName:     grmi.NewConditionItem(meta.ArtRouteOdsLst_CtrlPara1, grmi.Equal, false),
		meta.ArtRouteOdsLst_CtrlPara2.UrlParameterName:     grmi.NewConditionItem(meta.ArtRouteOdsLst_CtrlPara2, grmi.Equal, false),
		meta.ArtRouteOdsLst_CtrlPara3.UrlParameterName:     grmi.NewConditionItem(meta.ArtRouteOdsLst_CtrlPara3, grmi.Equal, false),
		meta.ArtRouteOdsLst_CtrlPara4.UrlParameterName:     grmi.NewConditionItem(meta.ArtRouteOdsLst_CtrlPara4, grmi.Equal, false),
		meta.ArtRouteOdsLst_WorkPlaceNr.UrlParameterName:   grmi.NewConditionItem(meta.ArtRouteOdsLst_WorkPlaceNr, grmi.Equal, false),
		meta.ArtRouteOdsLst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.ArtRouteOdsLst_LastModify, grmi.Approximate, false),
		meta.ArtRouteOdsLst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.ArtRouteOdsLst_LastUser, grmi.Equal, false),
		meta.ArtRouteOdsLst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.ArtRouteOdsLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArtRouteOdsLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
var DefaultConditionOfArtRouteOdsLstAndPaging = grmi.NewCondition(
	DefaultConditionOfArtRouteOdsLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArtRouteOdsLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArtRouteOdsLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
type ArtRouteOdsLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArtRouteOdsLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArtRouteOdsLstDAOImplement实例
 *
 * @Return Value             : ArtRouteOdsLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
func NewArtRouteOdsLstServiceImplement() *ArtRouteOdsLstServiceImplement {
	return &ArtRouteOdsLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/ArtRouteOdsLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ArtRouteOdsLstServiceImplement) InsertOne(user *models.Usertab, entity *model.ArtRouteOdsLst) error {

	grmi.Log(user, "/services/ods/implments/ArtRouteOdsLst.service.impl.go", "InsertOneArtRouteOdsLst", "插入一个ArtRouteOdsLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewArtRouteOdsLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ArtId, entity.OperationNr, entity.Pos)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	/**处理数据**/
	for k, _ := range entity.Valst {
		entity.Valst[k].ArtId = entity.ArtId
		entity.Valst[k].OperationNr = entity.OperationNr
		entity.Valst[k].Step = k + 1
		entity.Valst[k].Pos = entity.Pos
		entity.Valst[k].WorkPlaceNr = entity.WorkPlaceNr
	}
	lstdao := dal.NewArtRouteStepOdsLstDAO(session, user.Pid, user.Userid)
	err = lstdao.Insert(&entity.Valst)
	if err != nil {
		session.Rollback()
		return err
	}
	// add Commit() after all actions
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/ArtRouteOdsLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArtRouteOdsLstServiceImplement) DeleteOne(user *models.Usertab, artId string, operationNr int, pos int) error {

	grmi.Log(user, "/services/ods/implments/ArtRouteOdsLst.service.impl.go", "DeleteOneArtRouteOdsLst", "删除一个ArtRouteOdsLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewArtRouteOdsLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(artId, operationNr, pos)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(artId, operationNr, pos)
	if err != nil {
		session.Rollback()
		return err
	}
	lstdao := dal.NewArtRouteStepOdsLstDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteMust(artId)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/ArtRouteOdsLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ArtRouteOdsLstServiceImplement) SelectOne(user *models.Usertab, artId string, operationNr int, pos int) (*model.ArtRouteOdsLst, error) {

	grmi.Log(user, "/services/ods/implments/ArtRouteOdsLst.service.impl.go", "SelectOneArtRouteOdsLst", "查询一个ArtRouteOdsLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtRouteOdsLstDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(artId, operationNr, pos)
	if err != nil {
		return nil, err
	}
	lstdao := dal.NewArtRouteStepOdsLstDAO(session, user.Pid, user.Userid)
	list, err := lstdao.Select([]grmi.Predicate{
		meta.ArtRouteStepOdsLst_ArtId.NewPredicate(grmi.Equal, artId),
	}, nil)
	if err != nil {
		return nil, err
	}
	result.Valst = list
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/ArtRouteOdsLstService.SelectOneByWorkPlaceId
 *
 ******************************************************************************/
func (impl *ArtRouteOdsLstServiceImplement) SelectOneByWorkPlaceId(user *models.Usertab, orderId string, workPlaceId string) (*model.ArtRouteOdsLst, error) {

	grmi.Log(user, "/services/ods/implments/ODS_ArtRouteOdsLst.service.impl.go", "SelectOneArtRouteOdsLst", "查询一个ArtRouteOdsLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtRouteOdsLstDAO(session, user.Pid, user.Userid)

	/**** 校验条码 *****/
	bdal := basedal.NewLabelParserHeadDAO(session, user.Pid, user.Userid)
	data, err := bdal.SelectOne("Barcode")
	if err != nil {
		return nil, err
	}

	var artId string
	if utils.ValueIsEmpty(data.Split) {
		lstdao := basedal.NewLabelParserDetailLstDAO(session, user.Pid, user.Userid)
		list, err := lstdao.Select([]grmi.Predicate{
			bmeta.LabelParserDetailLst_ParserId.NewPredicate(grmi.Equal, "Barcode"),
		}, nil)
		if err != nil {
			return nil, err
		}
		data.Valst = list
		if len(orderId) < data.Length {
			return nil, grmi.NewBusinessError("序列订单错误!")
		}
		codeInfos := bmodel.ParseBarcode(orderId, data.Valst)
		artId = codeInfos["artId"]
	} else {
		artIds := strings.Split(orderId, data.Split)
		artId = artIds[0]
	}

	//根据工位编号查询工位Id
	placeDao := basedal.NewWorkPlaceDAO(session, user.Pid, user.Userid)
	placeInfo,err := placeDao.SelectById(workPlaceId)
	if err != nil {
		return nil, err
	}
	if placeInfo == nil{
		return nil, grmi.NewBusinessError("查询对应的工位失败!")
	}
	result, err := dao.SelectOneByWorkPlaceId(artId, placeInfo.WorkPlaceNr)
	if err != nil {
		return nil, err
	}
	if result == nil{
		return nil, err
	}
	lstdao := dal.NewArtRouteStepOdsLstDAO(session, user.Pid, user.Userid)
	list, err := lstdao.Select([]grmi.Predicate{
		meta.ArtRouteStepOdsLst_ArtId.NewPredicate(grmi.Equal, artId),
	}, nil)
	if err != nil {
		return nil, err
	}
	result.Valst = list
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/ArtRouteOdsLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArtRouteOdsLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.ArtRouteOdsLst) error {

	grmi.Log(user, "/services/ods/implments/ArtRouteOdsLst.service.impl.go", "UpdateOneArtRouteOdsLst", "修改一个ArtRouteOdsLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewArtRouteOdsLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ArtId, entity.OperationNr, entity.Pos)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	/**处理数据**/
	for k, _ := range entity.Valst {
		entity.Valst[k].ArtId = entity.ArtId
		entity.Valst[k].OperationNr = entity.OperationNr
		entity.Valst[k].Step = k + 1
		entity.Valst[k].Pos = entity.Pos
		entity.Valst[k].WorkPlaceNr = entity.WorkPlaceNr
	}
	lstdao := dal.NewArtRouteStepOdsLstDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteMust(entity.ArtId)
	if err != nil {
		session.Rollback()
		return err
	}
	err = lstdao.Insert(&entity.Valst)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/ArtRouteOdsLstService.Insert
 *
 ******************************************************************************/
func (impl *ArtRouteOdsLstServiceImplement) Insert(user *models.Usertab, entities *[]model.ArtRouteOdsLst) error {

	grmi.Log(user, "/services/ods/implments/ArtRouteOdsLst.service.impl.go", "InsertArtRouteOdsLst", "插入多个ArtRouteOdsLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtRouteOdsLstDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ArtId, entity.OperationNr, 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_LF_MOM_BACKEND/services/ods/ArtRouteOdsLstService.Delete
 *
 ******************************************************************************/
func (impl *ArtRouteOdsLstServiceImplement) Delete(user *models.Usertab, entities *[]model.ArtRouteOdsLst) error {

	grmi.Log(user, "/services/ods/implments/ArtRouteOdsLst.service.impl.go", "DeleteArtRouteOdsLst", "删除多个ArtRouteOdsLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtRouteOdsLstDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ArtId, entity.OperationNr, 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_LF_MOM_BACKEND/services/ods/ArtRouteOdsLstService.Select
 *
 ******************************************************************************/
func (impl *ArtRouteOdsLstServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/ods/implments/ArtRouteOdsLst.service.impl.go", "SelectArtRouteOdsLst", "查询ArtRouteOdsLst")

	condition := DefaultConditionOfArtRouteOdsLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewArtRouteOdsLstDAO(session, user.Pid, user.Userid)
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/ArtRouteOdsLstService.Update
 *
 ******************************************************************************/
func (impl *ArtRouteOdsLstServiceImplement) Update(user *models.Usertab, entities *[]model.ArtRouteOdsLst) error {

	grmi.Log(user, "/services/ods/implments/ArtRouteOdsLst.service.impl.go", "UpdateArtRouteOdsLst", "修改多个ArtRouteOdsLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtRouteOdsLstDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ArtId, entity.OperationNr, 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_LF_MOM_BACKEND/services/ods/ArtRouteStepOdsLstService.SelectByArtId
 *
 ******************************************************************************/
func (impl *ArtRouteOdsLstServiceImplement) SelectByArtId(user *models.Usertab, orderId string) (interface{}, error) {

	grmi.Log(user, "/services/ods/implments/ArtRouteOdsLst.service.impl.go", "SelectByArtId", "查询ArtRouteStepOdsLst")

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

	bdal := basedal.NewLabelParserHeadDAO(session, user.Pid, user.Userid)
	data, err := bdal.SelectOne("Barcode")
	if err != nil {
		return nil,err
	}
	var artId string
	if utils.ValueIsEmpty(data.Split) {
		lstdao := basedal.NewLabelParserDetailLstDAO(session, user.Pid, user.Userid)
		list, err := lstdao.Select([]grmi.Predicate{
			bmeta.LabelParserDetailLst_ParserId.NewPredicate(grmi.Equal, "Barcode"),
		}, nil)
		if err != nil {
			return nil, err
		}
		data.Valst = list
		if len(orderId) < data.Length {
			return nil, grmi.NewBusinessError("序列订单错误!")
		}
		codeInfos := bmodel.ParseBarcode(orderId, data.Valst)
		artId = codeInfos["artId"]
	} else {
		artIds := strings.Split(orderId, data.Split)
		artId = artIds[0]
	}
	dao := dal.NewArtRouteOdsLstDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectByArtId(artId)
	if err != nil {
		return nil, err
	}
	return result, nil

}
