Product.php 4.0 KB

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