自学内容网 自学内容网

微信支付开发-后台统计工厂实现

一、数据库设计图

二、后端统计工厂逻辑

        1、统计父抽象类

                a、StatisticsHandle.php

        2、统计工厂通道类

                a、StatisticsFactory.php

        3、查询实现类

                a、答题统计(Answer.php)

三、后端统计工厂代码实现

        1、统计父抽象类(StatisticsHandle.php)

<?php
/**
 * 统计父抽象类
 * User: 龙哥·三年风水
 * Date: 2024/9/11
 * Time: 10:07
 */
namespace Statistics;
abstract class StatisticsHandle
{
    /**
     * 获取统计列表
     * User: 龙哥·三年风水
     * Date: 2024/9/11
     * Time: 10:31
     * @ param $param 参数
     * @ return mixed
     */

    protected abstract function getList($param);

    /**
     * 获取统计详情
     * User: 龙哥·三年风水
     * Date: 2024/9/11
     * Time: 10:31
     * @ param $id 编号
     * @ return mixed
     */

    protected abstract function getInfo($id);

    /**
     * 导出统计信息
     * User: 龙哥·三年风水
     * Date: 2024/9/11
     * Time: 10:32
     * @ return mixed
     */
    protected abstract function exportInfo($param);
}

        2、统计工厂通道类(StatisticsFactory.php)

<?php
/**
 * 统计工厂选择类
 * User: 龙哥·三年风水
 * Date: 2024/9/11
 * Time: 10:12
 */
namespace Statistics;
use app\BaseError;
use Statistics\statistics\Answer;

class StatisticsFactory
{
    protected static $instance = null;//缓存实例
    protected $channelType = 0;//通道类型

    /**
     * 初始化参数
     * @ param $types 类型选择
     */

    public function __construct($channelType){
        $this->channelType = $channelType;
    }

    /**
     * 通道选择器
     * User: 龙哥·三年风水
     * Date: 2024/9/11
     * Time: 10:39
     * @ return null
     */
    public function statisticsHandle(){
        switch ((int)$this->channelType){
            case 1: //可疑网址查询

                break;
            case 2: //银行卡查询

                break;
            case 3: //人员被骗风险查询

                break;
            case 4: //答题记录查询
                self::$instance = new Answer();
                break;
            default:
                self::$instance = null;
                throw new BaseError("未设置任何通道",50000,200);
                break;
        }
        return self::$instance;
    }
}

        3、答题统计查询实现(Answer.php)

<?php
/**
 * 答题记录查询控制
 * User: 龙哥·三年风水
 * Date: 2024/9/11
 * Time: 10:29
 */
namespace Statistics\statistics;
use Statistics\StatisticsHandle;
use app\BaseError;
use app\model\anxun\shuziren\StatisticsAnswer as ASSAModel;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use Upload\UploadFactory;
class Answer extends StatisticsHandle
{
    //初始化使用的
    public function __construct(){
    }


    /**
     * 获取统计列表
     * User: 龙哥·三年风水
     * Date: 2024/9/11
     * Time: 10:31
     * @ return mixed
     */

    public function getList($param){
        // TODO: Implement getList() method.
        $where = [];
        if(!empty($param['digitized_human_name']))$where[] = ['digitized_human_name','like','%'.$param['digitized_human_name'].'%'];
        if(!empty($param['involved_date']))$where[] = ['involved_date','=',$param['involved_date']];
        if(!empty($param['id_card']))$where[] = ['id_card','=',$param['id_card']];
        $resList = ASSAModel::getList($where,'id,digitized_human_name,correct_rate,error_rate,id_card,create_time',[$param['currentPage'], $param['currentSize']]);
        $list  =  $resList['info'];
        $res['list'] = $list;
        $res['meat'] = $resList['page'];
        return $res;
    }


    /**
     * 获取统计详情
     * User: 龙哥·三年风水
     * Date: 2024/9/11
     * Time: 10:31
     * @ return mixed
     */

    public function getInfo($id){
        // TODO: Implement getInfo() method.
        $data = ASSAModel::dataFind(['id' => $id],'id,digitized_human_name,correct_rate,error_rate,id_card,create_time',true);
        return $data;
    }


    /**
     * 导出统计信息
     * User: 龙哥·三年风水
     * Date: 2024/9/11
     * Time: 10:32
     * @ return mixed
     */

    public function exportInfo($param){
        // TODO: Implement exportInfo() method.
        $where = [];
        if(!empty($param['digitized_human_name']))$where[] = ['digitized_human_name','like','%'.$param['digitized_human_name'].'%'];
        if(!empty($param['involved_date']))$where[] = ['involved_date','=',$param['involved_date']];
        if(!empty($param['id_card']))$where[] = ['id_card','=',$param['id_card']];
        $list = ASSAModel::getAll($where,'digitized_human_name,correct_rate,error_rate,id_card,create_time','create_time DESC');
        if(!$list)throw new BaseError('暂无可导出',50000,200);
        // 处理标题
        $title = ['机器人名称', '正确题数', '错误题数','身份证号','参与时间'];
        $filename = '答题参与记录';
        $spreadsheet = new Spreadsheet();
        $spreadsheet->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); //设置水平默认对齐方式
        $spreadsheet->getDefaultStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); //设置垂直默认对齐方式
        $sheet = $spreadsheet->getActiveSheet();
        $everyDay = date('Y-m-d', time()). '_' .uniqid();
        foreach ($title as $key => $value) {
            $sheet->setCellValueByColumnAndRow($key+1, 1, $value);
        }
        $row = 2;
        foreach ($list as $item) {
            $column = 1;
            foreach ($item as $k => $value) {
                $sheet->setCellValueByColumnAndRow($column, $row, $value);
                $column++;
            }
            $row++;
        }
        $savename = '/storage/'.$filename . '_' . $everyDay . '.xlsx';
        $object = date('Y-m-d',time()).'/' . $filename . '_' . $everyDay . '.xlsx';
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('.' . $savename);
        $UploadFactory = new UploadFactory();
        $uploadHandle = $UploadFactory->getUploadHandle();
        $res = $uploadHandle->uploadFile($object,'.' . $savename);
        $data['url'] = $res;
        $filePath = app()->getRuntimePath() . $savename;
        @unlink($filePath);
        return $data;
    }
}

原文地址:https://blog.csdn.net/m0_63603104/article/details/142319907

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!