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

package implments

import (
	basedal "LAPP_LF_MOM_BACKEND/dao/base"
	omdal "LAPP_LF_MOM_BACKEND/dao/om"
	dal "LAPP_LF_MOM_BACKEND/dao/qm"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/qm"
	bmeta "LAPP_LF_MOM_BACKEND/meta/base"
	model "LAPP_LF_MOM_BACKEND/models/qm"
	common "LAPP_LF_MOM_BACKEND/models/base"
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/models"
	"fmt"
	"strconv"
	"strings"
	"time"
)

/******************************************************************************
 *
 * @Description              : DefectRecord的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:18:57
 *
 ******************************************************************************/
var DefaultConditionOfDefectRecord = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.DefectRecord_RecNr.UrlParameterName:          grmi.NewConditionItem(meta.DefectRecord_RecNr, grmi.Equal, true),
		meta.DefectRecord_SerialOrderId.UrlParameterName:  grmi.NewConditionItem(meta.DefectRecord_SerialOrderId, grmi.Equal, false),
		meta.DefectRecord_OperationNr.UrlParameterName:    grmi.NewConditionItem(meta.DefectRecord_OperationNr, grmi.Equal, false),
		meta.DefectRecord_Qty.UrlParameterName:            grmi.NewConditionItem(meta.DefectRecord_Qty, grmi.Equal, false),
		meta.DefectRecord_ViewId.UrlParameterName:         grmi.NewConditionItem(meta.DefectRecord_ViewId, grmi.Equal, false),
		meta.DefectRecord_ViewDescr.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_ViewDescr, grmi.Equal, false),
		meta.DefectRecord_ViewPicture.UrlParameterName:    grmi.NewConditionItem(meta.DefectRecord_ViewPicture, grmi.Equal, false),
		meta.DefectRecord_ViewCode.UrlParameterName:       grmi.NewConditionItem(meta.DefectRecord_ViewCode, grmi.Equal, false),
		meta.DefectRecord_Width.UrlParameterName:          grmi.NewConditionItem(meta.DefectRecord_Width, grmi.Equal, false),
		meta.DefectRecord_Height.UrlParameterName:         grmi.NewConditionItem(meta.DefectRecord_Height, grmi.Equal, false),
		meta.DefectRecord_EndProduct.UrlParameterName:     grmi.NewConditionItem(meta.DefectRecord_EndProduct, grmi.Equal, false),
		meta.DefectRecord_AreaId.UrlParameterName:         grmi.NewConditionItem(meta.DefectRecord_AreaId, grmi.Equal, false),
		meta.DefectRecord_XStart.UrlParameterName:         grmi.NewConditionItem(meta.DefectRecord_XStart, grmi.Equal, false),
		meta.DefectRecord_XEnd.UrlParameterName:           grmi.NewConditionItem(meta.DefectRecord_XEnd, grmi.Equal, false),
		meta.DefectRecord_YStart.UrlParameterName:         grmi.NewConditionItem(meta.DefectRecord_YStart, grmi.Equal, false),
		meta.DefectRecord_YEnd.UrlParameterName:           grmi.NewConditionItem(meta.DefectRecord_YEnd, grmi.Equal, false),
		meta.DefectRecord_XPos.UrlParameterName:           grmi.NewConditionItem(meta.DefectRecord_XPos, grmi.Equal, false),
		meta.DefectRecord_YPos.UrlParameterName:           grmi.NewConditionItem(meta.DefectRecord_YPos, grmi.Equal, false),
		meta.DefectRecord_ReworkplaceId.UrlParameterName:  grmi.NewConditionItem(meta.DefectRecord_ReworkplaceId, grmi.Equal, false),
		meta.DefectRecord_Status.UrlParameterName:         grmi.NewConditionItem(meta.DefectRecord_Status, grmi.Equal, false),
		meta.DefectRecord_DefectGrpId.UrlParameterName:    grmi.NewConditionItem(meta.DefectRecord_DefectGrpId, grmi.Equal, false),
		meta.DefectRecord_DefectGrpDescr.UrlParameterName: grmi.NewConditionItem(meta.DefectRecord_DefectGrpDescr, grmi.Equal, false),
		meta.DefectRecord_DefectReasonId.UrlParameterName: grmi.NewConditionItem(meta.DefectRecord_DefectReasonId, grmi.Equal, false),
		meta.DefectRecord_DefectDescr.UrlParameterName:    grmi.NewConditionItem(meta.DefectRecord_DefectDescr, grmi.Equal, false),
		meta.DefectRecord_InspectTime.UrlParameterName:    grmi.NewConditionItem(meta.DefectRecord_InspectTime, grmi.Approximate, false),
		meta.DefectRecord_InspectShiftId.UrlParameterName: grmi.NewConditionItem(meta.DefectRecord_InspectShiftId, grmi.Equal, false),
		meta.DefectRecord_Inspector.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_Inspector, grmi.Equal, false),
		meta.DefectRecord_ReworkTime.UrlParameterName:     grmi.NewConditionItem(meta.DefectRecord_ReworkTime, grmi.Approximate, false),
		meta.DefectRecord_ReworkShiftId.UrlParameterName:  grmi.NewConditionItem(meta.DefectRecord_ReworkShiftId, grmi.Equal, false),
		meta.DefectRecord_Reworker.UrlParameterName:       grmi.NewConditionItem(meta.DefectRecord_Reworker, grmi.Equal, false),
		meta.DefectRecord_BuildCode.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_BuildCode, grmi.Equal, false),
		meta.DefectRecord_DDCCustC1.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_DDCCustC1, grmi.Equal, false),
		meta.DefectRecord_DDCCustC2.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_DDCCustC2, grmi.Equal, false),
		meta.DefectRecord_DDCCustC3.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_DDCCustC3, grmi.Equal, false),
		meta.DefectRecord_DDCCustC4.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_DDCCustC4, grmi.Equal, false),
		meta.DefectRecord_DDCCustI1.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_DDCCustI1, grmi.Equal, false),
		meta.DefectRecord_DDCCustI2.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_DDCCustI2, grmi.Equal, false),
		meta.DefectRecord_DDCCustI3.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_DDCCustI3, grmi.Equal, false),
		meta.DefectRecord_DDCCustI4.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_DDCCustI4, grmi.Equal, false),
		meta.DefectRecord_DDCCustT1.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_DDCCustT1, grmi.Approximate, false),
		meta.DefectRecord_DDCCustT2.UrlParameterName:      grmi.NewConditionItem(meta.DefectRecord_DDCCustT2, grmi.Approximate, false),
		meta.DefectRecord_WorkPlaceNr.UrlParameterName:    grmi.NewConditionItem(meta.DefectRecord_WorkPlaceNr, grmi.Equal, false),
		meta.DefectRecord_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.DefectRecord_LastModify, grmi.Approximate, false),
		meta.DefectRecord_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.DefectRecord_LastUser, grmi.Equal, false),
		meta.DefectRecord_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.DefectRecord_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : DefectRecord的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:18:57
 *
 ******************************************************************************/
var DefaultConditionOfDefectRecordAndPaging = grmi.NewCondition(
	DefaultConditionOfDefectRecord.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : DefectRecordServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : DefectRecord的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:18:57
 *
 ******************************************************************************/
type DefectRecordServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewDefectRecordDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个DefectRecordDAOImplement实例
 *
 * @Return Value             : DefectRecordDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:18:57
 *
 ******************************************************************************/
func NewDefectRecordServiceImplement() *DefectRecordServiceImplement {
	return &DefectRecordServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/DefectRecordService.InsertOne
 *
 ******************************************************************************/
func (impl *DefectRecordServiceImplement) InsertOne(user *models.Usertab, entity *model.DefectRecord) error {

	grmi.Log(user, "/services/qm/implments/DefectRecord.service.impl.go", "InsertOneDefectRecord", "插入一个DefectRecord")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDefectRecordDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.RecNr)
	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/qm/DefectRecordService.DeleteOne
 *
 ******************************************************************************/
func (impl *DefectRecordServiceImplement) DeleteOne(user *models.Usertab, recNr int) error {

	grmi.Log(user, "/services/qm/implments/DefectRecord.service.impl.go", "DeleteOneDefectRecord", "删除一个DefectRecord")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/DefectRecordService.SelectOne
 *
 ******************************************************************************/
func (impl *DefectRecordServiceImplement) SelectOne(user *models.Usertab, recNr int) (*model.DefectRecord, error) {

	grmi.Log(user, "/services/qm/implments/DefectRecord.service.impl.go", "SelectOneDefectRecord", "查询一个DefectRecord")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/DefectRecordService.UpdateOne
 *
 ******************************************************************************/
func (impl *DefectRecordServiceImplement) UpdateOne(user *models.Usertab, entity *model.DefectRecord) error {

	grmi.Log(user, "/services/qm/implments/DefectRecord.service.impl.go", "UpdateOneDefectRecord", "修改一个DefectRecord")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDefectRecordDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.RecNr)
	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/qm/DefectRecordService.Insert
 *
 ******************************************************************************/
// 2021-03-18 chenglei.wei@le-it.com.cn
func (impl *DefectRecordServiceImplement) Insert(user *models.Usertab, entities model.SerialOrderRuting) error {

	grmi.Log(user, "/services/qm/implments/DefectRecord.service.impl.go", "InsertDefectRecord", "插入多个DefectRecord")

	count := len(entities.DefectRecord)

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

	sdao := dal.NewSerialOrderRutingDAO(session, user.Pid, user.Userid)
	OrderInfo, err := sdao.SelectOne(entities.SerialorderId)
	if err != nil {
		return grmi.NewBusinessError("序列订单查询错误!")
	}
	if OrderInfo != nil && OrderInfo.Status == common.CHECK_STATUS_CLOSED{
		return grmi.NewBusinessError("该序列订单已经检测!")
	}
	orderId := entities.SerialorderId

	bdal := basedal.NewLabelParserHeadDAO(session, user.Pid, user.Userid)

	result, err := bdal.SelectOne("Barcode")
	if err != nil {
		return err
	}
	var artId string
	if utils.ValueIsEmpty(result.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 err
		}
		result.Valst = list
		if len(orderId) < result.Length {
			return grmi.NewBusinessError("序列订单错误!")
		}
		codeInfos := common.ParseBarcode(orderId, result.Valst)
		artId = codeInfos["artId"]
	} else {
		artIds := strings.Split(orderId, result.Split)
		artId = artIds[0]
	}
	if count <= 0 {
		//判断是否是修复
		if OrderInfo != nil && OrderInfo.Status == common.CHECK_STATUS_RUNNING{
			//判断是否修复
			recordDAO := dal.NewDefectRecordDAO(session, user.Pid, user.Userid)
			records, err := recordDAO.Select([]grmi.Predicate{meta.DefectRecord_SerialOrderId.NewPredicate(grmi.Equal, orderId),meta.DefectRecord_Status.NewPredicate(grmi.LessThen, common.ORDER_STATUS_CLOSED)}, nil)
			if err != nil {
				return err
			}
			if records != nil && len(records) > 0 {
				return grmi.NewBusinessError("该序列工单返工未完成!")
			}
			//更新状态
			entities.Status = common.CHECK_STATUS_CLOSED
			entities.IsPack = 0
			entities.ArtId = artId
			err = sdao.UpdateOne(&entities)
			if err != nil {
				return grmi.NewBusinessError("更新记录失败!")
			}
			return nil
		}else {
			/*******无提交记录，即为正常订单********/
			entities.Status = common.CHECK_STATUS_CLOSED
			entities.IsPack = 0
			entities.ArtId = artId
			err = sdao.InsertOne(&entities)
			if err != nil {
				return grmi.NewBusinessError("提交记录失败!")
			}
			//更新时间
			omDAO := omdal.NewSerialOrderDAO(session, user.Pid, user.Userid)
			info ,err := omDAO.SelectBySerialOrder(orderId)
			if err != nil {
				return err
			}
			if info == nil{
				return grmi.NewBusinessError("该记录不存在!")
			}
			if info.UsedResourceId == "LaserWelding"{
				info.ActEndTime = grmi.DateTime(time.Now())
				err = omDAO.UpdateInfo(info)
				if err != nil {
					return err
				}
			}
			return nil
		}
	}

	dao := dal.NewDefectRecordDAO(session, user.Pid, user.Userid)
	records, err := dao.Select([]grmi.Predicate{meta.DefectRecord_SerialOrderId.NewPredicate(grmi.Equal, orderId)}, nil)
	if err != nil {
		return err
	}
	if records != nil && len(records) > 0 {
		return grmi.NewBusinessError("该唯一码已经记录过数据!")
	}
	viewDAO := dal.NewViewDAO(session, user.Pid, user.Userid)
	groupDAO := dal.NewDefectGrpDAO(session, user.Pid, user.Userid)
	reasonDAO := dal.NewDefectReasonDAO(session, user.Pid, user.Userid)
	areaDAO := dal.NewViewValstDAO(session, user.Pid, user.Userid)
	articleViewDAO := dal.NewArtViewDAO(session, user.Pid, user.Userid)
	viewGroupDAO := dal.NewViewDefectGrplstDAO(session, user.Pid, user.Userid)
	groupReasonDAO := dal.NewDefectGrpReasonLstDAO(session, user.Pid, user.Userid)
	//添加记录
	entities.Status = common.CHECK_STATUS_RUNNING
	entities.ArtId = artId
	entities.IsPack = 0
	err = sdao.InsertOne(&entities)
	if err != nil {
		return grmi.NewBusinessError("提交记录失败!")
	}
	DefectRecord := make([]model.DefectRecord, 0)
	for i := 0; i < len(entities.DefectRecord); i++ {
		entity := entities.DefectRecord[i]
		if entity.XPos == 0 {
			return grmi.NewBusinessError("没有记录横坐标!")
		}
		if entity.YPos == 0 {
			return grmi.NewBusinessError("没有记录纵坐标!")
		}
		view, err := viewDAO.SelectOne(entity.ViewId)
		if err != nil {
			return err
		}
		if view == nil {
			return grmi.NewBusinessError(fmt.Sprintf("没有找到视图:%s!", entity.ViewId))
		}
		group, err := groupDAO.SelectOne(entity.DefectGrpId)
		if err != nil {
			return err
		}
		if group == nil {
			return grmi.NewBusinessError(fmt.Sprintf("没有找到缺陷分组:%s!", entity.DefectGrpId))
		}
		reason, err := reasonDAO.SelectOne(entity.DefectReasonId)
		if err != nil {
			return err
		}
		if reason == nil {
			return grmi.NewBusinessError(fmt.Sprintf("没有找到缺陷原因:%s!", entity.DefectReasonId))
		}
		area, err := areaDAO.SelectOne(entity.ViewId, entity.AreaId)
		if err != nil {
			return err
		}
		if area == nil {
			return grmi.NewBusinessError(fmt.Sprintf("没有找到缺陷原因:%s#%d!", entity.ViewId, entity.AreaId))
		}
		artView, err := articleViewDAO.SelectOne(artId, entity.ViewId)
		if err != nil {
			return err
		}
		if artView == nil {
			return grmi.NewBusinessError(fmt.Sprintf("没有找到Article(%s)和视图(%s)的关联!", artId, entity.ViewId))
		}
		viewGroup, err := viewGroupDAO.SelectOne(entity.ViewId, entity.DefectGrpId)
		if err != nil {
			return err
		}
		if viewGroup == nil {
			return grmi.NewBusinessError(fmt.Sprintf("没有找到视图(%s)和视缺陷分组(%s)的关联!", entity.ViewId, entity.DefectGrpId))
		}
		groupReason, err := groupReasonDAO.SelectOne(entity.DefectGrpId, entity.DefectReasonId)
		if err != nil {
			return err
		}
		if groupReason == nil {
			return grmi.NewBusinessError(fmt.Sprintf("没有找到缺陷分组(%s)和缺陷原因(%s)的关联!", entity.DefectGrpId, entity.DefectReasonId))
		}
		entity.SerialOrderId = entity.OrderId
		entity.Qty = 1
		entity.Status = strconv.Itoa(common.ORDER_STATUS_RELEASED)
		entity.ViewCode = entity.AreaId
		entity.ViewDescr = view.Descr
		entity.ViewPicture = view.Picture
		entity.Width = view.Width
		entity.Height = view.Height
		entity.XStart = area.XStart
		entity.XEnd = area.XStart + area.Width
		entity.YStart = area.YStart
		entity.YEnd = area.YStart + area.Height
		entity.DefectGrpDescr = group.Descr
		entity.DefectDescr = reason.Descr
		entity.InspectTime = grmi.DateTime(time.Now())
		entity.Inspector = user.Userid
		DefectRecord = append(DefectRecord, entity)
	}

	err = dao.Insert(&DefectRecord)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/DefectRecordService.Delete
 *
 ******************************************************************************/
func (impl *DefectRecordServiceImplement) Delete(user *models.Usertab, entities *[]model.DefectRecord) error {

	grmi.Log(user, "/services/qm/implments/DefectRecord.service.impl.go", "DeleteDefectRecord", "删除多个DefectRecord")

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

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

	grmi.Log(user, "/services/qm/implments/DefectRecord.service.impl.go", "SelectDefectRecord", "查询DefectRecord")

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

	grmi.Log(user, "/services/qm/implments/DefectRecord.service.impl.go", "UpdateDefectRecord", "修改多个DefectRecord")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.RecNr)
		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/qm/DefectRecordService.Rework
 *
 ******************************************************************************/
func (impl *DefectRecordServiceImplement) Rework(user *models.Usertab, recNr int) error {

	grmi.Log(user, "/services/qm/implments/QM_DefectRecord.service.impl.go", "Rework", "返工")

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

	if record.Status != strconv.Itoa(common.ORDER_STATUS_RELEASED) {
		return grmi.NewBusinessError("状态错误!")
	}

	//sdao := dal.NewSerialOrderRutingDAO(session, user.Pid, user.Userid)
	//OrderInfo, err := sdao.SelectOne(record.SerialOrderId)
	//if err != nil {
	//	return grmi.NewBusinessError("序列订单查询错误!")
	//}
	//OrderInfo.Status = common.CHECK_STATUS_CLOSED
	//err = sdao.UpdateOne(OrderInfo)
	//if err != nil {
	//	return grmi.NewBusinessError("序列订单查询错误!")
	//}
	record.Status = strconv.Itoa(common.ORDER_STATUS_CLOSED)
	record.InspectTime = grmi.DateTime(time.Now())
	record.Inspector = user.Userid
	err = dao.UpdateOne(record)
	if err != nil {
		return err
	}
	return nil
}
