| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389 |
- <?php
- namespace bizGhs\product\classes;
- use biz\item\classes\PtItemClass;
- use biz\item\classes\PtItemClassClass;
- use biz\item\classes\UnitClass;
- use biz\shop\services\ShopService;
- use biz\wx\classes\WxMessageClass;
- use bizGhs\base\classes\BaseClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\item\classes\ItemClass;
- use bizGhs\item\classes\ItemClassClass;
- use bizGhs\merchant\classes\ShopClass;
- use bizGhs\order\classes\CheckOrderClass;
- use bizGhs\order\classes\CheckOrderItemClass;
- use bizGhs\order\traits\OrderTrait;
- use bizGhs\stock\classes\StockRecordClass;
- use bizGhs\stock\services\StockRecordService;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\stringUtil;
- use common\components\util;
- use common\services\xhItemService;
- use Yii;
- use yii\di\ServiceLocator;
- class ProductClass extends BaseClass
- {
- use OrderTrait;
- public static $baseFile = '\bizGhs\product\models\Product';
- const PRICE_LABEL_AUTO = 1; //自动调价
- const PRICE_LABEL_CHANGE = 2; //改价
- const LOCK_STOCK = 'lock_stock';//修改库存锁
- const LOCK_PRICE = 'lock_price'; //改价锁
- const LOCK_CHECK_STOCK = 'lock_check_stock'; //判断库存
- //根据itemIds获取相应的花材信息
- public static function getProductInfoByItemIds(array $itemIds, $hdMainId)
- {
- $where = ['itemId' => ['in', $itemIds], 'delStatus' => 0, 'mainId' => $hdMainId];
- $data = self::getAllList('*', $where);
- $data = self::groupItemBaseInfo($data);
- return $data;
- }
- //根据花材ID,获取花材名称,图片
- public static function groupItemBaseInfo($list)
- {
- //取得itemIds
- $itemIds = array_column($list, 'itemId');
- //获取相应的图片,名称,py
- $where = ['id' => ['in', $itemIds]];
- $data = xhItemService::getAllByCondition($where, null, '*');
- if (!empty($data)) {
- $data = array_column($data, NULL, 'id');
- foreach ($list as $k => $v) {
- if (isset($data[$v['itemId']]['cover']) && !empty($data[$v['itemId']]['cover'])) {
- $cover = self::groupImg($data[$v['itemId']]['cover']);
- } else {
- //没有图片时取默认图片 ssh 2021.2.18
- $cover = imgUtil::getPrefix() . '/hhb_small.png';
- }
- $unitNum = $data[$v['itemId']]['ratio'] ?? 0;
- $stockInfo = self::formatStock($v['stock'], $unitNum);
- $list[$k]['cover'] = $cover;
- $list[$k]['bigNum'] = $stockInfo['bigNum'];
- $list[$k]['smallNum'] = $stockInfo['smallNum'];
- $list[$k]['itemName'] = $data[$v['itemId']]['name'] ?? '';
- $list[$k]['py'] = $data[$v['itemId']]['py'] ?? '';
- $list[$k]['ratio'] = $unitNum ?? 0;
- //换算大小单位对应价格
- $list[$k]['bigPrice'] = $v['price']; //大单位价格就是product表存的价格
- $smallPrice = 0;
- if ($unitNum) {
- $smallPrice = bcdiv($v['price'], $unitNum, 2);
- }
- $list[$k]['smallPrice'] = $smallPrice;
- $list[$k]['bigUnit'] = $data[$v['itemId']]['bigUnit'];
- $list[$k]['smallUnit'] = $data[$v['itemId']]['smallUnit'];
- }
- }
- return $list;
- }
- //获取详情 ruidian 2021.5.3
- public static function getItemInfo($id, $ghs = [])
- {
- $info = self::getById($id);
- if (empty($info)) {
- return $info;
- }
- $level = $ghs->giveLevel ?? 1;
- $list = self::groupProductInfo([$info], $level);
- return current($list);
- }
- //花材图片,名称,大小单位,小单位价格
- public static function groupProductInfo($list, $level = 0)
- {
- //各个等级对应的price addPrice字段
- $priceMap = CustomClass::$levelPriceKeyMap;
- $addPriceMap = CustomClass::$levelAddPriceKeyMap;
- foreach ($list as $k => $v) {
- $shortCover = $v['cover'] ?? '';
- $cover = imgUtil::getPrefix() . 'hhb_small.png';
- $bigCover = imgUtil::getPrefix() . 'hhb_small.png';
- if (!empty($shortCover)) {
- $parse = parse_url(self::groupImg($shortCover));
- if (empty($parse['query'])) {
- $cover = self::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_110,w_110";
- } else {
- $cover = self::groupImg($shortCover);
- }
- $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
- }
- $ratio = isset($v['ratio']) && $v['ratio'] > 0 ? $v['ratio'] : 1;
- $stockInfo = self::formatStock($v['stock'], $ratio);
- $list[$k]['cover'] = $cover;
- $list[$k]['bigCover'] = $bigCover;
- $list[$k]['shortCover'] = $shortCover;
- $list[$k]['bigNum'] = $stockInfo['bigNum'];
- $list[$k]['smallNum'] = $stockInfo['smallNum'];
- $list[$k]['itemName'] = $v['name'] ?? '';
- $list[$k]['prePrice'] = $v['price'] ?? 0;
- //今日特价、会员价
- $price = self::getFinalPrice($v, $level, $priceMap, $addPriceMap);
- $list[$k]['price'] = $price;
- $list[$k]['bigPrice'] = $price;
- $smallRatio = $v['smallRatio'] ?? 0;
- $smallPrice = bcdiv($price, $ratio, 2);
- $smallPrice = bcmul($smallPrice, $smallRatio, 2);
- $list[$k]['smallPrice'] = $smallPrice;
- //这个很重要不能随意乱改
- $autoPrice = bcadd($v['cost'], $v['addPrice'], 2);
- $skMore = $v['skMore'] ?? 0;
- $skAutoPrice = bcadd($autoPrice, $skMore, 2);
- $list[$k]['autoPrice'] = $autoPrice;
- $list[$k]['autoSkPrice'] = $skAutoPrice;
- }
- return $list;
- }
- //输出价格 ssh 20211009
- public static function getFinalPrice($product, $level, $priceMap, $addPriceMap, $changePrice = 0)
- {
- //开单改价优先级最高
- if ($changePrice > 0) {
- return $changePrice;
- }
- if (empty($product) || isset($product['price']) == false) {
- return 0;
- }
- //今日特价
- $discountPrice = $product['discountPrice'] ?? 0;
- $currentPriceField = $priceMap[$level] ?? 'price';
- $price = $product[$currentPriceField] ?? 0;
- if ($discountPrice > 0) {
- $discountAddPrice = $product['addPrice'] ?? 0;
- $currentAddPriceField = $addPriceMap[$level] ?? 'addPrice';
- $currentAddPrice = $product[$currentAddPriceField] ?? 0;
- $diff = bcsub($discountAddPrice, $currentAddPrice, 2);
- $price = bcsub($discountPrice, $diff, 2);
- }
- return $price;
- }
- //根据花材ID,库存=》 计算出多少扎,多少支
- public static function formatStockByItemId($itemId, $stock)
- {
- $unitNum = xhItemService::getItemUnitNum($itemId);
- return self::formatStock($stock, $unitNum);
- }
- //花材的库存转换: 库存是小数,根据各个花材的unitNum, 计算出多少扎,多少支
- public static function formatStock($stock, $unitNum)
- {
- $pn = true; //正数
- if ($stock < 0) {
- $pn = false;
- $stock = abs($stock);
- }
- $bigNum = floor($stock); // 扎
- $stockArr = explode('.', $stock);
- $smallNum = 0; //支
- if (count($stockArr) === 2) {
- $smallNum = bcmul(($stock - $bigNum), $unitNum);
- }
- if (!$pn) {
- $bigNum *= -1;
- $smallNum *= -1;
- }
- return [
- 'bigNum' => $bigNum,
- 'smallNum' => $smallNum,
- ];
- }
- /**
- * 根据 花材ID,将花材大小数量 换算成小数
- * @param $bigNum 大单位 数量
- * @param $smallNum 小单位 数量
- * @param $itemId 花材ID
- * @return string
- */
- public static function mergeItemNumByItemId($bigNum, $smallNum, $itemId)
- {
- $unitNum = xhItemService::getItemUnitNum($itemId);
- return self::mergeItemNum($bigNum, $smallNum, $unitNum);
- }
- //花材扎、支 合并转换 小数
- /**
- * @param $bigNum 大单位 数量
- * @param $smallNum 小单位 数量
- * @param $unitNum 花材比例
- * @return string
- */
- public static function mergeItemNum($bigNum, $smallNum, $unitNum)
- {
- $stockDecimal = 0;
- if ($unitNum > 0) {
- $stockDecimal = bcdiv($smallNum, $unitNum, 2);
- }
- $itemNum = bcadd($bigNum, $stockDecimal, 2);
- return $itemNum;
- }
- //获取常用的itemIds
- public static function getOftenItemIds($shopId)
- {
- $data = self::getLimitList("itemId", ['shopId' => $shopId], 10, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC]);
- if ($data) {
- $data = array_column($data, 'itemId');
- }
- return $data;
- }
- //加库存,允许负库存,负数数量(盘点时)
- /**
- * @param $productId 门店下的花材ID(xhGhsItemInfo 主键ID)
- * @param $itemNumBundle 花材数量(扎)
- * @param $itemNumPiece 花材数量(支)
- * @return mixed
- */
- public static function addStock($productId, $itemNumBundle, $itemNumPiece)
- {
- $key = self::LOCK_STOCK . '_' . $productId;
- $lock = util::lock($key);
- if (!$lock) {
- util::fail("系统繁忙中,请稍后再试!");
- }
- $productData = self::getById($productId);
- if (!$productData) {
- util::unlock($key);
- util::fail("花材不存在");
- }
- // $itemId = $productData['itemId'];
- $ratio = $productData['ratio'];
- //转成小数,加库存
- $itemNum = self::mergeItemNum($itemNumBundle, $itemNumPiece, $ratio);
- $newStock = bcadd($productData['stock'], $itemNum, 2);
- self::updateStockById($productId, $newStock);
- //上架
- if ($newStock > 0 && $productData['status'] == 2) {
- self::changeStatus($productId, 1);
- }
- util::unlock($key);
- return ['oldStock' => $productData['stock'], 'newStock' => $newStock, 'itemNum' => $itemNum];
- }
- //todo 库存修改 加锁
- /**
- * 对商品ID 加库存
- * @param $productId 商品ID
- * @param $itemNum 数量
- * @return mixed
- */
- public static function addStockByItemNum($productId, $itemNum)
- {
- $key = self::LOCK_STOCK . '_' . $productId;
- $lock = util::lock($key);
- if (!$lock) {
- util::fail("系统繁忙中,请稍后再试!");
- }
- $productData = self::getById($productId, true);
- if (!$productData) {
- util::unlock($key);
- util::fail("花材不存在");
- }
- $newStock = bcadd($productData->stock, $itemNum, 2);
- self::updateStockById($productId, $newStock);
- //上架
- if ($newStock > 0 && $productData->status == 2) {
- self::changeStatus($productId, 1);
- }
- util::unlock($key);
- return ['oldStock' => $productData['stock'], 'newStock' => $newStock, 'itemNum' => $itemNum];
- }
- //减库存,允许负库存,负数数量(盘点时)
- /**
- * @param $productId 门店下的花材ID(xhGhsItemInfo 主键ID)
- * @param $itemNumBundle 花材数量(扎)
- * @param $itemNumPiece 花材数量(支)
- * @param $checkStock boolean 是否需要判断库存
- * @return mixed
- */
- public static function decreaseStock($productId, $itemNumBundle, $itemNumPiece, $checkStock = false)
- {
- $key = self::LOCK_STOCK . '_' . $productId;
- $lock = util::lock($key);
- if (!$lock) {
- util::fail("系统繁忙中,请稍后再试!");
- }
- $productData = self::getById($productId, true);
- if (!$productData) {
- util::unlock($key);
- util::fail("花材不存在");
- }
- $ratio = $productData->ratio;
- //转成小数,更新库存
- $itemNum = self::mergeItemNum($itemNumBundle, $itemNumPiece, $ratio);
- $newStock = bcsub($productData->stock, $itemNum, 2);
- if ($checkStock && $newStock < 0) {
- //需要判断库存是否充足
- util::unlock($key);
- $currentName = $productData->name ?? '';
- $currentRemainStock = floatval($productData->stock);
- util::fail("{$currentName} 只剩{$currentRemainStock}{$productData->bigUnit}");
- }
- self::updateStockById($productId, $newStock);
- //减库存,加销量 2021.6.21 lqh
- $actualSold = $productData->actualSold;
- $newActualSold = bcadd($actualSold, $itemNum, 2);
- self::updateActualSoldById($productId, $newActualSold);
- //库存=0 下架商品 lqh 2021.7.9
- if ($newStock <= 0) {
- self::changeStatus($productId, 2);
- }
- util::unlock($key);
- return ['oldStock' => $productData['stock'], 'newStock' => $newStock, 'itemNum' => $itemNum];
- }
- //根据数量减库存
- /**
- * @param $productId
- * @param $itemNum
- * @return mixed 返回减之前的库存,和减之后的库存
- */
- public static function decreaseStockByItemNum($productId, $itemNum, $checkStock = false)
- {
- $key = self::LOCK_STOCK . '_' . $productId;
- $lock = util::lock($key);
- if (!$lock) {
- util::fail("系统繁忙中,请稍后再试!");
- }
- $productData = self::getById($productId);
- if (!$productData) {
- util::unlock($key);
- util::fail("花材不存在");
- }
- $newStock = bcsub($productData['stock'], $itemNum, 2);
- if ($checkStock && $newStock < 0) {
- //需要判断库存是否充足
- util::unlock($key);
- util::fail("库存不足");
- }
- self::updateStockById($productId, $newStock);
- //减库存,加销量 2021.6.21 lqh
- $actualSold = $productData['actualSold'];
- $newActualSold = bcadd($actualSold, $itemNum, 2);
- self::updateActualSoldById($productId, $newActualSold);
- util::unlock($key);
- return ['oldStock' => $productData['stock'], 'newStock' => $newStock, 'itemNum' => $itemNum];
- }
- //计算价格
- /**
- * @param $productId 门店下的花材ID(xhGhsItemInfo 主键ID)
- * @param $bigNum 花材数量(扎)
- * @param $smallNum 花材数量(支)
- * @return float
- */
- public static function computedPrice($productId, $bigNum, $smallNum)
- {
- //获取itemId的价格
- $productData = self::getById($productId);
- if (!$productData) {
- util::fail("花材不存在");
- }
- $price = $productData['price'];
- $itemId = $productData['itemId'];
- $unitNum = xhItemService::getItemUnitNum($itemId);
- //换算出一支多少钱
- $piecePrice = 0;
- if ($unitNum > 0) {
- //有支
- $unitPrice = bcdiv($price, $unitNum, 2);
- $piecePrice = bcmul($unitPrice, $smallNum, 2);
- }
- $bundlePrice = bcmul($price, $bigNum, 2);
- $total = bcadd($bundlePrice, $piecePrice, 2);
- return $total;
- }
- //根据门店ID, 平台花材ID, 获取门店的花材ID
- public static function getGhsProductId($shopId, $itemId)
- {
- $data = self::getGhsProductDataByItemId($shopId, $itemId);
- if ($data) {
- return $data['id'];
- }
- return 0;
- }
- public static function getGhsProductDataByItemId($shopId, $itemId)
- {
- $where = ['shopId' => $shopId, 'itemId' => $itemId];
- $data = self::getByCondition($where);
- return $data;
- }
- //根据多个itemIds shopId 获取对应的 productId lqh 2021.1.24
- public static function getGhsProductDataByItemIds($mainId, $itemIds)
- {
- $where = ['itemId' => ['in', $itemIds], 'mainId' => $mainId];
- $data = self::getAllByCondition($where, null, "*");
- return $data;
- }
- //补全itemId (针对入库时 当前门店下无对应的花材 itemId) lqh 2021.1.24
- public static function complementProduct($sjId, $mainId, $shopId, $itemInfo)
- {
- $itemIds = array_column($itemInfo, "itemId");
- $oldProductIds = array_column($itemInfo, "productId");
- $productData = self::getGhsProductDataByItemIds($mainId, $itemIds);
- $defaultClass = ItemClassClass::getByCondition(['mainId' => $mainId, 'isDefault' => 1], true);
- $defaultClassId = $defaultClass->id ?? 0;
- if (empty($defaultClassId)) {
- util::fail('没有找到默认分类');
- }
- $shop = ShopClass::getById($shopId, true);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- if (count($productData) !== count($itemIds)) {
- $productDataItemIds = array_column($productData, 'itemId');
- //取差集
- $diffItemIds = array_diff($itemIds, $productDataItemIds);
- if ($diffItemIds) {
- $oldProductInfo = ProductClass::getAllByCondition(['id' => ['in', $oldProductIds]], null, '*', 'itemId');
- //新增到product表,price = itemPrice (出库时的售价), stock=0
- foreach ($diffItemIds as $v) {
- $addData = [];
- $addData['sjId'] = $sjId;
- $addData['shopId'] = $shopId;
- $addData['mainId'] = $mainId;
- $addData['itemId'] = $v;
- $addData['name'] = $oldProductInfo[$v]['name'] ?? '';
- $addData['price'] = $oldProductInfo[$v]['price'] ?? 0;
- $addData['classId'] = $defaultClassId;
- $addData['smallRatio'] = $oldProductInfo[$v]['smallRatio'] ?? 2;
- $addData['skPrice'] = $oldProductInfo[$v]['skPrice'] ?? 0;
- $addData['hjPrice'] = $oldProductInfo[$v]['hjPrice'] ?? 0;
- $addData['cost'] = $oldProductInfo[$v]['cost'] ?? 0;
- $addData['addPrice'] = $oldProductInfo[$v]['addPrice'] ?? 0;
- $addData['skAddPrice'] = $oldProductInfo[$v]['skAddPrice'] ?? 0;
- $addData['hjAddPrice'] = $oldProductInfo[$v]['hjAddPrice'] ?? 0;
- $addData['stockWarning'] = $oldProductInfo[$v]['stockWarning'] ?? 5;
- $addData['weight'] = $oldProductInfo[$v]['weight'] ?? 0;
- $addData['alias'] = $oldProductInfo[$v]['alias'] ?? '';
- $addData['py'] = $oldProductInfo[$v]['py'] ?? '';
- $addData['ratio'] = $oldProductInfo[$v]['ratio'] ?? 20;
- $addData['bigUnit'] = $oldProductInfo[$v]['bigUnit'] ?? '';
- $addData['smallUnit'] = $oldProductInfo[$v]['smallUnit'] ?? '';
- $addData['bigUnitId'] = $oldProductInfo[$v]['bigUnitId'] ?? 0;
- $addData['smallUnitId'] = $oldProductInfo[$v]['smallUnitId'] ?? 0;
- $addData['status'] = 1;
- $addData['delStatus'] = 0;
- $addData['inTurn'] = 100;
- self::addProduct($addData, $shop);
- }
- }
- }
- }
- public static function check($info, $mainId)
- {
- if (empty($info)) {
- util::fail('没有花材信息');
- }
- if (isset($info->mainId) == false || $info->mainId != $mainId) {
- util::fail('无效花材');
- }
- return true;
- }
- //有效花材判断,包括id有效、同个门店等 ssh 2021.1.19
- public static function valid($list, $mainId)
- {
- $ids = array_unique(array_filter(array_column($list, 'productId')));
- $productList = self::getByIds($ids, null, 'id');
- if (!empty($productList)) {
- foreach ($productList as $product) {
- if (isset($product['mainId']) == false || $product['mainId'] != $mainId) {
- util::fail('只能选择同一家的商品下单');
- }
- }
- if (count($productList) != count($ids)) {
- util::fail('存在无效商品');
- }
- } else {
- util::fail('商品不存在');
- }
- }
- //通过ids 批量获取花材商品的信息
- public static function getProductByIds($ids)
- {
- return self::getByIds($ids);
- }
- //修改花材商品的库存: 统一方法
- public static function updateStockById($id, $stock, $adminId = 0)
- {
- $data['stock'] = $stock;
- if ($adminId) {
- $data['adminId'] = $adminId;
- }
- $res = self::updateById($id, $data);
- return $res;
- }
- //订单退回库存
- public static function backStockByOrderItemInfo($orderItemInfo)
- {
- foreach ($orderItemInfo as $v) {
- ProductClass::addStockByItemNum($v['productId'], $v['itemNum']);
- }
- }
- //获取当前门店下商品信息 lqh 2021.1.28
- public static function getProductData($id, $mainId, $obj = false)
- {
- return ProductClass::getByCondition(['id' => $id, 'mainId' => $mainId], $obj);
- }
- //单个修改价格 lqh 2021.1.28
- public static function changeSinglePrice($shop, $ptItemId, $price, $skPrice = null)
- {
- $mainId = $shop->mainId ?? 0;
- $productInfo = ProductClass::getByCondition(['itemId' => $ptItemId, 'mainId' => $mainId], true);
- if (empty($productInfo)) {
- return false;
- }
- $productInfo->price = $price;
- if (empty($skPrice)) {
- $skMore = $productInfo->skMore ?? 0;
- $productInfo->skPrice = bcadd($price, $skMore, 2);
- } else {
- $productInfo->skPrice = $skPrice;
- }
- $productInfo->save();
- }
- //下单时计算商品的价格 lqh 2021.1.28
- public static function formatProductInfo($itemInfo, $level = 0)
- {
- $data = [];
- if (empty($itemInfo)) {
- util::fail('请选择花材');
- }
- $itemInfo = self::mergeItemInfo($itemInfo);
- $productIds = array_column($itemInfo, 'productId');
- $productData = ProductClass::getProductByIds($productIds);
- $productData = array_column($productData, NULL, 'id');
- $totalPrice = 0;
- $totalBigNum = 0;
- $totalSmallNum = 0;
- //不同等级对应 price addPrice
- $priceMap = CustomClass::$levelPriceKeyMap;
- $addPriceMap = CustomClass::$levelAddPriceKeyMap;
- $totalItemNum = 0;
- foreach ($itemInfo as $v) {
- $productId = $v['productId'];
- $itemId = $productData[$productId]['itemId'] ?? 0;
- $ratio = $productData[$productId]['ratio'] ?? 0;
- $smallRatio = $productData[$productId]['smallRatio'] ?? 1;
- $cost = $productData[$productId]['cost'] ?? 0;
- $bigNum = $v['bigNum'] ?? 0;
- $smallNum = $v['smallNum'] ?? 0;
- //大小数量合并多少扎
- $itemNum = self::mergeItemNum($bigNum, $smallNum, $ratio);
- $totalItemNum = bcadd($totalItemNum, $itemNum, 2);
- //供货商开单可以传单价过来
- $changePrice = isset($v['price']) && $v['price'] > 0 ? $v['price'] : 0;
- //今日特价、会员价
- $currentProduct = $productData[$productId] ?? [];
- $itemPrice = self::getFinalPrice($currentProduct, $level, $priceMap, $addPriceMap, $changePrice);
- $bigItemPrice = $itemPrice;
- if ($smallNum > 0) {
- //如果是小单位数量,则传过来的就是小单位价格,无需转换
- if (empty($changePrice)) {
- $itemPrice = bcdiv($itemPrice, $ratio, 2);
- $itemPrice = bcmul($itemPrice, $smallRatio, 2);
- }
- $price = bcmul($smallNum, $itemPrice, 2);
- } else {
- $price = bcmul($bigNum, $itemPrice, 2);
- }
- $tmp = [];
- $tmp['productId'] = $productId;
- $tmp['price'] = $price;
- $totalPrice = bcadd($totalPrice, $price, 2);
- $tmp['name'] = $productData[$productId]['name'] ?? '';
- $tmp['smallUnit'] = $productData[$productId]['smallUnit'] ?? '支';
- $tmp['bigUnit'] = $productData[$productId]['bigUnit'] ?? '扎';
- $tmp['cover'] = $productData[$productId]['cover'] ?? '';
- $tmp['itemId'] = $itemId;
- $tmp['rank'] = $productData[$productId]['rank'] ?? '';
- $tmp['ratio'] = $productData[$productId]['ratio'] ?? 1;
- $tmp['variety'] = $productData[$productId]['variety'] ?? 0;
- $tmp['unitPrice'] = $bigItemPrice;
- $tmp['unitWeight'] = $productData[$productId]['weight'] ?? 0;;
- $tmp['num'] = $itemNum;
- $tmp['bigNum'] = $v['bigNum'];
- $tmp['smallNum'] = $v['smallNum'];
- $tmp['preNum'] = $itemNum;
- $tmp['preBigNum'] = $v['bigNum'];
- $tmp['preSmallNum'] = $v['smallNum'];
- $tmp['userPrice'] = $price;
- $tmp['cost'] = $cost;
- $tmp['weight'] = $productData[$productId]['weight'] ?? 0;
- $tmp['sjId'] = $productData[$productId]['sjId'] ?? 0;
- $tmp['shopId'] = $productData[$productId]['shopId'] ?? 0;
- $tmp['mainId'] = $productData[$productId]['mainId'] ?? 0;
- $tmp['smallUnitPrice'] = $itemPrice;
- $totalBigNum += $bigNum;
- $totalSmallNum += $smallNum;
- if ($bigNum > 0) {
- $xhUnitType = dict::getDict('unitType', 'big');
- $xhNum = $bigNum;
- $xhUnitName = $tmp['bigUnit'] ?? '';
- } else {
- $xhUnitType = dict::getDict('unitType', 'small');
- $xhNum = $smallNum;
- $xhUnitName = $tmp['smallUnit'] ?? '';
- }
- $xhPrice = bcmul($xhNum, $itemPrice, 2);
- $tmp['xhNum'] = $xhNum;
- $tmp['xhUnitName'] = $xhUnitName;
- $tmp['xhUnitType'] = $xhUnitType;
- $tmp['xhUnitPrice'] = $itemPrice;
- $tmp['xhPrice'] = $xhPrice;
- $tmp['xhPreNum'] = $xhNum;
- $tmp['xhPreUnitName'] = $xhUnitName;
- $tmp['xhPreUnitType'] = $xhUnitType;
- $tmp['xhPreUnitPrice'] = $itemPrice;
- $tmp['xhPrePrice'] = $xhPrice;
- $data[] = $tmp;
- }
- return ['product' => $data, 'smallNum' => $totalSmallNum, 'bigNum' => $totalBigNum, 'price' => $totalPrice, 'totalItemNum' => $totalItemNum];
- }
- // //修改某个花材的成本价(采购之后要用到) lqh 2021.04.06
- public static function changeCost($productId, $cost)
- {
- return self::updateById($productId, ['cost' => $cost]);
- }
- //更新某个花材的加价 2021.04.06
- public static function changeAddPrice($productId, $addPrice)
- {
- return self::updateById($productId, ['addPrice' => $addPrice]);
- }
- //根据productId 计算花材的重量
- public static function getWeight($productId, $bigNum, $smallNum)
- {
- $productInfo = self::getById($productId);
- if (empty($productInfo)) {
- return 0;
- }
- $ratio = $productInfo['ratio'];
- $weight = $productInfo['weight'];
- $mergeNum = self::mergeItemNum($bigNum, $smallNum, $ratio);
- return bcmul($mergeNum, $weight, 2);
- }
- //修改product
- public static function updateProduct($data, $shop)
- {
- $id = $data['id'] ?? 0;
- $sjId = $shop->sjId ?? 0;
- $shopId = $shop->id ?? 0;
- $mainId = $shop->mainId ?? 0;
- $product = self::getLockById($id);
- if (empty($product)) {
- util::fail("该花材不存在");
- }
- $upData = [];
- //库存可以修改
- if (isset($data['stock']) && is_numeric($data['stock'])) {
- $preStock = $product->stock ?? 0;
- if ($data['stock'] != $preStock) {
- if ($data['stock'] < 0) {
- util::fail("库存不能小于0");
- }
- if (floor($data['stock']) != $data['stock']) {
- util::fail('库存请填写整数');
- }
- //增加盘点记录 2021.7.27
- $ratio = $product->ratio ?? 0;
- $formNum = ProductClass::formatStock($data['stock'], $ratio);
- $ghsItemInfo = [['productId' => $product->id, 'bigNum' => $formNum['bigNum'], 'smallNum' => $formNum['smallNum'],]];
- $pdData = [];
- $pdData['itemInfo'] = $ghsItemInfo;
- $pdData['sjId'] = $sjId;
- $pdData['shopId'] = $shopId;
- $pdData['adminId'] = $data['adminId'];
- $pdData['mainId'] = $mainId;
- $pdData['staffName'] = $data['staffName'] ?? '';
- CheckOrderClass::opOrder($pdData, false, false);
- }
- unset($data['stock']);
- }
- unset($data['adminId']);
- if (isset($data['name'])) {
- if (empty($data['name'])) {
- util::fail("请输入花材名称");
- }
- $upData['name'] = $data['name'];
- }
- if (isset($data['py']) && !empty($data['py'])) {
- $upData['py'] = $data['py'];
- }
- if (isset($data['cover']) && !empty($data['cover'])) {
- $upData['cover'] = $data['cover'];
- }
- if (isset($data['discountPrice']) && is_numeric($data['discountPrice'])) {
- $discountPrice = $data['discountPrice'] ?? 0;
- $currentPrice = $data['price'] ?? 0;
- if ($discountPrice >= $currentPrice) {
- util::fail('今日特价要小于售价');
- }
- $upData['discountPrice'] = $discountPrice;
- }
- //加价
- $addPrice = $data['addPrice'] ?? 0;
- $upData['addPrice'] = $addPrice;
- //价格
- $price = $data['price'] ?? 0;
- if (is_numeric($price) == false || $price < 0) {
- util::fail('请填写价格');
- }
- if (isset($data['skMore']) && is_numeric($data['skMore'])) {
- $upData['skMore'] = $data['skMore'];
- }
- if (isset($data['stockWarning'])) {
- $upData['stockWarning'] = $data['stockWarning'];
- }
- if (isset($data['inTurn'])) {
- $upData['inTurn'] = $data['inTurn'];
- }
- if (isset($data['delStatus'])) {
- if (!in_array($data['delStatus'], [0, 1])) {
- util::fail("上下架参数错误");
- }
- $upData['delStatus'] = $data['delStatus'];
- }
- if (isset($data['status']) && is_numeric($data['status'])) {
- $upData['status'] = $data['status'];
- }
- if (isset($data['variety']) && is_numeric($data['variety'])) {
- $upData['variety'] = $data['variety'];
- }
- //分类ID
- if (isset($data["classId"])) {
- $classId = $data['classId'];
- $upData['classId'] = $classId;
- }
- if (isset($data["scanNum"]) && is_numeric($data["scanNum"])) {
- $upData['scanNum'] = $data['scanNum'];
- }
- if (isset($data["smallRatio"]) && is_numeric($data["smallRatio"])) {
- $upData['smallRatio'] = $data['smallRatio'];
- }
- if (isset($data["smallShow"]) && is_numeric($data["smallShow"])) {
- $upData['smallShow'] = $data['smallShow'];
- }
- $paramsBack = $upData;
- $ptItemId = $product->itemId ?? 0;
- $currentSkPrice = null;
- if (isset($data['skMore']) && is_numeric($data['skMore'])) {
- $currentSkPrice = bcadd($price, $data['skMore'], 2);
- }
- if ($price != $product->price) {
- //花店价有变化
- self::changeSinglePrice($shop, $ptItemId, $price, $currentSkPrice);
- unset($upData['price']);
- } else {
- //花店价没有变化,但散客价比花店价贵多少有修改时,也要改价
- if (isset($data['skMore']) && is_numeric($data['skMore']) && $data['skMore'] != $product->skMore) {
- self::changeSinglePrice($shop, $ptItemId, $price, $currentSkPrice);
- unset($upData['price']);
- }
- }
- self::updateById($id, $upData);
- //不影响其它直营店花材的上下架
- unset($upData['status']);
- if (isset($shop->default) && $shop->default == 1) {
- //首店修改同步到所有直营店
- $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
- foreach ($chainShopList as $chainShop) {
- $chainShopId = $chainShop->id ?? 0;
- if (isset($chainShop->join) && $chainShop->join == 1) {
- continue;
- }
- if ($chainShopId == $shop->id) {
- //前面已经添加过了
- continue;
- }
- $chainMainId = $chainShop->mainId ?? 0;
- $params = $paramsBack;
- //所有直营店上下架、启用停用不同步
- unset($params['status']);
- unset($params['delStatus']);
- //如果有同步分类则移动,没有则不移动
- if (isset($params['classId'])) {
- $masterClassId = $params['classId'];
- unset($params['classId']);
- $masterClass = ItemClassClass::getById($masterClassId, true);
- $masterClassName = $masterClass->name ?? '';
- $classList = ItemClassClass::getAllByCondition(['mainId' => $chainMainId], null, '*', null, true);
- if (!empty($classList)) {
- foreach ($classList as $currentClass) {
- if ($currentClass->name == $masterClassName) {
- $params['classId'] = $currentClass->id ?? 0;
- }
- }
- }
- }
- $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
- if (empty($chainProduct)) {
- continue;
- }
- if ($price != $chainProduct->price) {
- //花店价有变化
- self::changeSinglePrice($chainShop, $ptItemId, $price, $currentSkPrice);
- unset($params['price']);
- } else {
- //花店价没有变化,但散客价比花店价贵多少有修改时,也要改价
- if (isset($data['skMore']) && is_numeric($data['skMore']) && $data['skMore'] != $chainProduct->skMore) {
- self::changeSinglePrice($chainShop, $ptItemId, $price, $currentSkPrice);
- unset($params['price']);
- }
- }
- self::updateById($chainProduct->id, $params);
- }
- }
- }
- //变成自动改价 2021.4.14
- public static function autoPriceById($productId)
- {
- $product = self::getById($productId, true);
- if (empty($product)) {
- return false;
- }
- $price = bcadd($product->cost, $product->addPrice, 2);
- self::changePrice($product, $price, self::PRICE_LABEL_AUTO);
- }
- //获取当前门店下所有的product
- public static function getAllProduct($sjId, $shopId, $field = "*")
- {
- $where = [
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- ];
- return self::getAllByCondition($where, null, $field);
- }
- //获取当前门店下所有花材的库存数量,及成本价,库存预警的花材数量 lqh 2021-04-20
- public static function getProductStockByShopId($sjId, $shopId)
- {
- $productInfo = self::getAllProduct($sjId, $shopId, "*");
- $data = [
- 'stockBigNum' => 0,
- 'stockSmallNum' => 0,
- 'stockCost' => 0,
- 'stockWarning' => 0,
- ];
- if ($productInfo) {
- foreach ($productInfo as $v) {
- $stock = self::formatStock($v['stock'], $v['ratio']);
- $data['stockBigNum'] = bcadd($data['stockBigNum'], $stock['bigNum']);
- $data['stockSmallNum'] = bcadd($data['stockSmallNum'], $stock['smallNum']);
- $data['stockCost'] = bcadd($data['stockCost'], bcmul($v['stock'], $v['cost'], 2), 2);
- if ($v['stock'] <= $v['stockWarning']) {
- $data['stockWarning']++;
- }
- }
- }
- return $data;
- }
- //补className
- public static function groupClassName($sjId, $data)
- {
- $itemClassInfo = ItemClassClass::getAll($sjId, "id,name");
- $classNameMap = [];
- foreach ($itemClassInfo as $v) {
- $classNameMap[$v['id']] = $v['name'];
- }
- foreach ($data as $k => $v) {
- $classId = $v['classId'];
- $className = $classNameMap[$classId] ?? '';
- $v['className'] = $className;
- $data[$k] = $v;
- }
- return $data;
- }
- //供货商的productId => 零售端的productId ,不存在的补productId(放在默认分类)
- //productList [{"productId":"113","bigNum":1,"smallNum":0,"classId":"-2","itemId":1}]
- public static function toggleProductList($productList, $hdShopId, $hdSjId, $hdMainId)
- {
- $itemIds = array_column($productList, "itemId");
- $hdProductData = self::getProductInfoByItemIds($itemIds, $hdMainId);
- $hdProductData = array_column($hdProductData, null, "itemId");
- //获取默认分类ID
- $defaultClassId = ItemClassClass::getDefaultClassId($hdMainId);
- if (empty($defaultClassId)) {
- util::fail('没有找到花材的默认分类');
- }
- foreach ($productList as $k => $v) {
- $itemId = $v['itemId'] ?? 0;
- if (!isset($hdProductData[$itemId])) {
- //不存在,则从平台新增
- $ptItem = \biz\item\classes\PtItemClass::getById($itemId);
- unset($ptItem['addTime']);
- unset($ptItem['updateTime']);
- $productData = $ptItem;
- $productData['sjId'] = $hdSjId;
- $productData['shopId'] = $hdShopId;
- $productData['mainId'] = $hdMainId;
- $productData['itemId'] = $itemId;
- $productData['classId'] = $defaultClassId;
- unset($productData['id']);
- $productData['rank'] = 'A';
- //计算价格
- $cost = $v['cost'] ?? 0;
- $addPrice = $v['addPrice'] ?? 0;
- $productData['price'] = bcadd($cost, $addPrice, 2);
- $addProduct = ProductClass::add($productData);
- $hdProductId = $addProduct['id'];
- } else {
- $hdProductId = $hdProductData[$itemId]['id'];
- }
- $productList[$k]['hdProductId'] = $hdProductId;
- }
- return $productList;
- }
- //供货商、零售端新开门店时,初始化花材
- public static function newShopInitProduct($sjId, $shopId, $newShopId, $adminId)
- {
- $product = self::getAllProduct($sjId, $shopId, "name,cover,classId,itemId,rank,price,priceLabel,cost,addPrice,stockWarning,weight,alias,py,ratio,bigUnit,smallUnit,bigUnitId,smallUnitId,inTurn");
- $productData = [];
- foreach ($product as $v) {
- $tmp = $v;
- $tmp['sjId'] = $sjId;
- $tmp['shopId'] = $newShopId;
- $tmp['adminId'] = $adminId;
- $productData[] = $tmp;
- }
- self::batchAdd($productData);
- }
- //花材销量增加
- public static function updateActualSoldById($id, $itemNum)
- {
- $data['actualSold'] = $itemNum;
- $res = self::updateById($id, $data);
- return $res;
- }
- //新增花材
- public static function addProduct($data, $currentShop)
- {
- $currentMainId = $currentShop->mainId ?? 0;
- $classId = $data['classId'] ?? 0;
- $mainId = $data['mainId'] ?? 0;
- $stockWarning = $data['stockWarning'] ?? false;
- $data['py'] = stringUtil::py($data['name']);
- $name = $data['name'] ?? false;
- $data['alias'] = $data['alias'] ?? '';
- $data['cover'] = isset($data['cover']) && !empty($data['cover']) ? $data['cover'] : '';
- $ratio = $data['ratio'] ?? false;
- $weight = $data['weight'] ?? false;
- $data['bigUnit'] = $data['bigUnit'] ?? '扎';
- $data['smallUnit'] = $data['smallUnit'] ?? '支';
- $data['shopId'] = $data['shopId'] ?? 0;
- $currentShopId = $data['shopId'];
- $data['sjId'] = $data['sjId'] ?? 0;
- if (!$data['sjId']) {
- util::fail("参数错误");
- }
- $stock = 0;
- if (isset($data['stock']) && is_numeric($data['stock'])) {
- if ($data['stock'] < 0) {
- util::fail("库存数不能小于0");
- }
- $stock = $data['stock'];
- }
- //没有库存则下架
- $data['status'] = $stock == 0 ? 2 : 1;
- if (empty($classId)) {
- util::fail("请选择分类");
- }
- if (isset($data['price']) == false || is_numeric($data['price']) == false || $data['price'] < 0) {
- util::fail("请输入价格");
- }
- if (isset($data['addPrice']) == false || is_numeric($data['addPrice']) == false) {
- util::fail("请输入加价");
- }
- if ($stockWarning === false) {
- util::fail("请输入库存预警值");
- }
- if ($name === false) {
- util::fail("请输入花材名称");
- }
- if ($ratio === false) {
- util::fail("请输入花材比例");
- }
- if ($weight === false) {
- util::fail("请输入花材重量");
- }
- $cat = ItemClassClass::getById($classId, true);
- if (empty($cat)) {
- util::fail("花材分类不存在");
- }
- if ($cat->mainId != $mainId) {
- util::fail('不是您的分类');
- }
- $catName = $cat->name ?? '';
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- if (isset($data['itemId']) == false || empty($data['itemId'])) {
- //添加新品种先在平台新增花材
- $defaultInfo = PtItemClassClass::getByCondition(['isDefault' => 1]);
- $defaultClassId = $defaultInfo['id'] ?? 0;
- if (empty($defaultClassId)) {
- util::fail('没有找到平台默认分类');
- }
- $itemData = $data;
- $itemData['classId'] = $defaultClassId;
- $itemData['skAddPrice'] = $itemData['skAddPrice'] ?? 3;
- $itemData['hjAddPrice'] = $itemData['hjAddPrice'] ?? 2;
- $itemData['zsAddPrice'] = $itemData['zsAddPrice'] ?? 1;
- $ptItemInfo = PtItemClass::addItem($itemData);
- $data['itemId'] = $ptItemInfo['id'];
- } else {
- //从平台导入花材
- $has = self::getByCondition(['mainId' => $mainId, 'itemId' => $data['itemId']], true);
- if (!empty($has)) {
- util::fail('已经导入过了哦');
- }
- }
- $ptItemId = $data['itemId'] ?? 0;
- //小菊的结构
- $ptItemInfo = PtItemClass::getById($ptItemId, true);
- $data['variety'] = $ptItemInfo->variety ?? 0;
- //花店价比进货价贵多少 2021-12-07 lqh 如果前端有传取前端的,没有则取pt
- $addPrice = $data['addPrice'] ?? $ptItemInfo->addPrice;
- $data['addPrice'] = $addPrice;
- //花店价
- $price = $data['price'] ?? 0;
- if (empty($price) || $price <= 0) {
- util::fail('请填写价格');
- }
- //散客价
- $skMore = isset($data['skMore']) && $data['skMore'] > 0 ? $data['skMore'] : 0;
- if ($skMore <= 0) {
- util::fail('请填写散客价比花店价贵多少');
- }
- $data['skPrice'] = bcadd($price, $skMore, 2);
- $data['property'] = 1;
- $respond = self::add($data, true);
- if ($stock > 0) {
- //有库存数量,增加盘盈记录 2021.7.9 lqh
- //增加盘点订单
- $pdOrderSn = orderSn::getGhsCheckSn();
- $pdOrderData = [];
- $pdOrderData['orderSn'] = $pdOrderSn;
- $pdOrderData['sjId'] = $data['sjId'];
- $pdOrderData['shopId'] = $data['shopId'];
- $pdOrderData['status'] = CheckOrderClass::STATUS_COMPLETE;
- $pdOrderData['bigNum'] = $stock;
- $pdOrderData['adminId'] = $data['adminId'];
- $pdOrderData['smallNum'] = 0;
- $pdOrderData['price'] = $data['price'];
- $pdOrderData['remark'] = '初始库存';
- CheckOrderClass::add($pdOrderData);
- $pdOrderItemData = [];
- $pdOrderItemData['orderSn'] = $pdOrderSn;
- $pdOrderItemData['productId'] = $respond->id ?? 0;
- $pdOrderItemData['itemId'] = $data['itemId'];
- $pdOrderItemData['itemId'] = $data['itemId'];
- $pdOrderItemData['itemNum'] = $stock;
- $pdOrderItemData['itemStock'] = 0;
- $pdOrderItemData['profitLostNum'] = $stock;
- $itemInfo = [
- 'itemName' => $name,
- 'itemCover' => $data['cover'],
- 'bigNum' => $stock,
- 'smallNum' => 0,
- 'bigNumStock' => 0,
- 'smallNumStock' => 0,
- 'bigNumProfitLoss' => $stock,
- 'smallNumProfitLoss' => 0,
- 'itemUnit' => 0,
- 'ratio' => $ratio,
- 'rank' => 'A',
- 'bigUnit' => $data['bigUnit'],
- 'smallUnit' => $data['smallUnit'],
- ];
- $pdOrderItemData['itemInfo'] = json_encode($itemInfo);
- $pdOrderItemData['itemPrice'] = $data['price'];
- $pdOrderItemData['itemCost'] = $data['cost'];
- CheckOrderItemClass::add($pdOrderItemData);
- $record = [];
- $record['sjId'] = $data['sjId'];
- $record['shopId'] = $data['shopId'];
- //增加中央ID
- $record['mainId'] = $currentMainId;
- $record['orderSn'] = $pdOrderSn;
- $record['itemId'] = $data['itemId'];
- $record['productId'] = $respond->id ?? 0;
- $record['itemNum'] = $stock;//盈亏数
- $record['oldStock'] = 0;
- $record['newStock'] = $stock; //新库存直接等于盘点的数量
- $record['relateName'] = $data['staffName'] ?? '';
- StockRecordClass::addCheckOrderRecord($record);
- }
- //在首店添加,则找到父级sj下的所有直营店,同步添加花材
- if (isset($currentShop->default) && $currentShop->default == 1) {
- $shopList = ShopClass::getAllByCondition(['sjId' => $data['sjId']], null, '*', null, true);
- foreach ($shopList as $shop) {
- $shopId = $shop->id ?? 0;
- if (isset($shop->join) && $shop->join == 1) {
- continue;
- }
- if ($shopId == $currentShop->id) {
- //前面已经添加过了
- continue;
- }
- $thisMainId = $shop->mainId ?? 0;
- $data['shopId'] = $shopId;
- $data['mainId'] = $thisMainId;
- //不是当前门店,库存默认为0并且是下架状态。花材分类放在同名分类下,如果没有则放默认分类
- if ($currentShopId != $shopId) {
- $data['stock'] = 0;
- $data['status'] = 2;
- $currentClassId = 0;
- $classList = ItemClassClass::getAllByCondition(['mainId' => $thisMainId], null, '*', null, true);
- if (!empty($classList)) {
- foreach ($classList as $currentClass) {
- $currentName = $currentClass->name ?? '';
- if ($currentName == $catName) {
- $currentClassId = $currentClass->id ?? 0;
- }
- }
- }
- if (empty($currentClassId)) {
- $defaultCat = ItemClassClass::getByCondition(['mainId' => $thisMainId, 'isDefault' => 1], true);
- $currentClassId = $defaultCat->id ?? 0;
- }
- if (empty($currentClassId)) {
- util::fail('直营店没有分类,也没有默认分类');
- }
- $data['classId'] = $currentClassId;
- }
- //已经有了不再添加
- $chainHas = self::getByCondition(['mainId' => $thisMainId, 'itemId' => $ptItemId], true);
- if (!empty($chainHas)) {
- continue;
- }
- self::add($data);
- }
- }
- $transaction->commit();
- } catch (\Exception $exception) {
- $transaction->rollBack();
- Yii::info('失败原因:' . $exception->getMessage());
- util::fail('添加失败');
- }
- }
- //花材上下架
- public static function changeStatus($productId, $status)
- {
- $data = ['status' => $status];
- if ($status == 2) {
- //库存=0时下架,特价一起去掉
- $data['discountPrice'] = 0;
- }
- return self::updateById($productId, $data);
- }
- //获取itemId=>classId
- public static function getItemIdClassIdMap($sjId)
- {
- $map = [];
- $itemData = self::getGhsItemAll($sjId, "itemId,classId");
- foreach ($itemData as $v) {
- $map[$v['itemId']] = $v['classId'];
- }
- return $map;
- }
- //获取当前供货商下的所有item
- public static function getGhsItemAll($sjId, $select = "*")
- {
- $where = [
- 'sjId' => $sjId
- ];
- $data = self::getAllList($select, $where);
- return $data;
- }
- //库存预警通知 ssh 20210909
- public static function stockWarningAddQueue($product, $stock)
- {
- $warningNum = $product['stockWarning'] ?? 0;
- if ($warningNum <= 0) {
- return false;
- }
- $date = date("Y-m-d");
- $productId = $product['id'] ?? 0;
- $key = 'stockWarning_' . $productId . '_' . $date;
- //当天已经放入队列通知的不再通知
- $hasWarning = Yii::$app->redis->executeCommand('GET', [$key]);
- if (isset($hasWarning) && !empty($hasWarning)) {
- return false;
- }
- if ($stock <= $warningNum) {
- Yii::$app->redis->executeCommand('SETEX', [$key, 86400, 'hasWarning']);
- $shopId = $product['shopId'] ?? 0;
- if (empty($shopId)) {
- return false;
- }
- //队列方式去处理
- $stockWarningListKey = 'stockWarningList_' . $shopId;
- Yii::$app->redis->executeCommand('LPUSH', [$stockWarningListKey, $productId]);
- }
- }
- //所有门店进行库存通知 ssh 20210909
- public static function allShopStockWarningFromQueue()
- {
- $ghs = dict::getDict('ptStyle', 'ghs');
- $shopList = ShopClass::getAllByCondition(['ptStyle' => $ghs], null, '*', null, true);
- if (!empty($shopList)) {
- foreach ($shopList as $key => $shop) {
- $ids = [];
- $shopId = $shop->id ?? 0;
- $stockWarningListKey = 'stockWarningList_' . $shopId;
- while ($count = Yii::$app->redis->executeCommand('LLEN', [$stockWarningListKey])) {
- $productId = Yii::$app->redis->executeCommand('RPOP', [$stockWarningListKey]);
- if (is_numeric($productId)) {
- $ids[] = $productId;
- }
- }
- if (empty($ids)) {
- continue;
- }
- $ids = array_unique(array_filter($ids));
- $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
- if (!empty($productList)) {
- WxMessageClass::stockWarningInform($shop, $productList);
- }
- }
- }
- }
- //通过花材ID 获取mainId lqh 2022.03.20
- public static function getMainIdByProductId($productId)
- {
- $product = self::getById($productId, false);
- return $product['mainId'] ?? 0;
- }
- }
|