QcodeProduct.php 13 KB

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