QcodeProduct.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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)->count();
  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" => $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)->count();
  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" => $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. 'code' =>substr($req['row']['product_code'],0,4),
  221. 'create_time' =>time(),
  222. ];
  223. //插入数据
  224. $re = $this->model->save($data);
  225. if($re){
  226. $this->success('成功');
  227. }else{
  228. $this->error('失败');
  229. }
  230. }
  231. return $this->view->fetch();
  232. }
  233. /**
  234. * 编辑
  235. */
  236. public function edit($ids = NULL)
  237. {
  238. if ($this->request->isAjax()){
  239. $req = $this->request->post();
  240. if(!isset($req['row']['product_code']) || empty($req['row']['product_code'])){
  241. $this->error('请填写编号');
  242. }else{
  243. $data = [
  244. 'product_code'=>$req['row']['product_code'],
  245. 'delete_time'=>'',
  246. ];
  247. if ($this->model->where($data)->where('_id','neq',$ids)->find()){
  248. $this->error('编号已存在');
  249. }
  250. }
  251. if(!isset($req['row']['product_name']) || empty($req['row']['product_name'])){
  252. $this->error('请填写名称');
  253. }else{
  254. $data = [
  255. 'product_name'=>$req['row']['product_name'],
  256. 'delete_time'=>'',
  257. ];
  258. if ($this->model->where($data)->where('_id','neq',$ids)->find()){
  259. $this->error('名称已存在');
  260. }
  261. }
  262. if(!isset($req['row']['temple']) || empty($req['row']['temple'])){
  263. $this->error('请填写辅料代号');
  264. }
  265. if(!isset($req['row']['main_unit'])){
  266. $this->error('请填写主计量单位');
  267. }
  268. if(!isset($req['row']['sec_unit'])){
  269. $this->error('请填写辅计量单位');
  270. }
  271. if(!isset($req['row']['proportion'])){
  272. $this->error('请填写换算关系');
  273. }
  274. $data = [
  275. 'product_code' =>$req['row']['product_code'],
  276. 'product_name' =>$req['row']['product_name'],
  277. 'temple' =>$req['row']['temple'],
  278. 'main_unit' =>$req['row']['main_unit'],
  279. 'sec_unit' =>$req['row']['sec_unit'],
  280. 'proportion' =>$req['row']['proportion'],
  281. 'update_time' =>time(),
  282. ];
  283. //修改数据
  284. $department = \app\admin\model\QcodeProduct::get(['_id' => new \MongoDB\BSON\ObjectID($ids)]);
  285. $re = $department->save($data);
  286. if($re){
  287. $this->success('修改成功');
  288. }else{
  289. $this->error('修改失败');
  290. }
  291. }
  292. $row = $this->model->where('_id',$ids)->find();
  293. $this->view->assign('row',$row);
  294. return $this->view->fetch();
  295. }
  296. /**
  297. * 删除
  298. */
  299. public function del($ids = NULL)
  300. {
  301. $department = \app\admin\model\QcodeProduct::get(['_id' => new \MongoDB\BSON\ObjectID($ids)]);
  302. $department->delete_time = time();
  303. $re = $department->save();
  304. if($re){
  305. $this->success('删除成功');
  306. }else{
  307. $this->error('删除失败');
  308. }
  309. }
  310. /**
  311. * 绑定
  312. */
  313. public function bind($ids = NULL)
  314. {
  315. $userInfo = Session::get('admin');
  316. $company_id = (int)$userInfo['company'];
  317. $db = new QcodeCompany();
  318. //查询是否已经添加
  319. $row = $db->name($company_id.'_'."qcode_company")
  320. ->where(['delete_time'=>'','product_id'=>$ids])
  321. ->find();
  322. if($row) $this->success('已添加');
  323. $data = [
  324. 'product_id'=>$ids,
  325. 'create_time'=>time(),
  326. ];
  327. //插入qcode_company数据
  328. $bool = $db->save($data);
  329. if($bool){
  330. $this->success('添加成功');
  331. }else{
  332. $this->error('添加失败');
  333. }
  334. }
  335. /**
  336. * 解绑
  337. */
  338. public function unbind($ids = NULL)
  339. {
  340. $userInfo = Session::get('admin');
  341. $company_id = (int)$userInfo['company'];
  342. $db = new QcodeCompany();
  343. //查询是否已经添加
  344. $bool = $db->name($company_id.'_'."qcode_company")
  345. ->where(['delete_time'=>'','product_id'=>$ids])
  346. ->update(['delete_time'=>time()]);
  347. if($bool){
  348. $this->success('删除成功');
  349. }else{
  350. $this->error('删除失败');
  351. }
  352. }
  353. }