where(['is_delete' => 0, 'store_id' => get_store_id()]); if ($this->status !== null && $this->status !== '' && in_array($this->status, [0, 1])) { $query->andWhere(['status' => $this->status]); } $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { if ((int)$item['type'] === 0) { $item['times'] = date("Y-m-d H:i:s", $item['times']); } $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']); } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $pagination['list'], 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function save () { try { if (!$this->name || (!$this->times && !$this->days)) { throw new \Exception("请将参数填充完整"); } $rules = DeliveryRules::findOne(['id' => $this->id, 'is_delete' => 0, 'store_id' => get_store_id()]); if (empty($rules)) { $rules = new DeliveryRules(); $rules->store_id = get_store_id(); } $rules->name = $this->name; $rules->type = (int)$this->type; // $rules->times = (int)$this->times; if ((int)$this->type === 0) { $rules->times = strtotime($this->times); } else { $rules->days = (int)$this->days; } if (!$rules->save()) { throw new \Exception(json_encode($rules->errors)); } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function setStatus () { try { if ($this->ids) { $ids = explode(',', $this->ids); if (in_array($this->status, [0, 1])) { DeliveryRules::updateAll(['status' => $this->status], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]); } if ((int)$this->status === 2) { DeliveryRules::updateAll(['is_delete' => 1], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]); } } else { $rules = DeliveryRules::findOne(['id' => $this->id, 'is_delete' => 0, 'store_id' => get_store_id()]); if (empty($rules)) { throw new \Exception("规则不存在"); } if (in_array($this->status, [0, 1])) { $rules->status = $this->status; } if ((int)$this->status === 2) { $rules->is_delete = 1; } if (!$rules->save()) { throw new \Exception(json_encode($rules->errors)); } } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function getSelectList() { try { $list = DeliveryRules::find()->where(['is_delete' => 0, 'store_id' => get_store_id()])->asArray()->all(); foreach ($list as &$item) { if ((int)$item['type'] === 0) { $item['times'] = date("Y-m-d H:i:s", $item['times']); } $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']); } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list ] ]; } catch (\Exception $e){ return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }