<?php

/**
 *
 * @method 消息自动回复
 * @author zhengweihua 
 * @copyright 2022-06-06
 */
class B2cRobotAuto extends B2cBase
{   
    /**
     * @param  $postData Array 参数数组
     * @method 保存消息自动回复
     * @author zhengweihua 
     * @copyright 2022-06-06
     */
    public function setRobotAuto($postData)
    {
        $wra_id = (int) $postData['wra_id'];
        $B2cBase = new B2cBase();
        $partern = array('/<p>(([\s\S])*?)<\/p>/','/<div>(([\s\S])*?)<\/div>/','/(([\s\S])*?)<br>/');
        $answer =  strip_tags(preg_replace($partern,"$1\r\n",$postData['wra_answer']),'<a>');
        $data = [
            'wra_wr_id' =>$postData['wra_wr_id'],
            'wra_name' => $postData['wra_name'],
            'wra_style' => $postData['wra_style']?:'txt',
            'wra_answer' => preg_replace('/<a .*?href="(.*?)".*?>/is',"<a href='$1'>",rtrim($answer)),
            'wra_etime' => time(),
        ];
        $this->B2CDb->Begin();
        if($wra_id)
        {
            $where = sprintf(" and wra_id=%d",$wra_id);
            $res = $B2cBase->set('wx_robot_auto',$data,$where);
        }
        else
        {
            $res = $B2cBase->add('wx_robot_auto',$data);
            $wra_id = $res['data'];
        }
        if(!empty($postData['autoword']))
        {   
            $autowordArr = $postData['autoword'];
            //检测数组中重复的值
            for ($i=0; $i < count($autowordArr['waw_word']); $i++) { 
                for ($j=$i; $j < count($autowordArr['waw_word'])-1; $j++) { 
                    if(($autowordArr['waw_word'][$i] == $autowordArr['waw_word'][$j+1]) && ($autowordArr['waw_style'][$i] == $autowordArr['waw_style'][$j+1]))
                    {
                        return LibFc::ReturnData(false,'关键词与匹配类型有重复');
                    }    
                }
            }

            if(!empty($wra_id))
            {
                $where = sprintf(" and waw_wra_id=%d",$wra_id);
                $B2cBase->del('wx_robot_autoword',$where);
            }
           
            foreach ($autowordArr['waw_word'] as $akey => $aitem)
            {
                $data = [
                    'waw_wra_id' => $wra_id,
                    'waw_style'=> $autowordArr['waw_style'][$akey],
                    'waw_word' => $aitem
                ];
                $arr[] = $data;
            } 
            $B2cBase->addAny('wx_robot_autoword',array_keys($autowordArr),$arr);
        }
        $this->B2CDb->End();
        return LibFc::ReturnData(true,true);
    }

    /**
     * @param  $wra_id int 自动回复ID
     * @method 删除自动回复规则
     * @author zhengweihua 
     * @copyright 2022-06-06
     */
    public function delAutoWord($wra_id)
    {
        $B2cBase = new B2cBase();
        $this->B2CDb->Begin();
        $B2cBase->del('wx_robot_auto',sprintf(" and wra_id=%d",(int)$wra_id)); 
        $B2cBase->del('wx_robot_autoword',sprintf(" and waw_wra_id=%d",(int)$wra_id));
        $this->B2CDb->End();
        return LibFc::ReturnData(true,true);

    }
}