<?php
/**
 * Created by PhpStorm.
 * User: fuju
 * Date: 2018/1/20
 * Time: 17:52
 */

namespace app\model;


class ACase extends BaseModel
{

    /**
     * 添加案场联系人
     *
     * @param $data
     * @return string
     * @throws \Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addCase($data) {
        $date = date('Y-m-d H:i:s');
        $data = array_unique($data);
        $case_arr = array();
        $id_str = '';
        foreach ($data as $k=>$v) {
            $data_arr = explode('-',$v);
            if (count($data_arr) == 2) {
                $check = $this->field('id')->where([
                    'name'  => $data_arr['0'],
                    'phone' => $data_arr['1'],
                    'status'    => 0
                ])->find();

                if ($check) {
                    $id_str .= $check['id'].',';
                    continue;
                } else {
                    $case_arr[$k]['name']  = $data_arr['0'];
                    $case_arr[$k]['phone'] = $data_arr['1'];
                    $case_arr[$k]['create_time'] = $date;
                    $case_arr[$k]['update_time'] = $date;
                }
            }
        }
        $res = $this->saveAll($case_arr);
        foreach ($res as $k=>$v) {
            $id_str .=  $v->id.',';
        }
        $id_str = rtrim($id_str,',');
        return $id_str;
    }
}