<?php
/**
 * CRM 基本模块
 * 默认接口方法，此方法内定议基础框架所需要的方法，用于验证模块合法性，模块版本，对接基础架构中需要的方法
 * 
 * ============================================================================
 * 版权所有 2017北京素玄科技，并保留所有权利。
 * 
 * 网站地址: http://www.suxuantech.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经允许的情况下，您不能对本系统代码做任何修改 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 如有修改需求，请联系素玄科技有限公司：contact@suxuantech.cn
 * ============================================================================
 * $Author: songdemei<songdemei@suxuantech.cn> 2017-10-17 $
 */
namespace app\crm\controller;
use think\Controller;

class Repair extends controller {
    public function test(){
        var_dump(md5(''));
        echo '<br />';
        $arr = [];
        var_dump(md5(json_encode($arr)));
    }
    // 转档修复 main
    public function repairMain(){
        set_time_limit(0);
        do {
            $main = db('crm_c_customerinfo_back')
                ->where('c_unique','null')
                ->field('customer_name,customer_tel,customer_wechat,customer_birthday,customer_idnumber,mate_name,mate_tel,mate_wechat,mate_birthday,mate_idnumber,home_phone,email,wedding_date,ready_baby_day,baby_birthday,baby_name,baby_sex,baby_address,county_id,county,customer_address,brandclass_id,brandclass,dj_day')
                ->group('customer_tel')
                ->order('id desc')
                ->limit('100')
                ->select();
            $insertData = [];
            foreach ($main as $key => $value) {
                $c_unique = db('crm_c_customer_main')
                    ->where('wphone',$value['customer_tel'])
                    ->value('c_unique');
                if ($c_unique) {
                    continue;
                }
                $temp['wname']        = $value['customer_name'];
                $temp['wphone']       = $value['customer_tel'];
                $temp['wwechat']      = $value['customer_wechat'];
                $temp['wbirthday']    = $value['customer_birthday'];
                $temp['wcardnumber']  = $value['customer_idnumber'];
                $temp['mname']        = $value['mate_name'];
                $temp['mphone']       = $value['mate_tel'];
                $temp['mwechat']      = $value['mate_wechat'];
                $temp['mbirthday']    = $value['mate_birthday'];
                $temp['mcardnumber']  = $value['mate_idnumber'];
                $temp['homephone']    = $value['home_phone'];
                $temp['email']        = $value['email'];
                $temp['marryday']     = $value['wedding_date'];
                $temp['readybabyday'] = $value['ready_baby_day'];
                $temp['babyname']     = $value['baby_name'];
                $temp['babybirthday'] = $value['baby_birthday'];
                $temp['babysex']      = $value['baby_sex'];
                $temp['babyhospital'] = $value['baby_address'];
                $temp['street']       = $value['county'];
                $temp['street_id']    = $value['county_id'];
                $temp['address']      = $value['customer_address'];
                $temp['brandclass_id']= $value['brandclass_id'];
                $temp['brandclass']   = $value['brandclass'];
                if ($value['dj_day']) {
                    $temp['c_unique']     = 'sx_'.str_shuffle(strtotime($value['dj_day']).generate_code());
                }else{
                    $temp['c_unique']     = 'sx_'.str_shuffle(time().generate_code());
                }
                $temp['create_time']  = $value['dj_day'];
                array_push($insertData, $temp);
                $res = db('crm_c_customerinfo_back')
                    ->where('customer_tel',$value['customer_tel'])
                    ->update(['c_unique' => $temp['c_unique']]);
                echo $res.'：'.$value['customer_tel'].'==>'.$temp['c_unique'].'<br />';
                unset($res);
                unset($temp);
            }
            $num = 1;
            $allnum = count($insertData);
            // var_dump($allnum);
            echo '<br />';
            foreach ($insertData as $key => $value) {
                $inData[$num] = $value;
                if($num>=50||$key == $allnum-1){
                    $res = db('crm_c_customer_main')->insertAll($inData);
                    echo $res.'：'.$num.'<br />';
                    unset($inData);
                    unset($res);
                    $num = 0;
                }
                $num++;
            }
            $mCount = db('crm_c_customerinfo_back')
                ->where('c_unique','null')
                ->count();
            echo $mCount.'<br />';
        } while ($mCount > 0);
    }
    // 转档修复 main 补充 erp订单表中的 crm_c_unique, crm_customer_number
    public function repairErpOrder(){
        set_time_limit(0);
        echo '开始==>'.microtime(true).'<br />';
        do {
            $oData = db('c_customerorder')->alias('o')
                ->join('c_customerinfo c','c.customerid=o.customerid', 'left')
                ->where('o.crm_customer_number|crm_c_unique', 'null')
                ->field('o.id,o.targetdate,o.shop_code,o.shop_name,c.wphone')
                ->limit('100')
                ->select();
            // echo db()->getLastSql();
            // exit;
            foreach ($oData as $key => $value) {
                echo $key.'s==>'.microtime(true).'<br />';
                // $cInfo = db('crm_c_customerinfo')
                $cInfo = db('crm_c_customerinfo_back')
                    ->where('customer_tel',$value['wphone'])
                    ->field('customer_number,c_unique')
                    ->order('id desc')
                    ->find();
                // echo db()->getLastSql();die;
                // 如果crm里有这个手机号
                if ($cInfo) {
                    db('c_customerorder')
                        ->where('id',$value['id'])
                        ->update([
                            'crm_customer_number'=>$cInfo['customer_number'],
                            'crm_c_unique'=>$cInfo['c_unique']
                        ]);
                }else{
                    // 新增客户
                    $mInsert['wname']        = $value['wname'];
                    $mInsert['wphone']       = $value['wphone'];
                    $mInsert['wwechat']      = $value['wwechat'];
                    $mInsert['wbirthday']    = $value['wbirthdate'];
                    $mInsert['wcardnumber']  = $value['wid'];
                    $mInsert['mname']        = $value['mname'];
                    $mInsert['mphone']       = $value['mphone'];
                    $mInsert['mwechat']      = $value['mwechat'];
                    $mInsert['mbirthday']    = $value['mbirthdate'];
                    $mInsert['mcardnumber']  = $value['mid'];
                    // $mInsert['homephone']    = $value['home_phone'];
                    $mInsert['email']        = $value['email'];
                    $mInsert['marryday']     = $value['weddingdate'];
                    $mInsert['readybabyday'] = $value['yc_day'];
                    $mInsert['babyname']     = $value['bbname'];
                    $mInsert['babybirthday'] = $value['bbithdate'];
                    $mInsert['babysex']      = $value['bbsex'];
                    $mInsert['babyhospital'] = $value['bb_hospital'];
                    $mInsert['street']       = $value['address_qu'];
                    // $mInsert['street_id']    = $value['county_id'];
                    $mInsert['address']      = $value['address'];
                    $mInsert['brandclass_id']= 1;
                    $mInsert['brandclass']   = '非常六加一儿童摄影';
                    if ($value['targetdate']) {
                        $mInsert['c_unique']     = 'sx_'.str_shuffle(strtotime($value['targetdate']).generate_code());
                    }else{
                        $mInsert['c_unique']     = 'sx_'.str_shuffle(time().generate_code());
                    }
                    $mInsert['create_time']  = $value['targetdate'] ? date('Y-m-d H:i:s',strtotime($value['targetdate'])) : '';
                    db('crm_c_customer_main_back')->insert($mInsert);

                    // 新增客资获取
                    $cInsert['c_unique']         = $mInsert['c_unique'];
                    $cInsert['customer_name']    = $value['mname'];
                    $cInsert['customer_tel']     = $value['mphone'];
                    $cInsert['customer_wechat']  = $value['mwechat'];
                    $cInsert['mate_name']        = $value['wname'];
                    $cInsert['mate_tel']         = $value['wphone'];
                    $cInsert['mate_wechat']      = $value['wwechat'];
                    $cInsert['customer_address'] = $value['address'];
                    $cInsert['customer_birthday']= $value['mbirthdate'];
                    $cInsert['mate_birthday']    = $value['wbirthdate'];
                    $cInsert['customer_idnumber']= $value['mid'];
                    $cInsert['mate_idnumber']    = $value['wid'];
                    $cInsert['baby_name']        = $value['bbname'];
                    $cInsert['baby_birthday']    = $value['bbithdate'];
                    $cInsert['baby_sex']         = $value['bbsex'];
                    $cInsert['email']            = $value['email'];
                    $cInsert['ready_baby_day']   = $value['yc_day'];
                    $cInsert['baby_address']     = $value['bb_hospital'];
                    $cInsert['brandclass_id']    = 1;
                    $cInsert['brandclass']       = '非常六加一儿童摄影';
                    // db('crm_c_customerinfo')->insert($cInsert);
                    db('crm_c_customerinfo_back')->insert($cInsert);
                }
                echo $key.'e==>'.microtime(true).'<br />';
            }
            unset($oData);
            echo '结束==>'.microtime(true).'<br />';
            // 计数
            $oNum = db('c_customerorder')->alias('o')
                ->join('c_customerinfo c','c.customerid=o.customerid', 'left')
                ->where('o.crm_customer_number|crm_c_unique', 'null')
                ->count();
            var_dump($oNum);
        } while ( $oNum > 0);

        

    }


}