<?php


namespace app\lib\model;


use think\Db;
use think\Model;

class FamilyModel extends Model
{
    // 表名
    protected $name = 'family';


    // 追加属性
    protected $append = [
        'surname_text',
    ];


    public function getSurnameTextAttr($value,$data){
        if(isset($data['surname_id'])){
           $value= Db::name("surname")->where(['ID'=>$data['surname_id']])->value("Surname");
        }
        return $value;
    }


    /**
     * @功能说明:
     */
    public static function dataInfo($where){

        $data=  self::where($where)->find();

        if($data) preg_match_all('/[\x{4e00}-\x{9fa5}]/u',$data['genealogy'],$string); $data['genealogyArr'] =$string[0];
        return $data;
    }



    /**
     * @功能说明:添加
     */
    public static function dataAdd($data){

        $data['createtime'] = time();

        $res = self::create($data);

        return $res;
    }


    /**
     * 查询列表
     */
    public static function dataList($where,$field="*,FROM_UNIXTIME(createtime,'%Y年%m月%d日') as createtime",$order="id DESC",$page=1,$pagesize=15){
        $data = self::where($where)->field($field)->page($page)->limit($pagesize)->order($order)->select();
        return $data;
    }


    /**
     * @功能说明:编辑
     */
    public static function dataUpdate($where,$data){

        return self::where($where)->update($data);
    }

    /**
     * 我加入的族谱
     */
    public static function myFamilyTreeList($where){
        $data=self::alias("f")
            ->join("family_tree ft","f.id=ft.family_id","LEFT")
            ->field("f.*,FROM_UNIXTIME(f.createtime,'%Y年%m月%d日') as createtime")
            ->where($where)
            ->group("f.id")
            ->select();
        return $data;
    }

    /**
     * 我加入的母族谱
     */
    public static function myFamilyTreeMateList($where){
        $data=self::alias("f")
            ->join("family_tree ft","f.id=ft.family_id","LEFT")
            ->join("family_tree_mate ftm","ft.id = ftm.mother_tree_id","LEFT")
            ->field("f.*,FROM_UNIXTIME(f.createtime,'%Y年%m月%d日') as createtime")
            ->where($where)
            ->group("f.id")
            ->select();
        return $data;
    }

}