skumatcher.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/1/9
  6. * Time: 下午1:35
  7. */
  8. class skumatcher
  9. {
  10. private $input_file;
  11. private $good_file;
  12. private $err_file;
  13. private $mod_goods_common;
  14. private $mod_goods;
  15. public function __construct($input)
  16. {
  17. if(file_exists($input) == false) {
  18. throw new Exception("no such file {$input}.");
  19. }
  20. $this->input_file = new SplFileObject($input);
  21. $this->input_file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);
  22. $this->good_file = new SplFileObject("./good.csv","a");
  23. $this->err_file = new SplFileObject("./err.csv","a");
  24. $this->mod_goods_common = Model('goods_common');
  25. $this->mod_goods = Model('goods');
  26. }
  27. public function proc()
  28. {
  29. foreach($this->input_file as $line)
  30. {
  31. $this->parase_one($line[0]);
  32. }
  33. }
  34. private function match_item($name)
  35. {
  36. $items = $this->mod_goods_common->field('goods_commonid,goods_name')->where(array('goods_name' => $name))->select();
  37. if(empty($items)) {
  38. $con['goods_name'] = array('like',"%{$name}%");
  39. $items = $this->mod_goods_common->field('goods_commonid,goods_name')->where($con)->select();
  40. }
  41. if(empty($items)) {
  42. $len = mb_strlen($name);
  43. $name_x = mb_substr($name,0,$len / 2);
  44. $con['goods_name'] = array('like',"%{$name_x}%");
  45. $items = $this->mod_goods_common->field('goods_commonid,goods_name')->where($con)->select();
  46. }
  47. return $items;
  48. }
  49. private function parase_one($name)
  50. {
  51. $items = $this->match_item($name);
  52. if(empty($items)) {
  53. $fields = array($name);
  54. $this->err_file->fputcsv($fields,',');
  55. return;
  56. }
  57. $goods_commonid = $items[0]['goods_commonid'];
  58. $goods = $this->mod_goods->field('*')->where(array('goods_commonid' => $goods_commonid))->limit(false)->select();
  59. if(empty(goods)) {
  60. $fields = array($name);
  61. $this->err_file->fputcsv($fields,',');
  62. return;
  63. }
  64. foreach($goods as $val)
  65. {
  66. $fields = array($val['goods_name'],$val['goods_serial_tm'],$val['num_iid']);
  67. $this->good_file->fputcsv($fields,',');
  68. }
  69. }
  70. }
  71. $matcher = new skumatcher('/Users/stanley-king/Desktop/name.csv');
  72. $matcher->proc();