酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

274 lines
9.5 KiB

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