Product.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Cache;
  5. use think\Db;
  6. use think\Exception;
  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. protected $detail = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\Product;
  24. $this->detail = new \app\admin\model\ProductDetail;
  25. $this->view->assign("statusList", $this->model->getStatusList());
  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. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $list = $this->model
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. $result = array("total" => $list->total(), "rows" => $list->items());
  52. return json($result);
  53. }
  54. return $this->view->fetch();
  55. }
  56. //获取生产批次号等相关信息
  57. public function ajax(){
  58. $bach = $this->request->post('bach');
  59. $str = explode(';',$bach);
  60. $batch = $str[(count($str))-1];
  61. $bach_info = Db::name('task')->where('bach',$batch)->find();
  62. Cache::set('bach',serialize($bach_info),0);
  63. $form = Db::name('formula')->find($bach_info['fid']);
  64. $res = Db::name('formula_detail')->where('pid = '.$bach_info['fid'].' and version = "'.$form['version'].'"')->field('id,pid,material,version,is_replace')->select();
  65. foreach($res as &$v){
  66. if($v['is_replace']==1){
  67. $v['replace'] = Db::name('formula_replace')->where('fid',$v['id'])->order('id','desc')->select();
  68. }else{
  69. $v['replace'] = [];
  70. }
  71. }
  72. //查出所有关于批次号的数据存储
  73. Cache::set('data',serialize($res),0);
  74. return json($bach_info);
  75. }
  76. //获取配方单信息
  77. public function get_formula(){
  78. $bach = unserialize(Cache::get('bach'));//获取配方单信息
  79. $formula = $this->request->post('formula');//配方名称
  80. $data = unserialize(Cache::get('data'));//获取当前批次号缓存数据
  81. $arr = [];
  82. $arr['material']=$formula;
  83. foreach($data as $vv) {
  84. //没有替代料 error==0为有当前配方, error==1为没有配方,,前台提示
  85. if ($vv['is_replace'] == 0) {
  86. if ($vv['material'] == $formula) {
  87. $arr['error'] = 0;
  88. break;
  89. } else {
  90. $arr['error'] = 1;
  91. }
  92. } else {//有替代料
  93. foreach ($vv['replace'] as $val) {
  94. if ($val['material'] == $formula) {
  95. $arr['error'] = 0;
  96. break;
  97. } else {
  98. $arr['error'] = 1;
  99. }
  100. }
  101. if ($arr['error'] == 0) {
  102. break;
  103. }
  104. }
  105. }
  106. //查询数据库↓ 缓存↑
  107. /*$form = Db::name('formula')->find($bach['fid']);
  108. $res = Db::name('formula_detail')->where('pid = '.$bach['fid'].' and material like "%'.encrypt($formula).'%" and version = "'.$form['version'].'"')->select();
  109. $arr = [];
  110. if(!$res){
  111. $arr['material'] = $formula;
  112. $arr['error']=1;
  113. }else{
  114. foreach($res as &$v){
  115. if($v['is_replace']==1){
  116. $v['material'] = Db::name('formula_replace')->where('fid',$v['id'])->where('material',encrypt($formula))->order('id','desc')->value('material');
  117. }
  118. $arr=$v;
  119. $arr['error']=0;
  120. }
  121. }*/
  122. $arr['pname'] = $bach['name'];//配方名称
  123. $arr['time'] = date("Y-m-d H:i:s");
  124. $arr['bach'] = $bach['bach'];
  125. return json($arr);
  126. }
  127. public function add()
  128. {
  129. if (false === $this->request->isPost()) {
  130. return $this->view->fetch();
  131. }
  132. $params = $this->request->post('row/a');
  133. if (empty($params)) {
  134. $this->error(__('Parameter %s can not be empty', ''));
  135. }
  136. Db::startTrans();
  137. try {
  138. $pro = [];
  139. foreach(explode(';',$params['bach']) as $key=> $val){
  140. if(!empty($val)){
  141. $res = [];
  142. $res['time'] = date("Y-m-d H:i:s");
  143. $res['audit'] = $params['audit'][$key];
  144. $res['status'] = $params['status'];
  145. $res['batch'] = $val;
  146. $res['pname'] = $params['proname'][$key];
  147. $res['specifications'] = $params['specifications'][$key];
  148. $res['unit'] = $params['unit'][$key];
  149. $pro[] = $res;
  150. }
  151. }
  152. foreach($pro as $v){
  153. $this->model->insert($v);
  154. $id = $this->model->getLastInsID();
  155. $arr = [];
  156. $prodet = [];
  157. foreach($params['material'] as $k=>$vv){
  158. if($v['batch']==$params['batch'][$k]){
  159. $arr['weight'] = $params['weight'][$k];
  160. $arr['pid'] = /*1;//*/$id;
  161. $arr['material'] = $params['material'][$k];
  162. $arr['createtime'] = date("Y-m-d H:i:s");
  163. $prodet[] = $arr;
  164. }
  165. }
  166. $this->detail->saveAll($prodet);
  167. }
  168. Db::commit();
  169. }catch(Exception $e){
  170. Db::rollback();
  171. $this->error($e->getMessage());
  172. }
  173. $this->success();
  174. return $this->view->fetch();
  175. }
  176. public function show($ids){
  177. $row = $this->model->get($ids);
  178. $row['data'] = $this->detail->where('pid',$row['id'])->select();
  179. // halt($row['data']);
  180. $this->view->assign("row", $row);
  181. return $this->view->fetch();
  182. }
  183. }