<commit_before><?php

namespace App\Export\CTILayout;

use App\Model\State;
use App\Utility\Chinghwa\Database\Query\Processors\Processor;
use App\Utility\Chinghwa\Helper\Excel\ExcelHelper;
use App\Utility\Chinghwa\ORM\CTI\Campaign;
use App\Utility\Chinghwa\ORM\CTI\CampaignCallList;
use App\Utility\Chinghwa\ORM\ERP\HRS_Employee;
use Input;

class FlapExportHandler implements \Maatwebsite\Excel\Files\ExportHandler 
{
    /**
     * ExcelHelper::rmi('Z') === 25
     * 
     * @param  App\Export\CTILayout\Export $export
     * @return App\Export\CTILayout\Export $export
     */
    public function handle($export)
    {
        return 1 === Input::get('is_split') ? $this->split($export)->export() : $this->single($export)->export();
    }

    protected function single($export)
    {        
        $callLists = CampaignCallList::fetchCtiRes([
            'agentCD'    => !empty(Input::get('code')) ? explode(',', trim(Input::get('code'))) : [], 
            'sourceCD'   => !empty(Input::get('source_cd')) ? explode(',', trim(Input::get('source_cd'))) : [], 
            'campaignCD' => !empty(Input::get('campaign_cd')) ? explode(',', trim(Input::get('campaign_cd'))) : [],
            'assignDate' => trim(Input::get('assign_date'))
        ]);

        $sheetName = 'export';

        $export->sheet($sheetName, $this->getSheetCallback($callLists));           

        return $export;
    }

    protected function split($export)
    {
        $codes = explode(',', trim(Input::get('code')));  
        $campaignCDs = explode(',', trim(Input::get('campaign_cd')));   
        
        $campaigns = $this->getCampaigns();

        foreach ($campaigns as $campaign) {
            $callLists = CampaignCallList::find([
                'agentCD' => $codes, 
                'campaignCD' => array_get($campaign, 'CampaignCD')
            ]);

            if (!$callLists) {
                continue;
            }

            $sheetName = trim(array_get($campaign, 'DefSchemaCD')) . '##' . trim(array_get($campaign, 'CampaignCD'));            

            $export->sheet($sheetName, $this->getSheetCallback($callLists));           
        }

        return $export;
    }

    protected function getSheetCallback($callLists)
    {
        return function ($sheet) use ($callLists) {
            $sheet->appendRow($this->getExportHead());

            foreach ($callLists as $calllist) {                   
                $member = array_get($this->getCTILayoutData(array_get($calllist, 'SourceCD')), 0);

                if (NULL === $member || !$this->inCorps($member)) {
                    continue;
                }                

                $hd = $this->getHospitalAndPeriod([array_get($member, '備註'), array_get($member, '備註1'), array_get($member, '備註2')]);

                $sheet->appendRow($this->getFilterMember($member, $hd, $calllist));
            }     
        };
    }

    protected function inCorps(array $member)
    {
        $corps = Input::get('corps');

        if (empty($corps)) {
            return true;
        }

        return in_array(array_get($member, '部門'), $corps);
    }

    protected function getFilterMember($member, $hd, $calllist)
    {
        $this->replaceWithNewCityState($member);

        return [
            array_get($member, '會員代號'),
            array_get($member, '會員姓名'),
            array_get($member, '性別'),
            array_get($member, '生日'),
            array_get($member, '身份證號'), 
            array_get($member, '連絡電話'), 
            array_get($member, '公司電話'), 
            array_get($member, '手機號碼'),
            array_get($member, '縣市'), 
            array_get($member, '區'),
            array_get($member, '郵遞區號'),
            array_get($member, '地址'),
            array_get($member, 'e-mail'),             
            array_get($calllist, 'AgentCD'),    //array_get($member, '開發人代號'),    
            array_get($calllist, 'AgentName'), //array_get($member, '開發人姓名'),
            array_get($member, '會員類別代號'), 
            array_get($member, '會員類別名稱'), 
            array_get($member, '區別代號'),
            array_get($member, '區別名稱'), 
            array_get($member, '首次購物金額'),
            array_get($member, '首次購物日'), 
            array_get($member, '最後購物金額'),
            array_get($member, '最後購物日'), 
            array_get($member, '累積購物金額'),
            array_get($member, '累積紅利點數'), 
            array_get($member, '輔翼會員參數'),
            array_get($hd, 'period'),
            array_get($hd, 'hospital')<commit_msg>Fix cti null, get empty flap data error<commit_after><?php

namespace App\Export\CTILayout;

use App\Model\State;
use App\Utility\Chinghwa\Database\Query\Processors\Processor;
use App\Utility\Chinghwa\Helper\Excel\ExcelHelper;
use App\Utility\Chinghwa\ORM\CTI\Campaign;
use App\Utility\Chinghwa\ORM\CTI\CampaignCallList;
use App\Utility\Chinghwa\ORM\ERP\HRS_Employee;
use Input;

class FlapExportHandler implements \Maatwebsite\Excel\Files\ExportHandler 
{
    /**
     * ExcelHelper::rmi('Z') === 25
     * 
     * @param  App\Export\CTILayout\Export $export
     * @return App\Export\CTILayout\Export $export
     */
    public function handle($export)
    {
        return 1 === Input::get('is_split') ? $this->split($export)->export() : $this->single($export)->export();
    }

    protected function single($export)
    {        
        $callLists = CampaignCallList::fetchCtiRes([
            'agentCD'    => !empty(Input::get('code')) ? explode(',', trim(Input::get('code'))) : [], 
            'sourceCD'   => !empty(Input::get('source_cd')) ? explode(',', trim(Input::get('source_cd'))) : [], 
            'campaignCD' => !empty(Input::get('campaign_cd')) ? explode(',', trim(Input::get('campaign_cd'))) : [],
            'assignDate' => trim(Input::get('assign_date'))
        ]);

        $sheetName = 'export';

        $export->sheet($sheetName, $this->getSheetCallback($callLists));           

        return $export;
    }

    protected function split($export)
    {
        $codes = explode(',', trim(Input::get('code')));  
        $campaignCDs = explode(',', trim(Input::get('campaign_cd')));   
        
        $campaigns = $this->getCampaigns();

        foreach ($campaigns as $campaign) {
            $callLists = CampaignCallList::find([
                'agentCD' => $codes, 
                'campaignCD' => array_get($campaign, 'CampaignCD')
            ]);

            if (!$callLists) {
                continue;
            }

            $sheetName = trim(array_get($campaign, 'DefSchemaCD')) . '##' . trim(array_get($campaign, 'CampaignCD'));            

            $export->sheet($sheetName, $this->getSheetCallback($callLists));           
        }

        return $export;
    }

    protected function getSheetCallback($callLists)
    {
        return function ($sheet) use ($callLists) {
            $sheet->appendRow($this->getExportHead());

            if (empty($calllists)) {
                $member = array_get($this->getCTILayoutData(Input::get('source_cd')), 0);

                if (NULL === $member || !$this->inCorps($member)) {
                    return;
                } 

                $hd = $this->getHospitalAndPeriod([array_get($member, '備註'), array_get($member, '備註1'), array_get($member, '備註2')]);

                $sheet->appendRow($this->getFilterMember($member, $hd, []));
            }

            foreach ($callLists as $calllist) {                   
                $member = array_get($this->getCTILayoutData(array_get($calllist, 'SourceCD')), 0);

                if (NULL === $member || !$this->inCorps($member)) {
                    continue;
                }                

                $hd = $this->getHospitalAndPeriod([array_get($member, '備註'), array_get($member, '備註1'), array_get($member, '備註2')]);

                $sheet->appendRow($this->getFilterMember($member, $hd, $calllist));
            }     
        };
    }

    protected function inCorps(array $member)
    {
        $corps = Input::get('corps');

        if (empty($corps)) {
            return true;
        }

        return in_array(array_get($member, '部門'), $corps);
    }

    protected function getFilterMember($member, $hd, $calllist)
    {
        $this->replaceWithNewCityState($member);

        return [
            array_get($member, '會員代號'),
            array_get($member, '會員姓名'),
            array_get($member, '性別'),
            array_get($member, '生日'),
            array_get($member, '身份證號'), 
            array_get($member, '連絡電話'), 
            array_get($member, '公司電話'), 
            array_get($member, '手機號碼'),
            array_get($member, '縣市'), 
            array_get($member, '區'),
            array_get($member, '郵遞區號'),
            array_get($member, '地址'),
            array_get($member, 'e-mail'),             
            array_get($calllist, 'AgentCD', array_get($member, '開發人代號')),    //array_get($member, '開發人代號'),    
            array_get($calllist, 'AgentName', array_get($member, '開發人姓名')), //array_get($member, '開發人姓名'),
            array_get($member, '會員類別代號'), 
            array_get($member, '會員類別名稱'), 
            array_get($member, '區別代號'),
            array_get($member, '區別名稱'), 
            array_get($member, '首次購物金額'),
            array_get($member, '首次購物日'), 
            array_get($member, '最後購物金額'),
            array_get($member, '最後購物日'), 
            array_get($member, '累積購物金額'),
            array_get($member, '累積紅利點數'), 
            array_get($member, '輔翼會員參數'),
            array_get($hd, 'period'),
            array_get($hd, 'hospital')
{'fraction_non_alphanumeric': 0.16931770615320219, 'fraction_numerical': 0.0015696944328170784, 'mean_word_length': 2.1920507682030728, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2389578', 'n_tokens_mistral': 3167, 'n_tokens_neox': 3105, 'n_words': 447}