QcodeBach.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\QcodeCompany;
  4. use app\common\controller\Backend;
  5. use \think\Session;
  6. /**
  7. *
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class QcodeBach extends Backend
  12. {
  13. /**
  14. * Product模型对象
  15. * @var \app\admin\model\QcodeBach
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\QcodeBach();
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. //当前是否为关联查询
  34. $this->relationSearch = false;
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags', 'trim']);
  37. if ($this->request->isAjax()) {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('keyField')) {
  40. return $this->selectpage();
  41. }
  42. $where = [
  43. 'delete_time'=> ''
  44. ];
  45. $req = input();
  46. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  47. $order = $req['order'];
  48. $offset = $req['offset'];
  49. $limit = $req['limit'];
  50. // 构造模糊查询条件
  51. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  52. // $filter = ['field' => $regex];
  53. $filter = json_decode($req['filter'], true);
  54. foreach ($filter as $k => $v){
  55. $where[$k] = new \MongoDB\BSON\Regex($v);
  56. }
  57. $list = $this->model->where($where)
  58. ->order($sort,$order)
  59. ->limit($limit)
  60. ->skip($offset)
  61. ->select();
  62. foreach ($list as $k=>$v) {
  63. $oid = $v['_id']->jsonSerialize();
  64. $list[$k]['id'] = $oid['$oid'];
  65. }
  66. $result = array("total" => count($list), "rows" => $list);
  67. return json($result);
  68. }
  69. return $this->view->fetch();
  70. }
  71. /**
  72. * 查看
  73. */
  74. public function table1()
  75. {
  76. //当前是否为关联查询
  77. $this->relationSearch = false;
  78. //设置过滤方法
  79. $this->request->filter(['strip_tags', 'trim']);
  80. if ($this->request->isAjax()) {
  81. //如果发送的来源是Selectpage,则转发到Selectpage
  82. if ($this->request->request('keyField')) {
  83. return $this->selectpage();
  84. }
  85. $where = [
  86. 'delete_time'=> ''
  87. ];
  88. $req = input();
  89. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  90. $order = $req['order'];
  91. $offset = $req['offset'];
  92. $limit = $req['limit'];
  93. // 构造模糊查询条件
  94. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  95. // $filter = ['field' => $regex];
  96. $filter = json_decode($req['filter'], true);
  97. foreach ($filter as $k => $v){
  98. $where[$k] = new \MongoDB\BSON\Regex($v);
  99. }
  100. $total = $this->model->where($where)
  101. ->order($sort,$order)
  102. ->skip($offset)
  103. ->select();
  104. $list = $this->model->where($where)
  105. ->order($sort,$order)
  106. ->limit($limit)
  107. ->skip($offset)
  108. ->select();
  109. foreach ($list as $k=>$v) {
  110. $oid = $v['_id']->jsonSerialize();
  111. $list[$k]['id'] = $oid['$oid'];
  112. }
  113. $result = array("total" => count($total), "rows" => $list);
  114. return json($result);
  115. }
  116. return $this->view->fetch();
  117. }
  118. /**
  119. * 查看
  120. */
  121. public function table2()
  122. {
  123. //当前是否为关联查询
  124. $this->relationSearch = false;
  125. //设置过滤方法
  126. $this->request->filter(['strip_tags', 'trim']);
  127. if ($this->request->isAjax()) {
  128. //如果发送的来源是Selectpage,则转发到Selectpage
  129. if ($this->request->request('keyField')) {
  130. return $this->selectpage();
  131. }
  132. $userInfo = Session::get('admin');
  133. $company_id = (int)$userInfo['company'];
  134. $where = [
  135. // 'company_id'=>(int)$userInfo['company'],
  136. 'delete_time'=> ''
  137. ];
  138. $req = input();
  139. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  140. $order = $req['order'];
  141. $offset = $req['offset'];
  142. $limit = $req['limit'];
  143. // 构造模糊查询条件
  144. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  145. // $filter = ['field' => $regex];
  146. $filter = json_decode($req['filter'], true);
  147. foreach ($filter as $k => $v){
  148. $where[$k] = new \MongoDB\BSON\Regex($v);
  149. }
  150. $db = new QcodeCompany();
  151. $rows = $db->name($company_id.'_'."qcode_company")
  152. ->where('delete_time','')
  153. ->column('product_id');
  154. $rows = array_values($rows);
  155. $total = $this->model->where($where)
  156. ->where('_id','in',$rows)
  157. ->order($sort,$order)
  158. ->skip($offset)
  159. ->select();
  160. $list = $this->model->where($where)
  161. ->where('_id','in',$rows)
  162. ->order($sort,$order)
  163. ->limit($limit)
  164. ->skip($offset)
  165. ->select();
  166. foreach ($list as $k=>$v) {
  167. $oid = $v['_id']->jsonSerialize();
  168. $list[$k]['id'] = $oid['$oid'];
  169. }
  170. $result = array("total" => count($total), "rows" => $list);
  171. return json($result);
  172. }
  173. return $this->view->fetch();
  174. }
  175. /**
  176. * 新增
  177. */
  178. public function add()
  179. {
  180. if ($this->request->isAjax()){
  181. $req = $this->request->post();
  182. if(!isset($req['row']['product_code']) || empty($req['row']['product_code'])){
  183. $this->error('请填写编号');
  184. }else{
  185. $data = [
  186. 'product_code'=>$req['row']['product_code'],
  187. 'delete_time'=>'',
  188. ];
  189. if ($this->model->where($data)->find()){
  190. $this->error('编号已存在');
  191. }
  192. }
  193. if(!isset($req['row']['product_name']) || empty($req['row']['product_name'])){
  194. $this->error('请填写名称');
  195. }else{
  196. $data = [
  197. 'product_name'=>$req['row']['product_name'],
  198. 'delete_time'=>'',
  199. ];
  200. if ($this->model->where($data)->find()){
  201. $this->error('名称已存在');
  202. }
  203. }
  204. if(!isset($req['row']['temple']) || empty($req['row']['temple'])){
  205. $this->error('请填写辅料代号');
  206. }
  207. if(!isset($req['row']['main_unit'])){
  208. $this->error('请填写主计量单位');
  209. }
  210. if(!isset($req['row']['sec_unit'])){
  211. $this->error('请填写辅计量单位');
  212. }
  213. if(!isset($req['row']['proportion'])){
  214. $this->error('请填写换算关系');
  215. }
  216. $data = [
  217. 'product_code' =>$req['row']['product_code'],
  218. 'product_name' =>$req['row']['product_name'],
  219. 'temple' =>$req['row']['temple'],
  220. 'main_unit' =>$req['row']['main_unit'],
  221. 'sec_unit' =>$req['row']['sec_unit'],
  222. 'proportion' =>$req['row']['proportion'],
  223. 'create_time' =>date('Y-m-d H:i:s'),
  224. ];
  225. //插入数据
  226. $re = $this->model->save($data);
  227. if($re){
  228. $this->success('成功');
  229. }else{
  230. $this->error('失败');
  231. }
  232. }
  233. return $this->view->fetch();
  234. }
  235. /**
  236. * 编辑
  237. */
  238. public function edit($ids = NULL)
  239. {
  240. if ($this->request->isAjax()){
  241. $req = $this->request->post();
  242. if(!isset($req['row']['product_code']) || empty($req['row']['product_code'])){
  243. $this->error('请填写编号');
  244. }else{
  245. $data = [
  246. 'product_code'=>$req['row']['product_code'],
  247. 'delete_time'=>'',
  248. ];
  249. if ($this->model->where($data)->where('_id','neq',$ids)->find()){
  250. $this->error('编号已存在');
  251. }
  252. }
  253. if(!isset($req['row']['product_name']) || empty($req['row']['product_name'])){
  254. $this->error('请填写名称');
  255. }else{
  256. $data = [
  257. 'product_name'=>$req['row']['product_name'],
  258. 'delete_time'=>'',
  259. ];
  260. if ($this->model->where($data)->where('_id','neq',$ids)->find()){
  261. $this->error('名称已存在');
  262. }
  263. }
  264. if(!isset($req['row']['temple']) || empty($req['row']['temple'])){
  265. $this->error('请填写辅料代号');
  266. }
  267. if(!isset($req['row']['main_unit'])){
  268. $this->error('请填写主计量单位');
  269. }
  270. if(!isset($req['row']['sec_unit'])){
  271. $this->error('请填写辅计量单位');
  272. }
  273. if(!isset($req['row']['proportion'])){
  274. $this->error('请填写换算关系');
  275. }
  276. $data = [
  277. 'product_code' =>$req['row']['product_code'],
  278. 'product_name' =>$req['row']['product_name'],
  279. 'temple' =>$req['row']['temple'],
  280. 'main_unit' =>$req['row']['main_unit'],
  281. 'sec_unit' =>$req['row']['sec_unit'],
  282. 'proportion' =>$req['row']['proportion'],
  283. 'update_time' =>date('Y-m-d H:i:s'),
  284. ];
  285. //修改数据
  286. $department = \app\admin\model\QcodeProduct::get(['_id' => new \MongoDB\BSON\ObjectID($ids)]);
  287. $re = $department->save($data);
  288. if($re){
  289. $this->success('修改成功');
  290. }else{
  291. $this->error('修改失败');
  292. }
  293. }
  294. $row = $this->model->where('_id',$ids)->find();
  295. $this->view->assign('row',$row);
  296. return $this->view->fetch();
  297. }
  298. /**
  299. * 删除
  300. */
  301. public function del($ids = NULL)
  302. {
  303. $department = \app\admin\model\QcodeProduct::get(['_id' => new \MongoDB\BSON\ObjectID($ids)]);
  304. $department->delete_time = date('Y-m-d H:i:s');
  305. $re = $department->save();
  306. if($re){
  307. $this->success('删除成功');
  308. }else{
  309. $this->error('删除失败');
  310. }
  311. }
  312. /**
  313. * 绑定
  314. */
  315. public function bind($ids = NULL)
  316. {
  317. $userInfo = Session::get('admin');
  318. $company_id = (int)$userInfo['company'];
  319. $db = new QcodeCompany();
  320. //查询是否已经添加
  321. $row = $db->name($company_id.'_'."qcode_company")
  322. ->where(['delete_time'=>'','product_id'=>$ids])
  323. ->find();
  324. if($row) $this->success('已添加');
  325. $data = [
  326. 'product_id'=>$ids,
  327. 'create_time'=>date('Y-m-d H:i:s'),
  328. ];
  329. //插入qcode_company数据
  330. $bool = $db->save($data);
  331. if($bool){
  332. $this->success('添加成功');
  333. }else{
  334. $this->error('添加失败');
  335. }
  336. }
  337. /**
  338. * 解绑
  339. */
  340. public function unbind($ids = NULL)
  341. {
  342. $userInfo = Session::get('admin');
  343. $company_id = (int)$userInfo['company'];
  344. $db = new QcodeCompany();
  345. //查询是否已经添加
  346. $bool = $db->name($company_id.'_'."qcode_company")
  347. ->where(['delete_time'=>'','product_id'=>$ids])
  348. ->update(['delete_time'=>date('Y-m-d H:i:s')]);
  349. if($bool){
  350. $this->success('删除成功');
  351. }else{
  352. $this->error('删除失败');
  353. }
  354. }
  355. }