<?php
/**
 * 微信设置管理公共控制器
 * 提供统一的业务逻辑，供admin和adminghd模块继承使用
 */
namespace app\common\controller;

use app\common\common\BaseController;
use think\Request;
use think\Db;

class WechatsetBase extends BaseController
{
    /**
     * 查询小程序资讯信息
     * @author hjc
     * @date 2024-05-14
     */
    public function getWechatRealTimeInfoList(Request $request)
    {
        $post = $request->param();
        
        $page = isset($post['page']) && !empty($post['page']) ? $post['page'] : 1; // 页数
        $page_size = isset($post['page_size']) && !empty($post['page_size']) ? $post['page_size'] : 30; // 每页条数
        
        // 搜索
        $where = [];
        if (isset($post['title']) && !empty($post['title'])) {
            $where['title_plain'] = ['like', '%' . $post['title'] . '%'];
        }
        
        // 获取资讯表名
        $tableName = $this->getTableName('real_time_info');
        
        // 升序获取菜单列表
        $res = Db::name($tableName)
            ->field('id,title_plain,thumbnail,excerpt_plain,url,create_time')
            ->where($where)
            ->order('create_time desc')
            ->paginate($page_size, false, ['page' => $page]);
        
        $lists = $res->items();
        $start = ($page - 1) * $page_size;
        foreach ($lists as $k => $v) {
            $start += 1;
            $lists[$k]['no_id'] = $start;
        }
        
        $result['lists'] = $lists;
        $result['lastPage'] = $res->lastPage(); // 总页数
        $result['currentPage'] = $res->currentPage(); // 当前页
        $result['erro'] = 0;
        $result['msg'] = '查询成功';
        return json($result);
    }
    
    /**
     * 删除小程序资讯信息
     * @author hjc
     * @date 2024-05-14
     */
    public function delWechatRealTimeInfo(Request $request)
    {
        $post = $request->param();
        if (!isset($post['id']) || empty($post['id'])) {
            $result['erro'] = -1;
            $result['msg'] = "请选择要删除的记录";
            return json($result);
        }
        
        $tableName = $this->getTableName('real_time_info');
        $id = Db::name($tableName)->where('id', $post['id'])->value('id');
        
        if (empty($id)) {
            $result['erro'] = -1;
            $result['msg'] = "该记录异常，无法删除";
            return json($result);
        }
        
        $res = Db::name($tableName)->where('id', $id)->delete();
        if ($res > 0) {
            $result['erro'] = 0;
            $result['msg'] = '删除成功';
        } else {
            $result['erro'] = -1;
            $result['msg'] = '删除失败';
        }
        return json($result);
    }
    
    /**
     * 保存小程序资讯信息
     * @author hjc
     */
    public function saveWechatRealTimeInfo(Request $request)
    {
        $post = $request->param();
        
        if (!isset($post['title_plain']) || empty($post['title_plain'])) {
            $result['erro'] = -1;
            $result['msg'] = "未传入标题";
            return json($result);
        }
        if (!isset($post['thumbnail']) || empty($post['thumbnail'])) {
            $result['erro'] = -1;
            $result['msg'] = "未传入图片";
            return json($result);
        }
        if (!isset($post['excerpt_plain']) || empty($post['excerpt_plain'])) {
            $result['erro'] = -1;
            $result['msg'] = "未传入简介";
            return json($result);
        }
        if (!isset($post['url']) || empty($post['url'])) {
            $result['erro'] = -1;
            $result['msg'] = "未传入资讯跳转链接";
            return json($result);
        }
        
        // 提交数据
        $data = [];
        $data['title_plain'] = $post['title_plain']; // 标题
        $data['thumbnail'] = $post['thumbnail']; // 图片
        $data['excerpt_plain'] = $post['excerpt_plain']; // 介绍
        $data['url'] = $post['url']; // 跳转连接
        
        $tableName = $this->getTableName('real_time_info');
        
        if (isset($post['id']) && !empty($post['id'])) {
            // 修改数据
            $res = Db::name($tableName)->where('id', $post['id'])->update($data);
        } else {
            // 保存记录数据
            $data['id'] = md5(time() . rand(100000, 999999));
            $data['create_time'] = date('Y-m-d H:i:s');
            $res = Db::name($tableName)->insert($data);
        }
        
        if ($res === false) {
            $result['erro'] = -1;
            $result['msg'] = "添加失败";
            return json($result);
        } else {
            $result['erro'] = 0;
            $result['msg'] = "添加成功";
            return json($result);
        }
    }
    
    /**
     * 查询资讯信息详情
     * @author hjc
     */
    public function getRealTimeInfoDetail(Request $request)
    {
        $post = $request->param();
        if (!isset($post['id']) || empty($post['id'])) {
            $result['erro'] = -1;
            $result['msg'] = "未传入资讯id";
            return json($result);
        }
        
        $tableName = $this->getTableName('real_time_info');
        $result['infro'] = Db::name($tableName)->where('id', $post['id'])->find();
        $result['erro'] = 0;
        $result['msg'] = "查询成功";
        return json($result);
    }
}
