Sfoglia il codice sorgente

登陆变更自动获取

shish 5 anni fa
parent
commit
043d69e9d0

+ 2 - 2
app/ghs/controllers/BaseController.php

@@ -31,8 +31,8 @@ class BaseController extends PublicController
 	public function beforeAction($action)
 	{
 		//token验证
-		//$adminId = jwt::getLoginId();
-		$adminId = 18;
+		$adminId = jwt::getLoginId();
+		//$adminId = 18;
 		if (empty($adminId)) {
 			util::notLogin();
 		}

+ 17 - 5
biz-ghs/base/classes/BaseClass.php

@@ -2,12 +2,24 @@
 
 namespace bizGhs\base\classes;
 
+use Yii;
+
 class BaseClass extends \common\base\classes\BaseClass
 {
+	
+	//组合图片
+	public static function groupImg($img)
+	{
+		if (empty($img)) {
+			return self::getDefaultImg();
+		}
+		return Yii::$app->params['imgHost'] . $img;
+	}
 
-    public static function groupImg($img)
-    {
-        return \Yii::$app->params['imgHost'].$img;
-    }
-
+	//获取默认图片
+	public static function getDefaultImg()
+	{
+		$cover = Yii::$app->params['imgHost'] . '/hhb_small.png';
+	}
+	
 }

+ 14 - 0
biz-ghs/order/classes/OrderExpressClass.php

@@ -24,4 +24,18 @@ class OrderExpressClass extends BaseClass
 		];
 	}
 
+	//取当前订单的配送信息
+	public static function getExpress($orderId)
+	{
+		$info = self::getByCondition(['orderId' => $orderId]);
+		if (empty($info)) {
+			return [];
+		}
+		$sideId = $info['sideId'];
+		$sideData = self::$side;
+		$sideName = isset($sideData[$sideId]) ? $sideData[$sideId] : '';
+		$info['sideName'] = $sideName;
+		return $info;
+	}
+
 }

+ 1 - 0
biz-ghs/order/services/OrderSendService.php

@@ -4,6 +4,7 @@ namespace bizGhs\order\services;
 
 use bizGhs\base\services\BaseService;
 use bizGhs\order\classes\OrderClass;
+use bizGhs\order\classes\OrderExpressClass;
 use bizGhs\order\classes\OrderSendClass;
 use Yii;
 

+ 449 - 451
biz-ghs/product/classes/ProductClass.php

@@ -12,455 +12,453 @@ use Yii;
 
 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'; //改价锁
-
-    //根据itemIds获取相应的花材信息
-    public static function getProductInfoByItemIds(array $itemIds, $shopId)
-    {
-        $where = ['itemId' => ['in', $itemIds], 'delStatus' => 0, 'shopId' => $shopId];
-        $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 {
-                    //没有图片时取默认图片 shish 2021.2.18
-                    $cover = Yii::$app->params['imgHost'] . '/hhb_small.png';
-                }
-                $unitNum = $data[$v['itemId']]['unitNum'] ?? 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'] = xhItemService::getUnitName($data[$v['itemId']]['bigUnit']);
-                $list[$k]['smallUnit'] = xhItemService::getUnitName($data[$v['itemId']]['smallUnit']);
-            }
-        }
-        return $list;
-    }
-
-
-    //根据花材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::getAllList("itemId", ['shopId' => $shopId], 'actualSold desc');
-        if ($data) {
-            $data = array_column($data, 'itemId');
-        }
-        return $data;
-    }
-
-
-
-    //加库存,允许负库存,负数数量(盘点时)
-
-    /**
-     * @param $productId  门店下的花材ID(xhGhsItemInfo 主键ID)
-     * @param $itemNumBundle  花材数量(扎)
-     * @param $itemNumPiece  花材数量(支)
-     * @return bool
-     */
-    public static function addStock($productId, $itemNumBundle, $itemNumPiece)
-    {
-        $productData = self::getById($productId);
-        if (!$productData) {
-            util::fail("花材不存在");
-        }
-        $itemId = $productData['itemId'];
-        $unitNum = xhItemService::getItemUnitNum($itemId);
-        //转成小数,加库存
-        $itemNum = self::mergeItemNum($itemNumBundle, $itemNumPiece, $unitNum);
-        $newStock = bcadd($productData['stock'], $itemNum, 2);
-        self::updateStockById($productId, $newStock);
-        return true;
-    }
-
-
-    //todo 库存修改 加锁
-
-    /**
-     * 对商品ID 加库存
-     * @param $productId  商品ID
-     * @param $itemNum  数量
-     * @return bool
-     */
-    public static function addStockByItemNum($productId, $itemNum)
-    {
-        $productData = self::getById($productId, true);
-        if (!$productData) {
-            util::fail("花材不存在");
-        }
-        $newStock = bcadd($productData->stock, $itemNum, 2);
-        self::updateStockById($productId, $newStock);
-        return true;
-    }
-
-    //减库存,允许负库存,负数数量(盘点时)
-    //todo 库存修改 加锁
-    /**
-     * @param $productId  门店下的花材ID(xhGhsItemInfo 主键ID)
-     * @param $itemNumBundle  花材数量(扎)
-     * @param $itemNumPiece  花材数量(支)
-     * @return bool
-     */
-    public static function decreaseStock($productId, $itemNumBundle, $itemNumPiece)
-    {
-        $productData = self::getById($productId, true);
-        if (!$productData) {
-            util::fail("花材不存在");
-        }
-        $itemId = $productData['itemId'];
-        $unitNum = xhItemService::getItemUnitNum($itemId);
-        //转成小数,更新库存
-        $itemNum = self::mergeItemNum($itemNumBundle, $itemNumPiece, $unitNum);
-        $newStock = bcsub($productData->stock, $itemNum, 2);
-        self::updateStockById($productId, $newStock);
-        return true;
-    }
-
-
-    //根据数量减库存
-    //todo 库存修改 加锁
-    /**
-     * @param $productId
-     * @param $itemNum
-     * @return bool
-     */
-    public static function decreaseStockByItemNum($productId, $itemNum)
-    {
-        $productData = self::getById($productId);
-        if (!$productData) {
-            util::fail("花材不存在");
-        }
-        $newStock = bcsub($productData['stock'], $itemNum, 2);
-        self::updateStockById($productId, $newStock);
-        return true;
-    }
-
-
-
-    //计算价格
-
-    /**
-     * @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  linqh 2021.1.24
-    public static function getGhsProductDataByItemIds($shopId, $itemIds)
-    {
-        $where = ['itemId' => ['in', $itemIds], 'shopId' => $shopId];
-        $data = self::getAllByCondition($where, null, "*");
-        return $data;
-    }
-
-    //补全itemId (针对入库时 当前门店下无对应的花材 itemId)  linqh 2021.1.24
-    public static function complementProduct($merchantId, $shopId, $itemInfo)
-    {
-        $itemIds = array_column($itemInfo, "itemId");
-        $itemData = array_column($itemInfo, null, "itemId");
-        $productData = self::getGhsProductDataByItemIds($shopId, $itemIds);
-
-        if (count($productData) !== count($itemIds)) {
-            $productDataItemIds = array_column($productData, 'itemId');
-            //取差集
-            $diffItemIds = array_diff($itemIds, $productDataItemIds);
-            if ($diffItemIds) {
-                //新增到product表,price = itemPrice (出库时的售价), stock=0
-                foreach ($diffItemIds as $v) {
-                    $addData = [];
-                    $addData['merchantId'] = $merchantId;
-                    $addData['shopId'] = $shopId;
-                    $addData['itemId'] = $v;
-                    $addData['price'] = $itemData[$v]['itemPrice'] ?? 0;
-                    self::add($addData);
-                }
-            }
-        }
-    }
-
-    //有效花材判断,包括id有效、同个门店等 shish 2021.1.19
-    public static function valid($list, $shopId)
-    {
-        $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['shopId']) == false || $product['shopId'] != $shopId) {
-                    util::fail('只能选择同一家的商品下单');
-                }
-            }
-            if (count($productList) != count($ids)) {
-                util::fail('存在无效商品');
-            }
-        } else {
-            util::fail('商品不存在');
-        }
-    }
-
-    //通过ids 批量获取花材商品的信息
-    public static function getProductByIds($ids)
-    {
-        return self::getByIds($ids);
-    }
-
-    //修改花材商品的库存: 统一方法
-    //todo 库存修改 加锁
-    public static function updateStockById($id, $stock, $adminId = 0)
-    {
-        $key = self::LOCK_STOCK . '_' . $id;
-        $lock = util::lock($key);
-        if (!$lock) {
-            util::fail("系统繁忙中,请稍后再试!");
-        }
-        $data['stock'] = $stock;
-        if ($adminId) {
-            $data['adminId'] = $adminId;
-        }
-        $res = self::updateById($id, $data);
-        util::unlock($key);
-        return $res;
-    }
-
-    //订单退回库存
-    public static function backStockByOrderItemInfo($orderItemInfo)
-    {
-        foreach ($orderItemInfo as $v) {
-            ProductClass::addStockByItemNum($v['productId'], $v['itemNum']);
-        }
-    }
-
-
-    //库存告警的itemIds (库存数量少于告警数量)  linqh 2021.1.26
-    public static function getItemIdsWarning($merchantId, $shopId)
-    {
-        //获取当前门店下的 itemId=>stock
-        $productData = ProductClass::getAllList("itemId,stock", ['shopId' => $shopId]);
-        $productItemStock = [];
-        if ($productData) {
-            foreach ($productData as $v) {
-                $productItemStock[$v['itemId']] = $v['stock'];
-            }
-        }
-
-        //获取merchantId 下 对应的itemId=>stockWaring
-        $itemData = ItemClass::getAllList("itemId,stockWarning", ['merchantId' => $merchantId]);
-        $itemStockWaring = [];
-        if ($itemData) {
-            foreach ($itemData as $v) {
-                $itemStockWaring[$v['itemId']] = $v['stockWarning'];
-            }
-        }
-
-        //取库存数量少于告警数量 的itemId
-        $itemIds = [];
-        foreach ($productItemStock as $k => $v) {
-            $stockWaring = $itemStockWaring[$k] ?? 0;
-            if ($v < $stockWaring) {
-                $itemIds[] = $k;
-            }
-        }
-        return $itemIds;
-    }
-
-    //获取当前门店下商品信息 linqh 2021.1.28
-    public static function getProductData($id, $shopId, $obj = false)
-    {
-        return ProductClass::getByCondition(['id' => $id, 'shopId' => $shopId], $obj);
-    }
-
-    //修改价格 linqh 2021.1.28
-    public static function changePrice($id, $shopId, $price, $priceLabel)
-    {
-
-        $res = ProductClass::updateByCondition(['id' => $id, 'shopId' => $shopId], ['price' => $price, 'priceLabel' => $priceLabel]);
-        return $res;
-    }
-
-    //下单时计算商品的价格  linqh 2021.1.28
-    //itemInfo : [{"classId":5,"productId":3,"bigNum":1,"smallNum":2},{"classId":5,"productId":4,"bigNum":1,"smallNum":5}]
-    //返回 [{"productId":3,"price":2.1}]
-    public static function computedPriceByItemInfo(array $itemInfo)
-    {
-        $data = [];
-        if (empty($itemInfo)) {
-            return $data;
-        }
-        $itemInfo = self::mergeItemInfo($itemInfo);
-        $productIds = array_column($itemInfo, 'productId');
-        $productData = ProductClass::getProductByIds($productIds);
-        $productData = array_column($productData, NULL, 'id');
-
-        $itemIds = array_column($productData, 'itemId');
-        $itemData = xhItemService::getByIds($itemIds);
-        $itemData = array_column($itemData, NULL, 'id');
-        foreach ($itemInfo as $v) {
-            $productId = $v['productId'];
-            $itemId = $productData[$productId]['itemId'] ?? 0;
-            $unitNum = $itemData[$itemId]['unitNum'] ?? 0;
-            //大小数量合并多少扎
-            $itemNum = self::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum);
-            //售卖价格
-            $itemPrice = $productData[$productId]['price'] ?? 0;
-            //合计价格
-            $price = bcmul($itemNum, $itemPrice, 2);
-            $cover = Yii::$app->params['imgHost'] . '/hhb_small.png';
-            if (isset($itemData[$itemId]['cover']) && !empty($itemData[$itemId]['cover'])) {
-                $cover = self::groupImg($itemData[$itemId]['cover'] ?? '');
-            }
-            $tmp = [];
-            $tmp['productId'] = $productId;
-            $tmp['price'] = $price;
-
-            $tmp['name'] = $itemData[$itemId]['name'] ?? '';
-            $tmp['smallUnit'] = xhItemService::getUnitName($itemData[$itemId]['smallUnit']);
-            $tmp['bigUnit'] = xhItemService::getUnitName($itemData[$itemId]['bigUnit']);
-            $tmp['cover'] = $cover;
-            $tmp['itemId'] = $itemId;
-            $tmp['rank'] = $productData[$productId]['rank'] ?? '';
-            $tmp['unitPrice'] = $itemPrice;
-            $tmp['num'] = $itemNum;
-            $tmp['bigNum'] = $v['bigNum'];
-            $tmp['smallNum'] = $v['smallNum'];
-
-            $data[] = $tmp;
-        }
-        return $data;
-    }
-
+	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'; //改价锁
+	
+	//根据itemIds获取相应的花材信息
+	public static function getProductInfoByItemIds(array $itemIds, $shopId)
+	{
+		$where = ['itemId' => ['in', $itemIds], 'delStatus' => 0, 'shopId' => $shopId];
+		$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 {
+					//没有图片时取默认图片 shish 2021.2.18
+					$cover = Yii::$app->params['imgHost'] . '/hhb_small.png';
+				}
+				$unitNum = $data[$v['itemId']]['unitNum'] ?? 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'] = xhItemService::getUnitName($data[$v['itemId']]['bigUnit']);
+				$list[$k]['smallUnit'] = xhItemService::getUnitName($data[$v['itemId']]['smallUnit']);
+			}
+		}
+		return $list;
+	}
+	
+	
+	//根据花材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::getAllList("itemId", ['shopId' => $shopId], 'actualSold desc');
+		if ($data) {
+			$data = array_column($data, 'itemId');
+		}
+		return $data;
+	}
+	
+	
+	
+	//加库存,允许负库存,负数数量(盘点时)
+	
+	/**
+	 * @param $productId  门店下的花材ID(xhGhsItemInfo 主键ID)
+	 * @param $itemNumBundle  花材数量(扎)
+	 * @param $itemNumPiece  花材数量(支)
+	 * @return bool
+	 */
+	public static function addStock($productId, $itemNumBundle, $itemNumPiece)
+	{
+		$productData = self::getById($productId);
+		if (!$productData) {
+			util::fail("花材不存在");
+		}
+		$itemId = $productData['itemId'];
+		$unitNum = xhItemService::getItemUnitNum($itemId);
+		//转成小数,加库存
+		$itemNum = self::mergeItemNum($itemNumBundle, $itemNumPiece, $unitNum);
+		$newStock = bcadd($productData['stock'], $itemNum, 2);
+		self::updateStockById($productId, $newStock);
+		return true;
+	}
+	
+	
+	//todo 库存修改 加锁
+	
+	/**
+	 * 对商品ID 加库存
+	 * @param $productId  商品ID
+	 * @param $itemNum  数量
+	 * @return bool
+	 */
+	public static function addStockByItemNum($productId, $itemNum)
+	{
+		$productData = self::getById($productId, true);
+		if (!$productData) {
+			util::fail("花材不存在");
+		}
+		$newStock = bcadd($productData->stock, $itemNum, 2);
+		self::updateStockById($productId, $newStock);
+		return true;
+	}
+	
+	//减库存,允许负库存,负数数量(盘点时)
+	//todo 库存修改 加锁
+	/**
+	 * @param $productId  门店下的花材ID(xhGhsItemInfo 主键ID)
+	 * @param $itemNumBundle  花材数量(扎)
+	 * @param $itemNumPiece  花材数量(支)
+	 * @return bool
+	 */
+	public static function decreaseStock($productId, $itemNumBundle, $itemNumPiece)
+	{
+		$productData = self::getById($productId, true);
+		if (!$productData) {
+			util::fail("花材不存在");
+		}
+		$itemId = $productData['itemId'];
+		$unitNum = xhItemService::getItemUnitNum($itemId);
+		//转成小数,更新库存
+		$itemNum = self::mergeItemNum($itemNumBundle, $itemNumPiece, $unitNum);
+		$newStock = bcsub($productData->stock, $itemNum, 2);
+		self::updateStockById($productId, $newStock);
+		return true;
+	}
+	
+	
+	//根据数量减库存
+	//todo 库存修改 加锁
+	/**
+	 * @param $productId
+	 * @param $itemNum
+	 * @return bool
+	 */
+	public static function decreaseStockByItemNum($productId, $itemNum)
+	{
+		$productData = self::getById($productId);
+		if (!$productData) {
+			util::fail("花材不存在");
+		}
+		$newStock = bcsub($productData['stock'], $itemNum, 2);
+		self::updateStockById($productId, $newStock);
+		return true;
+	}
+	
+	
+	
+	//计算价格
+	
+	/**
+	 * @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  linqh 2021.1.24
+	public static function getGhsProductDataByItemIds($shopId, $itemIds)
+	{
+		$where = ['itemId' => ['in', $itemIds], 'shopId' => $shopId];
+		$data = self::getAllByCondition($where, null, "*");
+		return $data;
+	}
+	
+	//补全itemId (针对入库时 当前门店下无对应的花材 itemId)  linqh 2021.1.24
+	public static function complementProduct($merchantId, $shopId, $itemInfo)
+	{
+		$itemIds = array_column($itemInfo, "itemId");
+		$itemData = array_column($itemInfo, null, "itemId");
+		$productData = self::getGhsProductDataByItemIds($shopId, $itemIds);
+		
+		if (count($productData) !== count($itemIds)) {
+			$productDataItemIds = array_column($productData, 'itemId');
+			//取差集
+			$diffItemIds = array_diff($itemIds, $productDataItemIds);
+			if ($diffItemIds) {
+				//新增到product表,price = itemPrice (出库时的售价), stock=0
+				foreach ($diffItemIds as $v) {
+					$addData = [];
+					$addData['merchantId'] = $merchantId;
+					$addData['shopId'] = $shopId;
+					$addData['itemId'] = $v;
+					$addData['price'] = $itemData[$v]['itemPrice'] ?? 0;
+					self::add($addData);
+				}
+			}
+		}
+	}
+	
+	//有效花材判断,包括id有效、同个门店等 shish 2021.1.19
+	public static function valid($list, $shopId)
+	{
+		$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['shopId']) == false || $product['shopId'] != $shopId) {
+					util::fail('只能选择同一家的商品下单');
+				}
+			}
+			if (count($productList) != count($ids)) {
+				util::fail('存在无效商品');
+			}
+		} else {
+			util::fail('商品不存在');
+		}
+	}
+	
+	//通过ids 批量获取花材商品的信息
+	public static function getProductByIds($ids)
+	{
+		return self::getByIds($ids);
+	}
+	
+	//修改花材商品的库存: 统一方法
+	//todo 库存修改 加锁
+	public static function updateStockById($id, $stock, $adminId = 0)
+	{
+		$key = self::LOCK_STOCK . '_' . $id;
+		$lock = util::lock($key);
+		if (!$lock) {
+			util::fail("系统繁忙中,请稍后再试!");
+		}
+		$data['stock'] = $stock;
+		if ($adminId) {
+			$data['adminId'] = $adminId;
+		}
+		$res = self::updateById($id, $data);
+		util::unlock($key);
+		return $res;
+	}
+	
+	//订单退回库存
+	public static function backStockByOrderItemInfo($orderItemInfo)
+	{
+		foreach ($orderItemInfo as $v) {
+			ProductClass::addStockByItemNum($v['productId'], $v['itemNum']);
+		}
+	}
+	
+	
+	//库存告警的itemIds (库存数量少于告警数量)  linqh 2021.1.26
+	public static function getItemIdsWarning($merchantId, $shopId)
+	{
+		//获取当前门店下的 itemId=>stock
+		$productData = ProductClass::getAllList("itemId,stock", ['shopId' => $shopId]);
+		$productItemStock = [];
+		if ($productData) {
+			foreach ($productData as $v) {
+				$productItemStock[$v['itemId']] = $v['stock'];
+			}
+		}
+		
+		//获取merchantId 下 对应的itemId=>stockWaring
+		$itemData = ItemClass::getAllList("itemId,stockWarning", ['merchantId' => $merchantId]);
+		$itemStockWaring = [];
+		if ($itemData) {
+			foreach ($itemData as $v) {
+				$itemStockWaring[$v['itemId']] = $v['stockWarning'];
+			}
+		}
+		
+		//取库存数量少于告警数量 的itemId
+		$itemIds = [];
+		foreach ($productItemStock as $k => $v) {
+			$stockWaring = $itemStockWaring[$k] ?? 0;
+			if ($v < $stockWaring) {
+				$itemIds[] = $k;
+			}
+		}
+		return $itemIds;
+	}
+	
+	//获取当前门店下商品信息 linqh 2021.1.28
+	public static function getProductData($id, $shopId, $obj = false)
+	{
+		return ProductClass::getByCondition(['id' => $id, 'shopId' => $shopId], $obj);
+	}
+	
+	//修改价格 linqh 2021.1.28
+	public static function changePrice($id, $shopId, $price, $priceLabel)
+	{
+		
+		$res = ProductClass::updateByCondition(['id' => $id, 'shopId' => $shopId], ['price' => $price, 'priceLabel' => $priceLabel]);
+		return $res;
+	}
+	
+	//下单时计算商品的价格  linqh 2021.1.28
+	//itemInfo : [{"classId":5,"productId":3,"bigNum":1,"smallNum":2},{"classId":5,"productId":4,"bigNum":1,"smallNum":5}]
+	//返回 [{"productId":3,"price":2.1}]
+	public static function computedPriceByItemInfo(array $itemInfo)
+	{
+		$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');
+		$itemIds = array_column($productData, 'itemId');
+		$itemData = xhItemService::getByIds($itemIds);
+		if (empty($itemData)) {
+			util::fail('请选择花材');
+		}
+		$itemData = array_column($itemData, NULL, 'id');
+		foreach ($itemInfo as $v) {
+			$productId = $v['productId'];
+			$itemId = $productData[$productId]['itemId'] ?? 0;
+			$unitNum = $itemData[$itemId]['unitNum'] ?? 0;
+			//大小数量合并多少扎
+			$itemNum = self::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum);
+			//售卖价格
+			$itemPrice = $productData[$productId]['price'] ?? 0;
+			//合计价格
+			$price = bcmul($itemNum, $itemPrice, 2);
+			$tmp = [];
+			$tmp['productId'] = $productId;
+			$tmp['price'] = $price;
+
+			$tmp['name'] = $itemData[$itemId]['name'] ?? '';
+			$tmp['smallUnit'] = xhItemService::getUnitName($itemData[$itemId]['smallUnit']);
+			$tmp['bigUnit'] = xhItemService::getUnitName($itemData[$itemId]['bigUnit']);
+			$tmp['cover'] = $itemData[$itemId]['shortCover'] ?? '';
+			$tmp['itemId'] = $itemId;
+			$tmp['rank'] = $productData[$productId]['rank'] ?? '';
+			$tmp['unitPrice'] = $itemPrice;
+			$tmp['num'] = $itemNum;
+			$tmp['bigNum'] = $v['bigNum'];
+			$tmp['smallNum'] = $v['smallNum'];
+
+			$data[] = $tmp;
+		}
+		return $data;
+	}
+	
 }

+ 2 - 0
common/components/dateUtil.php

@@ -61,6 +61,8 @@ class dateUtil
 				if (empty($endTime)) {
 					util::fail('请选择结束时间');
 				}
+				$startTime = $startTime . ' 00:00:00';
+				$endTime = $endTime . ' 23:59:59';
 				break;
 			default:
 				//今天

+ 83 - 63
common/services/xhItemService.php

@@ -7,72 +7,92 @@
 namespace common\services;
 
 
+use bizGhs\product\classes\ProductClass;
 use common\models\xhItem;
 use Yii;
+
 class xhItemService
 {
+	
+	//列表数据
+	public static function getItemList($select, $where, $order = '', $with = '')
+	{
+		$get = Yii::$app->request->get();
+		$page = isset($get['page']) ? $get['page'] : 1;
+		Yii::$app->params['page'] = $page;
+		$pageSize = isset($get['pageSize']) && !empty($get['pageSize']) ? $get['pageSize'] : Yii::$app->params['pageSize'];
+		$model = new xhItem();
+		$data = $model->getList($select, $where, $page, $pageSize, $order, $with);
+		$list = $data['list'];
+		if ($list) {
+			foreach ($list as $k => $v) {
+				$list[$k]['cover'] = Yii::$app->params['imgHost'] . $v['cover'];
+				$list[$k]['bigUnit'] = xhItemService::getUnitName($v['bigUnit']);
+				$list[$k]['smallUnit'] = xhItemService::getUnitName($v['smallUnit']);
+			}
+		}
+		$data['list'] = $list;
+		return $data;
+	}
+	
+	public static function getAllByCondition($condition, $order = null, $field = '*')
+	{
+		return (new xhItem())->getAllByCondition($condition, $order, $field);
+	}
+	
+	public static function getById($id)
+	{
+		return (new xhItem())->getById($id);
+	}
+	
+	public static function getItemUnitNum($id)
+	{
+		$itemData = self::getById($id);
+		$unitNum = $itemData['unitNum'];
+		return $unitNum;
+	}
+	
+	//批量获取item unitNum  [itemId=>unitNum]
+	public static function getItemUnitNums($ids)
+	{
+		$itemDatas = self::getByIds($ids);
+		$data = [];
+		if ($itemDatas) {
+			foreach ($itemDatas as $v) {
+				$data[$v['id']] = $v['unitNum'];
+			}
+		}
+		return $data;
+	}
+	
+	public static function getByIds($ids)
+	{
+		$list = (new xhItem())->getByIds($ids);
+		return self::groupBaseInfo($list);
+	}
+	
+	//组合常用信息 shish 2021.3.4
+	public static function groupBaseInfo($list)
+	{
+		if (empty($list)) {
+			return $list;
+		}
+		foreach ($list as $key => $val) {
+			$list[$key]['shortCover'] = $val['cover'] && !empty($val['cover']) ? $val['cover'] : '/hhb_small.png';
+			$cover = ProductClass::groupImg($val['cover']);
+			$list[$key]['cover'] = $cover;
+		}
+		return $list;
+	}
 
-    //列表数据
-    public static function getItemList($select, $where, $order = '', $with = '')
-    {
-        $get = Yii::$app->request->get();
-        $page = isset($get['page']) ? $get['page'] : 1;
-        Yii::$app->params['page'] = $page;
-        $pageSize = isset($get['pageSize']) && !empty($get['pageSize']) ? $get['pageSize'] : Yii::$app->params['pageSize'];
-        $model = new xhItem();
-        $data = $model->getList($select, $where, $page, $pageSize, $order, $with);
-        $list = $data['list'];
-        if($list){
-            foreach ($list as $k => $v) {
-                $list[$k]['cover'] = Yii::$app->params['imgHost'].$v['cover'];
-                $list[$k]['bigUnit'] = xhItemService::getUnitName($v['bigUnit']);
-                $list[$k]['smallUnit'] = xhItemService::getUnitName($v['smallUnit']);
-            }
-        }
-        $data['list'] = $list;
-        return $data;
-    }
-    public static function getAllByCondition($condition, $order = null, $field = '*')
-    {
-        return (new xhItem())->getAllByCondition($condition, $order,$field);
-    }
-
-    public static function getById($id){
-        return (new xhItem())->getById($id);
-    }
-
-    public static function getItemUnitNum($id)
-    {
-        $itemData = self::getById($id);
-        $unitNum = $itemData['unitNum'];
-        return $unitNum;
-    }
-
-    //批量获取item unitNum  [itemId=>unitNum]
-    public static function getItemUnitNums($ids)
-    {
-        $itemDatas = self::getByIds($ids);
-        $data = [];
-        if($itemDatas){
-            foreach ($itemDatas as $v){
-                $data[$v['id']] = $v['unitNum'];
-            }
-        }
-        return $data;
-    }
-
-    public static function getByIds($ids)
-    {
-        return (new xhItem())->getByIds($ids);
-    }
-
-    //先写死,之后查表
-    public static function getUnitName($unit)
-    {
-        $map = [
-            1=>'扎',
-            2=>'支',
-        ];
-        return $map[$unit]??'';
-    }
+	//先写死,之后查表
+	public static function getUnitName($unit)
+	{
+		$map = [
+			1 => '扎',
+			2 => '支',
+		];
+		return $map[$unit] ?? '支';
+	}
+	
 }

+ 5 - 1
sql.sql

@@ -1368,4 +1368,8 @@ ADD COLUMN `inTurn`  int(11) NOT NULL DEFAULT 0 COMMENT '排序,数字越大
 =====
 == shish 2021-3-4
 ALTER TABLE xhOrder DROP COLUMN `addTime`;
-ALTER TABLE xhOrder ADD `addTime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间' AFTER `createTime`;
+ALTER TABLE xhOrder ADD `addTime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间' AFTER `createTime`;
+ALTER TABLE xhGhsOrder MODIFY `status` TINYINT(4) NOT NULL DEFAULT '1' COMMENT '订单状态 1待付款 2待配送 3配送中 4已完成 5已取消';
+ALTER TABLE `xhOrder` MODIFY `status` TINYINT(4) NOT NULL DEFAULT '0' COMMENT '订单状态 1待付款 2待配送 3配送中 4已完成 5已取消';
+
+确认国兰全部订单已经配送完成后 请将国兰订单全部置为已完成或已取消状态 其中付款的已完成 未付款的已取消