<?php
namespace app\index\logic;

use app\index\model\appointmentmodel;
use app\index\model\payordermodel;
use think\Db;
use think\Log;

/**
 * 支付
 *
 * @author wj
 * @date 2022-07-22
 */
class paylogic extends baselogic
{
    /**
     * 设置请求数据规则
     * 20220107
     * wj
     */
    protected function setrules()
    {
        $list = [
            'getappointmentorder' => [
                ['name' => 'aid', 'title' => '申请单id', 'require' => true, 'type' => 'numeric'], //以元为单位
                ['name' => 'openid', 'title' => 'openid', 'require' => true, 'type' => 'string'],
                ['name' => 'total_fee', 'title' => '支付费用', 'require' => true, 'type' => 'numeric'], //以元为单位
            ],
        ];
        return $list;
    }
    /**
     * 创建小程序申请订单
     *
     * @return void
     * @author wj
     * @date 2022-07-22
     */
    public function getappointmentorderforxcx($arr)
    {
        $result = $this->checkparam(__FUNCTION__, $arr);
        if (1 != $result['status']) {
            return $result;
        }
        $data = $result['data'];
        $openid = $data['openid'];
        $aid = $data['aid'];
        $totalfee = bcmul($data['total_fee'], 1000, 0);

        $m_a = new appointmentmodel();
        $where = [
            'id' => $aid,
            'openid' => $openid,
        ];
        $ainfo = $m_a->getInfo($where);
        if (empty($ainfo)) {
            return backarr(0, "无申请数据");
        }
        $body = "核酸检测费用";
        $out_trade_no = createOrderNo();
        $notify_url = getselfurl('dev') . "index/appointment/pay_call_back";
        $trade_type = 'JSAPI';
        $orderinfo = [
            'openid' => $openid,
            'total_fee' => $totalfee,
            'body' => $body,
            'out_trade_no' => $out_trade_no,
            'notify_url' => $notify_url,
            'trade_type' => $trade_type,
            'wid' => $ainfo['wid'],
            'appointent_id' => $aid,
        ];
        return $orderinfo;
    }
    /**
     * 处理支付回调
     *
     * @return void
     * @author wj
     * @date 2022-07-23
     */
    public function handleorder($arr)
    {
        Db::startTrans();
        try {
            $orderstatus = 8;
            $ispay = 0;
            if (array_key_exists('trade_state', $arr)) {
                $paystatus = $arr['trade_state'];
            } else {
                $paystatus = $arr['result_code'];
            }
            if ("SUCCESS" == $paystatus) {
                $orderstatus = 3;
                $ispay = 1;
            }
            $l_p = new payordermodel();
            $outorderno = $arr['out_trade_no'];
            $orderInfo = $l_p->getorderinfobyoutorderno($outorderno);
            if (empty($orderInfo)) {
                $msg = "orderNo:[" . $outorderno . "]" . "无对应订单号";
                throw new \Exception($msg);
            }
            if (3 == $orderInfo['orderstatus'] && 1 == $orderInfo['ispay']) {
                Db::rollback();
                $msg = "orderNo:[" . $outorderno . "]" . "已缴费";
                return backarr(1, $msg);
            }
            $updateData = [
                'outorderno' => $orderInfo['outorderno'],
                'wid' => $orderInfo['wid'],
                'openid' => $orderInfo['openid'],
                'orderstatus' => $orderstatus,
                'ispay' => $ispay,
                'is_reat_back' => 1,
            ];
            if (isset($arr['transaction_id']) && !empty($arr['transaction_id'])) {
                $updateData['transaction_id'] = $arr['transaction_id'];
            }
            $row = $l_p->updateinfobyid($orderInfo['id'], $updateData);
            if (empty($row)) {
                $msg = "orderNo:[" . $outorderno . "]修改失败";
                throw new \Exception($msg);
            }
            $ordertype = $orderInfo['order_type'];
            switch ($ordertype) {
                case 1:
                    $appointentid = $orderInfo['appointent_id'];
                    $m_a = new appointmentmodel();
                    $ainfo = $m_a->getInfo(['id' => $appointentid, 'is_pay' => 0]);
                    if (empty($ainfo)) {
                        $msg = "申请单id:[" . $appointentid . "]不存在";
                        throw new \Exception($msg);
                    }
                    $code = $outorderno . "|" . date("YmdHis");
                    $aupdateData = [
                        'is_pay' => 1,
                        'payorderno' => $outorderno,
                        'code' => $code,
                    ];
                    $row = $ainfo->update(['id' => $appointentid], $aupdateData);
                    if (empty($row)) {
                        $msg = "申请单id:[" . $appointentid . "]修改失败";
                        throw new \Exception($msg);
                    }
                    break;
            }
            Db::commit();
            Log::info("commit");
            return backarr(1, "处理完成", ['id' => $orderInfo['id']]);
        } catch (\Exception $e) {
            Db::rollback();
            log::info("rollback");
            return backarr(0, $e->getMessage());
        }
    }
}
