|
|
@@ -6,10 +6,13 @@
|
|
|
|
|
|
namespace bizGhs\item\services;
|
|
|
|
|
|
-
|
|
|
use bizGhs\base\services\BaseService;
|
|
|
use bizGhs\item\classes\ItemClass;
|
|
|
+use bizGhs\item\classes\ItemClassClass;
|
|
|
+use bizGhs\item\classes\ItemInfoClass;
|
|
|
+use Yii;
|
|
|
use common\components\util;
|
|
|
+use yii\db\Exception;
|
|
|
|
|
|
|
|
|
class ItemService extends BaseService
|
|
|
@@ -21,9 +24,9 @@ class ItemService extends BaseService
|
|
|
$ghsId = $data['ghsId']??0;
|
|
|
$classId = $data['classId']??0;
|
|
|
$itemId = $data['itemId']??0;
|
|
|
-
|
|
|
- if(!$ghsId){
|
|
|
- util::fail("请选择供应商");
|
|
|
+ $shopId = $data['shopId']??0;
|
|
|
+ if(!$ghsId || !$shopId){
|
|
|
+ util::fail("请选择供应商/门店");
|
|
|
}
|
|
|
if(!$classId){
|
|
|
util::fail("请选择分类");
|
|
|
@@ -33,43 +36,84 @@ class ItemService extends BaseService
|
|
|
}
|
|
|
//判断花材是否存在 调用平台花材的方法
|
|
|
//todo
|
|
|
- $res = ItemClass::add($data);
|
|
|
- return $res;
|
|
|
+
|
|
|
+ $existItemClass = ItemClassClass::exists(['id'=>$classId,'ghsId'=>$ghsId]);
|
|
|
+ if(!$existItemClass){
|
|
|
+ util::fail("花材分类不存在");
|
|
|
+ }
|
|
|
+ $existItem = ItemClass::exists(['itemId'=>$itemId,'ghsId'=>$ghsId]);
|
|
|
+ if($existItem){
|
|
|
+ util::fail("花材已存在");
|
|
|
+ }
|
|
|
+ $connection = Yii::$app->db;
|
|
|
+ $transaction = $connection->beginTransaction();
|
|
|
+ try{
|
|
|
+ //写入一条item,同时写入一条itemInfo(初始化库存)
|
|
|
+ ItemClass::add($data);
|
|
|
+ ItemInfoClass::add($data);
|
|
|
+ $transaction->commit();
|
|
|
+ }catch (Exception $exception){
|
|
|
+ $transaction->rollBack();
|
|
|
+ util::fail("新增失败");
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- //批量新增花材(初始化使用)
|
|
|
- public static function batchAddGhsItem($data)
|
|
|
+ //初始化花材
|
|
|
+ public static function initGhsItem($data)
|
|
|
{
|
|
|
- ItemClass::batchAdd($data);
|
|
|
- return true;
|
|
|
+ $connection = Yii::$app->db;
|
|
|
+ $transaction = $connection->beginTransaction();
|
|
|
+ try{
|
|
|
+ ItemClass::batchAdd($data);
|
|
|
+ ItemInfoClass::batchAdd($data);
|
|
|
+ $transaction->commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ $transaction->rollBack();
|
|
|
+ util::fail("初始化失败");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- //修改花材的信息(库存预警、每扎加价、每扎重量)
|
|
|
- public static function updateGshItem($id,$stockWarning,$unitAddPrice,$unitWeight)
|
|
|
+ //修改花材信息
|
|
|
+ public static function updateGshItem($data)
|
|
|
{
|
|
|
- $model = ItemClass::getById($id);
|
|
|
- if(!$model){
|
|
|
- util::fail("该花材不存在");
|
|
|
+ $id = $data['id']??0;
|
|
|
+ $stockWarning = $data['stockWarning']??0;
|
|
|
+ $unitAddPrice = $data['unitAddPrice']??0;
|
|
|
+ $unitWeight = $data['unitWeight']??0;
|
|
|
+ if($stockWarning < 0 ){
|
|
|
+ util::fail("库存预警不能小于0");
|
|
|
}
|
|
|
- $data['stockWarning'] = $stockWarning;
|
|
|
- $data['unitAddPrice'] = $unitAddPrice;
|
|
|
- $data['unitWeight'] = $unitWeight;
|
|
|
- ItemClass::updateById($id,$data);
|
|
|
- return true;
|
|
|
-
|
|
|
+ if($unitAddPrice < 0 ){
|
|
|
+ util::fail("每扎加价不能小于0");
|
|
|
+ }
|
|
|
+ if($unitWeight < 0 ){
|
|
|
+ util::fail("每扎重量不能小于0");
|
|
|
+ }
|
|
|
+ ItemClass::updateGshItem($id,$stockWarning,$unitAddPrice,$unitWeight);
|
|
|
}
|
|
|
|
|
|
- //删除花材
|
|
|
- public static function deleteGhsItem($id)
|
|
|
+ //删除花材,同时修改itemInfo 的状态值 delStatus = 1
|
|
|
+ public static function deleteGhsItem($id,$shopId,$adminId)
|
|
|
{
|
|
|
$model = ItemClass::getById($id);
|
|
|
if(!$model){
|
|
|
util::fail("该花材不存在");
|
|
|
}
|
|
|
- //todo 要修改相应得花材详情表
|
|
|
|
|
|
- ItemClass::deleteById($id);
|
|
|
- return true;
|
|
|
+ $connection = Yii::$app->db;
|
|
|
+ $transaction = $connection->beginTransaction();
|
|
|
+ try{
|
|
|
+ ItemClass::deleteGhsItem($id);
|
|
|
+ $condition = [
|
|
|
+ 'itemId'=>$model['itemId'],
|
|
|
+ 'shopId'=>$shopId,
|
|
|
+ ];
|
|
|
+ ItemInfoClass::updateByCondition($condition,['delStatus'=>1,'adminId'=>$adminId]);
|
|
|
+ $transaction->commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ $transaction->rollBack();
|
|
|
+ util::fail("操作失败");
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
}
|