<?php

namespace app\user\controller;
use think\Lang;

class Renewal extends UserControl
{

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/db.lang.php');

    }
    public function index()
    {
        $condition['user_id'] = session('user_id');
        $type = input('get.type');
        $nowDate = date('Y-m-d');
        if (count($type)) {
            if ($type > 0) {
                $maturityTime = date('Y-m-d', strtotime ($type . " day"));
                $condition['userProduct_maturityTime'] = array('between', array($nowDate, $maturityTime));
            } else {
                $condition['userProduct_maturityTime'] = array('<', $nowDate);
            }
        }
        $userProduct = model('Userproduct')->getUserProduct($condition);
        $fatherProduct = model('product')->getProduct($condition);
        $productData = [];
        foreach ($fatherProduct as $v) {
            $productData[$v->product_id] = $v->product_name;
        }
        foreach ($userProduct as $key => $v) {
            $userProduct[$key]->fatherProduct = $productData[$v->product_pid];
            $userProduct[$key]->status = $nowDate > $v->userProduct_maturityTime ? '已过期' : '正常';
        }
        $this->assign('userProduct', $userProduct);
        $this->assign('show_page', $userProduct->render());
        $getRenewal = model('renewal')->getRenewal();
        $this->assign('renewalType', $getRenewal);

        return $this->fetch('index');
    }
    public function renewal()
    {
        $userId = session('user_id');
        $type = input('post.type');
        $userProduct_id = input('post.renewal_id');
        $product_id = input('post.product_id');
        $product = model('product')->getProductWhere(['product_id'=>$product_id, 'product_status'=>1]);
        if(!$product) {
            return ["msg"=>"错误：该产品已停售"];
        }
        $userInfo = model('userinfo')->getInfo($userId);
        switch (intval($type)) {
            case 1:
                $money = $product->product_TypeOne;
                break;
            case 2:
                $money = $product->product_TypeTwo;
                break;
            case 3:
                $money = $product->product_TypeThree;
                break;
            default:
                $money = $product->product_TypeFour;
        }
        if ($userInfo->userInfo_money >= $money) {
            $findRenewal = model('renewal')->findRenewal(['renewal_status'=>1,'renewal_identity'=>$type]);
        } else {
            return ["msg"=>"错误：您的余额不足"];
        }
        if (!$findRenewal) {
            return ["msg"=>"错误：该期限已不支持购买"];
        }

        $day = $findRenewal->renewal_day;
        $cashMoney = $userInfo->userInfo_money - $money;
        /*$model = model('userinfo');
        $model->startTrans();*/
        $updateUserInfo = model('userinfo')->updateInfo($userId, ['userInfo_money' => $cashMoney]);
        $nowDate = date('Y-m-d');
        $userProduct = model('Userproduct')->findUserProduct(['userProduct_id' => $userProduct_id]);
        $data['userProduct_dayNumber'] = $userProduct->userProduct_dayNumber + $day;
        $data['userproduct_identity'] = OrderID();
        $data['userProduct_buyTime'] = $nowDate;
        /*if ($userProduct->userProduct_maturityTime > $nowDate) {
            $data['userProduct_maturityTime'] = date('Y-m-d', strtotime(intval($day) . " day", strtotime($userProduct->userProduct_maturityTime)));
        } else {
            $data['userProduct_maturityTime'] = date('Y-m-d', strtotime(intval($day) . " day", $nowDate));
        }*/
        $updateUserProduct = model('Userproduct')->updateUserProduct(['userProduct_id' => $userProduct_id], $data);
        // 提交事务
        if ($updateUserInfo && $updateUserProduct) {
            /*$model->commit();*/
            return ["msg" => "续费成功"];
        } else {
            // 回滚事务
            /*$model->rollBack();*/
            return ["msg" => "操作失败"];
        }
    }

}

?>
