<?php
namespace app\app\logic;

use app\app\model\appointmentmodel;
use app\app\model\tubemodel;
use think\Db;

/**
 * 登记记录表
 *
 * @author wj
 * @date 2022-07-22
 */
class appointmentlogic extends baselogic
{
    /**
     * 设置请求数据规则
     * 20220107
     * wj
     */
    protected function setrules()
    {
        $list = [
            'getinfobyid' => [
                ['name' => 'id', 'title' => '申请id', 'require' => true, 'type' => 'numeric'],
            ],
            'finishedbyaid' => [
                ['name' => 'appointment_id', 'title' => '申请id', 'require' => true, 'type' => 'numeric'],
                ['name' => 'testtubeno', 'title' => '测试管号', 'require' => true, 'type' => 'numeric'],
            ],
        ];
        return $list;
    }
    /**
     * 获取用户当日申请最后一条数据
     *
     * @param  [type] $arr
     * @return void
     * @author wj
     * @date 2022-07-22
     */
    public function getinfobyid($arr)
    {
        $result = $this->checkparam(__FUNCTION__, $arr);
        if (1 != $result['status']) {
            return $result;
        }
        $m_a = new appointmentmodel();
        $where = ['id' => $arr['id']];
        $field = ['name', 'sfzid', 'telno', 'signurl', 'oprdate', 'ispay', 'isuse', 'payorderno'];
        $info = $m_a->getInfo($where, $field);
        if (empty($info)) {
            return backarr(0, "无申请数据");
        }
        if (!$info['ispay']) {
            return backarr(0, "未支付");
        }
        if ($info['isuse']) {
            return backarr(0, "已使用");
        }
        return backarr(1, "查询成功", $info);
    }
    /**
     * 请求数据完成
     * 根据appointment_id和testtubeno
     *
     * @return void
     * @author wj
     * @date 2022-07-25
     */
    public function finishedbyaid($arr)
    {
        $result = $this->checkparam(__FUNCTION__, $arr);
        if (1 != $result['status']) {
            return $result;
        }
        $data = $result['data'];
        $aid = $data['appointment_id'];
        $testtubeno = $data['testtubeno'];
        $m_t = new tubemodel();
        $m_a = new appointmentmodel();
        $where = ['id' => $aid];
        $field = ['name', 'sfzid', 'telno', 'signurl', 'oprdate', 'ispay', 'isuse', 'payorderno', 'openid', 'code', 'id'];
        $ainfo = $m_a->getInfo($where, $field);
        if (empty($ainfo)) {
            return backarr(0, "无申请数据");
        }
        if (!$ainfo['ispay']) {
            return backarr(0, "未支付");
        }
        if ($ainfo['isuse']) {
            return backarr(0, "已使用");
        }
        $where = ['appointment_id' => $aid, 'testtubeno' => $testtubeno];
        $tinfo = $m_t->getInfo($where);
        if (!empty($tinfo)) {
            return backarr(0, "数据已存在");
        }
        Db::startTrans();
        try {
            $tinsertData = [
                'openid' => $ainfo['openid'],
                'name' => $ainfo['openid'],
                'telno' => $ainfo['openid'],
                'sfzid' => $ainfo['openid'],
                'testtubeno' => $testtubeno,
                'code' => $ainfo['code'],
                'appointment_id' => $ainfo['id'],
            ];
            $tid = $m_t->insertData($tinsertData);
            if (empty($tid)) {
                throw new \Exception("试管信息添加失败");
            }
            $aupdateData = ['isuse' => 1];
            $awhere = ['id' => $aid];
            $row = $m_a->updateinfo($awhere, $aupdateData);
            if (empty($row)) {
                throw new \Exception("申请单修改失败");
            }
            Db::commit();
            return backarr(1, "操作成功", ['tid' => $tid]);
        } catch (\Exception $e) {
            Db::rollback();
            return backarr(0, $e->getMessage());
        }
    }
}
