<?php
/*
 * @Author: taowj 965042433@qq.com
 * @Date: 2023-07-19 11:01:40
 * @LastEditors: taowj 965042433@qq.com
 * @LastEditTime: 2023-07-21 17:28:27
 * @FilePath: \zhupu\application\api\model\PaymentLogModel.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2021/11/24
 * Time: 16:23
 */

namespace app\api\model;

use app\admin\model\worship\Worshipgoodsmodel;
use app\lib\model\FamilyPeopleModel;
use app\lib\model\FamilyTreeModel;
use app\lib\service\WorshipService;
use think\Config;
use think\Db;
use think\Exception;
use think\Log;
use think\Model;

class PaymentLogModel extends Model
{



    // 表名
    protected $name = 'payment_logs';

    /**
     * 判断支付金额
     */
    public static function checkPayMoney($pay_log, $amount)
    {
        if (strcmp($pay_log['order_type'], 'goods') == 0) {
            $order = OrderModel::where(['id' => $pay_log['order_id']])->find();
            $goodsInfo = Worshipgoodsmodel::dataInfo(['id' => $order['goods_id']]);
            //判断商品金额是否和直接金额相同
            if ($order['need_pay'] != $goodsInfo['price']) {
                $need_pay = $goodsInfo['price'];
            } elseif ($order['need_pay'] != $order['total_price']) {
                $need_pay = $order['need_pay'];
            } else {
                $need_pay = $order['total_price'];
            }
        } elseif (strcmp($pay_log['order_type'], 'recharge') == 0) {
            $recharge = RechargeModel::where(['id' => $pay_log['order_id']])->find();
            if ($recharge['is_certification'] == 1) {
                $need_pay = Config::get('site.certification_fee');
            } else {
                $need_pay = $recharge['payamount'];
            }
        }

        if ($amount != $need_pay) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * @param $message
     * 充值支付回调
     */
    public static function dealRechargeBack($message)
    {
        $pay_log = self::where(['order_sn' => $message['out_trade_no']])->find();
        if (!$pay_log || $pay_log['status'] == 2) {
            return false;
        }
        try {
            Db::startTrans();
            if (strcmp($pay_log['order_type'], 'recharge') == 0) {
//                Log::record('pay_log' . $pay_log['order_sn'], 'debug');
                self::where(['order_sn' => $pay_log['order_sn']])->update(['status' => 2]); //支付完成
                $rechargeinfo = RechargeModel::where(['id' => $pay_log['order_id']])->find(); //已支付
                RechargeModel::where(['id' => $pay_log['order_id']])->update(['status' => 'paid']); //已支付
                /* if($rechargeinfo['tree_id']){
                FamilyTreeModel::where(['id' =>$rechargeinfo['tree_id']])->update(['status' =>1]);
            }*/
//                Log::record('rechargeinfo:people_id' .$rechargeinfo['people_id'], 'debug');
                if ($rechargeinfo['people_id']) {
                    FamilyPeopleModel::where(['id' => $rechargeinfo['people_id']])->update(['is_pay' => 1,'status'=>1]);
                }
            } elseif (strcmp($pay_log['order_type'], 'goods') == 0) {
                //贡品订单支付回调
                WorshipService::conductWorship($pay_log['id']);
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
        return true;
    }
}
