<?php
namespace app\ais\controller\api\v1;
use app\ais\controller\api\Base;
use app\ais\model\AisConfig;
use app\ais\model\AisCouponUser;
use app\ais\model\AisStore;
use app\ais\model\AisStoreBill;
use app\ais\model\AisStoreGroup;
use app\ais\model\AisStoreUnion;
use filter\Filter;
use think\Db;

class Group extends Base{

    
    //商圈详情
    public function read(){
        $param['id'] = $this->request->param('id');    //商圈id
        $this->apiSign($param);
        $info = AisStoreGroup::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id']])->find();
        if (!$info) {
            return enjson(204);
        }
        $info->num = AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id'=> $info->id,'type' => 0])->count();
        //买单
        $order = AisStoreBill::where(['member_miniapp_id' => $this->miniapp_id])->whereIn('store_id',AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id' => $param['id'],'type' => 0])->column('store_id'))->sum('money');
        //次数
        $count = AisCouponUser::where(['member_miniapp_id' => $this->miniapp_id])->whereIn('store_id',AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id' => $param['id'],'type' => 0])->column('store_id'))->count();
        //互助
        $help = AisCouponUser::where(['member_miniapp_id' => $this->miniapp_id,'is_end' => 1])->where('','EXP',Db::raw("FIND_IN_SET(".$param['id'].",group_ids)"))->count();
        //商家列表
        $list = AisStore::where(['member_miniapp_id' => $this->miniapp_id])->whereIn('id',AisStoreUnion::where(['member_miniapp_id' => $this->miniapp_id,'group_id' => $param['id'],'type' => 0])->column('store_id'))->select();
        return enjson(200,['group' => $info,'order' => $order,'count' => $count,'help' => $help,'store' => $list]);
    }
}