|
|
@@ -23,13 +23,14 @@ class Lists extends Backend
|
|
|
*/
|
|
|
protected $model = null;
|
|
|
protected $productArea = null;
|
|
|
+ protected $areaCode = null;
|
|
|
|
|
|
public function _initialize()
|
|
|
{
|
|
|
parent::_initialize();
|
|
|
$this->productArea = new ProductArea;
|
|
|
$this->model = new \app\common\model\ProductLists;
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -70,6 +71,9 @@ class Lists extends Backend
|
|
|
public function add()
|
|
|
{
|
|
|
if (false === $this->request->isPost()) {
|
|
|
+
|
|
|
+
|
|
|
+ $this->assignconfig('areaCode', []);
|
|
|
return $this->view->fetch();
|
|
|
}
|
|
|
$params = $this->request->post('row/a');
|
|
|
@@ -77,7 +81,6 @@ class Lists extends Backend
|
|
|
$this->error(__('Parameter %s can not be empty', ''));
|
|
|
}
|
|
|
$params = $this->preExcludeFields($params);
|
|
|
-
|
|
|
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
|
|
$params[$this->dataLimitField] = $this->auth->id;
|
|
|
}
|
|
|
@@ -90,16 +93,14 @@ class Lists extends Backend
|
|
|
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
|
|
|
$this->model->validateFailException()->validate($validate);
|
|
|
}
|
|
|
- $areaArr = json_decode($params['product_area'], true);
|
|
|
- if(empty($areaArr)) throw new ValidateException('请添加商品关联地区');
|
|
|
- if(self::isEqualArea($areaArr) == false) throw new ValidateException('该商品地区存在重复');
|
|
|
- unset($params['product_area']);
|
|
|
+ $areaArr = json_decode($params['product_area'], true);
|
|
|
+ $areaArrTxt = json_decode($params['product_area_txt'], true);
|
|
|
+ if(empty($areaArr) || empty($areaArrTxt)) throw new ValidateException('请添加商品关联地区');
|
|
|
+ unset($params['product_area'], $params['product_area_txt']);
|
|
|
+
|
|
|
//商品
|
|
|
$add = $this->model->create($params);
|
|
|
- foreach ($areaArr as $row) {
|
|
|
- $row['product_id'] = $add->id;
|
|
|
- $result = $this->productArea::create($row);
|
|
|
- }
|
|
|
+ $result= $this->setEqualArea($add->id, $areaArr, $areaArrTxt);
|
|
|
Db::commit();
|
|
|
} catch (ValidateException|PDOException|Exception $e) {
|
|
|
Db::rollback();
|
|
|
@@ -132,8 +133,23 @@ class Lists extends Backend
|
|
|
if (false === $this->request->isPost()) {
|
|
|
|
|
|
$area = $this->productArea::where('product_id', $ids)->select();
|
|
|
+ $areaCode = array();
|
|
|
+ $areaTxt = array();
|
|
|
+ foreach ($area as $key =>$item) {
|
|
|
+ if($item->province > 0) $areaCode[$key][0] = $item->province;
|
|
|
+ if($item->city > 0) $areaCode[$key][1] = $item->city;
|
|
|
+ if($item->area > 0) $areaCode[$key][2] = $item->area;
|
|
|
+ if($item->county > 0) $areaCode[$key][3] = $item->county;
|
|
|
+ $areaTxt[] = [$item->address];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $areaCode = json_encode($areaCode);
|
|
|
+ $this->assignconfig('areaCode', $areaCode);
|
|
|
$this->view->assign('row', $row);
|
|
|
- $this->view->assign('area', json_encode($area, JSON_UNESCAPED_UNICODE) );
|
|
|
+ $this->view->assign('areaTxt', json_encode($areaTxt, JSON_UNESCAPED_UNICODE));
|
|
|
+ $this->view->assign('areaCode', $areaCode);
|
|
|
return $this->view->fetch();
|
|
|
}
|
|
|
$params = $this->request->post('row/a');
|
|
|
@@ -176,22 +192,17 @@ class Lists extends Backend
|
|
|
|
|
|
|
|
|
//判断是否存在相同元素
|
|
|
- private static function isEqualArea(array $areaArr): bool
|
|
|
+ private function setEqualArea(int $product_id,array $areaArr, array $areaArrTxt): bool
|
|
|
{
|
|
|
- $result = array();
|
|
|
- if(count($areaArr) > 1){
|
|
|
- foreach ($areaArr as $value) {
|
|
|
- $value = (array)$value;
|
|
|
- foreach ($result as $v) {
|
|
|
- if ($value['province'] == $v['province'] &&
|
|
|
- $value['city'] == $v['city'] &&
|
|
|
- $value['area'] == $v['area'] &&
|
|
|
- $value['county'] == $v['county']) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- $result[] = $value;
|
|
|
- }
|
|
|
+ foreach ($areaArr as $key=>$row)
|
|
|
+ {
|
|
|
+ $arr['product_id'] = $product_id;
|
|
|
+ $arr['province'] = $row[0]??0;
|
|
|
+ $arr['city'] = $row[1]??0 ;
|
|
|
+ $arr['area'] = $row[2]??0;
|
|
|
+ $arr['county'] = $row[3]??0;
|
|
|
+ $arr['address'] = $areaArrTxt[$key][0];
|
|
|
+ $this->productArea::create($arr);
|
|
|
}
|
|
|
return true;
|
|
|
}
|