Product.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. protected $noNeedRight = ['ajax','get_formula'];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Product;
  25. $this->detail = new \app\admin\model\ProductDetail;
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //当前是否为关联查询
  39. $this->relationSearch = false;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $list = $this->model
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  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. Db::startTrans();
  138. try {
  139. $pro = [];
  140. foreach(explode(';',$params['bach']) as $key=> $val){
  141. if(!empty($val)){
  142. $res = [];
  143. $res['time'] = date("Y-m-d H:i:s");
  144. $res['audit'] = $params['audit'][$key];
  145. $res['status'] = $params['status'];
  146. $res['pickor'] = $params['pickor'];
  147. $res['note'] = $params['note'];
  148. $res['warehouseor'] = $params['warehouseor'];
  149. $res['batch'] = $val;
  150. $res['pname'] = $params['proname'][$key];
  151. $res['specifications'] = $params['specifications'][$key];
  152. $res['unit'] = $params['unit'][$key];
  153. $pro[] = $res;
  154. }
  155. }
  156. foreach($pro as $v){
  157. $this->model->insert($v);
  158. $id = $this->model->getLastInsID();
  159. $arr = [];
  160. $prodet = [];
  161. foreach($params['material'] as $k=>$vv){
  162. if($v['batch']==$params['batch'][$k]){
  163. $arr['weight'] = $params['weight'][$k];
  164. $arr['pid'] = /*1;//*/$id;
  165. $arr['material'] = $params['material'][$k];
  166. $arr['createtime'] = date("Y-m-d H:i:s");
  167. $prodet[] = $arr;
  168. }
  169. }
  170. $this->detail->saveAll($prodet);
  171. }
  172. Db::commit();
  173. }catch(Exception $e){
  174. Db::rollback();
  175. $this->error($e->getMessage());
  176. }
  177. $this->success();
  178. return $this->view->fetch();
  179. }
  180. public function show($ids){
  181. $row = $this->model->get($ids);
  182. $row['data'] = $this->detail->where('pid',$row['id'])->select();
  183. // halt($row['data']);
  184. $this->view->assign("row", $row);
  185. return $this->view->fetch();
  186. }
  187. }