ModelBus.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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($model_id)
  38. {
  39. //todo 这里可以连表查询,可以多对多,以后再研究优化
  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($task_id, $data)
  51. {
  52. $result = (new Ccjc())->updateInfoByTaskID($task_id, $data);
  53. return $result;
  54. }
  55. //修改模型信息
  56. public function updateModelInfo($model_id,$model_info,$product='',$pesticides='')
  57. {
  58. ////todo ------------- foreach有问题!!! 直接复制的,要重写!!
  59. if ($product != '') {
  60. $product_update_data=[];
  61. foreach ($product as $k=>$v){
  62. $j=[];
  63. $j['test_model_id']=$model_id;
  64. $j['product_id']=$v['id'];
  65. $j['product_name']=$v['name'];
  66. $product_update_data[]=$j;
  67. }
  68. }
  69. if ($pesticides != '') {
  70. $pesticides_update_data=[];
  71. foreach ($pesticides as $i=>$o){
  72. $p=[];
  73. $p['test_model_id']=$model_id;
  74. $p['test_name']=$o['name'];
  75. $p['test_id']=$o['id'];
  76. $pesticides_update_data[]=$p;
  77. }
  78. }
  79. //更新model_test表中的信息
  80. (new TestModel())->updateModelInfoByModelID($model_id,$model_info);
  81. //删除原有Product中model_id的内容
  82. (new TestProduct())->deleteProductInfoByTestModelID($model_id);
  83. //删除原有Pesticides中model_id的内容
  84. (new TestPesticides())->deletePesticidesInfoByTestModelID($model_id);
  85. //添加新的Product中model_id的内容
  86. $updateProductInfo = (new TestProduct())->insertProductInfo($product_update_data);
  87. //添加新的Pesticides中model_id的内容
  88. $updatePesticidesInfo = (new TestPesticides())->insertPesticidesInfo($pesticides_update_data);
  89. if ($updateProductInfo == 0) {
  90. return showError(config('status.none_model_product'),config('status.none_model_product'));
  91. }
  92. if ($updatePesticidesInfo == 0) {
  93. return showError(config('status.none_model_pesticides'),config('status.none_model_pesticides'));
  94. }
  95. return true;
  96. }
  97. //新增模型
  98. public function createModelInfo($model_name,$type_id,$product_data,$pesticides_data)
  99. {
  100. $uid=$this->uid;
  101. $model_id = (new TestModel())->createModelInfo($model_name, $type_id, $uid);
  102. //格式化$product_data=》$product
  103. $product=[];
  104. //todo 需要重写foreach
  105. foreach ($product_data as $k=>$v){
  106. $j=[];
  107. $j['test_model_id']=$model_id;
  108. $j['product_id']=$v['id'];
  109. $j['product_name']=$v['name'];
  110. $product[]=$j;
  111. }
  112. //格式化$pesticides_data=》$pesticides
  113. $pesticides=[];
  114. foreach ($pesticides_data as $i=>$o){
  115. $p=[];
  116. $p['test_model_id']=$model_id;
  117. $p['test_name']=$o['name'];
  118. $p['test_id']=$o['id'];
  119. $pesticides[]=$p;
  120. }
  121. //todo 写到一个model里
  122. $update_test_product = (new TestProduct())->createProductInfo($product);
  123. if ($update_test_product == 0) {
  124. throw new ApiException(config('status.none_model_product'));
  125. }
  126. $update_test_pesticide =(new TestPesticides())->insertPesticidesInfo($pesticides);
  127. if ($update_test_pesticide == 0) {
  128. throw new ApiException(config('status.none_model_pesticides'));
  129. }
  130. return true;
  131. }
  132. //删除模型
  133. public function deleteModelInfo($del_arr)
  134. {
  135. //验证模型创建者是否一致
  136. foreach ($del_arr as $k => $v) {
  137. //从数据库获取当前模型的user_id
  138. if (empty($v['id'])) {
  139. throw new ApiException(config('status.none_model_info'));
  140. }
  141. $user_id = (new TestModel())->getUserIdByModelID($v['id'])['user_id'];
  142. if ($user_id != $this->uid) {
  143. throw new ApiException(config('status.none_authority'));
  144. }
  145. //删除test_model信息
  146. (new TestModel())->deleteModelInfoByID($v['id']);
  147. //删除test_product信息
  148. (new TestProduct())->deleteProductInfoByTestModelID($v['id']);
  149. //删除test_pesticides信息
  150. (new TestPesticides())->deletePesticidesInfoByTestModelID($v['id']);
  151. }
  152. return true;
  153. }
  154. }