Demands.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\service\DemandService;
  5. use Exception;
  6. use think\Db;
  7. use think\exception\DbException;
  8. use think\exception\HttpResponseException;
  9. use think\exception\PDOException;
  10. use think\exception\ValidateException;
  11. /**
  12. * 客户需求管理
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Demands extends Backend
  17. {
  18. /**
  19. * Demands模型对象
  20. * @var \app\common\model\Demands
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\common\model\Demands;
  27. $this->view->assign("typeList", $this->model->getTypeList());
  28. $this->view->assign("statusList", $this->model->getStatusList());
  29. $this->view->assign("workStatusList", $this->model->getWorkStatusList());
  30. $this->view->assign("acceptStatusList", $this->model->getAcceptStatusList());
  31. $this->view->assign("settleStatusList", $this->model->getSettleStatusList());
  32. $this->view->assign("publishStatusList", $this->model->getPublishStatusList());
  33. $this->view->assign("biddingStatusList", $this->model->getBiddingStatusList());
  34. }
  35. /**
  36. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  37. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  38. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  39. */
  40. /**
  41. * 查看
  42. */
  43. public function index()
  44. {
  45. //当前是否为关联查询
  46. $this->relationSearch = true;
  47. //设置过滤方法
  48. $this->request->filter(['strip_tags', 'trim']);
  49. if ($this->request->isAjax()) {
  50. //如果发送的来源是Selectpage,则转发到Selectpage
  51. if ($this->request->request('keyField')) {
  52. return $this->selectpage();
  53. }
  54. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  55. $list = $this->model
  56. ->with(['user'])
  57. ->where($where)
  58. ->order($sort, $order)
  59. ->paginate($limit);
  60. foreach ($list as $row) {
  61. $row->getRelation('user')->visible(['nickname', 'avatar', 'role']);
  62. }
  63. $result = array("total" => $list->total(), "rows" => $list->items());
  64. return json($result);
  65. }
  66. return $this->view->fetch();
  67. }
  68. /**
  69. * 添加
  70. *
  71. * @return string
  72. * @throws \think\Exception
  73. */
  74. public function add()
  75. {
  76. if (false === $this->request->isPost()) {
  77. return $this->view->fetch();
  78. }
  79. $params = $this->request->post('row/a');
  80. if (empty($params)) {
  81. $this->error(__('Parameter %s can not be empty', ''));
  82. }
  83. $params = $this->preExcludeFields($params);
  84. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  85. $params[$this->dataLimitField] = $this->auth->id;
  86. }
  87. $result = false;
  88. Db::startTrans();
  89. try {
  90. //是否采用模型验证
  91. if ($this->modelValidate) {
  92. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  93. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  94. $this->model->validateFailException()->validate($validate);
  95. }
  96. $result = $this->model->allowField(true)->save($params);
  97. Db::commit();
  98. } catch (ValidateException|PDOException|Exception $e) {
  99. Db::rollback();
  100. $this->error($e->getMessage());
  101. }
  102. if ($result === false) {
  103. $this->error(__('No rows were inserted'));
  104. }
  105. $this->success();
  106. }
  107. /**
  108. * 编辑
  109. *
  110. * @param $ids
  111. * @return string
  112. * @throws DbException
  113. * @throws \think\Exception
  114. */
  115. public function edit($ids = null)
  116. {
  117. $row = $this->model->get($ids);
  118. if (!$row) {
  119. $this->error(__('No Results were found'));
  120. }
  121. $adminIds = $this->getDataLimitAdminIds();
  122. // $row->files = implode(',', $row->files);
  123. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  124. $this->error(__('You have no permission'));
  125. }
  126. if (false === $this->request->isPost()) {
  127. $row = $row->toArray();
  128. $row['images'] = implode(',', $row['images']);
  129. $row['files'] = implode(',', $row['files']);
  130. $this->view->assign('row', $row);
  131. return $this->view->fetch();
  132. }
  133. $params = $this->request->post('row/a');
  134. if (empty($params)) {
  135. $this->error(__('Parameter %s can not be empty', ''));
  136. }
  137. $params = $this->preExcludeFields($params);
  138. $result = false;
  139. Db::startTrans();
  140. try {
  141. //是否采用模型验证
  142. if ($this->modelValidate) {
  143. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  144. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  145. $row->validateFailException()->validate($validate);
  146. }
  147. $params['images'] = explode(',', $params['images']);
  148. $params['files'] = explode(',', $params['files']);
  149. $result = $row->allowField(true)->save($params);
  150. Db::commit();
  151. } catch (ValidateException|PDOException|Exception $e) {
  152. Db::rollback();
  153. $this->error($e->getMessage());
  154. }
  155. if (false === $result) {
  156. $this->error(__('No rows were updated'));
  157. }
  158. $this->success();
  159. }
  160. /**
  161. * 重新投标
  162. */
  163. public function resetBidding($ids)
  164. {
  165. $row = $this->model->get(['id' => $ids]);
  166. if (!$row) {
  167. $this->error(__('No Results were found'));
  168. }
  169. if ($this->request->isAjax()) {
  170. try {
  171. DemandService::resetBiddingDeal($row);
  172. $this->success("重新投标成功", null, ['Success']);
  173. } catch (\Exception $e) {
  174. if ($e instanceof HttpResponseException) {
  175. throw $e;
  176. }
  177. $this->error('操作失败:' . $e->getMessage());
  178. }
  179. }
  180. $this->view->assign("row", $row->toArray());
  181. return $this->view->fetch();
  182. }
  183. /**
  184. * 取消订单
  185. */
  186. public function cancel($ids)
  187. {
  188. $row = $this->model->get(['id' => $ids]);
  189. if (!$row) {
  190. $this->error(__('No Results were found'));
  191. }
  192. if ($this->request->isAjax()) {
  193. //如果有中标人员则取消中标,并且退回冻结余额,如何冻结余额已经解冻则退回余额
  194. //如果订单已经完成结算,则不能取消
  195. //如果客户有支付预付款则退还预付款,如果有支付尾款,则退还尾款。最好通过协商,确定退款金额,并由客户申请
  196. $this->success("重新投标成功", null, ['id' => $ids]);
  197. }
  198. $this->view->assign("row", $row->toArray());
  199. return $this->view->fetch();
  200. }
  201. /**
  202. * 审核
  203. */
  204. public function audit($ids)
  205. {
  206. $row = $this->model->get(['id' => $ids]);
  207. if (!$row) {
  208. $this->error(__('No Results were found'));
  209. }
  210. if ($this->request->isAjax()) {
  211. try {
  212. DemandService::auditPass($ids);
  213. $this->success("审核成功", null);
  214. } catch (\Exception $e) {
  215. if ($e instanceof HttpResponseException) {
  216. throw $e;
  217. }
  218. $this->error($e->getMessage());
  219. }
  220. }
  221. $this->view->assign("row", $row->toArray());
  222. return $this->view->fetch();
  223. }
  224. }