Order.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Session;
  5. use think\Db;
  6. /**
  7. * 订单管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Order extends Backend
  12. {
  13. /**
  14. * Order模型对象
  15. * @var \app\admin\model\Order
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Order;
  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. * @return string|Json
  33. * @throws \think\Exception
  34. * @throws DbException
  35. */
  36. public function index()
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if (false === $this->request->isAjax()) {
  41. return $this->view->fetch();
  42. }
  43. //如果发送的来源是 Selectpage,则转发到 Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  48. $user_info = Session::get('admin');
  49. $map = [];
  50. if ($user_info['id'] !== 1){
  51. $map['company_id'] = $user_info['company_id'];
  52. }
  53. $list = $this->model
  54. ->where($where)
  55. ->where($map)
  56. ->order($sort, $order)
  57. ->paginate($limit);
  58. $result = ['total' => $list->total(), 'rows' => $list->items()];
  59. return json($result);
  60. }
  61. /**
  62. * 添加
  63. *
  64. * @return string
  65. * @throws \think\Exception
  66. */
  67. public function add()
  68. {
  69. if (false === $this->request->isPost()) {
  70. return $this->view->fetch();
  71. }
  72. $params = $this->request->post('row/a');
  73. if (empty($params)) {
  74. $this->error(__('Parameter %s can not be empty', ''));
  75. }
  76. $params = $this->preExcludeFields($params);
  77. $user_info = Session::get('admin');
  78. $params['user_id'] = $user_info['id'];
  79. $params['company_id'] = $user_info['company_id'];
  80. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  81. $params[$this->dataLimitField] = $this->auth->id;
  82. }
  83. // halt($params);die;
  84. $result = false;
  85. Db::startTrans();
  86. try {
  87. //是否采用模型验证
  88. if ($this->modelValidate) {
  89. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  90. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  91. $this->model->validateFailException()->validate($validate);
  92. }
  93. $result = $this->model->allowField(true)->save($params);
  94. Db::commit();
  95. } catch (ValidateException|PDOException|Exception $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. }
  99. if ($result === false) {
  100. $this->error(__('No rows were inserted'));
  101. }
  102. $this->success();
  103. }
  104. //生产作业票
  105. public function task(){
  106. $ids = input('ids');
  107. if (!$ids) {
  108. $this->error(__('No Results were found'));
  109. }
  110. if (false === $this->request->isPost()) {
  111. $order = Db::name('order')->where('id',$ids)->find();
  112. $map = [];
  113. $map['name'] = array('like','%'.$order['product'].'%');
  114. $map['examine_status'] = 2;
  115. $list = Db::name('formula')->where($map)->field('id,name,version')->order('id desc')->select();
  116. $res = [];
  117. if (!empty($list)){
  118. $res = $this->get_repeat_data($list);
  119. }
  120. // halt($res);die;
  121. $this->view->assign('ids',$ids);
  122. $this->view->assign('row', $res);
  123. return $this->view->fetch();
  124. }
  125. }
  126. //获取二维数组的重复数据
  127. function get_repeat_data($array){
  128. //计算出数组的总数量
  129. $count = count($array);
  130. $num = [];//算出那个值是不要的 去掉就行
  131. for($i=0;$i<$count;$i++){
  132. $i_data = $array[$i]['name'];
  133. for($j=$i+1;$j<$count;$j++){
  134. $j_data = $array[$j]['name'];
  135. if($i_data == $j_data){
  136. $i_version = substr($array[$i]['version'],1);
  137. $j_version = substr($array[$j]['version'],1);
  138. if ($j_version > $i_version){
  139. $num[$i] = $i;
  140. }else{
  141. $num[$i] = $j;
  142. }
  143. }
  144. }
  145. }
  146. foreach ($num as $key=>$value){
  147. unset($array[$value]);
  148. }
  149. $result = array_values($array);
  150. return $result;
  151. }
  152. }