<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-25 17:23
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;
use think\facade\View;
use app\BaseController;
use app\Request;
use library\services\UtilService;
use library\utils\UtilsTool;
use think\facade\Db;
use think\Image;

use app\model\api\AppBug;
use app\model\api\User as UserModel;
use app\model\api\ShowTemplate;
use app\model\api\UserShowTemplate;
use app\model\api\ServiceType;
use app\model\api\ServiceLabel;
use app\model\api\ServiceTimeType;
use app\model\api\UserWorkType;
use app\model\api\InfoAudit;
use app\model\api\TypeAudit;
use app\model\api\Sys as SysModel;

use app\model\api\Advert as AdvertModel;
use library\utils\WxpayV2 as wxpayApi;

class Pub extends BaseController
{
    private $user = null;
    public function checkUser($token=null){
        if(empty($token)){
            return false;
        }
        $memData = (new UserModel)
            ->where('token',$token)
            ->where('status',1)
            ->find();
        if(empty($memData)){
            return false;
        }
        $this->user = $memData->toArray();
        return true;
    }
    /**
     * 获取支付类型数据
     */
    public function getPayTypeData(){
        $data = [
            ["code"=>"wxpay","title"=>"微信支付","img"=> env('appinfo.app_api_domain', '')."/resource/icon/"."wxicon.png"],
            ["code"=>"balance","title"=>"余额支付","img"=>env('appinfo.app_api_domain', '')."/resource/icon/"."balance.png"],
        ];
        return app('json')->success($data);
    }
    
    /**
     * 获取分享信息
     * @return type
     */
    public function getShareInfo(){
        $data = (new SysModel)->getDataInfo("share");
        return app('json')->success($data);
    }
    
    /**
     * 获取模板详情
     * @param Request $request
     * @return type
     */
    public function getShowTemplateItem(Request $request){
        $post = UtilService::getMore([
            ['id', '','empty','参数错误'],
        ], $request);
        $data = (new ShowTemplate)
                ->field("id,title,price,old_price,status,imgs,is_hot,is_recommend,code,look_count,real_sales,unreal_sales")
                ->where("id",$post["id"])
                ->find();
        if(empty($data)){
            return app('json')->fail("模板不存在");
        }
        if($data["status"]!=1){
            return app('json')->fail("模板已下架");
        }
        $data = $data->toArray();
        $data["is_use"]      = 0;
        $data["imgs"]        = getImageAr($data["imgs"]);
        $data["img"]         = empty($data["imgs"]) ? "" : $data["imgs"][0];
        $data["sales_count"] = $data["real_sales"] + $data["unreal_sales"];
        unset($data["real_sales"]);
        unset($data["unreal_sales"]);
        //验证是否购买过并添加浏览记录
        $this->checkUser($request->header("token", ""));
        if(!empty($this->user)){
            $data["is_use"]=(new UserShowTemplate)->where("uid",$this->user["uid"])->where("show_template_id",$data["id"])->count()>0?1:0;
            (new ShowTemplate)->where('id', $data["id"])->inc('look_count', 1)->update();
        }
        return app('json')->success($data);
    }
    
    /**
     * 获取皮肤模板列表
     * @param Request $request
     */
    public function getShowTemplateList(Request $request){
        $post = UtilService::getMore([
            ['is_hot', '0'],
            ['is_recommend', '0'],
            ['page', 1],
            ['pageSize', 50],
        ], $request);
        $where["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
        $where["page"]     = $post["page"]<=0     ? 1  : (int)$post["page"];
        $where["status"]   = 1;
        if((int)$post["is_hot"]==1){
            $where["is_hot"]   = 1;
        }
        if((int)$post["is_recommend"]==1){
            $where["is_recommend"]   = 1;
        }
        $data = (new ShowTemplate)->getDataList($where, "id,title,price,old_price,status,imgs,is_hot,is_recommend,code,look_count,real_sales,unreal_sales");
        return app('json')->success($data);
    }
    /**
     * 获取轮播图
     * @param Request $request
     */
    public function getBannerList(Request $request){
        $post = UtilService::getMore([
            ['type', ''],
            ['page', 1],
            ['pageSize', 50],
        ], $request);
        $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
        $post["page"]     = $post["page"]<=0 ? 1 : (int)$post["page"];
        $where=[];
        $where[]=["is_show","=",1];
        $where[]=["page_id","=",70];
        $totalCount = (new AdvertModel)->where($where)->count();
        $data=null;
        if($totalCount>0){
            $data = (new AdvertModel)
                ->field("id,img,title,url")
                ->where($where)
                ->order("sort","desc")
                ->page($post["page"], $post["pageSize"])
                ->select();
        }
        $data = empty($data)?[]:$data;
        return app('json')->success(["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount]);
    }
    
    /**
     * 获取服务类型列表
     * @param Request $request
     * @return type
     */
    public function getServiceTypeList(Request $request){
        $post = UtilService::getMore([
            ['page', 1],
            ['pageSize', 50],
        ], $request);
        $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
        $post["page"]     = $post["page"]<=0 ? 1 : (int)$post["page"];
        $post["status"]   = 1;
        $data = (new ServiceType)->getList($post,"id,title,content,img");
        return app('json')->success($data);
    }
    /**
     * 获取标签列表
     * @param Request $request
     * @return type
     */
    public function getServiceLabelList(Request $request){
        $post = UtilService::getMore([
            ['page', 1],
            ['pageSize', 50],
        ], $request);
        $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
        $post["page"]     = $post["page"]<=0 ? 1 : (int)$post["page"];
        $post["status"]   = 1;
        $data = (new ServiceLabel)->getList($post,"id,title,type");
        return app('json')->success($data);
    }
    
    /**
     * 获取用户工作服务类型
     * @param Request $request
     */
    public function getUserWorkTypeList(Request $request){
        $post = UtilService::getMore([
            ['page', 1],
            ['pageSize', 50],
        ], $request);
        $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
        $post["page"]     = $post["page"]<=0     ? 1  : (int)$post["page"];
        $post["status"]   = 1;
        $data = (new UserWorkType)->getList($post,"id,title,content,img");
        return app('json')->success($data);
    }
    
    
    /**
     * 获取服务时长类型
     * @param Request $request
     * @return type
     */
    public function getServiceTimeTypeList(Request $request){
        $post = UtilService::getMore([
            ['page', 1],
            ['pageSize', 50],
        ], $request);
        $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
        $post["page"]     = $post["page"]<=0     ? 1  : (int)$post["page"];
        $post["status"]   = 1;
        $data = (new ServiceTimeType)->getList($post,"title,code");
        return app('json')->success($data);
    }
    
    
    
    
    
    
     
    /**
     * 获取系统信息
     * @param Request $request
     */
    public function getSysData(Request $request){
        $data = (new Sys)->where("id",1)->find()->toArray();
        $data=[];
        return app("json")->success($data);
    }
    /**
     * 首页轮播
     * @param Request $request
     */
    public function getHomeBanner(Request $request){
        $data = (new AdvertModel)
                ->field("id,img,title,url")
                ->where("page_id",70)
                ->where("is_show",1)
                ->order("sort","desc")
                ->select()
                ->toArray();
        $data = empty($data)?[]:$data;
        return app("json")->success($data);
    }
    
    /**
     * 获取其他用户名片信息
     * @param Request $request
     */
    public function getCommonUserCardInfo(Request $request){
        [$uid] = UtilService::getMore([
            ['uid', '','empty',"用户信息为空"],
        ], $request,true);
        $userData = (new UserModel)->where("uid",$uid)->find();
        if(empty($userData)){
            return app('json')->fail("用户不存在");
        }
        $userData->toArray();
        $data = (new InfoAudit)->getItem(["uid"=>$uid,"status"=>1]);
        if(empty($data)){
            return app('json')->fail("当前用户还未通过审核");
        }
        $typeData = (new TypeAudit)->where("uid",$uid)->order("id","desc")->find();
        $data["is_type_audit"] = (empty($typeData) || $typeData["status"]!=1)?0:1;
        //名片浏览次数
        (new UserModel)->where('uid', $uid)->inc('card_look_count', 1)->update();
        $data["card_look_count"] = $userData["card_look_count"]+1;
        return app('json')->success($data);
    }

   

    
    /**
     * bug提交
     * @param Request $request
     */
    public function appBugSub(Request $request){
        [$error] = UtilService::getMore([
            ['error', '','empty',"错误信息为空"],
        ], $request,true);
        $deviceId   = $request->header("deviceId", "");
        $deviceType = $request->header("deviceType", "");
        $fromPlat   = $request->header("fromPlat", "");
        $version    = $request->header("version", "");
        (new AppBug)->insert([
            "device_id"   => $deviceId,
            "device_type" => $deviceType,
            "from_plat"   => $fromPlat,
            "version"     => $version,
            "error"       => $error,
            "time"        => time()
        ]);
        return app("json")->success("提交成功");
    }
    
    /**
     * 测试
     */
    public function appTest(Request $request){
        
        
        $labelData = (new ServiceLabel)->where("status",1)->where("id","in",["",1,2,6,7,8,0])->column('id');
        var_dump($labelData);exit;
        
        $money = $request->post("money",0);
        $money = bcadd("0", $money."",2);
        echo $money;exit;
        
        
        $xml = "<xml><appid><![CDATA[wx57a473fc2f83f7e5]]></appid>
<attach><![CDATA[微信小程序支付]]></attach>
<bank_type><![CDATA[OTHERS]]></bank_type>
<cash_fee><![CDATA[1]]></cash_fee>
<fee_type><![CDATA[CNY]]></fee_type>
<is_subscribe><![CDATA[N]]></is_subscribe>
<mch_id><![CDATA[1640972417]]></mch_id>
<nonce_str><![CDATA[TTEMpfip075pcFNNhS5P60tDtis91veA]]></nonce_str>
<openid><![CDATA[oOqrp4tndyR_iDwrGC5P7Y4moP2Y]]></openid>
<out_trade_no><![CDATA[A202304201681959554328879382]]></out_trade_no>
<result_code><![CDATA[SUCCESS]]></result_code>
<return_code><![CDATA[SUCCESS]]></return_code>
<sign><![CDATA[146B5ADFF5ADC399A92C60D62C54638A]]></sign>
<time_end><![CDATA[20230420105928]]></time_end>
<total_fee>1</total_fee>
<trade_type><![CDATA[JSAPI]]></trade_type>
<transaction_id><![CDATA[4200001803202304202712861307]]></transaction_id>
</xml>";
        
        $wxpay = new wxpayApi();
//        $r = $wxpay->notifyCheckSign($xml);
        $r = $wxpay->searchOrderQuery("A202304201681959554328879382");
        var_dump($r);
        
        
    }
    
    
    
}