<?php

/**
 * 控制器:后台系统投诉或意见
 * date:2019-08-31
 */

namespace app\modules\manage\v1\controllers;

use app\modules\common\Helper;
use app\modules\logic\model\OpinionModel;
use app\modules\logic\model\BillingModel;
use Yii;
use yii\web\Controller;

class OpinionController extends MiddleController {
	/**
	 * 返回
	 * @param $code
	 * @param string $info
	 * @param string $controller
	 * @param string $total
	 */
	protected function response($code, $info = '', $total = '', $controller = 'opinion') {
		return parent::response($code, $info, $total, $controller);
	}
    /**
     * [actionList 投诉或意见列表]
     * @apiDoc
     * @api     v1/opinion/list
     * @group   opinion
     * @name    投诉/意见列表
     * @desc    管理后台-投诉或意见列表-无查看详情和回复
     * @method  POST
     * @param currentPage string 当前页 noreq 1
     * @param pageSize string 每页条数 noreq 15
     * @author JOHN.W
     * @version [1.0]
     * @return  json
        {
        "code": "000000",
        "msg": "操作成功",
        "total": "1",
        "info": [
        {
            "id": "1",
            "content": "要改进哦",
            "user_id": "1",
            "created": "1970-01-01 08:00:00",
            "username": "18980647855"
        }
        ]
        }
     */
	public function actionList(){
        $_where = [];
		/*分页处理*/
        $currentPage = 1;
        $pageSize = 15;
        if (isset($this->para['currentPage']) && !empty($this->para['currentPage']) && is_numeric($this->para['currentPage'])) {
            $currentPage = $this->para['currentPage'];
        }
        if (isset($this->para['pageSize']) && !empty($this->para['pageSize']) && is_numeric($this->para['pageSize'])) {
            $pageSize = $this->para['pageSize'];
        }
        $Opinion = new OpinionModel();
        $total = $Opinion->getWidgetTotal($_where);
        $list = $Opinion->getWidgetPages($_where, $pageSize, $pageSize * ($currentPage - 1));
        if (!empty($list)) {
            foreach ($list as &$value) {
                $value['username'] =Helper::getUsernameById($value['user_id']);
                $value['created'] = date("Y-m-d H:i:s", $value['created']);
            }
        }
        return $this->response('search_succ', $list, $total);
	}
    /**
     * [actionBglist description]
     * @apiDoc
     * @api    v1/opinion/bglist
     * @group  opinion
     * @name   开票列表
     * @desc   管理后台-开票列表
     * @method POST
     * @param currentPage string 当前页 noreq 1
     * @param pageSize string 每页条数 noreq 15
     * @param status int 开票状态 noreq
     * @param addtime_from string 查询时间【开始】 noreq
     * @param addtime_to string 查询时间【结束】 noreq
     * @param user_id int 商户ID【搜】 noreq
     * @param name string 商户名称【搜】 noreq
     * @return json
     {
    "code": "000000",
    "msg": "操作成功",
    "total": "21",
    "info": [
        {
            "id": "21",//订单号
            "head": "",//开票公司名称
            "tax_number": "",//税号
            "bank": "",//开户行
            "bank_account": "",//银行账户
            "money": "0.01",//申请开票金额（元）
            "created": "2019-12-19 09:40:48",//申请开票日期
            "prov_id": "四川省",//省
            "city_id": "成都市",//市
            "area_id": "青羊区",//区/县
            "username": "13810001001",//申请账户
            "name": "测试门店1",//商户名称
            "status": "待开票",//状态
            "address": ","//门店地址
        },
        {
            "id": "20",
            "head": "",
            "tax_number": "",
            "bank": "",
            "bank_account": "",
            "money": "0.04",
            "created": "2019-12-18 11:40:00",
            "prov_id": "四川省",
            "city_id": "成都市",
            "area_id": "青羊区",
            "username": "13810001002",
            "name": "1",
            "address": "北京市,北京市,东城区,"
        },
        {
            "id": "19",
            "head": "",
            "tax_number": "",
            "bank": "",
            "bank_account": "",
            "money": "0.04",
            "created": "2019-12-18 11:39:20",
            "prov_id": "四川省",
            "city_id": "成都市",
            "area_id": "青羊区",
            "username": "13810001002",
            "name": "1",
            "address": "北京市,北京市,东城区,"
        }
    ]
}
     */
    public function actionBglist(){
         $_where = [];
        /*分页处理*/
        $currentPage = 1;
        $pageSize = 15;
        if (isset($this->para['currentPage']) && !empty($this->para['currentPage']) && is_numeric($this->para['currentPage'])) {
            $currentPage = $this->para['currentPage'];
        }
        if (isset($this->para['pageSize']) && !empty($this->para['pageSize']) && is_numeric($this->para['pageSize'])) {
            $pageSize = $this->para['pageSize'];
        }

        $_where['cols'] = ['id','user_id','head','tax_number','bank','bank_account','money','status','created'];
        $_where['order'] = ['id'=>'desc'];
        if (isset($this->para['status'])) {
           $_where['status'] = $this->para['status'];
        }
        /*查询时间搜索*/
        if (!empty($this->para['addtime_from'])) {
            $_where['scope']['ltt']['created'] = strtotime($this->para['addtime_from']);
        }
        if (!empty($this->para['addtime_to'])) {
            $_where['scope']['mtt']['created'] = strtotime($this->para['addtime_to']);
        }
        if (isset($this->para['user_id']) && !empty($this->para['user_id'])) {
           $_where['user_id'] = $this->para['user_id'];
        }
        if (isset($this->para['name']) && !empty($this->para['name'])) {
           //通过名字查询user_id
           $name_arr = $this->getShopUserId($this->para['name']);
           if(!empty($name_arr)){
                $_where['in']['user_id'] = $name_arr;
           }else{
              return $this->response('search_succ', [], 0);
           }
        }
        $Billing = new BillingModel();
        $total = $Billing->getWidgetTotal($_where);
        $list = $Billing->getWidgetPages($_where, $pageSize, $pageSize * ($currentPage - 1));
        if (!empty($list)) {
            foreach ($list as &$value) {
                //$value['username'] =Helper::getUsernameById($value['user_id']);
                $shopinfo = $this->getBshopinfo($value['user_id']);
                $value['prov_id'] = $shopinfo['prov_id'];
                $value['city_id'] = $shopinfo['city_id'];
                $value['area_id'] = $shopinfo['area_id'];
                $value['username'] = $shopinfo['username'];
                $value['name'] = $shopinfo['name'];
                $value['address'] = $shopinfo['address'];
                $value['status'] = ($value['status']==0)?'待开票':'已开票';
                $value['created'] = date("Y-m-d H:i:s", $value['created']);
                unset($value['user_id']);
            }
        }
        return $this->response('search_succ', $list, $total);
    }
    /**
     * [getBshopinfo 获取开票门店信息]
     * @name   接口名(所有菜品)
     * @desc   接口描述
     */
    private function getBshopinfo($user_id = 0){
        $conn = Yii::$app->db;
        $command = $conn->createCommand('select prov_id,city_id,area_id,username from {{%users}} where user_id=:id');
        $command->bindValue(':id', $user_id);
        $result = $command->queryOne();
        if($result){
            $command = $conn->createCommand('select name,address from {{%users_shop_info}} where user_id=:id');
            $command->bindValue(':id', $user_id);
            $result_info = $command->queryOne();
        }
        $ret = [
            'prov_id' =>isset($result['prov_id'])?Helper::getRegion($result['prov_id']):'-',
            'city_id' =>isset($result['city_id'])?Helper::getRegion($result['city_id']):'-',
            'area_id' =>isset($result['area_id'])?Helper::getRegion($result['area_id']):'-',
            'username' =>isset($result['username'])?$result['username']:'-',
            'name' =>isset($result_info['name'])?$result_info['name']:'-',
            'address' =>isset($result_info['address'])?$result_info['address']:'-',
        ];
        return $ret;
    }

    //获取门店名称
    private  function getShopUserId($name = ''){
        $conn = Yii::$app->db;
        $command = $conn->createCommand('select user_id from {{%users_shop_info}} where name=:name');
        $command->bindValue(':name', $name);
        $result = $command->queryAll();
        return !empty($result)?array_column($result, 'user_id'):[];
    }
	
}