ModelBus.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. *
  4. *User:Administrator
  5. *Date:2021/10/11
  6. */
  7. namespace app\api\business;
  8. use app\api\exception\ApiException;
  9. use app\api\model\Ccjc;
  10. use app\api\model\Pesticides;
  11. use app\api\model\ProductClass;
  12. use app\api\model\TestModel;
  13. use app\api\model\TestPesticides;
  14. use app\api\model\TestProduct;
  15. use app\common\lib\Arr;
  16. use app\common\lib\auth\JwtAuth;
  17. class ModelBus
  18. {
  19. private $uid;
  20. public function __construct()
  21. {
  22. $jwtAuth = JwtAuth::getInstance();
  23. $this->uid = $jwtAuth->getUid();
  24. }
  25. //获取当前用户的模型页列表
  26. public function selectModelList($data)
  27. {
  28. $pageNum = $data['pageNum'];
  29. $pageSize = $data['pageSize'];
  30. $type_id = $data['type_id'];//1.种植业 2.畜牧业 3.渔业 4.其他
  31. $where = [];
  32. if (!empty($type_id)) {
  33. array_push($where, ['type_id', '=', (int)$type_id]);
  34. }
  35. $result = (new TestModel())->getListByUserId($this->uid,$pageNum,$pageSize,$where)->toArray();
  36. $count = (new TestModel())->countListByUserId($this->uid, $where);
  37. return ['rows'=>$result, 'total' => $count,];
  38. }
  39. //获取模型信息
  40. public function getModelInfo($tmp_data)
  41. {
  42. $model_id = $tmp_data['id'];
  43. $model_info=(new TestModel())->getModelInfoByModelID($model_id);//获取模型信息
  44. $model_product=(new TestModel())->getProductInfo($model_id)->toArray();//检测对象
  45. $model_pesticides = (new TestModel())->getPesticidesInfo($model_id)->toArray();//检测项
  46. $data=[
  47. 'name'=>$model_info,
  48. 'product'=>$model_product,
  49. 'pesticides'=>$model_pesticides
  50. ];
  51. return $data;
  52. }
  53. public function updateInfoByTaskID($data)
  54. {
  55. $task_id = $data['task_id'];
  56. $tmp_data = [
  57. 'endtime_cydsb' => $data['endtime_cydsb'],
  58. 'is_divide' => $data['is_divide'],
  59. 'test_model_id' => $data['test_model_id'],
  60. ];
  61. $result = (new Ccjc())->updateInfoByTaskID($task_id, $tmp_data);
  62. return $result;
  63. }
  64. //修改模型信息
  65. public function updateModelInfo($model_id,$model_info,$product='',$pesticides='')
  66. {
  67. //检测对象如果不为空则存在需要更新的信息,$product_update_Data
  68. $product_update_data = [];
  69. if ($product != '') {
  70. foreach ($product as $k => $value) {
  71. $product_update_data[$k] = [
  72. 'test_model_id' => $model_id,
  73. 'product_id' => $value['id'],
  74. 'product_name' => $value['name'],
  75. ];
  76. }
  77. //删除原有Product中model_id的内容
  78. (new TestProduct())->deleteProductInfoByTestModelID($model_id);
  79. //添加新的Product中model_id的内容
  80. (new TestProduct())->insertProductInfo($product_update_data);
  81. }
  82. //检测项目如果不为空则存在需要更新的信息,$product_update_Data
  83. $pesticides_update_data = [];
  84. if ($pesticides != '') {
  85. foreach ($pesticides as $k => $value) {
  86. $pesticides_update_data [$k]= [
  87. 'test_model_id' => $model_id,
  88. 'test_id' => $value['id'],
  89. 'test_name' => $value['name'],
  90. ];
  91. }
  92. //删除原有Pesticides中model_id的内容
  93. (new TestPesticides())->deletePesticidesInfoByTestModelID($model_id);
  94. //添加新的Pesticides中model_id的内容
  95. (new TestPesticides())->insertPesticidesInfo($pesticides_update_data);
  96. }
  97. //更新model_test表中的信息(model_name和type_id)
  98. (new TestModel())->updateModelInfoByModelID($model_id,$model_info);
  99. return true;
  100. }
  101. //新增模型
  102. public function createModelInfo($model_name,$type_id,$product_data,$pesticides_data)
  103. {
  104. $uid=$this->uid;
  105. $model_id = (new TestModel())->createModelInfo($model_name, $type_id, $uid);
  106. //格式化$product_data=》$product
  107. $product=[];
  108. //todo 需要重写foreach
  109. foreach ($product_data as $k=>$v){
  110. $j=[];
  111. $j['test_model_id']=$model_id;
  112. $j['product_id']=$v['id'];
  113. $j['product_name']=$v['name'];
  114. $product[]=$j;
  115. }
  116. //格式化$pesticides_data=》$pesticides
  117. $pesticides=[];
  118. foreach ($pesticides_data as $i=>$o){
  119. $p=[];
  120. $p['test_model_id']=$model_id;
  121. $p['test_name']=$o['name'];
  122. $p['test_id']=$o['id'];
  123. $pesticides[]=$p;
  124. }
  125. //todo 写到一个model里
  126. $update_test_product = (new TestProduct())->createProductInfo($product);
  127. if ($update_test_product == 0) {
  128. throw new ApiException(config('status.none_model_product'));
  129. }
  130. $update_test_pesticide =(new TestPesticides())->insertPesticidesInfo($pesticides);
  131. if ($update_test_pesticide == 0) {
  132. throw new ApiException(config('status.none_model_pesticides'));
  133. }
  134. return true;
  135. }
  136. //删除模型
  137. public function deleteModelInfo($data,$data_count)
  138. {
  139. for ($i = 0; $i < $data_count; $i++) {
  140. //从数据库获取当前模型的user_id //验证模型创建者是否一致
  141. $user_id = (new TestModel())->getUserIdByModelID($data[$i]['id'])['user_id'];
  142. //查询模型绑定任务,如果绑定则禁止删除
  143. $ccjc_info = (new Ccjc())->getTaskInfoByModelId($data[$i]['id']);
  144. if ( !($ccjc_info==[])) {
  145. throw new ApiException(config('status.err_disable_delete'));
  146. }
  147. if ($user_id != $this->uid) {
  148. throw new ApiException(config('status.none_authority'));
  149. }
  150. //删除test_model信息
  151. (new TestModel())->deleteModelInfoByID($data[$i]['id']);
  152. //删除test_product信息
  153. (new TestProduct())->deleteProductInfoByTestModelID($data[$i]['id']);
  154. //删除test_pesticides信息
  155. (new TestPesticides())->deletePesticidesInfoByTestModelID($data[$i]['id']);
  156. }
  157. return true;
  158. }
  159. //获取检测项目列表
  160. public function selectPesticidesList($pageNum,$pageSize,$key_word)
  161. {
  162. $data = (new Pesticides())->selectPesticidesList($key_word, $pageNum, $pageSize)->toArray();
  163. $count = (new Pesticides())->countPesticidesList($key_word);
  164. $result = [
  165. 'rows' => $data,
  166. 'total' => $count,
  167. ];
  168. return $result;
  169. }
  170. //获取检测对象列表
  171. public function selectProductList($type_id)
  172. {
  173. //todo cache缓存优化数据
  174. $field='id,parent_id,product_code,name,alias';
  175. $tmp_result = (new ProductClass())->selectProductListByTypeID($type_id,$field)->toArray();//字段调用的field过滤
  176. //todo 树状化需要看一下,回调可以优化
  177. $result = (new Arr())->Arr_tree($tmp_result,'0'.$type_id);
  178. return $result;
  179. }
  180. }