ModelBus.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. 'industry' => (new TestModel())->getModelInfoByModelID($data['test_model_id'])['type_id'],
  61. ];
  62. $result = (new Ccjc())->updateInfoByTaskID($task_id, $tmp_data);
  63. return $result;
  64. }
  65. //修改模型信息
  66. public function updateModelInfo($model_id,$model_info,$product='',$pesticides='')
  67. {
  68. //检测对象如果不为空则存在需要更新的信息,$product_update_Data
  69. $product_update_data = [];
  70. if ($product != '') {
  71. foreach ($product as $k => $value) {
  72. $product_update_data[$k] = [
  73. 'test_model_id' => $model_id,
  74. 'product_id' => $value['id'],
  75. 'product_name' => $value['name'],
  76. ];
  77. }
  78. //删除原有Product中model_id的内容
  79. (new TestProduct())->deleteProductInfoByTestModelID($model_id);
  80. //添加新的Product中model_id的内容
  81. (new TestProduct())->insertProductInfo($product_update_data);
  82. }
  83. //检测项目如果不为空则存在需要更新的信息,$product_update_Data
  84. $pesticides_update_data = [];
  85. if ($pesticides != '') {
  86. foreach ($pesticides as $k => $value) {
  87. $pesticides_update_data [$k]= [
  88. 'test_model_id' => $model_id,
  89. 'test_id' => $value['id'],
  90. 'test_name' => $value['name'],
  91. ];
  92. }
  93. //删除原有Pesticides中model_id的内容
  94. (new TestPesticides())->deletePesticidesInfoByTestModelID($model_id);
  95. //添加新的Pesticides中model_id的内容
  96. (new TestPesticides())->insertPesticidesInfo($pesticides_update_data);
  97. }
  98. //更新model_test表中的信息(model_name和type_id)
  99. (new TestModel())->updateModelInfoByModelID($model_id,$model_info);
  100. return true;
  101. }
  102. //新增模型
  103. public function createModelInfo($model_name,$type_id,$product_data,$pesticides_data)
  104. {
  105. $uid=$this->uid;
  106. $model_id = (new TestModel())->createModelInfo($model_name, $type_id, $uid);
  107. //格式化$product_data=》$product
  108. $product=[];
  109. //todo 需要重写foreach
  110. foreach ($product_data as $k=>$v){
  111. $j=[];
  112. $j['test_model_id']=$model_id;
  113. $j['product_id']=$v['id'];
  114. $j['product_name']=$v['name'];
  115. $product[]=$j;
  116. }
  117. //格式化$pesticides_data=》$pesticides
  118. $pesticides=[];
  119. foreach ($pesticides_data as $i=>$o){
  120. $p=[];
  121. $p['test_model_id']=$model_id;
  122. $p['test_name']=$o['name'];
  123. $p['test_id']=$o['id'];
  124. $pesticides[]=$p;
  125. }
  126. //todo 写到一个model里
  127. $update_test_product = (new TestProduct())->createProductInfo($product);
  128. if ($update_test_product == 0) {
  129. throw new ApiException(config('status.none_model_product'));
  130. }
  131. $update_test_pesticide =(new TestPesticides())->insertPesticidesInfo($pesticides);
  132. if ($update_test_pesticide == 0) {
  133. throw new ApiException(config('status.none_model_pesticides'));
  134. }
  135. return true;
  136. }
  137. //删除模型
  138. public function deleteModelInfo($data,$data_count)
  139. {
  140. for ($i = 0; $i < $data_count; $i++) {
  141. //从数据库获取当前模型的user_id //验证模型创建者是否一致
  142. $user_id = (new TestModel())->getUserIdByModelID($data[$i]['id'])['user_id'];
  143. //查询模型绑定任务,如果绑定则禁止删除
  144. $ccjc_info = (new Ccjc())->getTaskInfoByModelId($data[$i]['id']);
  145. if ( !($ccjc_info==[])) {
  146. throw new ApiException(config('status.err_disable_delete'));
  147. }
  148. if ($user_id != $this->uid) {
  149. throw new ApiException(config('status.none_authority'));
  150. }
  151. //删除test_model信息
  152. (new TestModel())->deleteModelInfoByID($data[$i]['id']);
  153. //删除test_product信息
  154. (new TestProduct())->deleteProductInfoByTestModelID($data[$i]['id']);
  155. //删除test_pesticides信息
  156. (new TestPesticides())->deletePesticidesInfoByTestModelID($data[$i]['id']);
  157. }
  158. return true;
  159. }
  160. //获取检测项目列表
  161. public function selectPesticidesList($pageNum,$pageSize,$key_word)
  162. {
  163. $data = (new Pesticides())->selectPesticidesList($key_word, $pageNum, $pageSize)->toArray();
  164. $count = (new Pesticides())->countPesticidesList($key_word);
  165. $result = [
  166. 'rows' => $data,
  167. 'total' => $count,
  168. ];
  169. return $result;
  170. }
  171. //获取检测对象列表
  172. public function selectProductList($type_id)
  173. {
  174. //todo cache缓存优化数据
  175. $field='id,parent_id,product_code,name,alias';
  176. $tmp_result = (new ProductClass())->selectProductListByTypeID($type_id,$field)->toArray();//字段调用的field过滤
  177. //todo 树状化需要看一下,回调可以优化
  178. $result = (new Arr())->Arr_tree($tmp_result,'0'.$type_id);
  179. return $result;
  180. }
  181. }