酒店预订平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CfItem.php 9.8 KiB

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