ModelBus.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\TestModel;
  11. use app\api\model\TestPesticides;
  12. use app\api\model\TestProduct;
  13. use app\common\lib\auth\JwtAuth;
  14. class ModelBus
  15. {
  16. private $uid;
  17. public function __construct()
  18. {
  19. $jwtAuth = JwtAuth::getInstance();
  20. $this->uid = $jwtAuth->getUid();
  21. }
  22. //获取当前用户的模型页列表
  23. public function selectModelList($data)
  24. {
  25. $pageNum = $data['pageNum'];
  26. $pageSize = $data['pageSize'];
  27. $type_id = $data['type_id'];//1.种植业 2.畜牧业 3.渔业 4.其他
  28. $where = [];
  29. if (!empty($type_id)) {
  30. array_push($where, ['type_id', '=', (int)$type_id]);
  31. }
  32. $result = (new TestModel())->getListByUserId($this->uid,$pageNum,$pageSize,$where)->toArray();
  33. $count = (new TestModel())->countListByUserId($this->uid, $where);
  34. return ['rows'=>$result, 'total' => $count,];
  35. }
  36. //获取模型信息
  37. public function getModelInfo($tmp_data)
  38. {
  39. $model_id = $tmp_data['id'];
  40. $model_info=(new TestModel())->getModelInfoByModelID($model_id);//获取模型信息
  41. $model_product=(new TestModel())->getProductInfo($model_id)->toArray();//检测对象
  42. $model_pesticides = (new TestModel())->getPesticidesInfo($model_id)->toArray();//检测项
  43. $data=[
  44. 'name'=>$model_info,
  45. 'product'=>$model_product,
  46. 'pesticides'=>$model_pesticides
  47. ];
  48. return $data;
  49. }
  50. public function updateInfoByTaskID($data)
  51. {
  52. $task_id = $data['task_id'];
  53. $tmp_data = [
  54. 'endtime_cydsb' => $data['endtime_cydsb'],
  55. 'is_divide' => $data['is_divide'],
  56. 'test_model_id' => $data['test_model_id'],
  57. ];
  58. $result = (new Ccjc())->updateInfoByTaskID($task_id, $tmp_data);
  59. return $result;
  60. }
  61. //修改模型信息
  62. public function updateModelInfo($model_id,$model_info,$product='',$pesticides='')
  63. {
  64. //检测对象如果不为空则存在需要更新的信息,$product_update_Data
  65. $product_update_data = [];
  66. if ($product != '') {
  67. foreach ($product as $k => $value) {
  68. $product_update_data[$k] = [
  69. 'test_model_id' => $model_id,
  70. 'product_id' => $value['id'],
  71. 'product_name' => $value['name'],
  72. ];
  73. }
  74. //删除原有Product中model_id的内容
  75. (new TestProduct())->deleteProductInfoByTestModelID($model_id);
  76. //添加新的Product中model_id的内容
  77. (new TestProduct())->insertProductInfo($product_update_data);
  78. }
  79. //检测项目如果不为空则存在需要更新的信息,$product_update_Data
  80. $pesticides_update_data = [];
  81. if ($pesticides != '') {
  82. foreach ($pesticides as $k => $value) {
  83. $pesticides_update_data [$k]= [
  84. 'test_model_id' => $model_id,
  85. 'test_id' => $value['id'],
  86. 'test_name' => $value['name'],
  87. ];
  88. }
  89. //删除原有Pesticides中model_id的内容
  90. (new TestPesticides())->deletePesticidesInfoByTestModelID($model_id);
  91. //添加新的Pesticides中model_id的内容
  92. (new TestPesticides())->insertPesticidesInfo($pesticides_update_data);
  93. }
  94. //更新model_test表中的信息(model_name和type_id)
  95. (new TestModel())->updateModelInfoByModelID($model_id,$model_info);
  96. return true;
  97. }
  98. //新增模型
  99. public function createModelInfo($model_name,$type_id,$product_data,$pesticides_data)
  100. {
  101. $uid=$this->uid;
  102. $model_id = (new TestModel())->createModelInfo($model_name, $type_id, $uid);
  103. //格式化$product_data=》$product
  104. $product=[];
  105. //todo 需要重写foreach
  106. foreach ($product_data as $k=>$v){
  107. $j=[];
  108. $j['test_model_id']=$model_id;
  109. $j['product_id']=$v['id'];
  110. $j['product_name']=$v['name'];
  111. $product[]=$j;
  112. }
  113. //格式化$pesticides_data=》$pesticides
  114. $pesticides=[];
  115. foreach ($pesticides_data as $i=>$o){
  116. $p=[];
  117. $p['test_model_id']=$model_id;
  118. $p['test_name']=$o['name'];
  119. $p['test_id']=$o['id'];
  120. $pesticides[]=$p;
  121. }
  122. //todo 写到一个model里
  123. $update_test_product = (new TestProduct())->createProductInfo($product);
  124. if ($update_test_product == 0) {
  125. throw new ApiException(config('status.none_model_product'));
  126. }
  127. $update_test_pesticide =(new TestPesticides())->insertPesticidesInfo($pesticides);
  128. if ($update_test_pesticide == 0) {
  129. throw new ApiException(config('status.none_model_pesticides'));
  130. }
  131. return true;
  132. }
  133. //删除模型
  134. public function deleteModelInfo($data,$data_count)
  135. {
  136. for ($i = 0; $i < $data_count; $i++) {
  137. //从数据库获取当前模型的user_id //验证模型创建者是否一致
  138. $user_id = (new TestModel())->getUserIdByModelID($data[$i]['id'])['user_id'];
  139. if ($user_id != $this->uid) {
  140. throw new ApiException(config('status.none_authority'));
  141. }
  142. //删除test_model信息
  143. (new TestModel())->deleteModelInfoByID($data[$i]['id']);
  144. //删除test_product信息
  145. (new TestProduct())->deleteProductInfoByTestModelID($data[$i]['id']);
  146. //删除test_pesticides信息
  147. (new TestPesticides())->deletePesticidesInfoByTestModelID($data[$i]['id']);
  148. }
  149. return true;
  150. }
  151. }