Delivery.php 9.33 KB
<?php

/**
 * Public 收货地址类 
 * User: elkan
 * Date: 2014-7-30
 * Time: PM1:08
 */
class YHMApi_App_V1_Delivery extends YHMApi_App_V1_Base {

    /**
     * 获取用户收货地址
     * @param array $params
     * @param string $fileds
     * @return array
     */
    static function getListByUid(array $params, $fileds = '*') {
        if (empty($params['uid'])) {
            return self::result(400, '用户id不能为空');
        }
        $data = YHMDelivery_Models_Delivery_Client::getUserDelivery($params['uid']);
        $getData = array();
        foreach ($data as $k => $v) {
            $Province = YHMPassport_Models_Area_Client::getAreaByCode(substr($v['area_code'], 0, 2));
            $City = YHMPassport_Models_Area_Client::getAreaByCode(substr($v['area_code'], 0, 4));
            $District = YHMPassport_Models_Area_Client::getAreaByCode($v['area_code']);
            $list = array(
                'id' => $v['id'],
                'delivery_name' => $v['delivery_name'],
                'mobile' => $v['mobile'],
                'area_code' => $v['area_code'],
                'address' => $v['address'],
                'is_default' => $v['is_default'],
                'zip_code' => $v['zip_code']
            );
            if ($Province == $City) {
                $list['area_text'] = $City . $District;
            } else {
                $list['area_text'] = $Province . $City . $District;
            }
            $getData[] = $list;
        }
        if (empty($data)) {
            return self::result(200, '暂无地址',array());
        }
        return self::result(200, '收获地址列表', $getData);
    }

    /**
     * 添加用户收货地址
     * @param array $params
     * @param type $fileds
     * @return type
     */
    static function add(array $params, $fileds = '*') {
        if (empty($params['uid'])) {
            return self::result(400, '用户id不能为空');
        }
        if (empty($params['delivery_name'])) {
            return self::result(400, '收获人姓名不能为空');
        }
        if (empty($params['area_code'])) {
            return self::result(400, '地区不能为空');
        }
        if (empty($params['address'])) {
            return self::result(400, '详细地址不能为空');
        }
        if (empty($params['zip_code'])) {
            return self::result(400, '邮编不能为空');
        }
        if (empty($params['mobile'])) {
            return self::result(400, '手机号不能为空');
        }
        $is_default = !empty($params['is_default']) ? $params['is_default'] : 'N';
        if ($is_default == 'Y') {
            //先把默认地址清零
            YHMDelivery_Models_Delivery_Client::updateEmptyDefaultAddress($params['uid']);
        }
         $data = YHMDelivery_Models_Delivery_Client::getUserDelivery($params['uid']);
         if (count($data)<1){
             $is_default='Y';
         }
        $id = YHMDelivery_Models_Delivery_Client::add($params['uid'], $params['delivery_name'], $params['area_code'], $params['address'], $params['zip_code'], $params['mobile'], $is_default);
        if ($id < 1) {
            return self::result(400, '添加失败');
        }
        return self::result(200, '添加成功', array("id" => $id));
    }

    /**
     * 设为默认收获地址
     * @param array $params
     * @param type $fileds
     * @return array
     */
    static function setDefault(array $params, $fileds = '*') {
        if (empty($params['id'])) {
            return self::result(400, '收获地址id不能为空');
        }
        if (empty($params['uid'])) {
            return self::result(400, '用户id不能为空');
        }
        //先把默认地址清零
        YHMDelivery_Models_Delivery_Client::updateEmptyDefaultAddress($params['uid']);
        //设置默认地址
        YHMDelivery_Models_Delivery_Client::updateUserDefaultAddress($params['id'], $params['uid']);
        return self::result(200, '设置成功');
    }

    /**
     * 修改地址信息
     * @param array $params
     * @param type $fileds
     * @return array
     */
    static function update(array $params, $fileds = '*') {
        if (empty($params['id'])) {
            return self::result(400, 'id不能为空');
        }
        if (empty($params['uid'])) {
            return self::result(400, '用户ID不能为空');
        }
        if (empty($params['delivery_name'])) {
            return self::result(400, '收货人姓名不能为空');
        }
        if (empty($params['area_code'])) {
            return self::result(400, '地区不能为空');
        }
        if (empty($params['address'])) {
            return self::result(400, '详细地址不能为空');
        }
        if (empty($params['zip_code'])) {
            return self::result(400, '邮编不能为空');
        }
        if (empty($params['mobile'])) {
            return self::result(400, '手机号不能为空');
        }
        $is_default = !empty($params['is_default']) ? $params['is_default'] : 'N';
        if($is_default == 'Y') {
        	//先把默认地址清零
        	YHMDelivery_Models_Delivery_Client::updateEmptyDefaultAddress($params['uid']);
        }
        YHMDelivery_Models_Delivery_Client::updateDeliveryAddress($params['id'], $params['uid'], $params['delivery_name'], $params['area_code'], $params['address'], $params['zip_code'], $params['mobile'], $is_default);
        return self::result(200, "修改成功");
    }

    /**
     * 删除修改地址
     * @param array $params
     * @param type $fileds
     * @return array
     */
    static function delete(array $params, $fileds = '*') {
        if (empty($params['id'])) {
            return self::result(400, 'id不能为空');
        }
        if (empty($params['uid'])) {
            return self::result(400, '用户id不能为空');
        }
        $id = YHMDelivery_Models_Delivery_Client::deleteDefaultAddress($params['id'], $params['uid']);
        if ($id < 1) {
            return self::result(400, '删除失败');
        }
        return self::result(200, '删除成功');
    }

    /**
     * 获取地址信息
     * @param array $params
     * @param string $fileds
     */
    public static function getSubAreaList(array $params, $fileds = '*') {
        $parent_id = intval($params['area_id']);
        $retData = array();
        $areaList = YHMPassport_Models_Area_Client::getSubAreaList($parent_id);
        foreach ($areaList as $area) {
            $retData[] = array(
                'id' => $area['id'],
                'code' => $area['code'],
                'caption' => $area['caption']
            );
        }
        return self::result(200, '地址列表', $retData);
    }

    /**
     * 通过经纬度坐标获取地区
     * @param array $params
     * @param string $fields
     */
    public static function getAreaByCoord(array $params, $fields = '*') {
        $lng = $params['lng'];
        $lat= $params['lat'];
        $addrData = YHMUtils_Lbs_Client::getAddrFromLatlng($lat, $lng);
        $retData = array();
        if(!empty($addrData)) {
        	$area_code = YHMPassport_Models_Area_Client::getIdByCaption($addrData['area']);
        	if(!empty($area_code)) {
        		$retData = array(
        				'area_code' => $area_code,
        				'province' => $addrData['province'],
        				'city' => $addrData['city'],
        				'area' => $addrData['area']
        		);
        		return self::result(200, '获取成功', $retData);
        	}
    
        }
        return self::result(412, '获取失败');
    }

    /**
     * 获取默认送货地址
     * @param array $params
     * @param string $fields
     */
    public static function getDefaultDelivery(array $params, $fields = '*') {
        $retData = YHMDelivery_Models_Delivery_Client::getDefaultDeliveryByUid($params['uid']);
        //如没有默认地址,取最新的地址
        if (empty($retData)) {
            $retData = YHMDelivery_Models_Delivery_Client::getDeliveryByUid($params['uid']);
        }
        //没有地址则返回暂无数据
        if(empty($retData)){
            return self::result(400,'暂无数据');
        }
        $retData = array(
            'id' => $retData['id'],
            'delivery_name' => $retData['delivery_name'],
            'mobile' => $retData['mobile'],
            'area_code' => $retData['area_code'],
            'address' => $retData['address'],
            'is_default' => $retData['is_default'],
            'zip_code' => $retData['zip_code']
        );
        $Province = YHMPassport_Models_Area_Client::getAreaByCode(substr($retData['area_code'], 0, 2));
        $City = YHMPassport_Models_Area_Client::getAreaByCode(substr($retData['area_code'], 0, 4));
        $District = YHMPassport_Models_Area_Client::getAreaByCode($retData['area_code']);
        if ($Province == $City) {
            $retData['area_text'] = $City . $District;
        } else {
            $retData['area_text'] = $Province . $City . $District;
        }
        return self::result(200, '成功', $retData);
    }

    /**
     * 通过id获取地区
     * @param int $id
     * @return type
     */
    public static function getDeliveryById(array $params, $fields = '*') {
        if ($params['id'] < 1) {
            return self::result(400, '用户id不能为空');
        }
        $retData = YHMDelivery_Models_Delivery_Client::getDeliveryById($params['id']);
        return self::result(200, '成功', $retData);
    }

}