<?php
namespace model;
use \model;
/**
 * Class procate
 * @package model
 */
class check extends model
{
	/**
	  * @method 检测数据是否存在
	  * @param $table string [必填]  m_series|m_model|pro_model_link_temp|pro_model_link|pro_model_link2(车型+安装位置等一样，GSP不一样)
	  * @param $dataArr array [必填]  字段信息
	  * @param $id int|string [选填] 主键值
	  * @author soul
	  * @return id 或者 null
	  */
	public function checkExist($tag, $dataArr, $id = 0)
	{
		$cofAllArr = [
			'work_oe_item'=>[
				'table'=>'work_oe_item',
				'key'=>'woi_id',
				'fileds'=>['woi_oe_format','woi_opuser']
			],
			'oe_numidx'=>[
				'table'=>'oe_numidx',
				'key'=>'on_id',
				'fileds'=>['on_pf_id','on_formart']
			],
			'oe_model_link'=>[
				'table'=>'oe_model_link',
				'key'=>'oml_id',
				'fileds'=>['oml_oe_id','oml_mm_id']
			],
			'oe_model_cn_link'=>[
				'table'=>'oe_model_cn_link',
				'key'=>'oml_id',
				'fileds'=>['oml_oe_id','oml_mod3_id']
			],
			'm_series'=>[
				'table'=>'m_series',
				//id对应的字段
				'key'=>'ms_id',
				//去重的字段
				'fileds'=>['ms_mk_id','ms_name']
			],
			'm_model_change'=>[
				'table'=>'m_model_change',
				'key'=>'mm_id',
				'fileds'=>['mm_mk_id','mm_ms_id','mm_year_from','mm_month_from','mm_year_to','mm_month_to','mm_name','mm_serie','mm_fuel','mm_disp','mm_chassis','mm_engine','mm_turbo','mm_kw','mm_hp','mm_cylinders','mm_values','mm_drive_type','mm_body','mm_doors','mm_platform','mm_vehicletype','mm_others1','mm_others2'],
				'othwhere'=>['mmc_act'=>'del']
			],
			'pro_model_link_temp'=>[
				'table'=>'pro_model_link_temp',
				'key'=>'pmlt_id',
				'fileds'=>['pmlt_status','pmlt_p_id','pmlt_mm_id','pmlt_sample','pmlt_sign_type','pmlt_innum','pmlt_outnum','pmlt_pos0','pmlt_pos1','pmlt_pos6','pmlt_pos2','pmlt_pos3','pmlt_pos4','pmlt_pos5','pmlt_node1']
			],
			'pro_model_link'=>[
				'table'=>'pro_model_link',
				'key'=>'pml_id',
				'fileds'=>['pml_p_id','pml_mm_id','pml_pos0','pml_pos1', 'pml_pos6','pml_pos2','pml_pos3','pml_pos4','pml_node1']
			],
			'pro_model_link_merge'=>[
				'table'=>'pro_model_link',
				'key'=>'pml_id',
				'fileds'=>['pml_p_id','pml_mm_id','pml_pos0','pml_pos1', 'pml_pos6','pml_pos2','pml_pos3','pml_pos4','pml_pos5','pml_node2','pml_node3','pml_node4']
			],
			//车型+安装位置等一样，GSP不一样
			'pro_model_link2'=>[
				'table'=>'pro_model_link',
				'key'=>'pml_id',
				'fileds'=>['pml_mm_id','pml_pos0','pml_pos1', 'pml_pos6','pml_pos2','pml_pos3','pml_pos4','pml_pos5','pml_node1']
			],
			'pro'=>[
				'table'=>'pro',
				'key'=>'p_id',
				'fileds'=>['p_innum']
			],
			'm_model'=>[
				'table'=>'m_model',
				'key'=>'mm_id',
				'fileds'=>['mm_mk_id','mm_ms_id','mm_segment','mm_year_from','mm_month_from','mm_year_to','mm_month_to','name','mm_serie','mm_fuel','mm_disp','mm_chassis','mm_engine','mm_turbo','mm_kw','mm_hp','mm_cylinders','mm_values','mm_drive_type','mm_body','mm_doors','mm_platform','mm_vehicletype','mm_others1','mm_others2']
			],
			'pro_model_change'=>[
				'table'=>'pro_model_change',
				'key'=>'pmc_id',
				'fileds'=>['pml_p_id','pml_mm_id','pml_pos0','pml_pos1', 'pml_pos6','pml_pos2','pml_pos3','pml_pos4','pml_node1'],
				'othwhere'=>['pmc_act'=>'del']
			],
			'sample_numidx'=>[
				'table'=>'sample_numidx',
				'key'=>'sn_id',
				'fileds'=>['sn_s_id','sn_pro_factory','sn_numfomart'],
			],
			'sample_model_link'=>[
				'table'=>'sample_model_link',
				'key'=>'sml_s_id',
				'fileds'=>['sml_s_id','sml_mm_id','sml_pos0','sml_pos1','sml_pos2','sml_pos3','sml_pos4','sml_pos5','sml_pos6','sml_node1'],
			],
			'sample_model_txt'=>[
				'table'=>'sample_model_txt',
				'key'=>'smt_id',
				'fileds'=>['smt_s_id','sm_make','sm_series'],
			],
			'sample_ccu'=>[
				'table'=>'sample_ccu',
				'key'=>'sccu_code',
				'fileds'=>['sccu_code_format'],
			],
			'pro_model_cn_link'=>[
				'table'=>'pro_model_cn_link',
				'key'=>'pml_id',
				'fileds'=>['pml_p_id','pml_mod3_id']
			],
			'pro_cate'=>[
				'table'=>'pro_cate',
				'key'=>'id',
				'fileds'=>['parent_id','cn_name','en_name']
			],
			'm_make'=>[
				'table'=>'m_make',
				'key'=>'mk_id',
				'fileds'=>['name']
			],
			'pro_fatherson'=>[
				'table'=>'pro_fatherson',
				'key'=>'pf_id',
				'fileds'=>['pf_father_pid', 'pf_son_pid']
			],
			'pro_exchange'=>[
				'table'=>'pro_exchange',
				'key'=>'pe_id',
				'fileds'=>['pe_type', 'pe_p_id1', 'pe_p_id2']
			],
			'pro_exchange_model_numidx'=>[
				'table'=>'pro_exchange_model_numidx',
				'key'=>'pemn_id',
				'fileds'=>['pemn_p_id1', 'pemn_p_id2']
			],
			'pro_pair'=>[
				'table'=>'pro_pair',
				'key'=>'pp_id',
				'fileds'=>['pp_p_id1', 'pp_p_id2']
			],	
			'pro_merge'=>[
				'table'=>'pro_merge',
				'key'=>'pch_id',
				'fileds'=>['pch_p_id_from', 'pch_p_id_to'],
				'othwhere'=>['pch_status'=>[0,1],'pch_isback'=>[0]]
			],
			'pro_tousu'=>[
				'table'=>'pro_tousu',
				'key'=>'pt_id',
				'fileds'=>['pt_p_id', 'pt_xiangci']
			],
			'oppo_brand_dimension'=>[
				'table'=>'oppo_brand_dimension',
				'key'=>'obd_id',
				'fileds'=>['obd_name']
			],
			'oppo_brand'=>[
				'table'=>'oppo_brand',
				'key'=>'od_id',
				'fileds'=>['od_name']
			],
			'pro_supplier'=>[
				'table'=>'pro_supplier',
				'key'=>'psu_id',
				'fileds'=>['psu_p_id','psu_sup_id','psu_pb_id']
			],
			'supplier_pro'=>[
				'table'=>'supplier_pro',
				'key'=>'spr_id',
				'fileds'=>['spr_cat_id','spr_sup_id','spr_format','spr_pf_id','spr_material','spr_area']
			],
			'pro_bom_assemble'=>[
				'table'=>'pro_bom_assemble',
				'key'=>'pba_id',
				'fileds'=>['pba_p_id','pba_name']
			],
			'pro_bom_pack'=>[
				'table'=>'pro_bom_pack',
				'key'=>'pbp_id',
				'fileds'=>['pbp_p_id','pbp_name']
			],
			'wdlist_title'=>[
				'table'=>'work_data_list',
				'key'=>'wdlist_id',
				'fileds'=>['wdlist_title']
			],
			'was_title'=>[
				'table'=>'work_ask',
				'key'=>'was_id',
				'fileds'=>['was_title']
			],
			'woe_title'=>[
				'table'=>'work_oe',
				'key'=>'woe_id',
				'fileds'=>['woe_title']
			],
			'competitor_price'=>[
				'table'=>'competitor_price',
				'key'=>'cop_id',
				'fileds'=>['cop_cor_id','cop_cat_id','cop_cop_num_format']
			],
			'oe_link'=>[
				'table'=>'oe_link',
				'key'=>'ol_id',
				'fileds'=>['ol_pf_id1','ol_number1','ol_pf_id2','ol_number2']
			],
			'project_item'=>[
				'table'=>'project_item',
				'key'=>'pi_id',
				'fileds'=>['pi_pr_id','pi_pf_id','pi_format','pi_cat_id']
			],
		];
		if(empty($cofAllArr[$tag])) exitJson('去重规则尚未配置');
		$cofArr = $cofAllArr[$tag];
		$table = $cofArr['table'];
		$oldArr = [];
		if($id > 0)
			$oldArr = $this->db->get($table, $cofArr['fileds'], [$cofArr['key']=>$id]);
		$chkwhere = [];
		foreach($cofArr['fileds'] as $filed)
		{
			if(isset($dataArr[$filed]))
				$chkwhere[$filed] = $dataArr[$filed];
			else if(isset($oldArr[$filed]))
				$chkwhere[$filed] = $oldArr[$filed];
		}
		if(empty($chkwhere)) exitJson('去重条件异常');
		if(!empty($cofArr['othwhere'])) $chkwhere = array_merge($chkwhere, $cofArr['othwhere']);
		if($id > 0) $chkwhere[$cofArr['key'].'[!]'] = $id;
		return $this->db->get($table, $cofArr['key'], $chkwhere);
	}


	/*
	新入的 内部编码 
	1 不能是系统已存在 的内部编码
	2 不能是系统已存在 的外部编码

	新入的 外部编码
	1 不能是系统已存在 的内部编码
	2 不能是系统已存在 的外部编码
	*/
	public function checkExistPid($number)
	{
		$where = [
			"OR" => [
				'p_innum'=>$number,
				'p_outnum'=>$number
			]
		];
		return $this->db->get('pro', 'p_id', $where);
	}

	
	//获取对应表的权限并返回条件数组
	public function getPowerWhere($tag, $rulecode)
	{
		$cofAllArr = [
			'pro_model_link'=>[
				'table'=>'pro_model_link',
				'power'=>[
					'cate'=>'p_cate'
				]
			],
			'pro_model_link_temp'=>[
				'table'=>'pro_model_link_temp',
				'power'=>[
					'cate'=>'p_cate'
				]
			],
			'pro_numidx'=>[
				'table'=>'pro_numidx',
				'power'=>[
					'cate'=>'p_cate'
				]
			],
			'pro_numidx_temp'=>[
				'table'=>'pro_numidx_temp',
				'power'=>[
					'cate'=>'p_cate'
				]
			],
			'pro'=>[
				'table'=>'pro',
				'power'=>[
					'cate'=>'p_cate'
				]
			],
			'pro_merge'=>[
				'table'=>'pro_merge',
				'power'=>[
					'cate'=>'prof.p_cate'
				]
			]
		];
		if(empty($cofAllArr[$tag])) exitJson('规则尚未配置');
		$cofArr = $cofAllArr[$tag];
		$table = $cofArr['table'];
		$ruleWhere = [];
		foreach($cofArr['power'] as $pok=>$pov)
		{
			switch($pok)
			{
				case 'cate':
					$ids = \p::cates($rulecode);
					if(!empty($ids)) 
						$ruleWhere['AND #RULE'][$pov] = explode(',', $ids);
				break;
			}
		}
		return $ruleWhere;
	}

	//检测小类权限检测
	public function checkProRuleCate($pidArr, $rulecode, $act = 'view', $type = 'pro')
	{
		$arr = ['ok'=>$pidArr, 'no'=>[]];
		if($act == 'add') $ids = \p::catesAdd($rulecode);
		else if($act == 'edit') $ids = \p::catesEdit($rulecode);
		else if($act == 'delete') $ids = \p::catesDelete($rulecode);
		else if($act == 'import') $ids = \p::catesImport($rulecode);
		else if($act == 'export') $ids = \p::catesExport($rulecode);
		else if($act == 'Done') $ids = \p::catesDone($rulecode);
		else if($act == 'check1') $ids = \p::catesCheck1($rulecode);
		else if($act == 'check2') $ids = \p::catesCheck2($rulecode);
		else if($act == 'check3') $ids = \p::catesCheck3($rulecode);
		else $ids = \p::cates($rulecode);

		if(empty($ids) || empty($pidArr)) return $arr;

		$arr['ok'] = [];
		$pidArr = array_unique($pidArr);
		$rows = $this->db->select('pro', 'p_id', ['p_id'=>$pidArr, 'p_cate'=>explode(',', $ids)]);
		$arr['ok'] = $rows;
		$arr['no'] = array_diff($pidArr, $arr['ok']);
		return $arr;
	}
}