<?php
namespace app\index\logic;

use app\index\model\appointmentmodel;
use app\index\model\wxusermodel;

/**
 * 登记记录表
 *
 * @author wj
 * @date 2022-07-22
 */
class appointmentlogic extends baselogic
{
    /**
     * 设置请求数据规则
     * 20220107
     * wj
     */
    protected function setrules()
    {
        $list = [
            'getlastinfo' => [
                ['name' => 'sfzid', 'title' => '身份证', 'require' => true, 'type' => 'string'],
            ],
            'newinfo' => [
                ['name' => 'wid', 'title' => '用户id', 'require' => true, 'type' => 'numeric'],
                ['name' => 'gps_lng', 'title' => '经度', 'require' => false, 'type' => 'numeric', 'default' => ''],
                ['name' => 'gps_lat', 'title' => '纬度', 'require' => false, 'type' => 'numeric', 'default' => ''],
                ['name' => 'signurl', 'title' => '签名', 'require' => true, 'type' => 'string'],
            ],
        ];
        return $list;
    }
    /**
     * 获取用户当日申请最后一条数据
     *
     * @param  [type] $arr
     * @return void
     * @author wj
     * @date 2022-07-22
     */
    public function getlastinfo($arr)
    {
        $result = $this->checkparam(__FUNCTION__, $arr);
        if (1 != $result['status']) {
            return $result;
        }
        $data = $result['data'];
        $m_a = new appointmentmodel();
        $info = $m_a->getlastinfo($data);
        if (empty($info)) {
            return backarr(0, "无申请数据");
        }
        return backarr(1, "查询成功", $info);
    }
    /**
     * 新建用户申请数据
     *
     * @param  [type] $arr
     * @return void
     * @author wj
     * @date 2022-07-22
     */
    public function newinfo($arr)
    {
        $result = $this->checkparam(__FUNCTION__, $arr);
        if (1 != $result['status']) {
            return $result;
        }
        $data = $result['data'];
        $wid = $data['wid'];
        $gps_lng = $data['gps_lng'];
        $gps_lat = $data['gps_lat'];
        $m_a = new appointmentmodel();
        $m_u = new wxusermodel();
        $info = $m_u->getinfobyid($wid, '*');
        if (empty($info)) {
            return backarr(0, "无用户数据");
        }
        $insertData = [
            'wid' => $info['id'],
            'openid' => $info['openid'],
            'name' => $info['real_name'],
            'sfzid' => $info['sfzid'],
            'telno' => $info['telno'],
            'oprdate' => date('Y-m-d H:i:s'),
            'isuse' => 0,
            'ispay' => 0,
        ];
        if (empty($gps_lng) && is_numeric($gps_lng)) {
            $insertData['gps_lng'] = $gps_lng;
        }
        if (empty($gps_lat) && is_numeric($gps_lat)) {
            $insertData['gps_lat'] = $gps_lat;
        }
        $id = $m_a->insertData($insertData);
        if (empty($id)) {
            return backarr(0, "添加失败");
        }
        return backarr(1, "添加成功", ['id' => $id]);
    }
}
