ItemController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace console\controllers;
  3. use biz\item\classes\ItemClass;
  4. use biz\item\classes\ItemClassClass;
  5. use biz\item\classes\PtItemClass;
  6. use biz\item\classes\PtItemClassClass;
  7. use biz\item\classes\UnitClass;
  8. use common\components\stringUtil;
  9. use yii\console\Controller;
  10. use Yii;
  11. use bizGhs\product\classes\ProductClass;
  12. /**
  13. * 花材初始化 ssh 2021.2.18
  14. */
  15. class ItemController extends Controller
  16. {
  17. //花材初始化 ./yii item/init
  18. public function actionInit()
  19. {
  20. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  21. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  22. $dir = Yii::$app->basePath;
  23. $filename = $dir . '/goods.xls';
  24. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  25. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  26. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  27. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  28. $highestRow = $sheet->getHighestRow();//取得总行数
  29. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  30. $changeData = [];
  31. for ($j = 2; $j <= $highestRow; $j++) {
  32. $str = '';
  33. for ($k = 'A'; $k <= $highestColumn; $k++) {
  34. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  35. }
  36. //explode:函数把字符串分割为数组。
  37. $string = explode("\\", $str);
  38. array_pop($string);
  39. array_push($changeData, $string);
  40. }
  41. if (!empty($changeData)) {
  42. $classList = ItemClassClass::getAllByCondition([], null, '*', 'name');
  43. $unitList = UnitClass::getAllByCondition([], null, '*', 'name');
  44. foreach ($changeData as $key => $item) {
  45. $name = $item[1];
  46. $alias = $item[2];
  47. $className = $item[3];
  48. $classId = isset($classList[$className]['id']) ? $classList[$className]['id'] : 0;
  49. $py = strtolower($item[4]);
  50. $big = $item[5] ?? '';
  51. $big = $big == '枝' ? '支' : $big;
  52. $bigId = isset($unitList[$big]['id']) ? $unitList[$big]['id'] : 0;
  53. $small = $item[6] ?? '';
  54. $small = $small == '枝' ? '支' : $small;
  55. $smallId = isset($unitList[$small]['id']) ? $unitList[$small]['id'] : 0;
  56. $ratio = isset($item[7]) && !empty($item[7]) ? $item[7] : 0;
  57. if(empty($name)){
  58. continue;
  59. }
  60. $data = [
  61. 'name'=>$name,
  62. 'alias'=>$alias,
  63. 'py'=>$py,
  64. 'classId'=>$classId,
  65. 'unitNum'=>$ratio,
  66. 'bigUnit'=>$bigId,
  67. 'smallUnit'=>$smallId,
  68. ];
  69. print_r($data);
  70. ItemClass::add($data);
  71. }
  72. }
  73. }
  74. //命令: ./yii item/sync name,cover 同步name 和cover
  75. //同步item, ptItem更新,同步到 ghsItem, ghsProduct, 同步字段(name,alias,py,cover,ratio,cover等)
  76. public function actionSync($field)
  77. {
  78. $allFiled = ['name','alias','cover','py','ratio','weight','bigUnit','smallUnit','bigUnitId','smallUnitId','stockWarning'];
  79. $fields = explode(",",$field);
  80. $syncFields = array_intersect($allFiled,$fields);
  81. if(empty($syncFields)){
  82. echo "参数不支持";die;
  83. }
  84. $item = PtItemClass::getAllList("id","");
  85. $itemIds = [];
  86. foreach ($item as $v){
  87. $itemIds[] = $v['id'];
  88. }
  89. if(!empty($itemIds)){
  90. foreach ($itemIds as $v){
  91. $itemInfo = PtItemClass::getById($v);
  92. $data =[];
  93. foreach ($syncFields as $f){
  94. $data[$f] = $itemInfo[$f];
  95. }
  96. ProductClass::updateByCondition(['itemId'=>$v],$data);
  97. \bizGhs\item\classes\ItemClass::updateByCondition(['itemId'=>$v],$data);
  98. }
  99. }
  100. echo "done";
  101. }
  102. //平台花材初始化导入 linqh 2021-05-08
  103. //命令: ./yii item/init-pt-item
  104. public function actionInitPtItem($clear=false)
  105. {
  106. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  107. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  108. $dir = Yii::$app->basePath;
  109. $filename = $dir . '/item.xls';
  110. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  111. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  112. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  113. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  114. $highestRow = $sheet->getHighestRow();//取得总行数
  115. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  116. $classData = [];
  117. $itemData = [];
  118. $classIdMap = [];
  119. for ($j = 2; $j <= $highestRow; $j++) {
  120. $str = '';
  121. for ($k = 'A'; $k <= $highestColumn; $k++) {
  122. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  123. }
  124. //explode:函数把字符串分割为数组。
  125. $string = explode("\\", $str);
  126. array_pop($string);
  127. $itemName = $string[0]??''; //花材名称
  128. $className = $string[1]??''; //分类名称
  129. $weight = $string[2]??0; //重量 kg
  130. $bigUnit = $string[3]??''; //大单位名称
  131. $smallUnit = $string[4]??''; //小单位名称
  132. $ratio = $string[5]??0; //比例
  133. $cost = $string[6]??0;//成本价
  134. $addPrice = $string[7]??0; //加价
  135. $stockWarning = $string[8]??0;
  136. $alias = $string[9]??'';
  137. $cover = $string[10]??'';
  138. $inTurn = $string[11]??0;
  139. if($itemName){
  140. $py = stringUtil::py($itemName);
  141. $item = [
  142. 'name'=>$itemName,
  143. 'alias'=>$alias,
  144. 'py'=>$py,
  145. 'cover'=>$cover,
  146. 'classId'=>0,
  147. 'ratio'=>$ratio,
  148. 'weight'=>$weight,
  149. 'stockWarning'=>$stockWarning,
  150. 'cost'=>$cost,
  151. 'addPrice'=>$addPrice,
  152. 'bigUnit'=>$bigUnit,
  153. 'smallUnit'=>$smallUnit,
  154. 'inTurn'=>$inTurn,
  155. ];
  156. $itemData[$className][] = $item;
  157. $classData[] = $className;
  158. }
  159. }
  160. $classData = array_unique($classData);
  161. //写入class
  162. if($clear){
  163. //清空数据
  164. Yii::$app->db->createCommand()->truncateTable('xhPtItemClass');
  165. Yii::$app->db->createCommand()->truncateTable('xhPtItem');
  166. }
  167. foreach ($classData as $v){
  168. $addClass = PtItemClassClass::add(['name'=>$v,'inTurn'=>0,'group'=>0]);
  169. $classIdMap[$v] = $addClass['id'];
  170. }
  171. $insertItemData = [];
  172. foreach ($itemData as $k=>$v){
  173. $classId = $classIdMap[$k]??0;
  174. if($classId){
  175. foreach ($v as $i){
  176. $i['classId'] = $classId;
  177. $insertItemData[] = $i;
  178. }
  179. }
  180. }
  181. if(!empty($insertItemData)){
  182. PtItemClass::batchAdd($insertItemData);
  183. }
  184. echo "done";
  185. }
  186. }