<?php
namespace app\user\model;

use think\Model;

/**
 * 用户管理
 */
class Userproduct extends Model
{
    public function getProductCount($userId)
    {
        $result = $this
            ->where(['user_id' => $userId])
            ->count();

        return $result;
    }
    public function getRenewProductCount($userId)
    {
        $allData = $this
            ->field(['userProduct_dayNumber', 'userProduct_buyTime'])
            ->where(['user_id' => $userId])
            ->select();
        $count = 0;
        foreach ($allData as $key => $value) {
            $numberDay = $value->userProduct_dayNumber - 15;
            $remindDate = date("Y-m-d",strtotime($numberDay . " day"));
            if ($value->userProduct_buyTime >= $remindDate) {
                $count++;
            }
        }

        return $count;
    }
    public function getUserProduct($condition)
    {
        $result = $this
            ->alias('a')
            ->join('product b', 'a.product_id = b.product_id')
            ->where($condition)
            ->paginate(10);

        return $result;
    }
    public function findUserProduct($condition)
    {
        $result = $this
            ->where($condition)
            ->find();

        return $result;
    }
    public function updateUserProduct($condition, $data)
    {
        $result = $this
            ->where($condition)
            ->update($data);

        return $result;
    }

}
