<?php 
namespace Api\Controller;

class UserOtherController extends BaseController
{
	/**
     * [my_address_list 获取我的地址 method POST]
     * @AuthorHTL
     * @DateTime  2018-02-07T16:22:47+0800
     * @return    [type]                   [description]
     */
    public function my_address_list()
    {
        if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid不能为空']);

        $user_id = M('users')->where(array('admin_id' => $this->data['business_id'], 'openid' => $this->data['openid']))->getField('id');

        if (empty($user_id)) apiReturn(['status' => -1, 'msg' => '没有此用户']);

        $list = M('address')->where(array('user_id' => $user_id))->select();

        if (empty($list)) apiReturn(['status' => -1, 'msg' => '没有数据']);

        apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);      
    }

    /**
     * [add_address 添加地址]
     * @AuthorHTL
     * @DateTime  2018-02-07T16:30:59+0800
     */
    public function add_address()
    {
        if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid为空']);
        if (empty($this->data['name'])) apiReturn(['status' => -1, 'msg' => '名字为空']);
        if (empty($this->data['mobile'])) apiReturn(['status' => -1, 'msg' => '联系方式为空']);
        if (empty($this->data['address'])) apiReturn(['status' => -1, 'msg' => '收货地址为空']);

        $user_id = M('users')->where(array('admin_id' => $this->data['business_id'], 'openid' => $this->data['openid']))->getField('id');

        $result = M('address')->add(
                array(
                    'user_id' => $user_id,
                    'mobile' => $this->data['mobile'],
                    'name' => $this->data['name'],
                    'address' => $this->data['address'],
                    'create_time' => time()
                )
            );
        if ($result) {
            apiReturn(['status' => 1, 'msg' => '添加成功']);
        } else {
            apiReturn(['status' => 1, 'msg' => '添加失败']);
        }
    }

    /**
     * [add_address 删除地址 method POST]
     * @AuthorHTL
     * @DateTime  2018-02-07T16:30:59+0800
     */
    public function del_address()
    {
        if (empty($this->data['id'])) apiReturn(['status' => -1, 'msg' => '地址id为空']);

        $result = M('address')->where(array('id' => $this->data['id']))->delete();
        if ($result) {
            apiReturn(['status' => 1, 'msg' => '删除成功']);
        } else {
            apiReturn(['status' => 1, 'msg' => '删除失败']);
        }
    }

    /**
     * [collect_goods 收藏商品 method POST]
     * @AuthorHTL
     * @DateTime  2018-02-07T17:06:44+0800
     * @return    [type]                   [description]
     */
    public function collect_goods()
    {
        if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid为空']);
        if (empty($this->data['goods_id'])) apiReturn(['status' => -1, 'msg' => '商品id为空']);

        $user_id = M('users')->where(array('admin_id' => $this->data['business_id'], 'openid' => $this->data['openid']))->getField('id');

        // 检测是否重复收藏
        $is_collect = M('collect')->where(array('goods_id' => $this->data['goods_id'], 'user_id' => $user_id))->find();
        if (!empty($is_collect)) apiReturn(['status' => -1, 'msg' => '您已收藏，不要重复收藏哦']);

        $result = M('collect')->add(
                        array(
                            'user_id' => $user_id,
                            'goods_id' => $this->data['goods_id'],
                            'create_time' => time()
                        )
                    );
        if ($result) {
            apiReturn(['status' => 1, 'msg' => '收藏成功']);
        } else {
            apiReturn(['status' => -1, 'msg' => '收藏失败']);
        }
    }

    /**
     * [cancle_collect 取消收藏 method POST]
     * @AuthorHTL
     * @DateTime  2018-02-07T17:25:08+0800
     * @return    [type]                   [description]
     */
    public function cancle_collect()
    {
        if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid为空']);
        if (empty($this->data['goods_id'])) apiReturn(['status' => -1, 'msg' => '商品id为空']);

        $user_id = M('users')->where(array('admin_id' => $this->data['business_id'], 'openid' => $this->data['openid']))->getField('id');

        $result = M('collect')->where(array('user_id' => $user_id, 'goods_id' => $this->data['goods_id']))->delete();

        if ($result) {
            apiReturn(['status' => 1, 'msg' => '取消收藏成功']);
        } else {
            apiReturn(['status' => -1, 'msg' => '取消收藏失败']);
        }
    }

    /**
     * [collect_list 商品收藏 method POST]
     * @AuthorHTL
     * @DateTime  2018-02-07T17:25:08+0800
     * @return    [type]                   [description]
     */
    public function collect_list()
    {
        if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid为空']);
        if (empty($this->data['page'])) apiReturn(['status' => -1, 'msg' => '分页为空']);

        $user_id = M('users')->where(array('admin_id' => $this->data['business_id'], 'openid' => $this->data['openid']))->getField('id');

        $list = M('collect c')->join(array('LEFT JOIN am_goods g ON g.id = c.goods_id'))->where(array('c.user_id' => $user_id, 'g.status' => 1))->page($this->data['page'])->field('c.id, g.id goods_id, g.title, g.price, g.img, g.max_buy, g.false_sell, g.sales')->select();

        if (empty($list)) apiReturn(['status' => -1, 'msg' => '没有数据']);

        foreach ($list as $key => &$value) {
            $value['sales'] += $value['false_sell'];
            $value['img'] = $this->url.'/Uploads/'.$value['img'];
            unset($list[$key]['false_sell']);
        }

        apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
    }

    /**
     * [get_member_level 获取会员等级 method POST]
     * @AuthorHTL
     * @DateTime  2018-02-08T17:05:36+0800
     * @return    [type]                   [description]
     */
    public function get_member_level()
    {
        if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid为空']);

        // 获取等级列表
        $level_list = M('member_level')->where(array('admin_id' => $this->data['business_id']))->field('level_name, money')->select();

        if (empty($level_list)) apiReturn(['status' => -1, 'msg' => '请在后台添加会员等级']);

        // 获取用户id
        $user_id = M('users')->where(array('admin_id' => $this->data['business_id'], 'openid' => $this->data['openid']))->getField('id');

        // 获取用户累计消费金额
        $money = M('user_money')->where(array('user_id' => $user_id))->getField('money');

        $level_list = $this->two_sort($level_list, 'money', 1);
        if (empty($money)) apiReturn(['status' => 1, 'msg' => '已获取用户等级', 'data' => $level_list[0]]);

        $level = array();
        foreach ($level_list as $key => $value) {
            if ($value['money'] <= $money) {
                $level = $value;
            }
        }

        if (empty($money)) apiReturn(['status' => 1, 'msg' => '已获取用户等级', 'data' => $level]);
    }

    /**
     * [get_user_info 获取用户信息]
     * @AuthorHTL
     * @DateTime  2018-03-26T15:34:53+0800
     * @return    [type]                   [description]
     */
    public function get_user_info()
    {
        if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid为空']);

        $info = M('users')->where(array('admin_id' => $this->data['business_id'], 'openid' => $this->data['openid']))->find();

        $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $info]);
    }

    /**
     * [mao_pao_sort 冒泡排序]
     * @AuthorHTL
     * @DateTime  2018-02-08T17:24:47+0800
     * @param     [type]                   $array [需要排序的数组]
     * @return    [type]                          [description]
     */
    private function mao_pao_sort($array = array())
    {
        $count = count($array);
        if (empty($count)) return $array;
        for ($i = 0; $i < $count; $i++) {
            for ($j = $count - 1; $j > $i; $j--) {
                if ($array[$i] > $array[$j]) {
                    $temp = $array[$i];
                    $array[$i] = $array[$j];
                    $array[$j] = $temp;
                }
            }
        }

        return $array;
    }

    /**
     * [two_sort 二维数组根据某个字段排序]
     * @AuthorHTL
     * @DateTime  2018-02-08T17:59:04+0800
     * @param     array                    $array [需要排序的数组]
     * @param     string                    $field [排序字段]
     * @param     string                    $type [1升序  2降序]
     * @return    [type]                          [description]
     */
    private function two_sort($array = array(), $field = '', $type = 1)
    {   
        $count = count($array);
        if (empty($count)) return $array;

        if ($type == 1) {
            for ($i = 0; $i < $count; $i++) {
                for ($j = $count - 1; $j > $i; $j--) {
                    if ($array[$i][$field] > $array[$j][$field]) {
                        $temp = $array[$i];
                        $array[$i] = $array[$j];
                        $array[$j] = $temp;
                    }
                }
            }
        } else if ($type == 2) {
             for ($i = 0; $i < $count; $i++) {
                for ($j = $count - 1; $j > $i; $j--) {
                    if ($array[$i][$field] < $array[$j][$field]) {
                        $temp = $array[$i];
                        $array[$i] = $array[$j];
                        $array[$j] = $temp;
                    }
                }
            }
        }

        return $array;
    }
}