<?php
class Service_PickProcess
{
    protected $_errorArr = array();
    protected $_params = array();
    protected $_userId = 0;
    protected $_pickerId = 0;
    protected $_warehouseId = 0;
    protected $_date = '';

    public function __construct()
    {
        $this->_date = date('Y-m-d H:i:s');
        $this->_warehouseId = Service_User::getUserWarehouseId();
        $this->_userId = Service_User::getUserId();
    }

    /**
     * @分配配货员
     * @return 成功与失败 都返回
     * @param array $pickCodes
     * @param int $pickerId
     * @return array
     * @throws Exception
     */
    public function bindPickerTransaction($pickCodes = array(), $pickerId = 0)
    {
        $this->_pickerId = $pickerId;
        $result = array('state' => 0, 'message' => array());
        $pickObj = new Service_Picking();
        $pdObj = new Service_PickingDetail();
        $orderObj = new Service_Orders();
        $db = Service_QualityControl::getModelInstance()->getAdapter();
        foreach ($pickCodes as $pickCode) {
            $pickRow = $pickObj->getByField($pickCode, 'picking_code');
            if ($pickRow['picking_status'] != '0') {
                $result['message'][] = '单号:' . $pickCode . ':配货单状态已完成,不允许分配.';
                continue;
            }
            try {
                $db->beginTransaction();
                if (!$pickObj->update(array('picker_id' => $this->_pickerId, 'is_assign' => 1, 'picking_update_time' => $this->_date), $pickCode, 'picking_code')) {
                    throw new Exception('单号:' . $pickCode . ':分配失败.');
                }
                if($pickRow['picking_model']=="1"){
                    $pickDetail = $pdObj->getByCondition(array('picking_code'=>$pickRow['picking_code']),"*");
                    $swoCodeArr = array();
                    foreach($pickDetail as $val){
                        if(!in_array($val['order_code'],$swoCodeArr)){
                            $swoCodeArr[] = $val['order_code'];
                            Service_SwapOrder::update(array('picker_id'=>$this->_pickerId),$val['order_code'],'swo_code');
                        }
                    }
                }else{
                    $orderList = $pdObj->getGroupOrderByPickingId($pickRow['picking_id']);
                    //更新订单配货员
                    foreach ($orderList as $val) {
                        if (!$orderObj->update(array('update_time' => $this->_date, 'picker_id' => $this->_pickerId), $val['order_id'], 'order_id')) {
                            throw new Exception($result['error'][] = '单号:' . $pickCode . ' OrderCode:' . $val['order_code'] . ':分配失败.');
                        } else {
                            $this->addOrderLog(array('picker_id' => $this->_pickerId, 'order_id' => $val['order_id'], 'order_code' => $val['order_code'], 'ol_type' => 1, 'order_status_from' => 5, 'order_status_to' => 5, 'ol_comments' => 'picker_id To ' . $this->_pickerId));
                        }
                    }
                }
                $db->commit();
                $result['message'][] = '单号:' . $pickCode . ':操作成功.';
            } catch (Exception $e) {
                $db->rollBack();
                $result['message'][] = $e->getMessage();
            }
        }
        $result['state'] = 1;
        return $result;
    }


    /**
     * @确认下架完成/一票多件
     * @return 成功与失败 都返回
     * @param array $pickCodes
     * @return array
     * @throws Exception
     */
    public function confirmPickerTransaction($pickCodes = array())
    {
        $result = array('state' => 0, 'message' => array());
        $pickObj = new Service_Picking();
        $pdObj = new Service_PickingDetail();
        $orderObj = new Service_Orders();
        $piProccess = new Service_ProductInventoryProcess();
        $db = Service_QualityControl::getModelInstance()->getAdapter();
        foreach ($pickCodes as $pickCode) {
            $pickRow = $pickObj->getByField($pickCode, 'picking_code');
            if ($pickRow['picking_type'] == '0') {
                $result['message'][] = '单号:' . $pickCode . ':配货单类型为一票一件,不须要下架确认.';
                continue;
            }
            if ($pickRow['picking_status'] != '0') {
                $result['message'][] = '单号:' . $pickCode . ':配货单状态已完成,不允许操作.';
                continue;
            }
            if ($pickRow['is_assign'] == '0') {
                $result['message'][] = '单号:' . $pickCode . ':配货员未分配,不允许操作.';
                continue;
            }
            $db->beginTransaction();
            try {
                if (!$pickObj->update(array('picking_status' => 1, 'picking_update_time' => $this->_date), $pickCode, 'picking_code')) {
                    throw new Exception('PL:' . $pickCode . ':操作失败.');
                }
                $pdObj->update(array('pd_status' => 1), $pickRow['picking_id'], 'picking_id');
                if($pickRow['picking_model']=="1"){
                    $pickDetail = $pdObj->getByCondition(array('picking_code'=>$pickRow['picking_code']),"*");
                    //$swoCodeArr = array();
                    $userId = Service_User::getUserId();
                    $ibo_ids = array();
                    $referenceNoArr =array();
                    $qcCodeArr = array();
                    foreach($pickDetail as $val){
                        //$swoCodeArr[] = $val['order_code'];
                        $ibo_ids[] = $val['ibo_id'];
                        $referenceNoArr[] = $val['order_code'];
                        Service_SwapOrder::update(array('swo_status'=>3),$val['order_code'],'swo_code');
                        $swapOrder = Service_SwapOrder::getByField($val['order_code'],'swo_code',"*");
                        //throw new Exception("mabbbbbbbb");
                        $swapOrderDetail = Service_SwapOrderDetail::getByCondition(array('swo_code'=>$val['order_code'],'product_id'=>$val['product_id'],'swod_id'=>$val['op_id']),"*");
                        $qcObj = new Service_QualityControl();
                        foreach($swapOrderDetail as $swd){
                            $existQcRow = Service_QualityControl::getByCondition(array('receiving_code' => $swapOrder['swo_code'],'rd_id'=>$swd['swod_id']),"*");
                            $product = Service_Product::getByField($swd['to_product_id'],"product_id","*");
                            $fromProduct = Service_Product::getByField($swd['product_id'],"product_id","*");
                            if(!empty($existQcRow)){
                                $updateQc = array(
                                    'qc_quantity'=>$existQcRow[0]['qc_quantity']+$val['pd_quantity']
                                );
                                Service_QualityControl::update($updateQc,$existQcRow[0]['qc_id'],"qc_id");
                                $reference_code = $existQcRow[0]['qc_code'];
                            }else{
                                $code = Common_GetNumbers::getCode('qualitycontrol', $swapOrder['warehouse_id'], 'QC');
                                $qcCodeArr[] = $code;
                                $result['code'][] = $code;
                                $reference_code = $code;
                                Service_SwapOrderDetail::update(array('swod_status'=>'3'),$swd['swod_id'],"swod_id");
                                $qcRow = array(
                                    'receiving_id' => $swapOrder['swo_id'],
                                    'warehouse_id' => $swapOrder['warehouse_id'],
                                    'receiving_code' => $swapOrder['swo_code'],
                                    'customer_id' => $swapOrder['customer_id'],
                                    'customer_code' => $swapOrder['customer_code'],
                                    'receiving_user_id' => $userId,
                                    'qc_status' =>0,
                                    'qc_code'=>$code,
                                    'product_barcode'=>$product['product_barcode'],
                                    'product_id'=>$swd['to_product_id'],
                                    'qc_quantity'=>$val['pd_quantity'],
                                    'rd_id'=>$swd['swod_id'],
                                    'qc_model'=>1,
                                );
                                //$qcRow['qc_code'] = $code;
                                //$qcRow['product_barcode'] = $product['product_barcode'];
                                //$qcRow['product_id'] = $swd['to_product_id'];
                                //$qcRow['qc_quantity'] = $val['pd_quantity'];
                                //$qcRow['rd_id'] = $swd['swod_id'];
                                //$qcRow['qc_model'] = 1;
                                $qcObj->add($qcRow);
                            }

                            //上架表
                            $putawayObj = new Service_Putaway();
                            $putawayRow = $putawayObj->getByField($swapOrder['swo_code'], 'receiving_code');
                            if (empty($putawayRow)) {
                                $code = Common_GetNumbers::getCode('putaway', $swapOrder['warehouse_id'],'PA');
                                $row = array(
                                    'putaway_code' => $code,
                                    'receiving_code' => $swapOrder['swo_code'],
                                    'warehouse_id' => $swapOrder['warehouse_id'],
                                    'putaway_add_time' => $this->_date
                                );
                                $putawayObj->add($row);
                            }

                            //处理库存
                            $condition = array(
                                'product_id'=>$swd['to_product_id'],
                                'warehouse_id'=>$swapOrder['warehouse_id'],
                            );
                            $inventory = Service_ProductInventory::getByCondition($condition,"*");
                            if(empty($inventory)){
                                $productInventoryRow = array(
                                    'product_barcode'=>$product['product_barcode'],
                                    'customer_id'=>$swapOrder['customer_id'],
                                    'product_id'=>$product['product_id'],
                                    'warehouse_id'=>$swapOrder['warehouse_id'],
                                    'pi_onway'=>$val['pd_quantity'],
                                    'pi_add_time'=>date('Y-m-d H:i:s',time()),
                                );
                                Service_ProductInventory::add($productInventoryRow);
                            }else{
                                $updatePi = array(
                                    'pi_onway'=>$inventory[0]['pi_onway']+$val['pd_quantity'],
                                    'pi_update_time'=>date("Y-m-d H:i:s"),
                                );
                                Service_ProductInventory::update($updatePi,$inventory[0]['pi_id'],"pi_id");
                            }
                            $params = array(
                                'product_id' => $swd['product_id'],
                                'quantity' => $val['pd_quantity'],
                                'operationType' => 6,
                                'warehouse_id' => $swapOrder['warehouse_id'],
                                'reference_code' => $reference_code, //操作单号
                                'application_code' => '', //操作类型
                                'note' => '易货单易货',
                                //'reference_code'=>$swapOrder['swo_code'],
                            );
                            $ret = $piProccess->update($params);
                            if($ret['state']==0) throw new Exception("{$fromProduct['product_barcode']}更新库存失败:".$ret['message']);
                        }
                    }
                    $result['qcCodeArr'] = $qcCodeArr;
                    //核销批次库存
                    if(empty($ibo_ids)) throw new Exception( '找不到库存批次');
                    $ibo_ids = array_unique($ibo_ids);
                    $iboList = Service_InventoryBatchOutbound::listInIboids($ibo_ids);
                    foreach($iboList as $iboRow) {
                        $ibRow = Service_InventoryBatch::getByField($iboRow['ib_id']);
                        //$product_id = $ibRow['product_id'];
                        //if(!isset($aQuantity[$product_id])) $aQuantity[$product_id] = 0;
                        //$aQuantity[$ibRow['product_id']] += $iboRow['ibo_quantity'];
                        $iboRet = Service_InventoryBatchOutbound::delete($iboRow['ibo_id']);
                        $ibolRet = Service_InventoryBatchOutboundLog::log($iboRow, '易货单易货');
                        if(!$iboRet || !$ibolRet) throw new Exception('数据库异常，删除核销库存失败');
                        $ib_quantity = $ibRow['ib_quantity']-$iboRow['ibo_quantity'];
                        if($ib_quantity==0) {
                            $ibRet = Service_InventoryBatch::delete($ibRow['ib_id']);
                            $note = '订单出货删除批次';
                        } else {
                            $updateRow['ib_quantity'] = $ib_quantity;
                            $updateRow['ib_update_time'] = date('Y-m-d H:i:s');
                            $ibRet = Service_InventoryBatch::update($updateRow, $ibRow['ib_id']);
                            $note = '订单出货修改批次库存';
                        }
                        $iblRet = Service_InventoryBatchLog::log($ibRow, $ibRow['ib_quantity'], $ib_quantity, $note);
                        //if(!$iboRet || !$ibolRet) throw new Exception('数据库异常，更新批次库存失败');
                    }
                    $referenceNoArr = array_unique($referenceNoArr);
                    foreach($referenceNoArr as $reference_no){
                        $ibsoList = Service_InventoryBatchSerialOutbound::getByCondition(array('reference_no'=>$reference_no),"*");
                        if(!empty($ibsoList)){
                            foreach($ibsoList as $ibso){
                                $inventoryBatchSerial = Service_InventoryBatchSerial::getByField($ibso['ibs_id'],'ibs_id',"*");
                                $ibsQuantiy = $inventoryBatchSerial['ibs_quantity'] - $ibso['ibso_quantity'];
                                if($ibsQuantiy==0){
                                    Service_InventoryBatchSerial::delete($ibso['ibs_id']);
                                }else{
                                    $ibsUpdateRow = array(
                                        'ibs_quantity'=>$ibsQuantiy,
                                    );
                                    Service_InventoryBatchSerial::update($ibsUpdateRow,$ibso['ibs_id'],'ibs_id');
                                }
                                Service_InventoryBatchSerialOutbound::delete($ibso['ibso_id'],'ibso_id');
                            }
                        }
                    }


                }else{
                    $orderList = $pdObj->getGroupOrderByPickingId($pickRow['picking_id']);

                    //更新订单状态 6
                    foreach ($orderList as $val) {
                        $orderRow = $orderObj->getByField($val['order_id'], 'order_id');

                        //判断客户是否冻结
                        $customerRow = Service_Customer::getByField($orderRow['customer_code'], 'customer_code', array('is_frozen', 'frozen_reason'));
                        
                        if($customerRow['is_frozen']){
                            throw new Exception($result['error'][] = '单号:' . $pickCode . ' 订单号:' . $val['order_code'] . ' 客户:'. $orderRow['customer_code'] .'已冻结，冻结原因：'.$customerRow['frozen_reason']);
                        }

                        if ($orderRow['order_status'] != '5') {
                            throw new Exception($result['error'][] = '单号:' . $pickCode . ' 订单号:' . $val['order_code'] . ':订单状态不是已打印,操作失败.');
                        }
                        if (!$orderObj->update(array('update_time' => $this->_date, 'order_status' => 6), $val['order_id'], 'order_id')) {
                            throw new Exception($result['error'][] = '单号:' . $pickCode . ' 订单号:' . $val['order_code'] . ':更新订单状态失败.');
                        } else {
                            $log = array(
                                'order_id' => $val['order_id'],
                                'order_code' => $val['order_code'],
                                'order_status_from' => 5,
                                'order_status_to' => 6,
                                'ol_comments' => 'confirmPick'
                            );
                            $this->addOrderLog($log);
                        }
                    }
                }
                $db->commit();
                $result['message'][] = '单号:' . $pickCode . ':下架完成,操作成功.';
            } catch (Exception $e) {
                $db->rollBack();
                $result['message'][] = $e->getMessage();
            }
        }
        $result['state'] = 1;
        return $result;
    }


    /**
     * @desc 订单操作日志
     * @param array $orderRow
     */
    private function addOrderLog($orderRow = array())
    {
        $orderLogObj = new Service_OrderLog();
        $log = array(
            'order_id' => $orderRow['order_id'],
            'order_code' => $orderRow['order_code'],
            'user_id' => $this->_userId,
            'ol_type' => isset($orderRow['ol_type']) ? $orderRow['ol_type'] : '0',
            'ol_add_time' => $this->_date,
            'order_status_from' => isset($orderRow['order_status_from']) ? $orderRow['order_status_from'] : '0',
            'order_status_to' => isset($orderRow['order_status_to']) ? $orderRow['order_status_to'] : '0',
            'ol_ip' => Common_Common::getIP(),
            'ol_add_time' => $this->_date,
            'ol_comments' => $orderRow['ol_comments'],
        );
        $orderLogObj->add($log);
    }


    /**
     * @desc 配货单
     * @param picking_code
     * @return array
     */
    public static function getPacking($pickCode = '' , $picking_type_all    =   false)
    {
        $result = array('state' => 0, 'message' => array(), 'data' => array());
        if (empty($pickCode)) {
            $result['message'][] = '配货单号不能为空.';
            return $result;
        }
        $pickObj = new Service_Picking();
        $pickRow = $pickObj->getByField($pickCode, 'picking_code');
        if (empty($pickRow)) {
            $result['message'][] = '单号:' . $pickCode . ' 不存在.';
            return $result;
        }

        if ($pickRow['is_assign'] == '0') {
            $result['message'][] = '单号:' . $pickCode . ' 未分配配货员,不允许打印.';
            return $result;
        }
        if ($pickRow['picking_type'] != '1'&&$pickRow['picking_type']!="2" && $picking_type_all == false) {
            $result['message'][] = '单号:' . $pickCode . ' 下架类型不是一票多件,不允许打印.';
            return $result;
        }
        $pickerName = '';
        if ($pickRow['picker_id'] != '0' && $pickRow['picker_id'] != '') {
            $userRow = Service_User::getByField($pickRow['picker_id'], 'user_id');
            $pickerName = $userRow['user_name'];
        }

        $pdObj = new Service_PickingDetail();
        $orderObj = new Service_Orders();
        $orderArr = $orders = array();
        $packageMapObj = new Service_ProductPackageMap();
        $packageObj = new Service_ProductPackage();
        $category = Common_DataCache::getProductCategory();
        if($pickRow['picking_model']=="1"){
            $total = 1;
            $pickingDetail = Service_PickingDetail::getByCondition(array('picking_code'=>$pickRow['picking_code']),"*");
            $existSwapOrder = array();
            $order['pickerName'] = $pickerName;
            $order['order_code'] = $pickingDetail[0]['order_code'];
            $order['page'] = 0;
            $order['loop'] = 1;
            $order['product']=array();
            $swapOrderRow = Service_SwapOrder::getByField($pickingDetail[0]['order_code'],"swo_code","*");
            $order['customer_code'] = $swapOrderRow['customer_code'];
            $customerRow = Service_Customer::getByField($order['customer_code'],"customer_code","*");
            $order['customer_company_name'] = $customerRow['customer_company_name'];
            $order['sm_code'] = "";
            $order['remark'] = $swapOrderRow['remark'];
            $order['is_abnormal'] = $swapOrderRow['is_abnormal'] ? '是' : '否';
            foreach($pickingDetail as $key=>$val){
                $packageMapRow = $packageMapObj->getByCondition(array('warehouse_id' => $swapOrderRow['warehouse_id'], 'product_id' => $val['product_id']), array('pp_id'));
                if (!empty($packageMapRow)) {
                    $productMapRow[$val['product_id']] = $packageMapRow[0]['pp_id'];
                    $packageRow = $packageObj->getByField($productMapRow[$val['product_id']], 'pp_id', array('pp_name'));
                    $order['package'] = isset($packageRow['pp_name']) ? $packageRow['pp_name'] : '';
                }
                $swapOrderDetail = Service_SwapOrderDetail::getByField($val['op_id'],"swod_id","*");
                $fromCustomer = Service_Customer::getByField($swapOrderDetail['from_customer_code'],"customer_code","*");
                $product = Service_Product::getByField($val['product_id'],"product_id","*");
                $swoDetail = Service_SwapOrderDetail::getByField($val['op_id'],"swod_id","*");
                $val['to_product_barcode'] = $swoDetail['to_product_barcode'];
                $val['sod_remark'] = $swoDetail['sod_remark'];
                $val['product_barcode'] = $product['product_barcode'];
                $val['product_title'] = $product['product_title'];
                $val['from_customer_name'] = $fromCustomer['customer_company_name'];
                $order['product'][$val['lc_code'] . '_' . $val['pd_id']] = $val;
                $order['product'][$val['lc_code'] . '_' . $val['pd_id']]['category'] = $category[$product['pc_id']]['pc_name'];

                $order['package']=isset($order['package'])?$order['package']:'';
            }
            $chunk = array_chunk($order['product'], 9);
            foreach ($chunk as $k => $v) {
                $order['product'] = $v;
                $order['page'] = $k;
                $orders['order'][$pickRow['picking_id'] . '_' . $val['lc_code'] . '_' . $val['order_id'] . '_' . $k] = $order;
            }
        }else{
            $orderCodeList = $pdObj->getGroupOrderByPickingId($pickRow['picking_id']);
            $total = count($orderCodeList);
            foreach ($orderCodeList as $key => $val) {
                $order['pickerName'] = $pickerName;
                $order['order_code'] = $val['order_code'];
                $order['page'] = 0;
                $order['loop'] = $key + 1;
                $order['product']=array();

                $orderPickProduct = $pdObj->getLeftJoinOrderProductByCondition(array('picking_id' => $pickRow['picking_id'], 'order_id' => $val['order_id'], '*', 0, 0, array('lc_code')));
                if (isset($orderArr[$val['order_id']])) {
                    $orderRow = $orderArr;
                } else {
                    $orderRow = $orderObj->getByField($val['order_id'], 'order_id', array('sm_code', 'remark', 'customer_code', 'warehouse_id'));
                    $orderArr[$val['order_id']] = $orderRow;
                }
                $order['customer_code'] = $orderRow['customer_code'];
                $order['sm_code'] = $orderRow['sm_code'];
                $order['remark'] = $orderRow['remark'];
                $productMapRow = array();
                foreach ($orderPickProduct as $k => $v) {
                    if (!isset($productMapRow[$v['product_id']]) && (!isset($order['package']) || $order['package'] == '')) {
                        $packageMapRow = $packageMapObj->getByCondition(array('warehouse_id' => $orderRow['warehouse_id'], 'product_id' => $v['product_id']), array('pp_id'));
                        if (!empty($packageMapRow)) {
                            $productMapRow[$v['product_id']] = $packageMapRow[0]['pp_id'];
                            $packageRow = $packageObj->getByField($productMapRow[$v['product_id']], 'pp_id', array('pp_name'));
                            $order['package'] = isset($packageRow['pp_name']) ? $packageRow['pp_name'] : '';
                        }
                    }
                    $order['product'][$v['lc_code'] . '_' . $v['pd_id']] = $v;
                    $order['product'][$v['lc_code'] . '_' . $v['pd_id']]['category'] = $category[$v['pc_id']]['pc_name'];
                }
                $order['package']=isset($order['package'])?$order['package']:'';
                //每页最多9个行td
                $chunk = array_chunk($order['product'], 9);
                foreach ($chunk as $k => $v) {
                    $order['product'] = $v;
                    $order['page'] = $k;
                    $orders['order'][$pickRow['picking_id'] . '_' . $orderPickProduct[0]['lc_code'] . '_' . $val['order_id'] . '_' . $k] = $order;
                }
            }
        }
        unset($orderCodeList);
        ksort($orders["order"]);
        if (isset($orders['order']) && !empty($orders['order'])) {
            $result['state'] = 1;
            $result['data'] = array(
                'orders' => $orders['order'],
                'total' => $total,
                'picking_code' => $pickCode,
                'date' => date('Y-m-d'),
                'datetime'=>$pickRow['picking_add_time']
            );
        }
        return $result;
    }

    /**
     * @desc 下架单
     * @param picking_code
     * @return array
     */
    public static function downGoodsShelves($pickCode = '')
    {
        $result = array('state' => 0, 'message' => array(), 'data' => array());
        if (empty($pickCode)) {
            $result['message'][] = '配货单号不能为空.';
            return $result;
        }
        $pickObj = new Service_Picking();
        $pickRow = $pickObj->getByField($pickCode, 'picking_code');
        if (empty($pickRow)) {
            $result['message'][] = '单号:' . $pickCode . ' 不存在.';
            return $result;
        }

        if ($pickRow['is_assign'] == '0') {
            $result['message'][] = '单号:' . $pickCode . ' 未分配配货员,不允许打印.';
            return $result;
        }
        /*if ($pickRow['picking_type'] != '0') {
            $result['message'][] = '单号:' . $pickCode . ' 下架类型不是一票一件,不允许打印.';
            return $result;
        }*/
        $pickerName = '';
        if ($pickRow['picker_id'] != '0' && $pickRow['picker_id'] != '') {
            $userRow = Service_User::getByField($pickRow['picker_id'], 'user_id');
            $pickerName = $userRow['user_name'];
        }

        $pdObj = new Service_PickingDetail();
        $category = Common_DataCache::getProductCategory();
        $orders = $tmpShelf = array();
        $order['page'] = 0;
        $orderPickProduct = $pdObj->getLeftJoinOrderProductByCondition(array('picking_id' => $pickRow['picking_id'], '*', 0, 0, array('lc_code')));
        foreach ($orderPickProduct as $k => $v) {
            if (!in_array($v['lc_code'], $tmpShelf)) {
                $tmpShelf[] = $v['lc_code'];
            }

            if (isset($order['products'][$v['lc_code'] . '_' . $v['product_id']])) {
                $order['products'][$v['lc_code'] . '_' . $v['product_id']]['pd_quantity'] += 1;
            } else {
                $order['products'][$v['lc_code'] . '_' . $v['product_id']] = array(
                    'product_id' => $v['product_id'],
                    'pd_quantity' => $v['pd_quantity'],
                    'lc_code' => $v['lc_code'],
                    'product_barcode' => $v['product_barcode'],
                    'product_title' => $v['product_title'],
                    'category' => isset($category[$v['pc_id']]['pc_name'])?$category[$v['pc_id']]['pc_name']:''
                );
            }
        }
        $shelfTotal = count($tmpShelf);
        unset($tmpShelf);
        //每页最多9个行td
        $chunk = array_chunk($order['products'], 9);

        $i = 1;
        foreach ($chunk as $k => $v) {
            $order['products'] = $v;
            $order['page'] = $i++;
            $orders[$pickRow['picking_id'] . '_' . $k . '_' . $order['page']] = $order;
        }
        unset($order);
        if (!empty($orders)) {
            $result['state'] = 1;
            $result['data'] = array(
                'total' => $shelfTotal,
                'pageTotal' => count($orders),
                'picking_code' => $pickCode,
                'pickerName' => $pickerName,
                'date' => date('Y-m-d'),
                'products' => $orders,
            );
        }
        return $result;
    }

}