<?php

namespace app\controller\manager;

use app\exception\RepositoryException;
use app\model\Account;
use app\model\BusinessFlow;
use app\model\Coupon;
use app\model\CouponMain;
use app\model\Recharge;
use app\model\Business as BusinessModel;
use app\model\Member;
use app\model\Tag;
use app\repository\AccountRepository;
use app\repository\BusinessRepository;
use app\repository\CouponRepository;
use app\repository\RechargeRepository;
use app\service\wx\WechatPay;
use Exception;

use think\facade\Db;
use think\response\Json;
use think\response\View;

/**
 * 消费者
 **/
class Consumer extends Base
{

    /**
     * 列表
     *
     * @return Json|View
     * @throws Exception
     */
    public function index()
    {
        $stateArray = Account::allState();
        if ($this->request->isPost()) {
            $repo = AccountRepository::getInstance();
            $keyword = $this->request->param('keyword/s', '');
            $state = $this->request->param('state/d', "-1");
            $page = $this->request->param('page/d', 1);
            $size = $this->request->param('size/d', 30);

            $whereMap = [["type", "=", Account::type_consumer]];
            $orders = ['id' => 'desc'];
            if (!empty($keyword)) {
                $whereMap[] = ['nick_name', 'like', "%$keyword%"];
            }
            if (isset($stateArray[$state])) {
                $whereMap[] = ['state', '=', $state];
            }
            $list = $repo->findList($whereMap, [], $page, $size, function ($q) {
                return $q->with("tag");
            }, $orders);
            $list["list"]->each(function ($item) {
                //优惠券领取总数
                $item->coupon_total_count = Coupon::where(["consumer_code" => $item->user_code])->count("id");
                //优惠券使用总数
                $item->coupon_used_count = Coupon::where(["consumer_code" => $item->user_code])->where("is_verificated", Coupon::is_verificated_on)->count("id");
                //优惠券未使用总数
                $item->coupon_not_use_count = Coupon::where(["consumer_code" => $item->user_code])->where("is_verificated", Coupon::is_verificated_off)->count("id");
            });

            return $this->json(0, 'success', $list);

        }
        $this->data["state"] = $stateArray;
        return $this->view();
    }

    /**
     * 消费者详情
     *
     * @return Json|View
     * @throws Exception
     */
    public function info()
    {
        $id = input("id/d", 0);
        $consumer = Account::findOne(["id" => $id], [], function ($q) {
            return $q->with("tag");
        });
        //只查询优惠券持有
        if ($this->request->isPost()) {
            $repo = AccountRepository::getInstance();
            $page = $this->request->param('page/d', 1);
            $size = $this->request->param('size/d', 30);

            $list = $repo->consumerCouponList($consumer['user_code'], $page, $size);
            $time = time();
            $list["list"]->each(function ($item) use ($time) {
                if(strtotime($item['end_time']) < $time){
                    $item->time_state = '已过期';
                }else{
                    $item->time_state = '未过期';
                }

            });
            return $this->json(0, 'success', $list);
        }
        if (empty($consumer)) {
            return $this->json(4001, "消费者不存在");
        }
        $rep = AccountRepository::getInstance();
        //评论总数数
        $this->data["totalComment"] = $rep->consumerTotalComment($consumer["user_code"]);
        //评论总数数
        $this->data["totalTheMonthComment"] = $rep->consumerTheMonthTotalComment($consumer["user_code"]);

        //优惠券领取总数
        $this->data["couponTotalCount"] = $rep->consumerTotalCoupon($consumer["user_code"]);

        //优惠券使用总数
        $this->data["couponUsedTotalCount"] = $rep->consumerUsedTotalCoupon($consumer["user_code"]);
        //优惠券未使用总数
        $this->data["couponNotUsedTotalCount"] = $rep->consumerNotUsedTotalCoupon($consumer["user_code"]);

        $this->data["consumer"] = $consumer->toArray();

        return $this->view();
    }

    /**
     * 黑名单列表
     *
     * @return Json|View
     * @throws Exception
     */
    public function blankList()
    {
        $stateArray = Account::allState();
        if ($this->request->isPost()) {
            $repo = AccountRepository::getInstance();
            $keyword = $this->request->param('keyword/s', '');
            $state = $this->request->param('state/d', "-1");
            $page = $this->request->param('page/d', 1);
            $size = $this->request->param('size/d', 30);

            $whereMap = [
                ["type", "=", Account::type_consumer],
                ["blank_total", ">", 0],
            ];
            $orders = ['id' => 'desc'];
            if (!empty($keyword)) {
                $whereMap[] = ['nick_name', 'like', "%$keyword%"];
            }
            if (isset($stateArray[$state])) {
                $whereMap[] = ['state', '=', $state];
            }
            $list = $repo->findList($whereMap, [], $page, $size, function ($q) {
                return $q->with("tag");
            }, $orders);
            $list["list"]->each(function ($item) {
                //优惠券领取总数
                $item->coupon_total_count = Coupon::where(["consumer_code" => $item->user_code])->count("id");
                //优惠券使用总数
                $item->coupon_used_count = Coupon::where(["consumer_code" => $item->user_code])->where("is_verificated", Coupon::is_verificated_on)->count("id");
                //优惠券未使用总数
                $item->coupon_not_use_count = Coupon::where(["consumer_code" => $item->user_code])->where("is_verificated", Coupon::is_verificated_off)->count("id");
            });

            return $this->json(0, 'success', $list);

        }
        $this->data["state"] = $stateArray;
        return $this->view();
    }

}