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


namespace app\lib\service;


use app\common\controller\Api;
use app\admin\model\worship\Worshipgoodsmodel;
use app\admin\model\worship\Worshiphallmodel;
use app\admin\model\worship\Worshiplogmodel;
use app\api\model\OrderModel;
use app\api\model\PaymentLogModel;
use app\api\model\UsersModel;
use app\lib\model\FamilyPeopleModel;
use app\lib\model\FamilyTreeModel;
use app\lib\model\FamilyModel;
use think\Db;
use think\Exception;

class WorshipService
{
    /**
     * 支付回调，根据订单，生成祭拜记录
     */
    public static function conductWorship($log_id)
    {
        //根据log_id查询支付记录
        $payLog = PaymentLogModel::where(['id' => $log_id])->find();
        //查询订单信息
        $orderInfo = OrderModel::where(['id' => $payLog['order_id']])->find();
        //贡品id
        $goods_id = $orderInfo['goods_id'];
        //商品信息
        $goodsInfo = Worshipgoodsmodel::dataInfo(['id' => $orderInfo['goods_id']]);
        //贡品名称0
        $goods_name = $goodsInfo['title'];
        //查找灵堂
        $worshipHallInfo = Worshiphallmodel::dataInfo(['user_id' => $orderInfo['consignee_id']]);
        //获取祭拜人的信息
        $userInfo = UsersModel::where(['id' => $orderInfo['user_id']])->find();
        //贡品信息
        $log = [
            'worship_hall_id' => $worshipHallInfo['id'],
            'user_id' => $orderInfo['user_id'],
            'type' => 2,
            'intro' =>  $userInfo['username'] . '进行了祭拜',
            'goods_id' => $goods_id,
            'goods_name' => $goods_name,
            'createtime' => time()
        ];
        Worshiplogmodel::create($log);
        //更新支付记录
        PaymentLogModel::where(['id' => $log_id])->update(['status' => 2]);
        //更新订单记录
        OrderModel::where(['id' => $payLog['order_id']])->update(['status' => 2, 'paytime' => time()]);
        //更新贡品售出数
        Worshipgoodsmodel::where(['id' => $goods_id])->update(['sold_num' => $goodsInfo['sold_num'] + 1]);
    }
}
