<?php

namespace app\admin\model;

use think\Model;

class WechatPlanRecord extends Model
{

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    public function getIsOpenList()
    {
        return ['0' => __('Is_open 0'), '1' => __('Is_open 1')];
    }
    public static function getNewCode()
    {
        do {
            list($msec, $sec) = explode(' ', microtime());
            $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
            $orderId = 'qy' . $msectime . mt_rand(10000, 99999);
        } while (self::where(['contract_code' => $orderId])->find());
        return $orderId;
    }
//    public static function plan_lst($where)
//    {
//        $model = new self;
//        $xwhere = null;
//        $order = "id desc";
//        if (isset($where['uid']) && $where['uid'] > 0) $xwhere['uid'] = $where['uid'];
//        if (isset($where['cid']) && $where['cid'] > 0) $xwhere['cid'] = $where['cid'];
//        $data = $model->where($xwhere)->where('is_signing',0)->order($order)->field('plan_id')->select();
//        var_dump($data);
//        $plan=new WechatPlan();
//        $data2=$plan->where('plan_id','in',['188873'])->select();
//        var_dump($data2);die();
////        $count = $plan->where('id','in',$data)->count();
//        return $data2;
//    }
    public static function record_lst($where)
    {
        $model = new self;
        $xwhere = null;
        $order = "id desc";
        if (isset($where['uid']) && $where['uid'] > 0) $xwhere['uid'] = $where['uid'];
        if (isset($where['cid']) && $where['cid'] > 0) $xwhere['cid'] = $where['cid'];
        $model = $model->where('is_signing','<>',2);
        $data = $model->where($xwhere)->order($order)->select();
        $wechatPlan = new WechatPlan();
        foreach ($data as $k => $v){
            $data[$k]['image'] = $wechatPlan->where('plan_id', $v['plan_id'])->value('image');
        }
        $count = $model->where($xwhere)->count();
        return compact('count', 'data');
    }
    public static function record_info($where)
    {
        $model = new self;
        $xwhere = null;
        $order = "id desc";
        if (isset($where['id']) && $where['id'] > 0) $xwhere['id'] = $where['id'];
        if (isset($where['cid']) && $where['cid'] > 0) $xwhere['cid'] = $where['cid'];
        if (isset($where['uid']) && $where['uid'] > 0) $xwhere['uid'] = $where['uid'];
//        if (isset($where['category_id']) && $where['category_id'] > 0) $xwhere['category_id'] = $where['category_id'];
        $model = $model->where('is_signing','<>',2);
        $data = $model->where($xwhere)->order($order)->find();
        $count = $model->where($xwhere)->count();
        $laveMonth = new LaveMonth();
        $list = $laveMonth->where('user_id', $data['uid'])->where('cid',$data['cid'])->where('record_id',$data['id'])->select();
        return compact('count', 'data','list');
    }
}
