<?php
/**
 * CRM 实现模块
 * CRM的基本功能实现，如客资录入，客资查询等。
 * 所有业务的真正实现全在lib目录下建controller并且访问modle等，供controller层调用
 *
 * ============================================================================
 * 版权所有 2017北京素玄科技，并保留所有权利。
 *
 * 网站地址: http://www.suxuantech.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经允许的情况下，您不能对本系统代码做任何修改 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 如有修改需求，请联系素玄科技有限公司：contact@suxuantech.cn
 * ============================================================================
 * $Author: wangyining<wangyining@suxuantech.cn> 2017-11-23 $
 */
namespace app\crm\lib;
use think\Controller;
use PHPExcel_IOFactory;
use Excel5;
use PHPExcel;
class AddCustomer extends Controller{
    private $ErrorId;
    private $ErrorMessage;

    public function add($data){
        $detail[0]=$data;
        return $detail;
    }

    public function addAll(){

    }

    public function addCustomer($data){
        foreach($data as $k => $v){
            $staff_info['data'] = getStaffInfoById(session('staff_id'));
            $get_brandclass=controller("Customer","lib")->shop(array('shop_name'=>$staff_info['data']['shop_name']));//品牌

            //手机号重复排除
            if($data['customer_tel']){
                $tel=db("c_customerinfo")->where(array('customer_tel'=>$v['customer_tel'],'brandclass'=>$get_brandclass[0]['brandclass']))->find();
                if($tel){
                    $this->ErrorId='1001';
                    $this->ErrorMessage="此客户已录入，手机号重复";

                    //$this->error('此客户已录入，手机号重复','/');
                }
            }
            //微信号重复排除
            if($data['customer_wechat']){
                $tel=db("c_customerinfo")->where(array('customer_wechat'=>$v['customer_wechat'],'brandclass'=>$get_brandclass[0]['brandclass']))->find();
                if($tel){
                    $this->error('此客户已录入，微信号重复','/');
                }
            }
            //QQ号重复排除
            if($data['customer_qq']){
                $tel=db("c_customerinfo")->where(array('customer_qq'=>$v['customer_qq'],'brandclass'=>$get_brandclass[0]['brandclass']))->find();
                if($tel){
                    $this->error('此客户已录入，QQ号重复','/');
                }
            }
            //微博重复排除
            if($data['customer_weibo']){
                $tel=db("c_customerinfo")->where(array('customer_weibo'=>$v['customer_weibo'],'brandclass'=>$get_brandclass[0]['brandclass']))->find();
                if($tel){
                    $this->error('此客户已录入，微博重复','/');
                }
            }

            //消费类型
            if(!empty($v['consultation_type_id'])){
                $consultation_type=controller("Customer","lib")->consumptionType(array('id'=>$v['consultation_type_id']));
                $data[$k]['consultation_type']=$consultation_type[0]['consumption_name'];
            }
            //客户渠道
            if(!empty($v['customer_channel_id'])){
                $customer_channel=controller("Customer","lib")->channel(array('id'=>$v['customer_channel_id']));
                $data[$k]['customer_channel']=$customer_channel[0]['channel_name'];
            }
            //客户来源
            if(!empty($v['customer_cource_id'])){
                $cource=controller("Customer","lib")->source(array('id'=>$v['customer_cource_id']));
                $data[$k]['customer_cource']=$cource[0]['cus_name'];
            }
            //客户意向
            if(!empty($v['customer_intention_id'])){
                $intention=controller("Customer","lib")->intention(array('id'=>$v['customer_intention_id']));
                $data[$k]['customer_intention']=$intention[0]['intention_name'];
            }
            //接单点
            if(!empty($v['customer_orderaddress_id'])){
                $orderaddress=controller("Customer","lib")->orderAddress(array('id'=>$v['customer_orderaddress_id']));
                $data[$k]['customer_orderaddress']=$orderaddress[0]['acceptoraddress_name'];
            }
            //获取方式
            if(!empty($v['access_model_id'])){
                $access_model=controller("Customer","lib")->accessModel(array('id'=>$v['access_model_id']));
                $data[$k]['access_model']=$access_model[0]['accessmodel'];
            }
            //客户分区
            if(!empty($v['customer_area_id'])){
                $customer_area=controller("Customer","lib")->areaSet(array('id'=>$v['customer_area_id']));
                $data[$k]['customer_area']=$customer_area[0]['area_name'];
            }
            //客户地域
            if(!empty($v['customer_region_id'])){
                $customer_region=controller("Customer","lib")->region(array('id'=>$v['customer_region_id']));
                $data[$k]['customer_region']=$customer_region[0]['region_name'];
            }

            $province=controller("Customer","lib")->address(array('id'=>$v['province_id']));//省市区
            $city=controller("Customer","lib")->address(array('id'=>$v['city_id']));
            $county=controller("Customer","lib")->address(array('id'=>$v['county_id']));
            $data[$k]['province']=$province[0]['name'];//省市区
            $data[$k]['city']=$city[0]['name'];
            $data[$k]['county']=$county[0]['name'];

            $data[$k]['dj_staff_id']=session('staff_id');
            $data[$k]['dj_staff']=$staff_info['data']['staffname'];
            $data[$k]['dj_staff_number']=$staff_info['data']['staffnumber'];
            $data[$k]['shop_name']=$staff_info['data']['shop_name'];
            $data[$k]['shop_code']=$staff_info['data']['shop_code'];
            $data[$k]['brandclass']=$get_brandclass[0]['brandclass'];
            $customer_number=controller("Customer","lib")->rand();
            $data[$k]['customer_number']=$customer_number;
            $data[$k]['dj_day']=date('Y-m-d H:i:s',time());
            $data[$k]['creator_day']=date('Y-m-d H:i:s',time());

            //介绍人信息
            if($v['customer_type']==2&&$v['introducer_number']){
                $introducer=controller("Customer","lib")->customerSearch(array('customer_number'=>$v['introducer_number']));
                $data[$k]['introducer_name']=$introducer[0]['customer_name'];
                $data[$k]['introducer_tel']=$introducer[0]['customer_tel'];
            }
            $res=db('CCustomerinfo')->insert($data[$k]);
            $customerId = db('CCustomerinfo')->getLastInsID();
            if($res){
                // $staff_id, $staff_name, $staff_number, $shop_code,$identity, $cid, $old_data, $new_data
                // 数据统计队列
                $listName = 'historyDataList';
                $message = array(
                    'staff_id'     => $data['dj_staff_id'],
                    'staff_name'   => $data['dj_staff'],
                    'staff_number' => $data['dj_staff_number'],
                    'shop_code'    => $data['shop_code'],
                    'identity'     => 'record',
                    'cid'          => $customerId,
                    'old_data'     => array(),
                    'new_data'     => $data
                );
                push_list($listName,$message);
                // action_log($data,'',$customerId,$data['dj_staff_id'],$data['dj_staff']);
                // 系统分配队列
                $listName_fp = 'fpCustomerList';
                $message_fp['receive']= '-1';
                $message_fp['body']   = $customerId;
                push_list($listName_fp, $message_fp);
                return $res;
            }

        }
    }
}
