<?php

namespace app\api\controller;

/**
 * ============================================================================
 * 
 * ============================================================================
 * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司，并保留所有权利。
 * 网站地址: https://www.valimart.net/
 * ----------------------------------------------------------------------------
 * 
 * ============================================================================
 * 卖家中心控制器
 */
class Sellerindex extends MobileSeller {

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * @api {POST} api/Sellerindex/index 商家中心
     * @apiVersion 1.0.0
     * @apiGroup Sellerindex
     *
     * @apiHeader {String} X-DS-KEY 卖家授权token
     * 
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object} result.seller_info  卖家信息
     * @apiSuccess {Int} result.seller_info.goods_lockup  违规商品数
     * @apiSuccess {Int} result.seller_info.goods_offline  下架商品数
     * @apiSuccess {Int} result.seller_info.goods_online  上架商品数
     * @apiSuccess {Int} result.seller_info.is_admin  是否主账号 0否1是
     * @apiSuccess {Int} result.seller_info.last_logintime  上次登录时间
     * @apiSuccess {Int} result.seller_info.last_logintime_fmt  上次登录时间描述
     * @apiSuccess {Int} result.seller_info.member_id  用户ID
     * @apiSuccess {Int} result.seller_info.order_nopay_count  待付款订单数
     * @apiSuccess {Int} result.seller_info.order_noreceipt_count  待收货订单数
     * @apiSuccess {Int} result.seller_info.order_noship_count  待发货订单数
     * @apiSuccess {Int} result.seller_info.seller_clienttype  客户端
     * @apiSuccess {Int} result.seller_info.seller_id  卖家账号ID
     * @apiSuccess {Int} result.seller_info.seller_name  卖家账号名称
     * @apiSuccess {Int} result.seller_info.sellergroup_id  卖家账号组
     * @apiSuccess {Int} result.seller_info.store_id  店铺ID
     * @apiSuccess {Object} result.statics  统计信息
     * @apiSuccess {Int} result.statics.bill_confirm  待确认结算单数
     * @apiSuccess {Int} result.statics.complain  投诉数
     * @apiSuccess {Int} result.statics.consult  咨询数
     * @apiSuccess {Int} result.statics.delivery  待发货订单数
     * @apiSuccess {Int} result.statics.goodscount  商品数
     * @apiSuccess {Int} result.statics.imagecount  图片数
     * @apiSuccess {Int} result.statics.lockup  违商品规数
     * @apiSuccess {Int} result.statics.offline  下架商品数
     * @apiSuccess {Int} result.statics.online  上架商品数
     * @apiSuccess {Int} result.statics.payment  已付款订单数
     * @apiSuccess {Int} result.statics.progressing  交易中订单数
     * @apiSuccess {Int} result.statics.refund  退款数
     * @apiSuccess {Int} result.statics.refund_lock  退款中数
     * @apiSuccess {Int} result.statics.return  退货数
     * @apiSuccess {Int} result.statics.return_lock  退货中数
     * @apiSuccess {Int} result.statics.verifyfail  审核失败商品数
     * @apiSuccess {Int} result.statics.waitverify  等待审核商品数
     * @apiSuccess {Object} result.store_info  店铺信息 （返回字段参考store表）
     * @apiSuccess {Object} result.store_info.daily_sales 今日销量
     * @apiSuccess {Float} result.store_info.daily_sales.orderamount 订单金额
     * @apiSuccess {Int} result.store_info.daily_sales.ordernum 订单量
     * @apiSuccess {Int} result.store_info.goods_count 商品数量
     * @apiSuccess {Int} result.store_info.grade_albumlimit 商品图片限制，0不限制
     * @apiSuccess {Int} result.store_info.grade_goodslimit 商品数量限制，0不限制
     * @apiSuccess {Int} result.store_info.grade_id 店铺等级
     * @apiSuccess {Int} result.store_info.grade_name 店铺等级名称
     * @apiSuccess {Object} result.store_info.monthly_sales 月销量
     * @apiSuccess {Float} result.store_info.monthly_sales.orderamount 订单金额
     * @apiSuccess {Int} result.store_info.monthly_sales.ordernum 订单量
     */
    public function index() {
        $seller_info = array();
        $seller_info = $this->seller_info;
        $store_info = $this->store_info;

        //最后登陆
        $seller_info['last_logintime_fmt'] = date('Y-m-d H:i:s', $seller_info['last_logintime']);
        $order_model = model('order');
        // 待付款
        $seller_info['order_nopay_count'] = intval($order_model->getOrderCountByID('store', $store_info['store_id'], 'NewCount'));
        // 待发货
        $seller_info['order_noship_count'] = intval($order_model->getOrderCountByID('store', $store_info['store_id'], 'PayCount'));
        // 待收货
        $seller_info['order_noreceipt_count'] = intval($order_model->getOrderCountByID('store', $store_info['store_id'], 'SendCount'));

        $goods_model = model('goods');
        // 出售中的商品
        $seller_info['goods_online'] = $goods_model->getGoodsCommonOnlineCount(array(array('store_id' ,'=', $store_info['store_id'])));
        // 仓库待上架的商品
        $seller_info['goods_offline'] = $goods_model->getGoodsCommonOfflineCount(array(array('store_id' ,'=', $store_info['store_id'])));
        // 违规下架的商品
        $seller_info['goods_lockup'] = $goods_model->getGoodsCommonLockUpCount(array(array('store_id' ,'=', $store_info['store_id'])));

        //店铺头像
        $store_info['store_avatar'] = get_store_logo($store_info['store_avatar'], 'store_avatar');

        //店铺标志
        $store_info['store_logo'] = get_store_logo($store_info['store_logo'], 'store_logo');

        //等级信息
        $store_info['grade_name'] = $this->store_grade['storegrade_name'];
        //商品数量限制
        $store_info['grade_goodslimit'] = $this->store_grade['storegrade_goods_limit'];
        //图片空间数量限制
        $store_info['grade_albumlimit'] = $this->store_grade['storegrade_album_limit'];

        /**
         * 销售情况统计
         */
        $field = 'COUNT(*) as ordernum,SUM(order_amount) as orderamount';
        $where = array();
        $where[] = array('store_id', '=', $this->store_info['store_id']);
        //有效订单
        $where[] = array('order_isvalid', '=', 1);
        //昨日销量
        $where[] = array('order_add_time', 'between', array(strtotime(date('Y-m-d', (TIMESTAMP - 3600 * 24))), strtotime(date('Y-m-d', TIMESTAMP)) - 1));
        $daily_sales = model('stat')->getoneByStatorder($where, $field);
        //月销量
        $where[] = array('order_add_time', '>', strtotime(date('Y-m', TIMESTAMP)));
        $monthly_sales = model('stat')->getoneByStatorder($where, $field);
        $store_info['daily_sales'] = $daily_sales;
        $store_info['monthly_sales'] = $monthly_sales;

        //统计
        $statics = $this->getStatics();
        ds_json_encode(10000, '', array('seller_info' => $seller_info, 'store_info' => $store_info, 'statics' => $statics));
    }

    /**
     * @api {POST} api/Sellerindex/getStatics 取得卖家统计类信息
     * @apiVersion 1.0.0
     * @apiGroup Sellerindex
     *
     * @apiHeader {String} X-DS-KEY 卖家授权token
     * 
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    private function getStatics() {
        $add_time_to = strtotime(date("Y-m-d")) + 60 * 60 * 24;   //当前日期 ,从零点来时
        $add_time_from = strtotime(date("Y-m-d", (strtotime(date("Y-m-d")) - 60 * 60 * 24 * 30)));   //30天前
        $goods_online = 0;      // 出售中商品
        $goods_waitverify = 0;  // 等待审核
        $goods_verifyfail = 0;  // 审核失败
        $goods_offline = 0;     // 仓库待上架商品
        $goods_lockup = 0;      // 违规下架商品
        $consult = 0;           // 待回复商品咨询
        $no_payment = 0;        // 待付款
        $no_delivery = 0;       // 待发货
        $no_receipt = 0;        // 待收货
        $refund_lock = 0;      // 售前退款
        $refund = 0;            // 售后退款
        $return_lock = 0;      // 售前退货
        $return = 0;            // 售后退货
        $complain = 0;          //进行中投诉

        $goods_model = model('goods');
        // 全部商品数
        $goodscount = $goods_model->getGoodsCommonCount(array('store_id' => $this->store_info['store_id']));
        // 出售中的商品
        $goods_online = $goods_model->getGoodsCommonOnlineCount(array(array('store_id' ,'=', $this->store_info['store_id'])));
        if (config('ds_config.goods_verify')) {
            // 等待审核的商品
            $goods_waitverify = $goods_model->getGoodsCommonWaitVerifyCount(array(array('store_id' ,'=', $this->store_info['store_id'])));
            // 审核失败的商品
            $goods_verifyfail = $goods_model->getGoodsCommonVerifyFailCount(array(array('store_id' ,'=', $this->store_info['store_id'])));
        }
        // 仓库待上架的商品
        $goods_offline = $goods_model->getGoodsCommonOfflineCount(array(array('store_id' ,'=', $this->store_info['store_id'])));
        // 违规下架的商品
        $goods_lockup = $goods_model->getGoodsCommonLockUpCount(array(array('store_id' ,'=', $this->store_info['store_id'])));

        // 等待回复商品咨询
        $consult = model('consult')->getConsultCount(array('store_id' => $this->store_info['store_id'], 'consult_reply' => ''));

        // 商品图片数量
        $imagecount = model('album')->getAlbumpicCount(array('store_id' => $this->store_info['store_id']));

        $order_model = model('order');
        // 交易中的订单
        $progressing = $order_model->getOrderCountByID('store', $this->store_info['store_id'], 'TradeCount');
        // 待付款
        $no_payment = $order_model->getOrderCountByID('store', $this->store_info['store_id'], 'NewCount');
        // 待发货
        $no_delivery = $order_model->getOrderCountByID('store', $this->store_info['store_id'], 'PayCount');

        $refundreturn_model = model('refundreturn');
        // 售前退款
        $condition = array();
        $condition[] = array('store_id', '=', $this->store_info['store_id']);
        $condition[] = array('refund_type', '=', 1);
        $condition[] = array('order_lock', '=', 2);
        $condition[] = array('refund_state', '<', 3);
        $refund_lock = $refundreturn_model->getRefundreturnCount($condition);
        // 售后退款
        $condition = array();
        $condition[] = array('store_id', '=', $this->store_info['store_id']);
        $condition[] = array('refund_type', '=', 1);
        $condition[] = array('order_lock', '=', 1);
        $condition[] = array('refund_state', '<', 3);
        $refund = $refundreturn_model->getRefundreturnCount($condition);
        // 售前退货
        $condition = array();
        $condition[] = array('store_id', '=', $this->store_info['store_id']);
        $condition[] = array('refund_type', '=', 2);
        $condition[] = array('order_lock', '=', 2);
        $condition[] = array('refund_state', '<', 3);
        $return_lock = $refundreturn_model->getRefundreturnCount($condition);
        // 售后退货
        $condition = array();
        $condition[] = array('store_id', '=', $this->store_info['store_id']);
        $condition[] = array('refund_type', '=', 2);
        $condition[] = array('order_lock', '=', 1);
        $condition[] = array('refund_state', '<', 3);
        $return = $refundreturn_model->getRefundreturnCount($condition);

        $condition = array();
        $condition[] = array('accused_id', '=', $this->store_info['store_id']);
        $condition[] = array('complain_state', 'between', array(10, 90));
        $complain_model = model('complain');
        $complain = $complain_model->getComplainCount($condition);

        //待确认的结算账单
        $bill_model = model('bill');
        $condition = array();
        $condition[] = array('ob_store_id','=',$this->store_info['store_id']);
        $condition[] = array('ob_state','=',BILL_STATE_CREATE);
        $bill_confirm_count = $bill_model->getOrderbillCount($condition);

        //统计数组
        $statistics = array(
            'goodscount' => $goodscount,
            'online' => $goods_online,
            'waitverify' => $goods_waitverify,
            'verifyfail' => $goods_verifyfail,
            'offline' => $goods_offline,
            'lockup' => $goods_lockup,
            'imagecount' => $imagecount,
            'consult' => $consult,
            'progressing' => $progressing,
            'payment' => $no_payment,
            'delivery' => $no_delivery,
            'refund_lock' => $refund_lock,
            'refund' => $refund,
            'return_lock' => $return_lock,
            'return' => $return,
            'complain' => $complain,
            'bill_confirm' => $bill_confirm_count
        );

        return $statistics;
    }

}

?>
