Browse Source

完善功能

gorden 9 months ago
parent
commit
ad59cd522b

+ 106 - 0
app/admin/controller/marketing/DeptPremisesController.php

@@ -20,6 +20,16 @@ class DeptPremisesController extends Curd
         $this->validateClass = new DeptValidate();
     }
 
+    public function selectList()
+    {
+        $premisses = SysDept::where('dept_category', '营业场所')
+            ->select('dept_id as key', 'dept_name as label')
+            ->get()
+            ->toArray();
+
+        return json_success('', $premisses);
+    }
+
     /**
      * @Desc 营业场所列表
      * @Author Gorden
@@ -40,6 +50,27 @@ class DeptPremisesController extends Curd
         return $this->doFormat($query, $format, $limit);
     }
 
+    public function afterQuery($items)
+    {
+        foreach ($items as &$item) {
+            if (!empty($item->dept_extend_json)) {
+                $extendJson = json_decode($item->dept_extend_json, true);
+                $item->dept_week = $extendJson['week'] ?? [];
+                $item->dept_work = [];
+                $deptWork = [];
+                if (!empty($extendJson['times'])) {
+                    $times = explode('~', $extendJson['times']);
+                    $deptWork[] = date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $times[0]));
+                    $deptWork[] = date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $times[1]));
+                }
+
+                $item->dept_work = $deptWork;
+            }
+        }
+
+        return $items;
+    }
+
     public function afterInfoQuery($item)
     {
         if (!empty($item->dept_city)) {
@@ -91,6 +122,13 @@ class DeptPremisesController extends Curd
         return json_success('success');
     }
 
+    protected function insertInput(Request $request): array
+    {
+        $data = $this->inputFilter($request->post());
+
+        return $data;
+    }
+
     /**
      * @Desc 更新
      * @Author Gorden
@@ -107,6 +145,7 @@ class DeptPremisesController extends Curd
 
         try {
             [$id, $data] = $this->updateInput($request);
+
             $this->doUpdate($id, $data);
         } catch (BusinessException $customException) {
             return json_fail($customException->getMessage());
@@ -116,4 +155,71 @@ class DeptPremisesController extends Curd
 
         return json_success('success');
     }
+
+    /**
+     * 更新前置方法
+     * @param Request $request
+     * @return array
+     * @throws BusinessException
+     */
+    protected function updateInput(Request $request): array
+    {
+        $work = $request->post('dept_work', []);
+        $dateWeek = $request->post('dept_week', []);
+        $primary_key = $this->model->getKeyName();
+        $id = $request->post($primary_key);
+        $data = $this->inputFilter($request->post());
+        $model = $this->model->find($id);
+        if (!$model) {
+            throw new BusinessException('记录不存在', 2);
+        }
+        $extendJson = [];
+        if (!empty($model->dept_extend_json)) {
+            $extendJson = json_decode($model->dept_extend_json, true);
+        }
+        if (isset($work[0]) && isset($work[1])) {
+            $start = date('H:i', strtotime($work[0]));
+            $end = date('H:i', strtotime($work[1]));
+            $extendJson['times'] = $start . '~' . $end;
+        } elseif (!isset($work[0]) && isset($extendJson['times'])) {
+            unset($extendJson['times']);
+        }
+
+        if ($dateWeek) {
+            $newDates = [];
+            foreach ($dateWeek as $date) {
+                $key = self::$week[$date];
+                $newDates[$key] = $date;
+            }
+            ksort($newDates);
+
+            $currentDate = current($newDates);
+            $lastDate = end($newDates);
+
+            $weekStr = '';
+            if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
+                $weekStr = implode(',', $newDates);
+            } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
+                $weekStr = $currentDate . '至' . $lastDate;
+            }
+
+            $extendJson['weeks'] = $weekStr;
+            $extendJson['week'] = $dateWeek;
+        }
+
+        $data['dept_extend_json'] = json_encode($extendJson);
+
+        unset($data[$primary_key]);
+        return [$id, $data];
+    }
+
+    public static $week = [
+        '周一' => 1,
+        '周二' => 2,
+        '周三' => 3,
+        '周四' => 4,
+        '周五' => 5,
+        '周六' => 6,
+        '周日' => 7,
+    ];
 }

+ 31 - 5
app/admin/service/goods/GoodsService.php

@@ -9,6 +9,7 @@ use app\model\GoodsLabel;
 use app\model\GoodsRunning;
 use app\model\GoodsSku;
 use app\model\SysCategory;
+use app\model\SysDept;
 use app\model\SysSerial;
 use support\Db;
 use support\exception\BusinessException;
@@ -411,7 +412,7 @@ class GoodsService
     {
         try {
             // 商品主表
-            $main = Goods::where('goods_id', $goodsId)->first();
+            $main = Goods::with('category')->where('goods_id', $goodsId)->first();
             if (!empty($main)) {
                 $main = $main->toArray();
                 $main['goods_sku_json'] = json_decode($main['goods_sku_json'], true);
@@ -448,6 +449,8 @@ class GoodsService
                     $goodsRunningOffJson = json_decode($running['goods_running_off_json'], true);
                     $running['goods_off_addtimes'] = isset($goodsRunningOffJson['time']) ? date("Y-m-d H:i", $goodsRunningOffJson['time']) : '';
                 }
+                $running['goods_running_storage'] = !empty($running['goods_running_storage']) ? intval($running['goods_running_storage']) : '';
+                $running['goods_running_sale'] = !empty($running['goods_running_sale']) ? intval($running['goods_running_sale']) : '';
             } else {
                 $running = [];
             }
@@ -512,6 +515,10 @@ class GoodsService
             if (!empty($data['goods_attribute_json'])) {
                 $extendJson = json_decode($data['goods_attribute_json'], true);
                 $data['goods_attribute_json'] = $extendJson;
+                if (isset($extendJson['premisses'])) {
+                    $data['goods_premisses'] = $extendJson['premisses'];
+                    $data['goods_premisses_str'] = SysDept::whereIn('dept_id', $extendJson['premisses'])->pluck('dept_name');
+                }
             }
             $data['appointment_times'] = [];
             if ($data['is_support_appointment'] == 'Y' && isset($extendJson['dates'])) {
@@ -805,7 +812,7 @@ class GoodsService
                 $goods->goods_attribute_json = json_encode($attributeJson);
             }
             $goods->save();
-            $sku = GoodsSku::where('join_sku_goods_id',$params['goods_id'])->first();
+            $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
             $sku->goods_sku_status = $params['goods_status'];
             $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
             $sku->goods_sku_market_price = $params['goods_sales_price'];
@@ -1017,9 +1024,8 @@ class GoodsService
             $model->goods_name = $params['goods_name'];
             $model->goods_market_price = $params['goods_market_price'] ?? 0;
             $model->goods_sales_price = $params['goods_sales_price'] ?? 0;
-            $model->goods_sku_json = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => []]);
-            $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '{}';
-//            $model->goods_service_json = !empty($params['goods_service_json']) ? $params['goods_service_json'] : '{}';
+            $model->goods_sku_json = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
+            $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '[]';
             $model->goods_title = $params['goods_title'] ?? '';
             $model->goods_cover = $params['goods_cover'] ?? '';
             $model->goods_on_addtimes = isset($params['goods_on_addtimes']) ? strtotime($params['goods_on_addtimes']) : null;
@@ -1124,6 +1130,17 @@ class GoodsService
                 $model->goods_json = '[]';
             }
 
+            if (!empty($params['goods_premisses'])) {
+                $attributeJson = [];
+                if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
+                    $attributeJson = json_decode($model->goods_attribute_json, true);
+                } elseif (empty($model->goods_attribute_json)) {
+                    $attributeJson = [];
+                }
+                $attributeJson['premisses'] = $params['goods_premisses'];
+                $model->goods_attribute_json = json_encode($attributeJson);
+            }
+
             if ($model->save()) {
                 return $model->goods_id;
             }
@@ -1378,6 +1395,15 @@ class GoodsService
                 }
                 $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
             }
+            if (!empty($params['goods_premisses'])) {
+                if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
+                    $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
+                } elseif (empty($data['goods_attribute_json'])) {
+                    $data['goods_attribute_json'] = [];
+                }
+                $data['goods_attribute_json']['premisses'] = $params['goods_premisses'];
+                $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
+            }
             if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
                 $goodsJson = json_decode($data['goods_json'], true);
                 foreach ($goodsJson as $key => $item) {

+ 1 - 1
app/admin/validate/sys_manage/DeptValidate.php

@@ -15,7 +15,7 @@ class DeptValidate extends Validate
         'dept_city|所在城市' => 'require',
         'dept_name|名称' => 'require',
         'dept_telephone|联系电话' => 'mobile',
-        'dept_position|坐标' => 'regex:/^[0-9\.]+$/',
+        'dept_position|坐标' => 'regex:/^[0-9\.\,]+$/',
         'dept_address|地址' => 'max:255',
 //        'dept_remark' => 'chsDash'
     ];

+ 2 - 1
app/model/Goods.php

@@ -41,7 +41,8 @@ class Goods extends Model
      */
     public function category()
     {
-        return $this->hasOne(SysCategory::class, 'category_id', 'join_goods_category_id');
+        return $this->hasOne(SysCategory::class, 'category_id', 'join_goods_category_id')
+            ->select('category_id','category_name');
     }
 
     /**

+ 1 - 0
route/admin.php

@@ -658,6 +658,7 @@ Route::group('/admin', function () {
         ]);
         /* 营业场所管理(部门表) */
         Route::group('/deptPremises', function () {
+            Route::get('/selectList', [\app\admin\controller\marketing\DeptPremisesController::class, 'selectList']);
             Route::get('/list', [\app\admin\controller\marketing\DeptPremisesController::class, 'select']);
             Route::get('/info', [\app\admin\controller\marketing\DeptPremisesController::class, 'info']);
             Route::post('/add', [\app\admin\controller\marketing\DeptPremisesController::class, 'insert']);