ItemController.php 8.1 KB

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