Carpriceholiday.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 价格假期
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Carpriceholiday extends Backend
  11. {
  12. /**
  13. * Carpriceholiday模型对象
  14. * @var \app\admin\model\Carpriceholiday
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Carpriceholiday;
  21. }
  22. /**
  23. * 添加
  24. *
  25. * @return string
  26. * @throws \think\Exception
  27. */
  28. public function add()
  29. {
  30. if (false === $this->request->isPost()) {
  31. return $this->view->fetch();
  32. }
  33. $params = $this->request->post('row/a');
  34. if (empty($params)) {
  35. $this->error(__('Parameter %s can not be empty', ''));
  36. }
  37. $params = $this->preExcludeFields($params);
  38. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  39. $params[$this->dataLimitField] = $this->auth->id;
  40. }
  41. $result = false;
  42. Db::startTrans();
  43. try {
  44. //是否采用模型验证
  45. if ($this->modelValidate) {
  46. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  47. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  48. $this->model->validateFailException()->validate($validate);
  49. }
  50. $date_json = $this->request->post('date_json/a');
  51. foreach($date_json as $key => $val){
  52. if(empty($val)){
  53. unset($date_json[$key]);
  54. }
  55. }
  56. $params['date_json'] = json_encode($date_json);
  57. $result = $this->model->allowField(true)->save($params);
  58. Db::commit();
  59. } catch (ValidateException|PDOException|Exception $e) {
  60. Db::rollback();
  61. $this->error($e->getMessage());
  62. }
  63. if ($result === false) {
  64. $this->error(__('No rows were inserted'));
  65. }
  66. $this->success();
  67. }
  68. /**
  69. * 编辑
  70. *
  71. * @param $ids
  72. * @return string
  73. * @throws DbException
  74. * @throws \think\Exception
  75. */
  76. public function edit($ids = null)
  77. {
  78. $row = $this->model->get($ids);
  79. if (!$row) {
  80. $this->error(__('No Results were found'));
  81. }
  82. $adminIds = $this->getDataLimitAdminIds();
  83. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  84. $this->error(__('You have no permission'));
  85. }
  86. if (false === $this->request->isPost()) {
  87. $date_json = json_decode($row['date_json'],true);
  88. // dump($date_json);exit;
  89. $this->view->assign('date_json', $date_json);
  90. $this->view->assign('row', $row);
  91. return $this->view->fetch();
  92. }
  93. $params = $this->request->post('row/a');
  94. if (empty($params)) {
  95. $this->error(__('Parameter %s can not be empty', ''));
  96. }
  97. $params = $this->preExcludeFields($params);
  98. $result = false;
  99. Db::startTrans();
  100. try {
  101. //是否采用模型验证
  102. if ($this->modelValidate) {
  103. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  104. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  105. $row->validateFailException()->validate($validate);
  106. }
  107. $date_json = $this->request->post('date_json/a');
  108. foreach($date_json as $key => $val){
  109. if(empty($val)){
  110. unset($date_json[$key]);
  111. }
  112. }
  113. $params['date_json'] = json_encode($date_json);
  114. $result = $row->allowField(true)->save($params);
  115. Db::commit();
  116. } catch (ValidateException|PDOException|Exception $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. }
  120. if (false === $result) {
  121. $this->error(__('No rows were updated'));
  122. }
  123. $this->success();
  124. }
  125. }