Exemplo n.º 1
0
 private function updateVariants($service_id, $variants)
 {
     $add = array();
     $update = array();
     $variants_model = new shopServiceVariantsModel();
     $products_model = new shopProductServicesModel();
     $old_variants = $variants_model->getByField('service_id', $service_id, 'id');
     $sort = 0;
     foreach ($variants as $item) {
         $item['sort'] = $sort;
         $sort += 1;
         if (empty($item['id']) || empty($old_variants[$item['id']])) {
             $item['service_id'] = $service_id;
             $add[] = $item;
         } else {
             $variant_id = $item['id'];
             $update[$variant_id] = $item;
             unset($old_variants[$variant_id]);
         }
     }
     $default_id = null;
     foreach ($add as $item) {
         $added_id = $variants_model->insert($item);
         if (!empty($item['default'])) {
             $default_id = $added_id;
         }
     }
     foreach ($update as $id => $item) {
         if (!empty($item['default'])) {
             $default_id = $id;
         }
         $variants_model->updateById($id, $item);
     }
     if ($old_variants) {
         $ids = array_keys($old_variants);
         $variants_model->delete($ids);
         $products_model->deleteByVariants($ids);
     }
     if (!$default_id) {
         $default_id = $this->query("\n                SELECT id FROM `shop_service_variants`\n                WHERE service_id = " . (int) $service_id . " LIMIT 1\n            ")->fetchField('id');
         $default_id = $default_id ? $default_id : null;
     }
     $this->updateById($service_id, array('variant_id' => $default_id));
     return array_keys($variants_model->getByField('service_id', $service_id, 'id'));
 }