<?php

namespace app\common\repositories\store\product;

use app\common\repositories\BaseRepository;
use app\common\dao\store\product\ProductGroupUserDao;

class ProductGroupUserRepository extends BaseRepository
{
    protected $dao;

    /**
     * ProductGroupRepository constructor.
     * @param ProductGroupDao $dao
     */
    public function __construct(ProductGroupUserDao $dao)
    {
        $this->dao = $dao;
    }

    public function create($userInfo,$data)
    {
        $_where = [
            'product_group_id' => $data['product_group_id'],
            'group_buying_id' => $data['group_buying_id'],
            'uid' => $userInfo->uid,
        ];
        $user = $this->getWhere($_where);
        if($user){
            throw new ValidateException('您已经参加过此团');
        }else{
            $data = [
                'product_group_id' => $data['product_group_id'],
                'group_buying_id' => $data['group_buying_id'],
                'is_initiator' => $data['is_initiator'],
                'order_id' => $data['order_id'],
                'uid' => $userInfo->uid,
                'nickname'=> $userInfo->nickname,
                'avatar' => $userInfo->avatar,
            ];
            makeLock('group_buying')->lock();
            try{
                $user = $this->dao->create($data);
            }catch (\Exception $exception){
                makeLock('group_buying')->unlock();
            }
            makeLock('group_buying')->unlock();

        }
        return  $user;
    }

    /**
     * TODO 团员列表
     * @param $id
     * @return array
     * @author Qinii
     * @day 1/12/21
     */
    public function getAdminList($where,$page,$limit)
    {
        $query = $this->dao->getSearch($where)->where('uid','<>',0)->where('is_del',0)->with([
            'orderInfo' => function($query){
                $query->field('order_id,order_sn,pay_price,status');
            },
        ])->order('create_time DESC');
        $count = $query->count();
        $list = $query->page($page,$limit)->select();
        return compact('count','list');
    }

    /**
     * TODO 团员列表
     * @param $where
     * @param $page
     * @param $limit
     * @return array
     * @author Qinii
     * @day 1/12/21
     */
    public function getApiList($where,$page,$limit)
    {
        $query = $this->dao->getSearch($where)->where('uid','<>',0)->where('is_del',0)->order('create_time DESC');
        $count = $query->count();
        $list = $query->page($page,$limit)->hidden(['uid','order_id','is_del'])->select();
        return compact('count','list');
    }


    /**
     * TODO 转移团长
     * @param $groupId
     * @return bool
     * @author Qinii
     * @day 1/13/21
     */
    public function changeInitator(int $groupId,$uid)
    {
        $user = $this->dao->getSearch(['group_buying_id' => $groupId])
            ->where('uid','<>',0)
            ->where('is_del',0)
            ->where('uid','<>',$uid)
            ->order('create_time ASC')->find();
        if($user) {
            $user->is_initiator = 1;
            $user->save();
        }
    }

}
