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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/api"
	qmdal "LAPP_LF_MOM_BACKEND/dao/qm"
	basedal "LAPP_LF_MOM_BACKEND/dao/base"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/api"
	qmmeta "LAPP_LF_MOM_BACKEND/meta/qm"
	bmeta "LAPP_LF_MOM_BACKEND/meta/base"
	model "LAPP_LF_MOM_BACKEND/models/api"
	common "LAPP_LF_MOM_BACKEND/models/base"
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/models"
	"fmt"
	"strings"
)

/******************************************************************************
 *
 * @Description              : ACCUNITID的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-12 10:08:28
 *
 ******************************************************************************/
var DefaultConditionOfACCUNITID = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ACCUNITID_RECID.UrlParameterName:      grmi.NewConditionItem(meta.ACCUNITID_RECID, grmi.Equal, true),
		meta.ACCUNITID_CTIME.UrlParameterName:      grmi.NewConditionItem(meta.ACCUNITID_CTIME, grmi.Approximate, false),
		meta.ACCUNITID_LINE.UrlParameterName:       grmi.NewConditionItem(meta.ACCUNITID_LINE, grmi.Equal, false),
		meta.ACCUNITID_UNITSN.UrlParameterName:     grmi.NewConditionItem(meta.ACCUNITID_UNITSN, grmi.Equal, false),
		meta.ACCUNITID_IDNAME.UrlParameterName:     grmi.NewConditionItem(meta.ACCUNITID_IDNAME, grmi.Equal, false),
		meta.ACCUNITID_IDVALUE.UrlParameterName:    grmi.NewConditionItem(meta.ACCUNITID_IDVALUE, grmi.Equal, false),
		meta.ACCUNITID_USEDT.UrlParameterName:      grmi.NewConditionItem(meta.ACCUNITID_USEDT, grmi.Approximate, false),
		meta.ACCUNITID_STATUS.UrlParameterName:     grmi.NewConditionItem(meta.ACCUNITID_STATUS, grmi.Equal, false),
		meta.ACCUNITID_ITEMPARTNO.UrlParameterName: grmi.NewConditionItem(meta.ACCUNITID_ITEMPARTNO, grmi.Equal, false),
		meta.ACCUNITID_OP.UrlParameterName:         grmi.NewConditionItem(meta.ACCUNITID_OP, grmi.Equal, false),
		meta.ACCUNITID_RUNTIMEID.UrlParameterName:  grmi.NewConditionItem(meta.ACCUNITID_RUNTIMEID, grmi.Equal, false),
		meta.ACCUNITID_CYCLEID.UrlParameterName:    grmi.NewConditionItem(meta.ACCUNITID_CYCLEID, grmi.Equal, false),
		meta.ACCUNITID_LastModify.UrlParameterName: grmi.NewConditionItem(meta.ACCUNITID_LastModify, grmi.Approximate, false),
		meta.ACCUNITID_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.ACCUNITID_LastUser, grmi.Equal, false),
		meta.ACCUNITID_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.ACCUNITID_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ACCUNITID的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-12 10:08:28
 *
 ******************************************************************************/
var DefaultConditionOfACCUNITIDAndPaging = grmi.NewCondition(
	DefaultConditionOfACCUNITID.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ACCUNITIDServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ACCUNITID的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-12 10:08:28
 *
 ******************************************************************************/
type ACCUNITIDServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewACCUNITIDDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ACCUNITIDDAOImplement实例
 *
 * @Return Value             : ACCUNITIDDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-12 10:08:28
 *
 ******************************************************************************/
func NewACCUNITIDServiceImplement() *ACCUNITIDServiceImplement {
	return &ACCUNITIDServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/ACCUNITIDService.InsertOne
 *
 ******************************************************************************/
func (impl *ACCUNITIDServiceImplement) InsertOne(user *models.Usertab, entity *model.ACCUNITID) error {

	grmi.Log(user, "/services/api/implments/ACCUNITID.service.impl.go", "InsertOneACCUNITID", "插入一个ACCUNITID")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewACCUNITIDDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.RECID)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/ACCUNITIDService.DeleteOne
 *
 ******************************************************************************/
func (impl *ACCUNITIDServiceImplement) DeleteOne(user *models.Usertab, rECID int64) error {

	grmi.Log(user, "/services/api/implments/ACCUNITID.service.impl.go", "DeleteOneACCUNITID", "删除一个ACCUNITID")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/ACCUNITIDService.SelectOne
 *
 ******************************************************************************/
func (impl *ACCUNITIDServiceImplement) SelectOne(user *models.Usertab, rECID int64) (*model.ACCUNITID, error) {

	grmi.Log(user, "/services/api/implments/ACCUNITID.service.impl.go", "SelectOneACCUNITID", "查询一个ACCUNITID")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewACCUNITIDDAO(session, user.Userid)
	result, err := dao.SelectOne(rECID)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/ACCUNITIDService.SelectInfo
 *
 ******************************************************************************/
func (impl *ACCUNITIDServiceImplement) SelectInfo(user *models.Usertab, reclinerNr string) (*model.ReclinerResult, error) {

	grmi.Log(user, "/services/api/implments/ACCUNITID.service.impl.go", "SelectOneACCUNITID", "查询一个ACCUNITID")

	reclinerResult := new(model.ReclinerResult)
	slaves := db.Eloquent.Slaves()
	slavesession := slaves[0].NewSession()
	defer slavesession.Close()

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

	dao := dal.NewACCUNITIDDAO(slavesession, user.Userid)
	result, err := dao.SelectInfo(reclinerNr)
	if err != nil {
		fmt.Println(11)
		return nil, err
	}

	orderId := result.UNITSN

	//截取物料id
	bdal := basedal.NewLabelParserHeadDAO(session, user.Pid, user.Userid)

	resultCode, err := bdal.SelectOne("Barcode")
	if err != nil {
		return nil, err
	}
	var artId string
	if utils.ValueIsEmpty(resultCode.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
		}
		resultCode.Valst = list
		if len(orderId) < resultCode.Length {
			return nil, grmi.NewBusinessError("序列订单错误!")
		}
		codeInfos := common.ParseBarcode(orderId, resultCode.Valst)
		artId = codeInfos["artId"]
	} else {
		artIds := strings.Split(orderId, resultCode.Split)
		artId = artIds[0]
	}

	// 获取Article
	articleDAO := basedal.NewArticleDAO(session, user.Pid, user.Userid)
	article, err := articleDAO.SelectOne(artId)
	if err != nil {
		return nil, err
	}
	if article == nil {
		return nil, grmi.NewBusinessError("不存在指定Article!")
	}
	// 获取Article和视图的关联
	artViewDAO := qmdal.NewArtViewDAO(session, user.Pid, user.Userid)
	artViews, err := artViewDAO.Select([]grmi.Predicate{qmmeta.ArtView_ArtId.NewPredicate(grmi.Equal, article.ArtId)}, nil)
	if err != nil {
		return nil, err
	}
	// 从关联中获取视图标识
	viewIds := make([]interface{}, len(artViews))
	for i, artView := range artViews {
		viewIds[i] = artView.ViewId
	}
	// 获取视图
	viewDAO := qmdal.NewViewDAO(session, user.Pid, user.Userid)
	views, err := viewDAO.Select([]grmi.Predicate{qmmeta.View_ViewId.NewPredicate(grmi.Include, viewIds...)}, nil)
	if err != nil {
		return nil, err
	}
	reclinerResult.ACCUNITID = *result
	reclinerResult.Views = views
	return reclinerResult, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/ACCUNITIDService.UpdateOne
 *
 ******************************************************************************/
func (impl *ACCUNITIDServiceImplement) UpdateOne(user *models.Usertab, entity *model.ACCUNITID) error {

	grmi.Log(user, "/services/api/implments/ACCUNITID.service.impl.go", "UpdateOneACCUNITID", "修改一个ACCUNITID")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewACCUNITIDDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.RECID)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/api/ACCUNITIDService.Insert
 *
 ******************************************************************************/
func (impl *ACCUNITIDServiceImplement) Insert(user *models.Usertab, entities *[]model.ACCUNITID) error {

	grmi.Log(user, "/services/api/implments/ACCUNITID.service.impl.go", "InsertACCUNITID", "插入多个ACCUNITID")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.RECID)
		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/api/ACCUNITIDService.Delete
 *
 ******************************************************************************/
func (impl *ACCUNITIDServiceImplement) Delete(user *models.Usertab, entities *[]model.ACCUNITID) error {

	grmi.Log(user, "/services/api/implments/ACCUNITID.service.impl.go", "DeleteACCUNITID", "删除多个ACCUNITID")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.RECID)
		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/api/ACCUNITIDService.Select
 *
 ******************************************************************************/
func (impl *ACCUNITIDServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/api/implments/ACCUNITID.service.impl.go", "SelectACCUNITID", "查询ACCUNITID")

	condition := DefaultConditionOfACCUNITIDAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewACCUNITIDDAO(session, 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/api/ACCUNITIDService.Update
 *
 ******************************************************************************/
func (impl *ACCUNITIDServiceImplement) Update(user *models.Usertab, entities *[]model.ACCUNITID) error {

	grmi.Log(user, "/services/api/implments/ACCUNITID.service.impl.go", "UpdateACCUNITID", "修改多个ACCUNITID")

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

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

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