<?php
/**
 * 微信小程序支付模块
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace Wxsmall\Model;
use Think\Model;
class CartModel extends Model{
    /**
     * 获取购物车列表页面
     * @param array $param 筛选参数
     * @return array 返回信息
     */
    public function index($param){
        $return = array('status'=>3,'msg'=>'获取信息失败');
        if(empty($param['uid'])){
            return $return;
        }
        $map = array();
        $data = $map = array();
        $map['user_id'] = $param['uid'];
        $map['cart_type'] = 3;//类型为外卖商品
        $cart_ids = M('Cart')->where($map)->getField('id', true);
        $data = D('Common/Cart')->cartlist('', $cart_ids,0,0,3);
        $shop_id = get_wxsmall_shop_id();
        $cart_list=array();
        foreach($data['list'] as $k=>$v){
            // 3-8 zjn 赠品图片不显示
            foreach ($v['promot_list'] as $ke => $val) {
                if($val['gift_list']){
                    $v['promot_list'][$ke]['gift_list']['img']=thumb($val['gift_list']['img'],289,289,'noimg_289x289.png');
                }
            }
            // -------end----
            $data['list'] = array();
            if($shop_id==$k){
                $cart_list['list'][$k] = $v;
                $cart_list['list'][$k]['send_desc'] = $this->get_send_desc($shop_id,$param);
            }else{
                $cart_list['other_list'][$k] = $v;
            }
        }
        if($data['status']==1){
            $cart_list['HCshop']=$data['HCshop'];
            $cart_list['all_price']=$data['all_price'];
            $cart_list['all_save_price']=$data['all_save_price'];
            $cart_list['new_save_price']=$data['new_save_price'];
            $cart_list['promot_lists_json']=$data['promot_lists_json'];
            $cart_list['shopgoods']=$data['shopgoods'];
            $cart_list['status']=$data['status'];
            return $cart_list;
        }
        return $return;
    }
    /**
     * 获取运费描述信息
     * @param int $shop_id 门店id
     * @param array $param 内容信息
     * @return array 返回信息
     */
    public function get_send_desc($shop_id,$param){
        $map = array();
        $desc = '';
        if($param['send']==1){//同城配送
            $map['shop_id'] = $shop_id;
            $result = M('ShopsDelivery')->where($map)->field(true)->find();
            if($result){
                $desc = '满'.format_price($result['start_price']).'包邮';
                //$desc = "起送费：".format_price($result['start_price']).'，配送费：'.format_price($result['delivery_price']);
            }
        }elseif($param['send']==2){//快递接口
            $map['xpoint'] = $param['xpoint'];
            $map['ypoint'] = $param['ypoint'];
            $geodata = D('Common/Area')->geocoding($param);
            $cityname = $geodata['result']['addressComponent']['city'];
            if($cityname){
                $city=strlen($cityname)>6?strtr($cityname,array('省'=>'','市'=>'','县'=>'','区'=>'','自治区'=>'',)):$cityname;
                                
                $map=array();
                $map['status']=1;
                $map['id']=array('like',$city);
        $city_id=M('Area')->where($map)->cache(true)->getField('id');
                if($city_id){//定位到城市
                    $field = array("sf.*");
                    $join = "LEFT JOIN __SHOPS__ s ON s.id=sf.shop_id AND sf.shop_id=".$shop_id." and s.city_id=".$city_id;
                    $map = array();
                    $map['s.status'] = 1;
                    $shop_shipinfo = M('ShippingFee')->alias('sf')->where($map)->join($join)->field($field)->find();
                    if($shop_shipinfo){
                        $shop_shipinfo = unserialize($shop_shipinfo['configure']);
                        $desc = '满'.format_price($shop_shipinfo['money']).'包邮';
                    }
                }
            }
        }
        return $desc;
    }
    /**
     * 满赠，加价购替换商品
     * @param array $param 内容信息
     * @return array 返回信息
     */
    public function change_pro_goods($param){
        $return = array('status'=>3,'msg'=>'获取信息失败');
        $res=D('Common/Cart')->change_pro_goods($param);
        if($res['status']==1){
            $return['status'] = 1;
            $return['data'] = $res['data'];
            unset($return['msg']);
        }
        return $return;
    }

    /**提交购物车
     * @param $param
     * @return array
     */
    public function sub_cart($param){
        $return = array('status'=>3);
        if(empty($param) || empty($param['uid'])){$return['msg']="参数错误";return $return;};
        $uid=$param['uid'];
        //$mobile=idtoname($uid,'Member','mobile','id',false);
        //if(empty($mobile)){$return['msg']="请完善资料";return $return;}
        $cart_ids=array();
        if(empty($param['cart_ids'])){$return['msg']="请选择结算商品";return $return;}
       foreach($param['cart_ids'] as $key=>$val) {
           $cart_ids[] = $val['id'];
           $map = array();
           $map['id'] = $val['id'];
           $cart_info = M("Cart")->where($map)->field(true)->find();
           if ($cart_info) {
               $id = intval($val['id']);
               $num = intval($val['num']);
               $gooda_attrs = unserialize($cart_info['goods_attr']);
               $gooda_attrs_values = array_values($gooda_attrs);
               $data = D("Common/Cart")->check_cart($cart_info['goods_id'], $gooda_attrs_values[0], $num, $gooda_attrs_values[1], 0, $cart_info['cart_type']);
               if ($cart_info['cart_type'] == 3) {
                   $type = 14;
               } else {
                   $type = 0;
               }
               if ($data['status'] == 0 || $data['status'] == 1) {
                   $return['msg'] = $data['msg'];
                   $return['url'] = $data['url'];
                   $return['data']=$data;
                   return $return;
               } else {
                   //修改加价购活动
                   $param_data = array();
                   $param_data['pro_id'] = $cart_info['cart_id'];
                   $param_data['user_id'] = $cart_info['user_id'];
                   D("Common/Cart")->check_manage_cart_activity($param_data);
                   //修改商品数量
                   $cart_data['goods_num'] = $num;
                   $cart_map['id'] = $id;
                   M("Cart")->where($cart_map)->save($cart_data);
               }
           }
       }

        $return['status'] = 1;
        $carts=array_filter($cart_ids);
        $return['cart_ids']=implode(',',$carts);
        $return['type']=$type;
        return $return;
    }
}