<?php

namespace app\controller\manager;

use app\model\Account;
use app\model\Coupon;
use app\repository\AccountRepository;
use Exception;

use think\response\Json;
use think\response\View;

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

    /**
     * 列表
     *
     * @return Json|View
     * @throws Exception
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $repo = AccountRepository::getInstance();
            $keyword = $this->request->param('keyword/s', '');
            $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%"];
            }
            $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");

                //优惠红包总金额
                $item->consumer_coupon_bill_total = AccountRepository::getInstance()->consumerCouponBillTotal($item->user_code);

            });

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

        }
        return $this->view();
    }

    /**
     * 消费者详情
     *
     * @return Json|View
     * @throws Exception
     */
    public function info()
    {
        $id = input("id/d", 0);
        $sign = input("sign/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);
            $where = [["consumer_code", "=", $consumer['user_code']]];
            if ($sign) {
                $where[] = ["is_verificated", "=", Coupon::is_verificated_on];
            }
            $list = $repo->consumerCouponList($where, $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();
        $this->data["sign"] = $sign;

        return $this->view();
    }

    /**
     * 黑名单列表
     *
     * @return Json|View
     * @throws Exception
     */
    public function blankList()
    {

        if ($this->request->isPost()) {
            $repo = AccountRepository::getInstance();
            $keyword = $this->request->param('keyword/s', '');

            $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%"];
            }

            $list = $repo->findList($whereMap, [], $page, $size, null, $orders);
            $time = time();
            $list["list"]->each(function ($item) use ($time) {

                //禁言总时长
                $item->blank_total_format = formatBlankTime(date("Y-m-d H:i:s",$time + ($item->blank_total * 60)),    date("Y-m-d H:i:s",$time ),);

//                echo  date("Y-m-d H:i:s",$time + $item->blank_total * 60);
//                echo  date("Y-m-d H:i:s",$time + $item->blank_total * 60);
//                echo  $item->blank_time;
                //剩余禁言总时长
                if(!empty($item->blank_time) && strtotime($item->blank_time)>$time){
                    $item->surplus_blank_total_format = formatBlankTime(
                        date("Y-m-d H:i:s",$time ),
                        $item->blank_time);
                }else{
                    $item->surplus_blank_total_format = '';
                }

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

        }
        return $this->view();
    }

}