<?php
/**
  * @method 跑街令统计
  * @author soul
  * @copyright 2019/5/8
  */
class PjlModel extends PjlBase
{
	/**
	  * @method 车架号纠错
	  * @param  $dataArr array  [必填]  pl_vin_error
	  * @author soul
	  * @return
	  */
	public function addVinJiucuo($dataArr, $imgdata = null)
	{
		$vin = LibFc::FormatNum($dataArr['ve_vin']);
		if(strlen($vin) != 17) return LibFc::ReturnData(false, '请提供17位的车架号');
		$hasArr = [];
		if($dataArr['ve_from'] == 'ROBOT'){
			$chk = $this->get('pl_vin_error', ['*'], sprintf(" and ve_from='ROBOT' and ve_userkey='%s' and ve_vin='%s' order by ve_atime desc limit 1", LibFc::Escape($dataArr['ve_userkey']), $vin), true);
			$hasArr = $chk['data'];
		}else{
			//$chk = $this->get('pl_vin_error', ['*'], sprintf(" and ve_from='ROBOT' and ve_userkey='%s' and ve_vin='%s' order by ve_atime desc limit 1", LibFc::Escape($dataArr['ve_userkey']), $vin), true);
		}
		$arr = ['ve_id'=>0, 'check'=>0];
		$dataArr['ve_atime'] = time();
		$again = false;
		if(!empty($hasArr)){
			$arr['ve_id'] = $hasArr['ve_id'];
			$addFlag = false;
			//重复提交限制
			if($hasArr['ve_status'] != 0 || (time() - $hasArr['ve_atime'] > 60)){
				$addFlag = true;
				$again = true;
			}
		}else{
			$addFlag = true;
		}
		if($addFlag){
			$isok = false;
			$YpwVin = new YpwVin();
			$res = $YpwVin->get('stand_model_any', ['sma_id, sma_from, sma_engine'], sprintf(" and sma_vin='%s' AND sma_gxb_code != ''", $vin), true);
			$hasSma = $res['data'];
			if(empty($hasSma['sma_engine']) && LibFc::VinYear($vin) < 2024){
				$res = $YpwVin->catchAnyVinfromZdApi([$vin], 3, 'jiucuo');
				if(!empty($res['data'][$vin]['viz_vin'])){
					$isok = true;
				}
			}
			if(!$isok && $hasSma){
				if($again){//二次纠错，已经无法再纠错
					if($hasSma['sma_engine'] && in_array($hasSma['sma_from'], ['catch', 'manual', 'ZDAPI'])){
						$isok = true;
					}
				}else if($hasSma){
					$isok = true;
				}
			}

			if($isok){
				$dataArr['ve_status'] = 1;
				$dataArr['ve_etime'] = time();
				$dataArr['ve_check'] = json_encode(['uid'=>8,'remark'=>'谢谢您的建议，已处理','time'=>time()],JSON_UNESCAPED_UNICODE);
				$arr['check'] = 1;
			}

			if(!empty($imgdata))
			{
				$OthFileCenter = new OthFileCenter();
				$imgArr = [
					'cate'=>'oth',
					'file'=>[
						'name'=>'vin.jpg',
						'data'=>base64_decode($imgdata),
					]
				];
				$ires = $OthFileCenter->upload($imgArr, 'pjl');
				if($ires['data']['sfile']) $dataArr['ve_imgs'] = $ires['data']['sfile'];
			}
			$res = $this->add('pl_vin_error', $dataArr);
			$arr['ve_id'] = $res['data'];
		}
		if(!$arr['check']) $this->noticeJiucuo($arr['ve_id']);
		return LibFc::ReturnData(true, $arr);
    }
	

	/**
	  * @method 机器人通知客服处理纠错
	  * @param  $veid int  [必填] 纠错id
	  * @author soul
	  * @return
	  */
	public function noticeJiucuo($veid, $uid = 0, $from = 'ROBOT')
	{
		$B2cBase = new B2cBase();
		$where = $uid > 0 ? sprintf(" and au_status = 1 and au_vinjiucuo =1 and au_wxid !='' and au_uid=%d", $uid): " and au_status = 1 and au_vinjiucuo =1 and au_wxid !=''";
		$ures = $B2cBase->get('an_user', ['au_wxid'], $where);
		foreach($ures['data'] as $uv)
		{
			$frommsg = '';
			if($from = 'ROBOT') $frommsg = '【机器人】';
			if($from = 'PJL') $frommsg = '【IM】';
			$notice = [
				'wrn_wrt_id'=>1,
				'wrn_room_id'=>'',
				'wrn_talker_id'=>$uv['au_wxid'],
				'wrn_info'=>json_encode(["act"=>"saymsg","saymsg"=>$frommsg."您有新的车架号纠错需要处理。\r\nhttps://admin.paojd.cn/ypcCar/standModelAnyAdd?ve_id=".$veid]),
				'wrn_atime'=>time()
			];
			$notice['wrn_unikey'] = md5($notice['wrn_wrt_id'].'@'.$notice['wrn_room_id'].'@'.$notice['wrn_talker_id'].'@'.$notice['wrn_info']);
			$res = $B2cBase->get('wx_robot_notice', ['wrn_id'], sprintf(" and wrn_status = 0 and wrn_unikey='%s'", $notice['wrn_unikey']));
			if(empty($res['data'])) $B2cBase->add('wx_robot_notice', $notice);
		}
		return LibFc::ReturnData(true);
	}
}