repository = $repository; } /** * @return mixed * @throws DbException * @throws DataNotFoundException * @throws ModelNotFoundException * @author xaboy * @day 2020-05-14 */ public function lst() { $where = $this->request->params(['is_full_give', 'status', 'is_give_subscribe', 'coupon_name', ['mer_id', null]]); [$page, $limit] = $this->getPage(); return app('json')->success($this->repository->getList($where['mer_id'], $where, $page, $limit)); } public function detail($id) { if (!$this->repository->exists($id)) return app('json')->fail('数据不存在'); $coupon = $this->repository->get($id)->append(['used_num', 'send_num']); return app('json')->success($coupon->toArray()); } /** * @param StoreCouponUserRepository $repository * @author xaboy * @day 2020/6/2 */ public function issue(StoreCouponUserRepository $repository) { [$page, $limit] = $this->getPage(); $where = $this->request->params(['username', 'coupon_title', 'status', 'mer_id']); return app('json')->success($repository->getList($where, $page, $limit)); } /** * @return mixed * @throws FormBuilderException * @author cabbage * @day 2020-11-05 */ public function createForm() { return app('json')->success(formToData($this->repository->form())); } /** * @param StoreCouponValidate $validate * @return mixed * @author cabbage * @day 2020-11-05 */ public function create(StoreCouponValidate $validate) { $data = $this->checkParams($validate); $mer_id = $this->request->params(['mer_id']); $data['mer_id'] = $mer_id['mer_id']; $data['is_admin'] = 1; $this->repository->create($data); return app('json')->success('发布成功'); } /** * @param StoreCouponValidate $validate * @return array * @author cabbage * @day 2020-11-05 */ public function checkParams(StoreCouponValidate $validate) { $data = $this->request->params(['title','coupon_price', 'use_min_price', 'coupon_type', 'coupon_time', ['use_start_time', []], 'sort', ['status', 0], 'type', ['product_id', []], ['range_date', ''], ['send_type', 0], ['full_reduction', 0], ['is_limited', 0], ['is_timeout', 0], ['total_count', ''], ['status', 0]]); $validate->check($data); if ($data['is_timeout']) { [$data['start_time'], $data['end_time']] = $data['range_date']; } if ($data['coupon_type']) { [$data['use_start_time'], $data['use_end_time']] = $data['use_start_time']; } else unset($data['use_start_time']); unset($data['range_date']); if ($data['is_limited'] == 0) $data['total_count'] = 0; return $data; } /** * @param $id * @return mixed * @throws DbException * @author cabbage * @day 2020-11-05 */ public function changeStatus($id) { $mer_id = $this->request->params(['mer_id']); $status = $this->request->param('status', 0) == 1 ? 1 : 0; if (!$this->repository->merExists($mer_id['mer_id'], $id)) return app('json')->fail('数据不存在'); $this->repository->update($id, compact('status')); return app('json')->success('修改成功'); } /** * @param $id * @return mixed * @throws DataNotFoundException * @throws DbException * @throws FormBuilderException * @throws ModelNotFoundException * @author cabbage * @day 2020-11-05 */ public function cloneForm($id) { $mer_id = $this->request->params(['mer_id']); if (!$this->repository->merExists($mer_id['mer_id'], $id)) return app('json')->fail('数据不存在'); return app('json')->success(formToData($this->repository->cloneCouponForm($id))); } /** * @return mixed * @author cabbage */ public function select() { $mer_id = $this->request->params(['mer_id']); $where = $this->request->params(['coupon_name']); $where['status'] = 1; $where['send_type'] = 3; [$page, $limit] = $this->getPage(); return app('json')->success($this->repository->getList($mer_id['mer_id'], $where, $page, $limit)); } /** * TODO * @param $id * @return mixed * @author cabbage * @day 2020-11-05 */ public function sendCoupon($id) { $mer_id = $this->request->params(['mer_id']); if (!$this->repository->merExists($mer_id['mer_id'], $id)) return app('json')->fail('数据不存在'); $uid = $this->request->param('uid', []); if (!is_array($uid) || count($uid) < 0) return app('json')->fail('选择用户'); app()->make(StoreCouponRepository::class)->sendCouponByUser($uid, $id); return app('json')->success('发送成功'); } /** * @param $id * @return mixed * @throws DbException * @author cabbage * @day 2020-11-05 */ public function delete($id) { $mer_id = $this->request->params(['mer_id']); if (!$this->repository->merExists($mer_id['mer_id'], $id)) return app('json')->fail('数据不存在'); $this->repository->delete($id); return app('json')->success('删除成功'); } }