Equipmentdevice.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace app\admin\controller\qingdongams\customer;
  3. use addons\qingdongams\model\Staff;
  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 Equipmentdevice extends Backend
  14. {
  15. /**
  16. * Equipmentdevice模型对象
  17. * @var \app\admin\model\qingdongams\customer\Equipmentdevice
  18. */
  19. protected $model = null;
  20. protected $Equipmentdevice = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\qingdongams\customer\Equipmentdevice;
  25. $this->Equipmentdevice = new \app\admin\model\qingdongams\customer\Equipmentdevice;
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. }
  28. public function index_no()
  29. {
  30. //设置过滤方法
  31. $this->request->filter(['strip_tags', 'trim']);
  32. if (false === $this->request->isAjax()) {
  33. return $this->view->fetch();
  34. }
  35. //如果发送的来源是 Selectpage,则转发到 Selectpage
  36. if ($this->request->request('keyField')) {
  37. return $this->selectpage();
  38. }
  39. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  40. $map=[];
  41. $idsArr = [];
  42. if($this->request->request('isselect')==1){
  43. $map['status']=0;
  44. }
  45. if ($this->request->request('ids')) {
  46. $ids = explode(',', $this->request->request('ids'));
  47. foreach ($ids as $value) {
  48. if ($value != "") {
  49. $idsArr[] = $value;
  50. }
  51. }
  52. }
  53. $list = $this->model
  54. ->where($where)
  55. ->where($map)
  56. ->whereNotIn('id', $idsArr)
  57. ->order($sort, $order)
  58. ->paginate($limit);
  59. $result = ['total' => $list->total(), 'rows' => $list->items()];
  60. return json($result);
  61. }
  62. /**
  63. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  64. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  65. * 113.224.136.184
  66. * 112.38.101.236
  67. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  68. */
  69. public function indexs(){
  70. $info=$this->model->where(array('status'=>0))->select();
  71. $data['list']=$info;
  72. return $data;
  73. }
  74. public function select_add(){
  75. $info=Db::name('qingdongams_contract')->where(array('id'=>input('ids')))->find();
  76. if ($this->request->isAjax()) {
  77. $params = $this->request->post("row/a");
  78. $json_data=json_decode($params['device_ids'],true);
  79. $dev_ids= implode(',', array_column($json_data, 'customer_product_id'));
  80. $data['device_ids']=$dev_ids;
  81. $data['device_ids_json']=$params['device_ids'];
  82. $result = Db::name('qingdongams_contract')->where(array('id' => $info['id']))->update($data);
  83. foreach($json_data as $key=>$val){
  84. $res=$this->Equipmentdevice->where(array('id'=>$val['customer_product_id']))->update(array('status'=>1,'customer_id'=>$info['customer_id'],'contract_id'=>$info['id']));
  85. }
  86. if (!$result) {
  87. $this->error('绑定失败');
  88. }
  89. $this->success('绑定成功');
  90. }
  91. $this->assign('row', $info);
  92. return $this->view->fetch();
  93. }
  94. /**
  95. * 查看
  96. */
  97. public function index()
  98. {
  99. //当前是否为关联查询
  100. $this->relationSearch = true;
  101. //设置过滤方法
  102. $this->request->filter(['strip_tags', 'trim']);
  103. if ($this->request->isAjax()) {
  104. //如果发送的来源是Selectpage,则转发到Selectpage
  105. if ($this->request->request('keyField')) {
  106. // $info=$this->model->where(array('status'=>0))->select();
  107. return $this->selectpage();
  108. // $data['list']=$info;
  109. // return $data;
  110. }
  111. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  112. $map=[];
  113. if($this->request->request('isselect')==1){
  114. $map['customer_id']=$this->request->request('customer_id');
  115. $ids = $this->request->request('ids');
  116. if($ids){
  117. $map['id'] = ['not in', $ids];
  118. }
  119. }
  120. $list = $this->model
  121. ->where($where)
  122. ->where($map)
  123. ->order($sort, $order)
  124. ->paginate($limit);
  125. foreach ($list as $value) {
  126. $type_id_arr = explode(',', $value['type_id']);
  127. $type_name = \app\admin\model\qingdongams\customer\Equipmenttype::where(['id'=> ['in', $type_id_arr]])->column('name');
  128. $value['type_id'] = implode(',', $type_name);
  129. }
  130. $result = array("total" => $list->total(), "rows" => $list->items());
  131. return json($result);
  132. }
  133. $this->assignconfig('isselect', $this->request->request('isselect'));
  134. $this->assignconfig('customer_id', $this->request->request('customer_id'));
  135. return $this->view->fetch();
  136. }
  137. /**
  138. * 添加
  139. *
  140. * @return string
  141. * @throws \think\Exception
  142. */
  143. public function add()
  144. {
  145. if (false === $this->request->isPost()) {
  146. return $this->view->fetch();
  147. }
  148. $params = $this->request->post('row/a');
  149. if (empty($params)) {
  150. $this->error(__('Parameter %s can not be empty', ''));
  151. }
  152. $params = $this->preExcludeFields($params);
  153. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  154. $params[$this->dataLimitField] = $this->auth->id;
  155. }
  156. $result = false;
  157. Db::startTrans();
  158. try {
  159. //是否采用模型验证
  160. if ($this->modelValidate) {
  161. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  162. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  163. $this->model->validateFailException()->validate($validate);
  164. }
  165. // 添加设备时判断是否需要增加客户id
  166. $customer_id = $this->request->request('customer_id') ?? '';
  167. if ($customer_id) {
  168. $params['customer_id'] = $customer_id;
  169. }
  170. $result = $this->model->allowField(true)->save($params);
  171. Db::commit();
  172. } catch (ValidateException|PDOException|Exception $e) {
  173. Db::rollback();
  174. $this->error($e->getMessage());
  175. }
  176. if ($result === false) {
  177. $this->error(__('No rows were inserted'));
  178. }
  179. $this->success();
  180. }
  181. /**
  182. * 搜索下拉列表
  183. */
  184. public function typeList()
  185. {
  186. $data = ['searchlist' => \app\admin\model\qingdongams\customer\Equipmenttype::getList()];
  187. $this->success('', null, $data);
  188. }
  189. }