<?php


namespace app\lib\model;


use think\Db;
use think\Model;

class FamilyTreeModel extends Model
{
    // 表名
    protected $name = 'family_tree';

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

    public function getSexTextAttr($value,$data){
        if(isset($data['sex']) && $data['sex']==0){
            $value="未知";
        }else if(isset($data['sex']) && $data['sex']==1){
            $value="男";
        }else if(isset($data['sex']) && $data['sex']==2){
            $value="女";
        }
        return $value;
    }


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

        return  self::where($where)->find();
    }


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

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

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

        return $res;
    }

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

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

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

        return self::where($where)->delete();
    }

    /**
     * 递归向上查询多级父节点
     */
    public static function parentFamilyTree($ids=0,$level=2){
        $sql="SELECT @r AS _id,
                        (SELECT @r := parent_id FROM zp_family_tree WHERE id = _id) AS parent_id,
                        @l := @l + 1 AS lvl
                        FROM
                        (SELECT @r := ".$ids.", @l := 0) vars, zp_family_tree AS h
                        WHERE @r <> 0 and  @l <= ".$level;
        $data=db()->query($sql);
        return $data;
    }

    /**
     * @功能说明:查询某个人所关联的族谱
     */
    public static function myFamilyTreePeople($where){
        $data=self::alias("ft")
            ->join("family_people fp","ft.people_id = fp.id")
            ->field("ft.*,fp.user_id as people_user_id,FROM_UNIXTIME(fp.createtime,'%Y年%m月%d日') as createtime")
            ->where($where)
            ->find();
        return $data;
    }

    /**
     * @功能说明:查询某个人所关联的族谱列表
     */
    public static function myFamilyTreePeopleList($where,$field="ft.*,fp.user_id as people_user_id,FROM_UNIXTIME(fp.createtime,'%Y年%m月%d日') as createtime"){
        $data=self::alias("ft")
            ->join("family_people fp","ft.people_id = fp.id")
            ->field($field)
            ->where($where)
            ->select();
        return $data;
    }

    /**
     * 认领族谱
     */
    public static function claimFamilyPeople($where){
        $data=self::alias("ft1")
            ->join("family_tree ft2","ft1.parent_id=ft2.id","LEFT")
            ->join("family_tree ft3","ft2.parent_id=ft3.id","LEFT")
            ->field("ft1.*,ft2.`name` as ft2_name,ft3.`name` as ft3_name")
            ->where($where)
            ->select();
        return $data;
    }


    /**
     * 家族所有成员树状图
     */
    public static function familyTreePeopleList($where){
        $data=self::alias("ft")
            ->join("family_people fp","ft.people_id=fp.id","LEFT")
            ->field("ft.id,ft.parent_id,ft.name,ft.photo,ft.sex,ft.parent_id,ft.people_id,ft.level,fp.number,fp.fastim_id,fp.firstname,fp.ext0,fp.ext5,fp.is_pay,fp.status")
            ->where($where)
            ->order("ft.parent_id asc,ft.id asc")
            ->select();
        return $data;
    }
    /*public static function familyTreePeopleList($where){
        $data=self::alias("ft")
            ->join("family_tree_mate ftm","FIND_IN_SET(ft.id,ftm.family_tree_id)","LEFT")
            //->join("family_people fp","(ft.people_id=fp.id or ftm.people_id=fp.id)","LEFT")
            ->field("ft.id,ft.name,ft.photo,ft.sex,ft.parent_id,ft.people_id,ftm.id as mate_id,ftm.name as mate_name,ftm.sex as mate_sex,ftm.photo as mate_photo,ftm.people_id as mate_people_id")
            ->where($where)
            ->order("ft.parent_id asc,ft.id asc")
            //->group("ft.id")
            ->select();
        return $data;
    }*/


    /**
     * 递归查询某个节点下
     */
    public  static function  recursionFamilyTreePeopleList($ids=0){
        $sql=" SELECT ft.id,ft.name,ft.photo,ft.sex,ft.parent_id,ft.people_id,ft.level,fp.number,fp.firstname,fp.ext0,fp.ext5,fp.is_pay,fp.fastim_id,fp.status  from  (  ";

        $sql= $sql."
SELECT 
	DATA.* 
FROM
	(
	SELECT
		@ids AS _ids,
		( SELECT @ids := GROUP_CONCAT( id ) FROM zp_family_tree WHERE FIND_IN_SET(parent_id, @ids ) ) AS cids,
		@l := @l + 1 AS LEVEL 
	FROM
		zp_family_tree,
		( SELECT @ids := ".$ids.", @l := 0 ) b 
	WHERE
		@ids IS NOT NULL
	) ID,	zp_family_tree DATA 
WHERE
	FIND_IN_SET( DATA.id, ID._ids ) and ID.LEVEL<=4
ORDER BY
	ID.LEVEL ASC";

        $sql= $sql." ) as ft LEFT JOIN zp_family_people as fp ON fp.id=ft.people_id  ORDER BY ft.LEVEL ASC ";

        $data=db()->query($sql);
        return $data;
    }


    /*public  static function  recursionFamilyTreePeopleList($ids=0){
        $sql=" SELECT ft.id,ft.name,ft.photo,ft.sex,ft.parent_id,ft.people_id,ftm.id as mate_id,ftm.name as mate_name,ftm.sex as mate_sex,ftm.photo as mate_photo,ftm.people_id as mate_people_id  from  (  ";

        $sql= $sql."
SELECT 
	DATA.* 
FROM
	(
	SELECT
		@ids AS _ids,
		( SELECT @ids := GROUP_CONCAT( id ) FROM zp_family_tree WHERE FIND_IN_SET(parent_id, @ids ) ) AS cids,
		@l := @l + 1 AS LEVEL 
	FROM
		zp_family_tree,
		( SELECT @ids := ".$ids.", @l := 0 ) b 
	WHERE
		@ids IS NOT NULL
	) ID,	zp_family_tree DATA 
WHERE
	FIND_IN_SET( DATA.id, ID._ids ) and ID.LEVEL<=5
ORDER BY
	ID.LEVEL ASC";

        $sql= $sql." ) as ft LEFT JOIN zp_family_tree_mate as ftm ON FIND_IN_SET(ft.id,ftm.family_tree_id)  ORDER BY ft.LEVEL ASC ";


        $data=db()->query($sql);
        return $data;
    }*/

}