<?php


namespace app\lib\service;


use app\api\model\UsersModel;
use app\lib\model\FamilyModel;
use app\lib\model\FamilyPeopleModel;

class FamilyTreeService
{

    /**
     * 成员信息
     */
    public static function addFamilyPeople($param,$family_id=null){

        if(isset($param['identity_card']) && !empty($param['identity_card'])){
            $people['user_id']=UsersModel::where(['identity_card'=>$param['identity_card']])->value('user_id');
        }
        $people['surname']=isset($param['surname'])?$param['surname']:'';
        $people['firstname']=isset($param['firstname'])?$param['firstname']:'';
        $people['name']=$people['surname'].$people['firstname'];
        $people['sex']=isset($param['sex'])?$param['sex']:'';
        $people['ranking']=isset($param['ranking'])?$param['ranking']:'';
        $people['identity_card']=isset($param['identity_card'])?$param['identity_card']:'';
        $people['photo']=isset($param['photo'])?$param['photo']:'assets/img/photo.png';
        $people['ext0']=isset($param['ext0'])?$param['ext0']:'';
        $people['ext1']=isset($param['ext1'])?$param['ext1']:'';
        $people['ext2']=isset($param['ext2'])?$param['ext2']:'';
        $people['ext3']=isset($param['ext3'])?$param['ext3']:'';
        $people['ext4']=isset($param['ext4'])?$param['ext4']:'';
        $people['ext5']=isset($param['ext5'])?$param['ext5']:'';
        $people['ext6']=isset($param['ext6'])?$param['ext6']:'';
        $people['ext7']=isset($param['ext7'])?$param['ext7']:'';
        $people['ext8']=isset($param['ext8'])?$param['ext8']:'';
        $people['province_id']=isset($param['province_id'])?$param['province_id']:'';
        $people['city_id']=isset($param['city_id'])?$param['city_id']:'';
        $people['area_id']=isset($param['area_id'])?$param['area_id']:'';
        $people['town_id']=isset($param['town_id'])?$param['town_id']:'';
        $people['village_id']=isset($param['village_id'])?$param['village_id']:'';
        $people['address']=isset($param['address'])?$param['address']:'';
        $people['sleep_place']=isset($param['sleep_place'])?$param['sleep_place']:'';

        if(isset($param['people_id']) && !empty($param['people_id'])){//如果成员存在
            $people['audit']=$people['status']=0;
            FamilyPeopleModel::dataUpdate(['id'=>$param['people_id']],$people);
            $people['id']=$param['people_id'];
            return $people;
        }else{
            $family_id = isset($param['family_id'])?$param['family_id']:$family_id;
            if(!empty($family_id)) FamilyModel::where(['id'=>$family_id])->setInc('join_number');
            return FamilyPeopleModel::dataAdd($people);
        }
    }


    /**
     *字辈
     */
    public static function genealogyList($family_id,$people_id,$type){

        $Family=FamilyModel::dataInfo(['id'=>$family_id]);

        $genealogyArr=implode("",$Family['genealogyArr']);

        $data['genealogyArr'] = $Family['genealogyArr'];

        if(!empty($people_id)){
            $people= FamilyPeopleModel::dataInfo(['id'=>$people_id]);

            if($type == 1){
                $data['val']= mb_substr(mb_substr($genealogyArr,0,mb_strripos($genealogyArr,$people['ext5'])),-1);
                if(empty($data['val'])){
                    $data['val']= mb_substr($genealogyArr,-1);
                }
            }else if($type == 2){
                $data['val']=  mb_substr($genealogyArr,mb_strripos($genealogyArr,$people['ext5'])+1,1);
                if(empty($data['val'])){
                    $data['val']= mb_substr($genealogyArr,0,1);
                }
            }else{
                $data['val']=$people['ext5'];
            }

        }

        return $data;
    }

}