酒店预订平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

240 řádky
8.6 KiB

  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Area;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 配置-附加项目
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class CfItem extends Backend
  14. {
  15. /**
  16. * CfItem模型对象
  17. * @var \app\admin\model\CfItem
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\CfItem;
  24. }
  25. public function import()
  26. {
  27. parent::import();
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $group_id=$this->auth->getGroupId();
  48. $list = $this->model
  49. ->alias("a")
  50. ->join('hbp_admin c','a.create_id = c.id','left')
  51. ->join('hbp_admin d','a.purchase_user_id = d.id','left')
  52. ->field("a.*,c.nickname,d.nickname as purchase_user_name")
  53. ->where($where);
  54. if ($group_id){
  55. $list = $list
  56. ->where("group_id","=",$group_id)
  57. ->order($sort, $order)
  58. ->paginate($limit);
  59. }else{
  60. $list = $list
  61. ->order($sort, $order)
  62. ->paginate($limit);
  63. }
  64. $config = \think\Config::get("site.item_category");
  65. $config = json_decode($config,true);
  66. $config1 = \think\Config::get("site.item_unit");
  67. $config1 = json_decode($config1,true);
  68. $res = $list->items();
  69. foreach ($res as $key=>$val){
  70. $res[$key]["item_type_name"]=$config[$val["item_type"]];
  71. $res[$key]["item_unit"]=$config1[$val["item_unit"]];
  72. }
  73. $result = array("total" => $list->total(), "rows" => $list->items());
  74. return json($result);
  75. }
  76. return $this->view->fetch();
  77. }
  78. /**
  79. * 添加
  80. */
  81. public function add()
  82. {
  83. if ($this->request->isPost()) {
  84. $params = $this->request->post("row/a");
  85. if ($params) {
  86. $params = $this->preExcludeFields($params);
  87. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  88. $params[$this->dataLimitField] = $this->auth->id;
  89. }
  90. $result = false;
  91. Db::startTrans();
  92. try {
  93. //是否采用模型验证
  94. if ($this->modelValidate) {
  95. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  96. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  97. $this->model->validateFailException(true)->validate($validate);
  98. }
  99. $area=new Area();
  100. if ($params["area"]){
  101. $params["area_name"]=$area->where("id","=",$params["area"])->value("name");
  102. }
  103. if ($params["province"]){
  104. $params["province_name"]=$area->where("id","=",$params["province"])->value("name");
  105. }
  106. if ($params["city"]){
  107. $params["city_name"]=$area->where("id","=",$params["city"])->value("name");
  108. }
  109. $params['create_id']=$this->auth->id;
  110. $params['group_id']=$this->auth->getGroupId();
  111. $result = $this->model->allowField(true)->save($params);
  112. Db::commit();
  113. } catch (ValidateException $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. } catch (PDOException $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. } catch (Exception $e) {
  120. Db::rollback();
  121. $this->error($e->getMessage());
  122. }
  123. if ($result !== false) {
  124. $this->success();
  125. } else {
  126. $this->error(__('No rows were inserted'));
  127. }
  128. }
  129. $this->error(__('Parameter %s can not be empty', ''));
  130. }
  131. return $this->view->fetch();
  132. }
  133. /**
  134. * 编辑
  135. */
  136. public function edit($ids = null)
  137. {
  138. $row = $this->model->get($ids);
  139. if (!$row) {
  140. $this->error(__('No Results were found'));
  141. }
  142. $adminIds = $this->getDataLimitAdminIds();
  143. if (is_array($adminIds)) {
  144. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  145. $this->error(__('You have no permission'));
  146. }
  147. }
  148. if ($this->request->isPost()) {
  149. $params = $this->request->post("row/a");
  150. if ($params) {
  151. $params = $this->preExcludeFields($params);
  152. $result = false;
  153. Db::startTrans();
  154. try {
  155. //是否采用模型验证
  156. if ($this->modelValidate) {
  157. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  158. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  159. $row->validateFailException(true)->validate($validate);
  160. }
  161. $area=new Area();
  162. if ($params["area"]){
  163. $params["area_name"]=$area->where("id","=",$params["area"])->value("name");
  164. }
  165. if ($params["province"]){
  166. $params["province_name"]=$area->where("id","=",$params["province"])->value("name");
  167. }
  168. if ($params["city"]){
  169. $params["city_name"]=$area->where("id","=",$params["city"])->value("name");
  170. }
  171. $result = $row->allowField(true)->save($params);
  172. Db::commit();
  173. } catch (ValidateException $e) {
  174. Db::rollback();
  175. $this->error($e->getMessage());
  176. } catch (PDOException $e) {
  177. Db::rollback();
  178. $this->error($e->getMessage());
  179. } catch (Exception $e) {
  180. Db::rollback();
  181. $this->error($e->getMessage());
  182. }
  183. if ($result !== false) {
  184. $this->success();
  185. } else {
  186. $this->error(__('No rows were updated'));
  187. }
  188. }
  189. $this->error(__('Parameter %s can not be empty', ''));
  190. }
  191. $this->view->assign("row", $row);
  192. return $this->view->fetch();
  193. }
  194. public function getList(){
  195. $name=$this->request->post('name');
  196. $keyValue=$this->request->post('keyValue');
  197. $this->model->field('id,item_name as name,item_unit,item_type,item_memo');
  198. if($keyValue){
  199. $this->model->where(['id'=>$keyValue]);
  200. }elseif($name){
  201. $this->model->where(['item_name'=>['like','%'.$name.'%']]);
  202. }
  203. $result= $this->model->select()->toArray();
  204. $config = \think\Config::get("site.item_category");
  205. $config = json_decode($config,true);
  206. foreach ($result as $key=>$value){
  207. $result[$key]["item_type_name"]= $config[$value["item_type"]];
  208. }
  209. $config = \think\Config::get("site.item_unit");
  210. $config = json_decode($config,true);
  211. foreach ($result as $key=>$value){
  212. $result[$key]["item_unit"]= $config[$value["item_unit"]];
  213. }
  214. if($keyValue){
  215. return json(['list' => $result]);
  216. }
  217. return json(['list' => $result]);
  218. }
  219. }