<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 获取配置
 */
namespace app\smartbc\controller\api\v1;
use app\smartbc\controller\api\Base;
use app\smartbc\model\SmartbcConfig;
use app\smartbc\model\SmartbcCouponUser;
use app\smartbc\model\SmartbcOrder;
use app\smartbc\model\SmartbcStore;
use app\smartbc\model\SmartbcStoreBill;
use app\smartbc\model\SmartbcStoreGroup;
use app\smartbc\model\SmartbcStoreUnion;
use app\common\facade\Upload;
use think\Db;

class Config extends Base{

     /**
     * 获取应用配置
     **/
    public function index(){
        $info = SmartbcConfig::getConfig($this->miniapp->id);
        if(!isset($info)) {
            return enjson(403,'应用未配置');
        }
        $article = json_decode($info->article);
        $config['service_telephone'] = $info->service_telephone ??'';
        $config['shore_img']         = $article->shore_img ??'';
        $config['shore_text']        = $article->shore_text ??'';
        $config['mch_text']          = $article->mch_text ??'';
        $config['mch_qrcode']        = $article->mch_qrcode ??'';
        //判断用户是否商家
        $config['store']             = ['is_store' => 0,'info' => []];
        //判断是否圈主
        $config['group']             = [
            'is_group'     => 0,
            'join_num'     => 0,
            'order'        => 0,
            'count'        => 0,
            'help'         => 0,
            'group_member' => $info->group_member,
            'group_join'   => $info->group_join,
            'info'         => []
        ];
        $config['user_coupon_num']   = 0; //优惠券
        $config['user_order_num']    = 0; //买单数
        $config['user_save_money']   = 0; //已省钱
        $config['user_bindphone']    = ""; //未绑定手机
        if($this->user){
            $config['user_bindphone']    = $this->user->phone_uid ?? ''; //未绑定手机
            //优惠券
            $config['user_coupon_num']  = SmartbcCouponUser::where(['member_miniapp_id' => $this->miniapp_id, 'is_end' => 0, 'uid' => $this->user->id])->count();
            //买单数
            $config['user_order_num']   = SmartbcOrder::where(['member_miniapp_id' => $this->miniapp_id, 'state'  => 1, 'uid' => $this->user->id])->count();
            //已省钱
            $config['user_save_money']  = SmartbcCouponUser::where(['member_miniapp_id' => $this->miniapp_id, 'is_end' => 1, 'uid' => $this->user->id])->where('money', '>', 0)->sum('money');
            $store = SmartbcStore::manageStore($this->user->id);
            if ($store) {
                $config['store']['is_store'] = 1;
                $config['store']['info']     = $store->toArray();
                $group_ids = SmartbcStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'type' => 0,'store_id' => $store->id])->column('group_id');
                //圈主加群数量
                $config['group']['join_num'] = SmartbcStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $store->id,'type' => 0])->count();
                //买单
                $config['group']['order']    = SmartbcStoreBill::where(['member_miniapp_id' => $this->miniapp_id])->whereIn('store_id',SmartbcStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'type' => 0])->whereIn('group_id',$group_ids)->column('store_id'))->sum('money');
                //次数
                $config['group']['count']    = SmartbcCouponUser::where(['member_miniapp_id' => $this->miniapp_id])->whereIn('store_id',SmartbcStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'type' => 0])->whereIn('group_id',$group_ids)->column('store_id'))->count();
                //互助
                $help = 0;
                foreach ($group_ids as $group_id){
                    $help += SmartbcCouponUser::where(['member_miniapp_id' => $this->miniapp_id,'is_end' => 1])->where('','EXP',Db::raw("FIND_IN_SET(".$group_id.",group_ids)"))->count();
                }
                $config['group']['help'] = $help;
            }
            $group = SmartbcStoreGroup::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id])->find();
            if ($group) {
                $config['group']['is_group'] = 1;
                $config['group']['info']     = $group->toArray();
            }
       }
       return enjson(200,'应用配置',$config);
    }

    /**
     * 上传图片
     * @return void
     */
    public function upImg(){
        if(request()->isPost()){
            $rel = Upload::index();
            if($rel['error'] == 0){
                return json(['code'=>200,'msg'=>'success','data' => $rel['url']]);
            }else{
                return json(['code'=>204,'msg'=>'error']);
            }
        }
    }
}