<?php

namespace app\admin\controller\index;
use app\admin\controller\BaseController;
use app\admin\server\index\CardServer;
use support\Request;
use support\Db;
use app\admin\model\User;
use app\admin\server\sms\VerificationCodeServer;
use app\admin\server\payment\UnionOrder;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

/**
 * 储值卡接口
 * Class Users
 * @package app\api\controller\user
 */
class Card extends BaseController
{

    /**
     * Notes:获取会员账户
     * @return \support\Response
     * User: yym
     * Date: 2022/7/26
     */
    public function getCardList()
    {
        $mobile = $this->request->get('mobile');
        $core_member_id = "";
        /*
        $core_member_id = DB::table('user')->where('user_account',$mobile)->value('core_member_id');
        if(empty($core_member_id)){
            throw new \Exception('会员未绑定核心库');
        }
        */
        $result = CardServer::getCardList($core_member_id,$mobile);

        return $result;
    }

    /**
     * Notes:会员绑定卡
     * @return \support\Response
     * User: ycp
     * Date: 2023/5/4
     */
    public function userBindCard()
    {

        $mobile = $this->request->post('mobile');
        $core_member_id = DB::table('user')->where('user_account',$mobile)->value('core_member_id');
        if(empty($core_member_id)){
            throw new \Exception('会员未绑定核心库');
        }
        $cardno = $this->request->post('cardno');
        $cardpswd = $this->request->post('cardpswd');
        $cardmonies = $this->request->post('cardmonies',0); 
        $cardadded  = $this->request->post('cardadded',0);

        $card_oct     = substr($cardno, 15, 3);
        /* 八进制转十进制 */
        $card_dec = octdec((int)$card_oct);
        /* verifycode */
        $verifycode = md5($cardno . $cardpswd . $card_dec . ".YXJ");


        $result4 = CardServer::memberBindCard($cardno,$verifycode,$cardpswd,$cardmonies,$cardadded,$core_member_id);
        $result = json_decode($result4,true);
            
        if($result['errorcode'] == 313)
        {
            throw new \Exception("不能绑定同类型的卡");
        }
        if($result['errorcode'] == 312)
        {
            throw new \Exception("卡不存在或已被使用");
        }

        return json_success($result, '绑定成功');
    }

    /**
     * Notes:会员账户出入帐
     * @return \support\Response
     * User: yym
     * Date: 2022/7/26
     */
    public function memberAccountPay()
    {
        $mobile = $this->request->post('mobile');
        $core_member_id = DB::table('user')->where('user_account',$mobile)->value('core_member_id');
        if(empty($core_member_id)){
            throw new \Exception('会员未绑定核心库');
        }
        $cardno = $this->request->post('cardno');
        $orderno = $this->request->post('orderno');
        $io = $this->request->post('io'); 
        $expenses = $this->request->post('expenses');
	$added = $this->request->post('added',0);  
        $detail = $this->request->post('detail','');

        $result = CardServer::memberAccountPay($core_member_id,$cardno,$orderno,$io,$expenses,$added,$detail);

        return $result;
    }

    /**
     * Notes:卡品种列表
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardMainCategoryList()
    {
        $result = CardServer::cardMainCategoryList();

        return $result;
    }

    /**
     * Notes:添加卡品种
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardMainCategoryAdd()
    {
        $card_main_category_name = $this->request->get('card_main_category_name');
        $result = CardServer::cardMainCategoryAdd($card_main_category_name);

        return $result;
    }

    /**
     * Notes:删除卡品种
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardMainCategoryDel()
    {
        $card_main_category_name = $this->request->get('card_main_category_name');
        $result = CardServer::cardMainCategoryDel($card_main_category_name);

        return $result;
    }

    /**
     * Notes:编辑卡品种
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardMainCategoryUpd()
    {
        $card_main_category_name = $this->request->get('card_main_category_name');
        $card_main_category_configs = $this->request->get('card_main_category_configs','&');
        $card_main_category_addtimes = $this->request->get('card_main_category_addtimes',1);
        $result = CardServer::cardMainCategoryUpd($card_main_category_name,$card_main_category_configs,$card_main_category_addtimes);
        //var_dump(http_build_query($result));
        return $result;
    }

    /**
     * Notes:卡品种列表
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardMainList()
    {
        $card_main_category = $this->request->get('card_main_category');
        $result = CardServer::cardMainList($card_main_category);

        return $result;
    }

    /**
     * Notes:储值卡-卡分类-创建[C]
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardMainAdd()
    {
        $card_main_status = $this->request->get('card_main_category_configs','ACTIVED');
        $card_main_name = $this->request->get('card_main_name');
        $card_main_category = $this->request->get('card_main_category');
        $card_main_validday = $this->request->get('card_main_validday',0);
        $card_main_configs = $this->request->get('card_main_configs','&');
        $result = CardServer::cardMainAdd($card_main_status,$card_main_name,$card_main_category,$card_main_validday,$card_main_configs);

        return $result;
    }

    /**
     * Notes:编辑卡分类
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardMainUpd()
    {
        $card_main_id = $this->request->get('card_main_id');
        $card_main_status = $this->request->get('card_main_status','ACTIVED');
        $card_main_name = $this->request->get('card_main_name');
        $card_main_category = $this->request->get('card_main_category');
        $card_main_validday = $this->request->get('card_main_validday',1);
        $card_main_configs = $this->request->get('card_main_configs',1);
        $result = CardServer::cardMainUpd($card_main_id,$card_main_status,$card_main_name,$card_main_category,$card_main_validday,$card_main_configs);

        return $result;
    }

    /**
     * Notes:删除卡品种
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardMainDel()
    {
        $card_main_id = $this->request->get('card_main_id');
        $result = CardServer::cardMainDel($card_main_id);

        return $result;
    }

    /**
     * Notes:储值卡-卡台账-创建[批量]
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardListAddBatch()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";
        $params['func'] = "functions.funcCardListAddBatch";
        $params['card_city'] = $this->request->get('card_city','370100');
        $params['card_main_id'] = $this->request->get('card_main_id');
        $params['card_status'] = $this->request->get('card_status','ACTIVED');
        $params['card_batchcode'] = $this->request->get('card_batchcode');
        $params['card_def_monies'] = $this->request->get('card_def_monies',0);
        $params['card_num'] = $this->request->get('card_num');

        $result = curlPost($url,$params);
        return $result;

    }

    /**
     * Notes:储值卡-卡台账-创建
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardListAdd()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";
        $params['func'] = "functions.funcCardListAdd";
        $params['card_no'] = $this->request->get('card_no');
        $params['card_no_chip'] = $this->request->get('card_no_chip');
        $params['join_card_main_id'] = $this->request->get('join_card_main_id');
        $params['card_status'] = $this->request->get('card_status','INIT');
        $params['card_pswd'] = $this->request->get('card_pswd','123456');
        $params['card_def_monies'] = $this->request->get('card_def_monies',0);
        $params['card_qrcode'] = $this->request->get('card_qrcode');
        $params['card_batchcode'] = $this->request->get('card_batchcode');
        $params['card_extend'] = $this->request->get('card_extend');
        $params['card_addtimes'] = time();

        $result = curlPost($url,$params);
        return $result;

    }

    /**
     * Notes:储值卡-卡台账-编辑
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardListUpd()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";
        
        $data['card_no'] = $this->request->get('card_no');
        $data['card_no_chip'] = $this->request->get('card_no_chip');
        $data['join_card_main_id'] = $this->request->get('join_card_main_id');
        $data['card_status'] = $this->request->get('card_status','INIT');
        $data['card_pswd'] = $this->request->get('card_pswd','123456');
        $data['card_def_monies'] = $this->request->get('card_def_monies',0);
        $data['card_qrcode'] = $this->request->get('card_qrcode');
        $data['card_batchcode'] = $this->request->get('card_batchcode');
        $data['card_extend'] = $this->request->get('card_extend');
        $data['card_addtimes'] = time();

        $params['func'] = "functions.funcCardListUpd";
        $params['where'] = $this->request->get('where');
        $params['card_no'] = $this->request->get('card_no');
        $params['data'] = $data;

        $result = curlPost($url,json_encode($params));
        return $result;

    }

    /**
     * Notes:储值卡-卡台账-创建
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardListDel()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";
        $params['func'] = "functions.funcCardListDel";
        $params['card_no'] = $this->request->get('card_no');
        $params['where'] = $this->request->get('where');

        $result = curlPost($url,$params);
        return $result;

    }

    /**
     * Notes:储值卡-卡台账-读取[R]
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardListList()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";
        $params['func'] = "functions.funcCardListList";
        
        if($this->request->get('card_no') != ''){
            $params['card_no'] = $this->request->get('card_no');
        }
        
        if($this->request->get('join_card_main_id') != ''){
            $params['join_card_main_id'] = $this->request->get('join_card_main_id');
        }
        
        if($this->request->get('card_main_category') != ''){
            $params['card_main_category'] = $this->request->get('card_main_category');
        }
        

        if($this->request->get('pageno')){
            $params['pageno'] = $this->request->get('pageno');
        }else{
            $params['pageno'] = 1;
        }

        
        $result = curlPost($url,$params);
        return $result;

    }

    /**
     * Notes:储值卡-卡动作-创建[U]
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardActionAdd()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";
        $params['func'] = "functions.funcCardActionAdd";
        $params['card_action_status'] = $this->request->get('card_action_status');
        $params['card_action_category'] = $this->request->get('card_action_category');
        $params['card_action_name'] = $this->request->get('card_action_name');
        $params['card_action_code'] = $this->request->get('card_action_code');
        $params['join_action_card_main_id'] = $this->request->get('join_action_card_main_id');
        $params['card_action_user_name'] = $this->request->get('card_action_user_name');
        $params['card_action_apply_user_name'] = $this->request->get('card_action_apply_user_name');
        $params['card_action_apply_orgi_name'] = $this->request->get('card_action_apply_orgi_name');
        $params['card_action_apply_count'] = $this->request->get('card_action_apply_count');
        $params['card_action_apply_content'] = $this->request->get('card_action_apply_content');
        $params['card_action_apply_extend'] = $this->request->get('card_action_apply_extend');
        $params['cardcard_action_result_no'] = $this->request->get('card_action_result');
        $params['card_action_result_count'] = $this->request->get('card_action_result_count');

        $result = curlPost($url,$params);
        return $result;

    }

    /**
     * Notes:储值卡-卡动作-创建[U]
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardActionUpd()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";   
        $data['card_action_status'] =  $this->request->get('card_action_status');
        $data['card_action_category'] = $this->request->get('card_action_category');
        $data['card_action_name'] = $this->request->get('card_action_name');
        $data['card_action_code'] = $this->request->get('card_action_code');
        $data['join_action_card_main_id'] = $this->request->get('join_action_card_main_id');
        $data['card_action_user_name'] = $this->request->get('card_action_user_name');
        $data['card_action_apply_user_name'] = $this->request->get('card_action_apply_user_name');
        $data['card_action_apply_orgi_name'] = $this->request->get('card_action_apply_orgi_name');
        $data['card_action_apply_count'] = $this->request->get('card_action_apply_count');
        $data['card_action_apply_content'] = $this->request->get('card_action_apply_content');
        $data['card_action_apply_extend'] = $this->request->get('card_action_apply_extend');
        $data['cardcard_action_result_no'] = $this->request->get('card_action_result');
        $data['card_action_result_count'] = $this->request->get('card_action_result_count');

        $params['func'] = "functions.funcCardActionUpd";
        $params['card_action_id'] = $this->request->get('card_action_id');
        $params['data'] = $data;

        $result = curlPost($url,json_encode($params));
        return $result;

    }

    /**
     * Notes:储值卡-卡动作-读取[R]
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardActionList()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";   
        $params['func'] = "functions.funcCardActionList";
        $params['card_action_id'] = $this->request->get('card_action_id');

        $result = curlPost($url,$params);
        return $result;

    }

    /**
     * Notes:储值卡-卡动作-删除[D]
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardActionDel()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";   
        $params['func'] = "functions.funcCardActionDel";
        $params['card_action_id'] = $this->request->get('card_action_id');

        $result = curlPost($url,$params);
        return $result;

    }

    /**
     * Notes:储值卡-卡动作-执行[X]
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardActionExecute()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";   
        $params['func'] = "functions.funcCardActionExecute";
        $params['card_action_id'] = $this->request->get('card_action_id');
        $params['card_action_card_no'] = $this->request->get('card_action_card_no','');
        $params['card_action_api'] = $this->request->get('card_action_api','{"type":"", "process":""}');

        $result = curlPost($url,$params);
        return $result;

    }

    /**
     * Notes:储值卡-卡动作明细-创建[C]
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardActionDetailAdd()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";   
        $params['func'] = "functions.funcCardActionDetailAdd";
        $params['join_card_action_id'] = $this->request->get('join_card_action_id');
        $params['card_action_card_no'] = $this->request->get('card_action_card_no');

        $result = curlPost($url,$params);
        return $result;

    }

    /**
     * Notes:储值卡-卡动作明细-编辑[C]
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardActionDetailUpd()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";   
        $data['join_card_action_id'] = $this->request->get('join_card_action_id');
        $data['card_action_card_no'] = $this->request->get('card_action_card_no');

        $params['func'] = "functions.funcCardActionDetailUpd";
        $params['card_action_detail_id'] = $this->request->get('card_action_detail_id');
        $params['data'] = $data;

        $result = curlPost($url,json_encode($params));
        return $result;

    }

    /**
     * Notes:储值卡-卡动作明细-读取[C]
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardActionDetailList()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";   
        $params['func'] = "functions.funcCardActionDetailList";
        $params['join_card_action_id'] = $this->request->get('join_card_action_id');

        $result = curlPost($url,$params);
        return $result;

    }

    /**
     * Notes:储值卡-卡动作明细-删除[C]
     * @return \support\Response
     * User: ycp
     * Date: 2023/8/7
     */
    public function cardActionDetailDel()
    {
        
        $url = "http://core.wanyuewellness.cn/api.php";   
        $params['func'] = "functions.funcCardActionDetailDel";
        $params['card_action_detail_id'] = $this->request->get('card_action_detail_id');

        $result = curlPost($url,$params);
        return $result;

    }

    /**
     * Notes:批量导入储值卡芯片码
     * @return \support\Response
     * @throws \Exception
     * User: YCP
     * Date: 2023/8/11
     */
    public function cardImport()
    {
        $excel_address = $this->request->get('excel_address');//文件地址;
        $admin_id = $this->request->admin_id;
        require_once 'vendor/phpoffice/phpexcel/Classes/PHPExcel.php';
        require_once 'vendor/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php';
        $objPHPExcel = \PHPExcel_IOFactory::load($excel_address);
        // 逐行读取 sheet 内的内容（常用）
        foreach ($objPHPExcel->getworksheetiterator() as $sheet){   //循环sheet
            foreach ($sheet->getrowiterator() as $row){     //循环row
                if ($row->getrowindex() < 2){  // 默认从第二行开始
                    continue;
                }
                foreach ($row->getcelliterator() as $cell){     //循环cell
                    $data[] = $cell->getvalue();
                }
                if(!empty($data)){

                    $params['card_no_chip'] = $data[0];
                    $result = DB::connection('mysql2')->table('tbl__core_card_list')->where(['card_no' => $data[1]])->update($params);
                    if (!empty($result)){
                        $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '导入储值卡芯片码-编号: ' . $result;
                        plog('user-card-update', '储值卡-导入芯片码', $msg);
                    }
      
                    unset($data);
                }else{
                    throw new \Exception('读取失败');
                }
            }
        }

        return json_success($excel_address, '导入成功');
    }

    

}
