<?php
/**
  * @class  店铺订单类
  * @author liao
  * @copyright 2018-09-28
  */
class orderAction extends baseAction
{
	/**
	 * @method    getShopOrderLIst			获取订单列表
	 * @http      (post|get)
	 * @param     isAjax       	       [必填]   		1
	 * @param     page         int     [选填]   		当前页码
	 * @param     perPage      int     [选填]   		每页数量
	 * @param     status       string  [选填]       订单状态  [creat 待支付 | paied 已支付 | send 已发货 | cancel 已取消 | refund 售后中 | completed 已完成] 
	 * @param     keyword      string  [选填]		关键字搜索
	 * @param     orderId      int     [选填]		订单 id 搜索
	 * @param     userId       int     [选填]		订单用户 id 
	 * @author    liao
	 * @copyright 2018-09-28
	 * @return    {"status":true,"data":[{"ord_id":"100808","ord_uid":"172999","ord_shop_id":"223009","ord_status":"creat","ord_total_money":"10.00","ord_freight_money":"0.00","ord_product_money":"10.00","ord_product_discount":"0.00","ord_pay_money":"10.00","ord_add_time":"1536312836","ord_pay_time":null,"ord_send_time":null,"ord_completed_time":null,"ord_evaluate_time":null,"ord_shop_remark":"","ord_add_name":"杨晓松","ord_add_pct_ids":"19,1901,14423,14424","ord_add_pct":"广东省广州市白云三元里街道","ord_add_address":"三元里大道中1181","ord_add_tel":"12824406153","ord_add_mobile":"12824406153","ord_add_zip":"      ","ord_incognito":"0","ord_shipping_method":"0","ord_user_delete":"0","ord_seller_delete":"0","ord_evaluate":"0","ord_cancel_reason":null,"ord_from":"paojd","rn":1,"is_send":0,"proArr":{"p1145":{"orp_id":"1145","orp_ord_id":"100808","orp_pro_id":"118831","orp_pro_name":"测试2323232323","orp_status":"creat","orp_count":"1","orp_price":"10.00","orp_prh_id":"18038","orp_send_time":null,"orp_ext_id":null,"orp_sale_price":"10.00","orp_discount":"无优惠","orp_set_inventory":"0","orp_evaluate":"0","orp_discount_val":"10.00","orp_promotion_price":"10.00","orp_prpa_id":"0","orp_completed_time":null,"orp_money":"10.00","orp_discount_money":"0.00","orp_completed":"0","orp_refund_status":null,"pp_pro_id":"118831","spi_id":"15324","spi_path":"shop223009\/product\/","spi_name":"f4dd197925371d33.jpg","notEvalate":1,"viewHistory":0,"succssTotal":0,"isRefund":0,"allowRefund":0}},"shop_domain":"http:\/\/ww.yiparts.cn:8096\/shop223009\/","shop_top_domain":null,"shopUser":"luwenzhao","shopUserId":"223009","userName":"yipartsb2c","shopName":"卢先生的店","shop_main_name":"小卢","shop_main_tel":"18826233660","total":1}],"code":0}
	 */
	public function getShopOrderList()
	{
		//必须开通店铺
		$this->checkRule(2);
		$Params = $_REQUEST;

		$B2cOrder = new B2cOrder();
		$B2cProduct=new B2cProduct();

		$where=sprintf(' and ord_shop_id=%d and ord_seller_delete=0 and ord_user_delete=0', $this->dataConfig['_shopid']);

		$where.=(isset($Params['userId'])&&LibFc::Int($Params['userId']))? sprintf(' and ord_uid=%d',LibFc::Int($Params['userId'])) : '';

		$perPage = (int) $Params['perPage'];
		$perPage = $perPage > 0 ? $perPage : 10;
		$nowPage = (int) $Params['page'];
		$nowPage = $nowPage > 0 ? $Params['page'] : 1;
		$status=!empty($Params['status']) ? $Params['status'] : '';
		switch ($status) {			
			case 'creat':
				$where.=" and ord_status='creat'";
				break;
			case 'paied':
				$where.=" and ord_status='paied'";
				break;
			case 'send':
				$where.=" and ord_status='send'";
				break;
			case 'cancel':
				$where.=" and ord_status='cancel'";
				break;
			case 'refund':
				if(count($reOrder['data']))
				{
					$where.=sprintf(' and ord_id in(%s)',implode(',',$reOrder['data']));
				}else
				{
					$where.=' and ord_id <-1';
					// goto end;
				}
				break;		
			case 'completed':
				$where.=" and ord_status='completed'";
				break;
		}
		//配件名称和配件ID处理	
		if(isset($Params['keyword'])&&!empty($Params['keyword']))
		{
			$keywordArr=LibFc::keyword($Params['keyword']);
			if(count($keywordArr))
			{
				$keywordStr="'".implode("','",$keywordArr)."'";
				$keywordWhere=sprintf(' and key_txt in(%s)',$keywordStr);
				$keywordProData=$B2cProduct->get('bc_keyword right join bc_product_keyword on prk_key_id=key_id',['prk_pro_id'],$keywordWhere);
			}
			if(!count($keywordArr)||!count($keywordProData['data']))
			{
				//关键字搜索为空，搜索名称
				$nameOrder=$B2cProduct->get('bc_product left join or_order_product on orp_pro_id=pro_id',['orp_ord_id'],sprintf(" and orp_pro_name like '%%%s%%' group by orp_ord_id",LibFc::Escape($Params['keyword'])));
				if(count($nameOrder['data']))
				{
					$orderResArr=array_column($nameOrder['data'], 'orp_ord_id');
					$where.=sprintf(' and ord_id in(%s)',implode(',',$orderResArr));
				}
				else
				{
					$where.=' and ord_id <-1';
				}
			}else
			{
				$keywordPro=array_unique(array_column($keywordProData['data'],'prk_pro_id'));
				$where.=count($keywordPro)?sprintf(" and orp_pro_id in (%s)",implode(',',$keywordPro)):'';
			}
		}	
		$where.=(isset($Params['orderId'])&&!empty($Params['orderId'])) ? sprintf(" and ord_id =%d",LibFc::Escape($Params['orderId'])): '';
		$baseOrderBy='ord_add_time desc';
		$orderBy=$baseOrderBy;
		$orderListData=$B2cOrder->getOrderList($nowPage,$perPage,$orderBy,$where);
		if (!empty($orderListData['data']))
		{
			foreach ($orderListData['data'] as $key => $value) 
			{
				$tempArr=$value['proArr'];
				$orderListData['data'][$key]['proArr']=[];
				foreach ($tempArr as $v) 
				{
					$orderListData['data'][$key]['proArr'][] = $v;
				}
			}
		}
		LibFc::ajaxJsonEncode($orderListData);
	}

	/**
	 * @method    getShopOrderById      获取某一订单详细
	 * @http      (post|get)
	 * @param     isAjax                 [必填]   		1
	 * @param     ordId		      int    [必填]			订单 id 
	 * @author    liao
	 * @copyright 2018-09-28
	 * @return    {"ord_id":"100808","ord_uid":"172999","ord_shop_id":"223009","ord_status":"creat","ord_total_money":"10.00","ord_freight_money":"0.00","ord_product_money":"10.00","ord_product_discount":"0.00","ord_pay_money":"10.00","ord_add_time":"1536312836","ord_pay_time":null,"ord_send_time":null,"ord_completed_time":null,"ord_evaluate_time":null,"ord_shop_remark":"","ord_add_name":"杨晓松","ord_add_pct_ids":"19,1901,14423,14424","ord_add_pct":"广东省广州市白云三元里街道","ord_add_address":"三元里大道中1181","ord_add_tel":"12824406153","ord_add_mobile":"12824406153","ord_add_zip":"      ","ord_incognito":"0","ord_shipping_method":"0","ord_user_delete":"0","ord_seller_delete":"0","ord_evaluate":"0","ord_cancel_reason":null,"ord_from":"paojd","proArr":{"p1145":{"orp_id":"1145","orp_ord_id":"100808","orp_pro_id":"118831","orp_pro_name":"测试2323232323","orp_status":"creat","orp_count":"1","orp_price":"10.00","orp_prh_id":"18038","orp_send_time":null,"orp_ext_id":null,"orp_sale_price":"10.00","orp_discount":"无优惠","orp_set_inventory":"0","orp_evaluate":"0","orp_discount_val":"10.00","orp_promotion_price":"10.00","orp_prpa_id":"0","orp_completed_time":null,"orp_money":"10.00","orp_discount_money":"0.00","orp_completed":"0","orp_refund_status":null,"pp_pro_id":"118831","spi_id":"15324","spi_path":"shop223009\/product\/","spi_name":"f4dd197925371d33.jpg"}},"shopUser":"luwenzhao","userName":"yipartsb2c","shopName":"卢先生的店"}
	 */
	public function getShopOrderById()
	{
		//必须开通店铺
		$this->checkRule(2);
		$Params = $_REQUEST;

		$ordId = (int) $Params['ordId'];
		if( $ordId < 1)
			LibFc::ajaxJsonEncode(['status'=>false, 'data'=>'参数不正确']);

		$B2cOrder = new B2cOrder();
		$res=$B2cOrder->getOrderDetail( $ordId, $this->dataConfig['_shopid']);
		if (!empty($res['data']))
		{
			$tempArr=$res['data']['proArr'];
			$res['data']['proArr']=[];
			foreach ($tempArr as $v) 
			{
				$res['data']['proArr'][]=$v;
			}
		}
		LibFc::ajaxJsonEncode($res);
	}

	/**
	 * @method    editOrder  修改订单价格
	 * @http      (post)
	 * @param     isAjax                    [必填]   		1
	 * @param     orderId		   int    	[必填]			订单 id 
	 * @param     discount		   array    [必填]			折扣    discount[1142]: 5.00  【代表5折】
	 * @param     price		       array    [必填]			优惠价格   price[1142]: -136.00    【代表减 -136.00】
	 * @param     frtPrice		   float    [必填]			运费
	 * @author    liao
	 * @copyright 2018-09-28
	 * @return    {status:true,data:true}
	 */
	public function editOrder()
	{
		// print_r($_REQUEST);
		//必须开通店铺
		$this->checkRule(2);

		$Params = $_POST;
		if (empty($Params))
			LibFc::ajaxJsonEncode(['status'=>false, 'data'=>'参数不正确']);

		$B2cOrder = new B2cOrder();
		$res=$B2cOrder->editorOrderPrice($this->dataConfig['_shopid'], $Params['discount'],$Params['price'], $Params['frtPrice'], $Params['orderId']);
		if($res['status'])
		{
			$res=$B2cOrder->get('or_order',['ord_id','ord_freight_money','ord_pay_money'],sprintf(' and ord_id=%d',$Params['orderId']),true);
		}
		LibFc::ajaxJsonEncode($res);
	}
}