Product.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Cache;
  5. use think\Db;
  6. /**
  7. * 生产管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Product extends Backend
  12. {
  13. /**
  14. * Product模型对象
  15. * @var \app\admin\model\Product
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Product;
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //当前是否为关联查询
  35. $this->relationSearch = false;
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags', 'trim']);
  38. if ($this->request->isAjax()) {
  39. //如果发送的来源是Selectpage,则转发到Selectpage
  40. if ($this->request->request('keyField')) {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $list = $this->model
  45. ->where($where)
  46. ->order($sort, $order)
  47. ->paginate($limit);
  48. foreach ($list as $row) {
  49. $row->visible(['id','time','weight','material','status']);
  50. $row['material'] = decode($row['material']);
  51. }
  52. $result = array("total" => $list->total(), "rows" => $list->items());
  53. return json($result);
  54. }
  55. return $this->view->fetch();
  56. }
  57. //获取生产批次号等相关信息
  58. public function ajax(){
  59. $bach = $this->request->post('bach');
  60. $str = explode(';',$bach);
  61. $batch = $str[(count($str))-1];
  62. $bach_info = Db::name('task')->where('bach',$batch)->find();
  63. Cache::set('bach',serialize($bach_info),0);
  64. $form = Db::name('formula')->find($bach_info['fid']);
  65. $res = Db::name('formula_detail')->where('pid = '.$bach_info['fid'].' and version = "'.$form['version'].'"')->field('id,pid,material,version,is_replace')->select();
  66. foreach($res as &$v){
  67. if($v['is_replace']==1){
  68. $v['replace'] = Db::name('formula_replace')->where('fid',$v['id'])->order('id','desc')->select();
  69. }else{
  70. $v['replace'] = [];
  71. }
  72. }
  73. //查出所有关于批次号的数据存储
  74. Cache::set('data',serialize($res),0);
  75. return json($bach_info);
  76. }
  77. //获取配方单信息
  78. public function get_formula(){
  79. $bach = unserialize(Cache::get('bach'));//获取配方单信息
  80. $formula = $this->request->post('formula');//配方名称
  81. $data = unserialize(Cache::get('data'));//获取当前批次号缓存数据
  82. $arr = [];
  83. $arr['material']=$formula;
  84. foreach($data as $vv) {
  85. //没有替代料 error==0为有当前配方, error==1为没有配方,,前台提示
  86. if ($vv['is_replace'] == 0) {
  87. if ($vv['material'] == $formula) {
  88. $arr['error'] = 0;
  89. break;
  90. } else {
  91. $arr['error'] = 1;
  92. }
  93. } else {//有替代料
  94. foreach ($vv['replace'] as $val) {
  95. if ($val['material'] == $formula) {
  96. $arr['error'] = 0;
  97. break;
  98. } else {
  99. $arr['error'] = 1;
  100. }
  101. }
  102. if ($arr['error'] == 0) {
  103. break;
  104. }
  105. }
  106. }
  107. //查询数据库↓ 缓存↑
  108. /*$form = Db::name('formula')->find($bach['fid']);
  109. $res = Db::name('formula_detail')->where('pid = '.$bach['fid'].' and material like "%'.encrypt($formula).'%" and version = "'.$form['version'].'"')->select();
  110. $arr = [];
  111. if(!$res){
  112. $arr['material'] = $formula;
  113. $arr['error']=1;
  114. }else{
  115. foreach($res as &$v){
  116. if($v['is_replace']==1){
  117. $v['material'] = Db::name('formula_replace')->where('fid',$v['id'])->where('material',encrypt($formula))->order('id','desc')->value('material');
  118. }
  119. $arr=$v;
  120. $arr['error']=0;
  121. }
  122. }*/
  123. $arr['pname'] = $bach['name'];//配方名称
  124. $arr['time'] = date("Y-m-d H:i:s");
  125. $arr['bach'] = $bach['bach'];
  126. return json($arr);
  127. }
  128. public function add()
  129. {
  130. if (false === $this->request->isPost()) {
  131. return $this->view->fetch();
  132. }
  133. $params = $this->request->post('row/a');
  134. if (empty($params)) {
  135. $this->error(__('Parameter %s can not be empty', ''));
  136. }
  137. $arr = [];
  138. foreach($params['material'] as $k=>$v){
  139. $arr[$k]['status'] = $params['status'];
  140. $arr[$k]['batch'] = $params['batch'][$k];
  141. $arr[$k]['pname'] = $params['pname'][$k];
  142. $arr[$k]['specifications'] = $params['specifications'][$k];
  143. $arr[$k]['time'] = $params['time'][$k];
  144. $arr[$k]['audit'] = $params['audit'];
  145. $arr[$k]['unit'] = $params['unit'][$k];
  146. $arr[$k]['material'] = encrypt($params['material'][$k]);
  147. $arr[$k]['weight'] = $params['weight'][$k];
  148. }
  149. $result = $this->model->saveAll($arr);
  150. if ($result === false) {
  151. $this->error(__('No rows were inserted'));
  152. }
  153. $this->success();
  154. return $this->view->fetch();
  155. }
  156. }