| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775 |
- <?php
- namespace console\controllers;
- use biz\ghs\classes\GhsClass;
- use biz\item\classes\PtItemClass;
- use biz\item\classes\PtItemClassClass;
- use biz\product\classes\XjClass;
- use biz\sj\classes\SjClass;
- use bizGhs\custom\classes\CustomClass;
- use biz\shop\classes\ShopClass;
- use bizGhs\item\classes\ItemClass;
- use bizGhs\item\classes\ItemClassClass;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\classes\OrderItemClass;
- use bizGhs\order\classes\PurchaseOrderItemClass;
- use bizGhs\order\classes\StockWastageOrderClass;
- use bizGhs\order\classes\StockWastageOrderItemClass;
- use bizHd\saas\classes\ApplyClass;
- use bizHd\saas\models\Apply;
- use bizHd\saas\services\ApplyService;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\stringUtil;
- use common\components\util;
- use yii\console\Controller;
- use Yii;
- use bizGhs\product\classes\ProductClass;
- /**
- * 花材同步脚本 ssh 20230619
- */
- class ItemController extends Controller
- {
- public $sjId, $shopId, $sj;
- //复制迁移花材,请用这个方法,其他方法不用看了。迁移前从分类和花材清掉,然后下面相应的mainId修改一下就好了。 ssh 20260607
- public function actionMigrate()
- {
- if (getenv('YII_ENV') == 'production') {
- $newMainId = 100880;
- $oldMainId = 16299;
- } else {
- $newMainId = 762;
- $oldMainId = 644;
- util::stop('测试环境未开通');
- }
- $list = ItemClass::getAllByCondition(['mainId' => $newMainId], null, '*');
- if (!empty($list)) {
- echo '已经有花材了';
- exit();
- }
- $classList = ItemClassClass::getAllByCondition(['mainId' => $oldMainId], null, '*', null, true);
- if (empty($classList)) {
- echo '没有找到老店的分类';
- exit();
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- foreach ($classList as $class) {
- $oldId = $class->id ?? 0;
- $cover = $class->cover ?? '';
- $name = $class->name ?? '';
- $inTurn = $class->inTurn ?? 0;
- $isDefault = $class->isDefault ?? 0;
- $newClass = [
- 'mainId' => $newMainId,
- 'cover' => $cover,
- 'name' => $name,
- 'inTurn' => $inTurn,
- 'isDefault' => $isDefault,
- ];
- $newRespond = ItemClassClass::add($newClass, true);
- $newClassId = $newRespond->id ?? 0;
- $oldItemList = ItemClass::getAllByCondition(['classId' => $oldId, 'delStatus' => 0], null, '*');
- foreach ($oldItemList as $oldItem) {
- $oldItemId = $oldItem['id'];
- unset($oldItem['id']);
- $oldItem['classId'] = $newClassId;
- $oldItem['stock'] = 0;
- $oldItem['onStock'] = 0;
- $oldItem['status'] = 2;
- $oldItem['mainId'] = $newMainId;
- $oldItem['sjId'] = 0;
- $oldItem['shopId'] = 0;
- $new = ItemClass::add($oldItem, true);
- if (isset($oldItem['variety']) && $oldItem['variety'] == 1) {
- $xjList = XjClass::getAllByCondition(['itemId' => $oldItemId], null, '*');
- if (!empty($xjList)) {
- foreach ($xjList as $oneXj) {
- unset($oneXj['id']);
- unset($oneXj['sjId']);
- unset($oneXj['shopId']);
- unset($oneXj['mainId']);
- unset($oneXj['itemId']);
- unset($oneXj['ptItemId']);
- unset($oneXj['addTime']);
- unset($oneXj['updateTime']);
- $oneXj['mainId'] = $newMainId;
- $oneXj['itemId'] = $new->id ?? 0;
- $oneXj['ptItemId'] = $new->itemId ?? 0;
- XjClass::add($oneXj);
- }
- }
- }
- }
- }
- $transaction->commit();
- } catch (\Exception $e) {
- $transaction->rollBack();
- $msg = $e->getMessage();
- echo "报错了:" . $msg;
- }
- }
- public function actionCQ()
- {
- $itemList = ItemClass::getAllByCondition(['mainId' => 14128], null, '*', null, true);
- if (!empty($itemList)) {
- foreach ($itemList as $item) {
- $classId = $item->classId;
- if (!empty($classId)) {
- $class = ItemClassClass::getById($classId, true);
- if ($class->mainId != 14128) {
- echo $item->name . "\n";
- }
- }
- }
- }
- }
- //花材修改分类同步 ssh 20240523
- public function actionModifyItemClassSync()
- {
- ini_set('memory_limit', '5045M');
- set_time_limit(0);
- $productIdCachedKey = 'item_class_modify_sync';
- $idList = [];
- while ($count = Yii::$app->redis->executeCommand('LLEN', [$productIdCachedKey])) {
- if (count($idList) > 20) {
- break;
- }
- $currentId = Yii::$app->redis->executeCommand('RPOP', [$productIdCachedKey]);
- $idList[] = $currentId;
- }
- if (empty($idList)) {
- return false;
- }
- $list = ProductClass::getAllByCondition(['id' => ['in', $idList]], null, '*', null, true);
- if (!empty($list)) {
- foreach ($list as $info) {
- $classId = $info->classId ?? 0;
- $productId = $info->id ?? 0;
- OrderItemClass::updateByCondition(['productId' => $productId], ['classId' => $classId]);
- \bizHd\order\classes\OrderItemClass::updateByCondition(['itemId' => $productId], ['classId' => $classId]);
- PurchaseOrderItemClass::updateByCondition(['productId' => $productId], ['classId' => $classId]);
- //noticeUtil::push("productId:{$productId} classId:{$classId}", '15280215347');
- }
- }
- //noticeUtil::push(implode(',', $idList) . ' 这些花材ID修改了分类,已同步到订单和采购单', '15280215347');
- }
- //【重要】将花材的分类id同步到批发销售单、批发采购单和零售销售单【小丽优先】
- public function actionSyncClass()
- {
- ini_set('memory_limit', '5120M');
- set_time_limit(0);
- $itemList = ItemClass::getAllByCondition(['mainId' => 10975], null, '*', null, true);
- if (empty($itemList)) {
- util::stop('没有找到花材');
- }
- foreach ($itemList as $item) {
- $classId = $item->classId ?? 0;
- $productId = $item->id ?? 0;
- if (!empty($classId) && !empty($productId)) {
- OrderItemClass::updateByCondition(['productId' => $productId], ['classId' => $classId]);
- \bizHd\order\classes\OrderItemClass::updateByCondition(['itemId' => $productId], ['classId' => $classId]);
- PurchaseOrderItemClass::updateByCondition(['productId' => $productId], ['classId' => $classId]);
- }
- }
- echo "ok\n";
- }
- //包装纸丝带同步 ssh 20230525
- public function actionPaperSync()
- {
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $oldItemList = ItemClass::getAllByCondition(['classId' => 26013, 'delStatus' => 0], null, '*');
- foreach ($oldItemList as $oldItem) {
- unset($oldItem['id']);
- $oldItem['classId'] = 31106;
- $oldItem['stock'] = 0;
- $oldItem['status'] = 2;
- $oldItem['mainId'] = 4553;
- $oldItem['sjId'] = 16936;
- $oldItem['shopId'] = 0;
- ItemClass::add($oldItem);
- }
- $transaction->commit();
- } catch (\Exception $e) {
- $transaction->rollBack();
- $msg = $e->getMessage();
- echo "报错了:" . $msg;
- }
- }
- public function actionWaste()
- {
- ini_set('memory_limit', '2045M');
- set_time_limit(0);
- $list = StockWastageOrderClass::getAllByCondition(['id>' => 0], null, '*', null, true);
- if (!empty($list)) {
- foreach ($list as $order) {
- $orderSn = $order->orderSn ?? '';
- $itemList = StockWastageOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- $totalCost = 0;
- if (!empty($itemList)) {
- foreach ($itemList as $v) {
- $num = $v->itemNum ?? 0;
- $itemCost = $v->itemCost ?? 0;
- $totalCost = bcadd($totalCost, bcmul($num, $itemCost, 2), 2);
- }
- }
- $order->cost = $totalCost;
- $order->save();
- }
- }
- }
- public function actionWastage()
- {
- ini_set('memory_limit', '2045M');
- set_time_limit(0);
- $list = StockWastageOrderClass::getAllByCondition(['id>' => 0], null, '*', null, true);
- if (!empty($list)) {
- foreach ($list as $order) {
- $orderSn = $order->orderSn ?? '';
- $itemList = StockWastageOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- $totalPrice = 0;
- if (!empty($itemList)) {
- foreach ($itemList as $v) {
- $num = $v->itemNum ?? 0;
- $itemPrice = $v->itemPrice ?? 0;
- $totalPrice = bcadd($totalPrice, bcmul($num, $itemPrice, 2), 2);
- }
- }
- $order->price = $totalPrice;
- $order->save();
- }
- }
- }
- //小向花材迁移 ssh 20250215
- public function actionXx()
- {
- if (getenv('YII_ENV') == 'production') {
- $newMainId = 49988;
- $oldMainId = 23390;
- } else {
- $newMainId = 762;
- $oldMainId = 644;
- util::stop('已取消');
- }
- $list = ItemClass::getAllByCondition(['mainId' => $newMainId], null, '*');
- if (!empty($list)) {
- echo '已经有花材了';
- exit();
- }
- $classList = ItemClassClass::getAllByCondition(['mainId' => $oldMainId], null, '*', null, true);
- if (empty($classList)) {
- echo '没有找到目标门店的分类';
- exit();
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- foreach ($classList as $class) {
- $oldId = $class->id ?? 0;
- $cover = $class->cover ?? '';
- $name = $class->name ?? '';
- $inTurn = $class->inTurn ?? 0;
- $isDefault = $class->isDefault ?? 0;
- $newClass = [
- 'mainId' => $newMainId,
- 'cover' => $cover,
- 'name' => $name,
- 'inTurn' => $inTurn,
- 'isDefault' => $isDefault,
- ];
- $newRespond = ItemClassClass::add($newClass, true);
- $newClassId = $newRespond->id ?? 0;
- $oldItemList = ItemClass::getAllByCondition(['classId' => $oldId, 'delStatus' => 0], null, '*');
- foreach ($oldItemList as $oldItem) {
- $oldItemId = $oldItem['id'];
- unset($oldItem['id']);
- $oldItem['classId'] = $newClassId;
- $oldItem['stock'] = 0;
- $oldItem['status'] = 2;
- $oldItem['mainId'] = $newMainId;
- $oldItem['sjId'] = 0;
- $oldItem['shopId'] = 0;
- $new = ItemClass::add($oldItem, true);
- if (isset($oldItem['variety']) && $oldItem['variety'] == 1) {
- $xjList = XjClass::getAllByCondition(['itemId' => $oldItemId], null, '*');
- if (!empty($xjList)) {
- foreach ($xjList as $oneXj) {
- unset($oneXj['id']);
- unset($oneXj['sjId']);
- unset($oneXj['shopId']);
- unset($oneXj['mainId']);
- unset($oneXj['itemId']);
- unset($oneXj['ptItemId']);
- unset($oneXj['addTime']);
- unset($oneXj['updateTime']);
- $oneXj['mainId'] = $newMainId;
- $oneXj['itemId'] = $new->id ?? 0;
- $oneXj['ptItemId'] = $new->itemId ?? 0;
- XjClass::add($oneXj);
- }
- }
- }
- }
- }
- $transaction->commit();
- } catch (\Exception $e) {
- $transaction->rollBack();
- $msg = $e->getMessage();
- echo "报错了:" . $msg;
- }
- }
- //花材反向更新 ./yii item/reverse-update
- public function actionReverseUpdate()
- {
- $shopId = 10;
- if (getenv('YII_ENV') != 'production') {
- $shopId = 36225;
- }
- $list = ProductClass::getAllByCondition(['shopId' => $shopId], null, '*', null, true);
- if (empty($list)) {
- return false;
- }
- foreach ($list as $cKey => $product) {
- $ptItemId = $product->itemId ?? 0;
- $ptItem = PtItemClass::getById($ptItemId, true);
- if (empty($ptItem)) {
- continue;
- }
- $ptItem->name = $product->name ?? '';
- $ptItem->py = $product->py ?? '';
- $ptItem->cover = $product->cover ?? '';
- $ptItem->ratio = $product->ratio ?? 1;
- $ptItem->shelfLife = $product->shelfLife ?? 7;
- $ptItem->weight = $product->weight ?? 0.1;
- $ptItem->stockWarning = $product->stockWarning ?? 5;;
- $ptItem->cost = $product->cost ?? 1;
- $ptItem->addPrice = 5;
- $ptItem->bigUnit = $product->bigUnit;
- $ptItem->smallUnit = $product->smallUnit;
- $ptItem->bigUnitId = $product->bigUnitId;
- $ptItem->smallUnitId = $product->smallUnitId;
- $ptItem->save();
- echo $product->id . " " . $product->name . " 已同步\n";
- }
- }
- //命令: ./yii item/sync name,cover 同步name 和cover
- //同步item, ptItem更新,同步到 ghsItem, ghsProduct, 同步字段(name,alias,py,cover,ratio,cover等)
- public function actionSync($field)
- {
- $allFiled = ['name', 'alias', 'cover', 'py', 'ratio', 'weight', 'bigUnit', 'smallUnit', 'bigUnitId', 'smallUnitId', 'stockWarning'];
- $fields = explode(",", $field);
- $syncFields = array_intersect($allFiled, $fields);
- if (empty($syncFields)) {
- echo "参数不支持";
- die;
- }
- $item = PtItemClass::getAllList("id", "");
- $itemIds = [];
- foreach ($item as $v) {
- $itemIds[] = $v['id'];
- }
- if (!empty($itemIds)) {
- foreach ($itemIds as $v) {
- $itemInfo = PtItemClass::getById($v);
- $data = [];
- foreach ($syncFields as $f) {
- $data[$f] = $itemInfo[$f];
- }
- ProductClass::updateByCondition(['itemId' => $v], $data);
- \bizGhs\item\classes\ItemClass::updateByCondition(['itemId' => $v], $data);
- }
- }
- echo "done";
- }
- //平台花材初始化导入 lqh 2021-05-08
- //命令: ./yii item/init-pt-item
- public function actionInitPtItem($clear = false)
- {
- $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
- require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
- $dir = Yii::$app->basePath;
- $filename = $dir . '/item.xls';
- $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
- $excelReader = \PHPExcel_IOFactory::createReader($fileType);
- $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
- $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
- $highestRow = $sheet->getHighestRow();//取得总行数
- $highestColumn = $sheet->getHighestColumn(); //取得总列数
- $classData = [];
- $itemData = [];
- $classIdMap = [];
- for ($j = 2; $j <= $highestRow; $j++) {
- $str = '';
- for ($k = 'A'; $k <= $highestColumn; $k++) {
- $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
- }
- //explode:函数把字符串分割为数组。
- $string = explode("\\", $str);
- array_pop($string);
- $itemName = $string[0] ?? ''; //花材名称
- $className = $string[1] ?? ''; //分类名称
- $weight = $string[2] ?? 0; //重量 kg
- $bigUnit = $string[3] ?? ''; //大单位名称
- $smallUnit = $string[4] ?? ''; //小单位名称
- $ratio = $string[5] ?? 0; //比例
- $cost = $string[6] ?? 0;//成本价
- $addPrice = $string[7] ?? 0; //加价
- $stockWarning = $string[8] ?? 0;
- $alias = $string[9] ?? '';
- $cover = $string[10] ?? '';
- $inTurn = $string[11] ?? 0;
- if ($itemName) {
- $py = stringUtil::py($itemName);
- $item = [
- 'name' => $itemName,
- 'alias' => $alias,
- 'py' => $py,
- 'cover' => $cover,
- 'classId' => 0,
- 'ratio' => $ratio,
- 'weight' => $weight,
- 'stockWarning' => $stockWarning,
- 'cost' => $cost,
- 'addPrice' => $addPrice,
- 'bigUnit' => $bigUnit,
- 'smallUnit' => $smallUnit,
- 'inTurn' => $inTurn,
- ];
- $itemData[$className][] = $item;
- $classData[] = $className;
- }
- }
- $classData = array_unique($classData);
- //写入class
- if ($clear) {
- //清空数据
- Yii::$app->db->createCommand()->truncateTable('xhPtItemClass')->execute();
- Yii::$app->db->createCommand()->truncateTable('xhPtItem')->execute();
- }
- foreach ($classData as $v) {
- $addClass = PtItemClassClass::add(['name' => $v, 'inTurn' => 0, 'group' => 0]);
- $classIdMap[$v] = $addClass['id'];
- }
- $insertItemData = [];
- foreach ($itemData as $k => $v) {
- $classId = $classIdMap[$k] ?? 0;
- if ($classId) {
- foreach ($v as $i) {
- $i['classId'] = $classId;
- $insertItemData[] = $i;
- }
- }
- }
- if (!empty($insertItemData)) {
- PtItemClass::batchAdd($insertItemData);
- }
- echo "done";
- }
- //供货商初始化 ./yii item/gys ./yii.bat item/gys
- public function actionGys()
- {
- $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
- require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
- $dir = Yii::$app->basePath;
- $filename = $dir . '/gys.xls';
- $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
- $excelReader = \PHPExcel_IOFactory::createReader($fileType);
- $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
- $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
- $highestRow = $sheet->getHighestRow();//取得总行数
- $highestColumn = $sheet->getHighestColumn(); //取得总列数
- $changeData = [];
- for ($j = 2; $j <= $highestRow; $j++) {
- $str = '';
- for ($k = 'A'; $k <= $highestColumn; $k++) {
- $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
- }
- //explode:函数把字符串分割为数组。
- $string = explode("\\", $str);
- array_pop($string);
- array_push($changeData, $string);
- }
- if (!empty($changeData)) {
- $mobileList = array_values(array_column($changeData, 1));
- $ptStyle = dict::getDict('ptStyle', 'kmGhs');
- Yii::$app->params['ptStyle'] = $ptStyle;
- $has = Apply::find()->where(['in', 'mobile', $mobileList])->andWhere(['style' => $ptStyle])->all();
- if (!empty($has)) {
- echo "这些已经添加过了,请先删除:\n";
- foreach ($has as $item) {
- echo $item->name . ' ' . $item->mobile . "\n";
- }
- exit();
- }
- if (getenv('YII_ENV') == 'production') {
- $this->sjId = 12367;
- $this->shopId = 10;
- } else {
- $this->sjId = 12615;
- $this->shopId = 36119;
- }
- foreach ($changeData as $currentKey => $currentData) {
- $mobile = $currentData[1] ?? '';
- $name = $currentData[0] ?? '';
- $address = $currentData[2] ?? '';
- if (stringUtil::isMobile($mobile) == false) {
- echo "请填写正确的手机号 \n";
- continue;
- }
- if (empty($name)) {
- echo "{$mobile} 名称不能为空 \n";
- continue;
- }
- if (stringUtil::getWordNum($name) > 8) {
- echo "{$mobile} 名称不能超过8个汉字 \n";
- continue;
- }
- $has = ApplyClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_KM_SUPPLIER]);
- if (!empty($has)) {
- echo "{$mobile} 名称已经存在 \n";
- continue;
- }
- $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_KM_SUPPLIER]);
- if (!empty($has)) {
- echo "{$mobile} 手机号已经添加过 \n";
- continue;
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $sjId = $this->sjId;
- $shopId = $this->shopId;
- $sjName = $this->sj->name ?? '';
- $applyData = [
- 'name' => $name,
- 'licenseNo' => $mobile,
- 'certType' => ApplyClass::CERT_TYPE_MOBILE,
- 'mobile' => $mobile,
- 'introSjId' => $sjId,
- 'introSjName' => $sjName,
- 'introShopId' => $shopId,
- 'introStyle' => SjClass::STYLE_SUPPLIER,
- 'from' => ApplyClass::FROM_GHS,
- 'style' => dict::getDict('ptStyle', 'kmGhs'),
- 'address' => $address,
- 'status' => ApplyClass::STATUS_PASS,
- 'replace' => 1,//1表示供货商添加的客户或供货商
- ];
- $apply = ApplyService::replaceKmGhs($applyData);
- $id = $apply['id'] ?? 0;
- $respond = ApplyService::pass($id);
- $currentShop = $respond['shop'] ?? [];
- $sj = $respond['sj'] ?? [];
- $currentSjId = $sj['id'] ?? 0;
- $currentShopId = $currentShop->id ?? 0;
- ApplyClass::updateById($id, ['sjId' => $currentSjId, 'shopId' => $currentShopId]);
- if (empty($currentShopId)) {
- echo "{$mobile} 供货商添加失败 \n ";
- continue;
- }
- GhsClass::build($currentShopId, $shopId, 1);
- $transaction->commit();
- echo "{$mobile} 供货商添加成功!\n";
- } catch (\Exception $exception) {
- $transaction->rollBack();
- echo "{$mobile} 供货商添加失败,原因:" . $exception->getMessage() . "\n";
- }
- }
- }
- }
- //客户初始化 ./yii item/custom ./yii.bat item/custom
- public function actionCustom()
- {
- $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
- require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
- $dir = Yii::$app->basePath;
- $filename = $dir . '/custom.xls';
- $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
- $excelReader = \PHPExcel_IOFactory::createReader($fileType);
- $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
- $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
- $highestRow = $sheet->getHighestRow();//取得总行数
- $highestColumn = $sheet->getHighestColumn(); //取得总列数
- $changeData = [];
- for ($j = 2; $j <= $highestRow; $j++) {
- $str = '';
- for ($k = 'A'; $k <= $highestColumn; $k++) {
- $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
- }
- //explode:函数把字符串分割为数组。
- $string = explode("\\", $str);
- array_pop($string);
- array_push($changeData, $string);
- }
- if (!empty($changeData)) {
- $mobileList = array_values(array_column($changeData, 1));
- $ptStyle = dict::getDict('ptStyle', 'hd');
- Yii::$app->params['ptStyle'] = $ptStyle;
- $has = Apply::find()->where(['in', 'mobile', $mobileList])->andWhere(['style' => $ptStyle])->all();
- if (!empty($has)) {
- echo "这些已经添加过了,请先删除:\n";
- foreach ($has as $item) {
- echo $item->name . ' ' . $item->mobile . "\n";
- }
- exit();
- }
- if (getenv('YII_ENV') == 'production') {
- $this->sjId = 12367;
- $this->shopId = 10;
- } else {
- $this->sjId = 12615;
- $this->shopId = 36119;
- }
- $shop = ShopClass::getById($this->shopId, true);
- $sj = SjClass::getById($this->sjId, true);
- $this->sj = $sj;
- foreach ($changeData as $currentKey => $currentData) {
- $mobile = $currentData[1] ?? '';
- $name = $currentData[0] ?? '';
- $address = $currentData[2] ?? '';
- if (empty($name)) {
- echo "{$mobile} 名称不能为空 \n";
- continue;
- }
- if (stringUtil::getWordNum($name) > 8) {
- echo "{$mobile} 名称不能超过8个汉字 \n";
- continue;
- }
- if (stringUtil::isMobile($mobile) == false) {
- echo "{$mobile} 手机号错误 \n";
- continue;
- }
- $has = SjClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_RETAIL]);
- if (!empty($has)) {
- echo "{$mobile} 名称已经存在 \n";
- continue;
- }
- $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_RETAIL]);
- if (!empty($has)) {
- echo "{$mobile} 手机号已经添加过 \n";
- continue;
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $sjId = $this->sjId;
- $shopId = $this->shopId;
- $sjName = $this->sj->name ?? '';
- $city = $shop->city ?? '';
- $dist = $shop->dist ?? '';
- $applyData = [
- 'name' => $name,
- 'licenseNo' => $mobile,
- 'certType' => ApplyClass::CERT_TYPE_MOBILE,
- 'mobile' => $mobile,
- 'introSjId' => $sjId,
- 'introSjName' => $sjName,
- 'introShopId' => $shopId,
- 'introStyle' => SjClass::STYLE_SUPPLIER,
- 'from' => ApplyClass::FROM_GHS,
- 'style' => SjClass::STYLE_RETAIL,
- 'province' => $shop->province ?? '',
- 'city' => $city,
- 'dist' => $dist,
- 'address' => $address,
- 'status' => ApplyClass::STATUS_PASS,
- 'replace' => 1,//1表示供货商添加的客户
- ];
- $apply = ApplyService::replaceRetail($applyData);
- $id = $apply['id'] ?? 0;
- $respond = ApplyService::pass($id);
- $currentShop = $respond['shop'] ?? [];
- $sj = $respond['sj'] ?? [];
- $hdSjId = $sj['id'] ?? 0;
- $hdShopId = $currentShop->id ?? 0;
- ApplyClass::updateById($id, ['sjId' => $hdSjId, 'shopId' => $hdShopId]);
- if (empty($hdShopId)) {
- echo "{$mobile} 客户添加失败 \n";
- continue;
- }
- $custom = CustomClass::build($this->shopId, $hdShopId);
- $custom['avatar'] = imgUtil::groupImg($custom['avatar']);
- $transaction->commit();
- echo "{$mobile} 添加成功! \n";
- } catch (\Exception $exception) {
- $transaction->rollBack();
- echo "{$mobile} 添加失败,报错:" . $exception->getMessage() . "\n";
- }
- }
- }
- }
- //更新特价 ssh 20230630
- public function actionDiscount()
- {
- ini_set('memory_limit', '2045M');
- set_time_limit(0);
- $list = ItemClass::getAllByCondition(['discountPrice>' => 0,], null, '*', null, true);
- if (!empty($list)) {
- foreach ($list as $item) {
- $discountPrice = $item->discountPrice ?? 0;
- $skMore = $item->skMore ?? 0;
- $addPrice = $item->addPrice ?? 0;
- $diff = bcsub($skMore, $addPrice, 2);
- if ($diff > 0) {
- $item->skDiscountPrice = bcadd($discountPrice, $diff, 2);
- } else {
- $item->skDiscountPrice = $discountPrice;
- }
- $item->hjDiscountPrice = $discountPrice;
- $item->save();
- }
- }
- }
- }
|