<?php

namespace Content\Lib;

use Dever;

class Xuniren
{
    # 获取几条数据
    public function getHome($limit = 10)
    {
        $where = array();

        $where['limit'] = '0,' . $limit;
        $data = Dever::db('content/xuniren')->getHome($where);

        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->getPic($v);
                $data[$k]['link'] = Dever::url('main/xnr-detail?id=' . $v['id']);
            }
        }

        return $data;
    }

    public function getDataByCreater($creater_id)
    {
        $where = array();

        //$where['creater_id'] = $creater_id;
        //$data = Dever::db('content/xuniren')->getDataByCreater($where);
        $where['cid'] = $creater_id;
        $data = Dever::db('content/creater_xuniren')->getAll($where);

        $result = array();
        if ($data) {
            foreach ($data as $k => $v) {
                $info = Dever::db('content/xuniren')->one($v['xid']);
                if ($info) {
                    $info = $this->getPic($info);
                    $info['sname'] = $info['name'] . ' / ' . $info['ename'];
                    $info['link'] = Dever::url('main/xnr-detail?id=' . $info['id']);
                    $info['info'] = '<span>简介</span> ' . $info['info'];
                    $result[] = $info;
                }
            }
        }

        return $result;
    }

    private function getPic($data)
    {
        if (strstr($data['pic'], ',')) {
            $data['pic_list'] = explode(',', $data['pic']);
            $data['pic'] = $data['pic_list'][0];
        } else {
            $data['pic_list'][] = $data['pic'];
        }

        return $data;
    }

    public function getInfo($id)
    {
        $data = Dever::db('content/xuniren')->one(array('id' => $id));

        if ($data) {
            $data = $this->getPic($data);
            $data['news'] = Dever::array_decode($data['news']);
            if ($data['bdate']) {
                $data['age'] = $this->birthday($data['bdate']);
                if ($data['age'] <= 0) {
                    $data['age'] = '不详';
                }
            } else {
                $data['age'] = '不详';
            }

            if ($data['sex'] == 1) {
                $data['sex'] = '男';
            } elseif ($data['sex'] == 2) {
                $data['sex'] = '女';
            } else {
                $data['sex'] = '不详';
            }
            $data['sname'] = $data['name'] . ' / ' . $data['ename'];

            $data['pdate'] = date('Y/m/d', $data['pdate']);

            $data['creater_name'] = '';
            $creater_name = array();
            $where['xid'] = $data['id'];
            $info = Dever::db('content/creater_xuniren')->getAll($where);
            if ($info) {
                foreach ($info as $k => $v) {
                    $creater = Dever::db('content/creater')->one(array('id' => $v['cid']));
                    if ($creater) {
                        $creater_name[] = $creater['desc'];
                    }
                }
                if ($creater_name) {
                    $data['creater_name'] = implode(' & ', $creater_name);
                }
            }
        }

        return $data;
    }

    public function birthday($age)
    {
        $temp = explode("-",date("Y-m-d",$age));
        $y1 = $temp[0];
        $m1 = $temp[1];
        $d1 = $temp[2];

        $temp = explode("-",date("Y-m-d"));
        $y2 = $temp[0];
        $m2 = $temp[1];
        $d2 = $temp[2];

        $age = $y2 - $y1; 
        if((int)($m2.$d2) < (int)($m1.$d1)) 
        $age -= 1;
        return $age; 
    } 
}