Index.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\model\Ry;
  4. use app\api\model\Ryjr as RyjrModel;
  5. use app\api\model\Subject;
  6. use app\api\model\Ry as RyModel;
  7. use OpenApi\Annotations as OA;
  8. use app\BaseController;
  9. use thans\jwt\facade\JWTAuth;
  10. use think\facade\Db;
  11. use OSS\Core\OssException;
  12. use OSS\OssClient;
  13. use think\facade\Request as re;
  14. use think\Request;
  15. use think\facade\Cache;
  16. use thans\jwt\JWT;
  17. use rsa\Rsa;
  18. use function Symfony\Component\VarDumper\Dumper\esc;
  19. class Index extends BaseController
  20. {
  21. use ResponseJson;
  22. protected $request;
  23. protected $middleware = [
  24. 'jwt' => ['except' => ['login']],
  25. // 'check' => ['except' => ['login']]
  26. ];
  27. public function __construct(Request $request)
  28. {
  29. $this->request = $request;
  30. }
  31. //asasdfasdf123
  32. public function login()
  33. {
  34. $data = $this->request->post();
  35. $res = Db::name("ry")->field('rymc,qydm,rybh,groupname')->where([['rybh', '=', $data['login_name']], ['kl', '=', $data['pwd']],['groupname','<>','企业']])->findOrEmpty();
  36. $res2 = Db::name("jcdw")->field('qydm,name,groupname,id')->where([['login_name', '=', $data['login_name']], ['pwd', '=', $data['pwd']]])->findOrEmpty();
  37. // $res['qymc'] = Db::name("qyBasic")->field('qymc')->where('qydm','=',$res['qydm'])->find();
  38. if(isset($res['qydm'])){
  39. $res['token'] = JWTAuth::builder([
  40. 'qydm' => $res['qydm'],
  41. 'groupname' => $res['groupname'],
  42. ]);
  43. $res['re_ip'] = $this->request->ip();
  44. Cache::store('redis')->set($res['re_ip'],$res['token'],1 * 24 * 3600);
  45. $res['message'] = '登录成功';
  46. return $this->jsonSuccessData($res);
  47. } else if(isset($res2['qydm'])){
  48. $res2['token'] = JWTAuth::builder([
  49. 'qydm' => $res2['qydm'],
  50. 'unit_id' => $res2['id'],
  51. 'groupname' => $res2['groupname'],
  52. ]);
  53. $res2['re_ip'] = $this->request->ip();
  54. Cache::store('redis')->set($res2['re_ip'],$res2['token'],1 * 24 * 3600);
  55. $res2['message'] = '登录成功';
  56. return $this->jsonSuccessData($res2);
  57. }else{
  58. return $this->jsonData(-1, "用户名或密码错误");
  59. }
  60. }
  61. /**
  62. * @return \think\response\Json
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * 获取任务接口:param: pageNum pageSize
  67. */
  68. //渲染任务列表(监管单位)
  69. public function getTaskList(){
  70. $data = $this->request->post();
  71. $pageNum=$data['pageNum'];
  72. $pageSize=$data['pageSize'];
  73. $map1=[];
  74. $map2=[];
  75. $map3=[];
  76. $map4=[];
  77. if(isset($data['year'])){
  78. $map1=[
  79. ['year','=',$data['year']],
  80. ['task_class','=',$data['task_class']]
  81. ];
  82. }
  83. if(isset($data['ispublic'])){
  84. $map2=[
  85. ['ispublic','=',$data['ispublic']],
  86. ['task_class','=',$data['task_class']]
  87. ];
  88. }
  89. if(isset($data['task_name'])){
  90. $map4=[
  91. ['task_name','LIKE','%'.$data['task_name'].'%'],
  92. ['task_class','=',$data['task_class']]
  93. ];
  94. }
  95. if(isset($data['starttime'])&&isset($data['endtime'])){
  96. $map3=[
  97. ['starttime','>=',$data['starttime']],
  98. ['endtime','<=',$data['endtime']],
  99. ['task_class','=',$data['task_class']]
  100. ];
  101. }
  102. $sumMap=[];
  103. if (count($map1)!=0){
  104. $sumMap[]=$map1;
  105. }
  106. if (count($map2)!=0){
  107. $sumMap[]=$map2;
  108. }
  109. if (count($map3)!=0){
  110. $sumMap[]=$map3;
  111. }if (count($map4)!=0){
  112. $sumMap[]=$map4;
  113. }else{
  114. $sumMap[]=[
  115. ['task_class','=',$data['task_class']]
  116. ];
  117. }
  118. if (count($sumMap)!=0){
  119. $res['rows'] = Db::name("ccjc")->where($sumMap)->page($pageNum,$pageSize)->select();
  120. $res['total'] = Db::name("ccjc")->where($sumMap)->count();
  121. }else{
  122. $res['rows'] = Db::name("ccjc")->page($pageNum,$pageSize)->select();
  123. $res['total'] = Db::name("ccjc")->count();
  124. }
  125. return $this->jsonSuccessData($res);
  126. }
  127. /**、
  128. * @return \think\response\Json
  129. * 添加任务
  130. */
  131. /***
  132. * todo 增加没写
  133. */
  134. //添加任务并关联牵头单位
  135. public function AddTask(){
  136. $data = $this->request->post();
  137. $resid=Db::name("ccjc")->strict(false)->insertGetId($data);
  138. for($i=0;$i<count($data['orgs']);$i++){
  139. $orgs[$i]['task_id']=$resid;
  140. $orgs[$i]['unit_id']=$data['orgs'][$i]['id'];
  141. $orgs[$i]['unit_name']=$data['orgs'][$i]['name'];
  142. }
  143. $res = Db::name('orgs')->strict(false)->insertAll($orgs);
  144. return $this->jsonSuccessData($res);
  145. }
  146. public function ossUpload(Request $request){
  147. $file = $request->file('file');
  148. if ($file == null) { //判断文件是否为空
  149. return $this ->jsonResponse(-1,'未上传文件');
  150. }
  151. $data= $request -> param();
  152. // 阿里云主账号AccessKey,在OSS控制台主页常用入口处有快速通道可以快速查看你的AccessKey ID($accessKeyId)以及Access Key Secret($accessKeySecret )
  153. $accessKeyId = 'LTAI4G1xGunUnwkgq48jFWi9';
  154. $accessKeySecret = '17PK5dY7WnLJR1ejgsopTUBUOzS05S';
  155. //Endpoint(地域节点)查看位置在文章下方
  156. $endpoint = 'http://oss-cn-hangzhou.aliyuncs.com';
  157. //创建Bucket填写的名称
  158. $bucket = 'syjcimage';
  159. //设置保存的路径(前面不要加'/',否则会出错)
  160. $path = 'govFile/'.time(). '/';
  161. //下面这三项应该都知道
  162. $name = $_FILES['file']['name'];
  163. $tmp_name = $_FILES['file']['tmp_name'];
  164. $error = $_FILES['file']['error'];
  165. //过滤错误(临时测试时使用,代码写的比较简陋)
  166. if ($error) {
  167. switch ($error) {
  168. case 1 :
  169. $error_message = '您上传的文件超过了PHP.INI配置文件中UPLOAD_MAX-FILESIZE的大小';
  170. break;
  171. case 2 :
  172. $error_message = '您上传的文件超过了PHP.INI配置文件中的post_max_size的大小';
  173. break;
  174. case 3 :
  175. $error_message = '文件只被部分上传';
  176. break;
  177. case 4 :
  178. $error_message = '文件不能为空';
  179. break;
  180. default :
  181. $error_message = '未知错误';
  182. }
  183. die($error_message);
  184. }
  185. //文件名生成
  186. $arr_name = explode('.', $name);
  187. //源文件的后缀
  188. $extension = end($arr_name);
  189. /**
  190. * 判断上传文件是否合法
  191. * 判断截取上传文件名是否为
  192. * jpeg,jpg,png其中之一
  193. */
  194. if (!in_array($extension, array("jpeg", "jpg", "png", "PNG", "JPG", "JPEG","pdf","PDF","XLS","xls","doc","DOC","docx","xlsx"))) {
  195. return $this->jsonData(-2, '上传文件不合法');
  196. }
  197. // try {
  198. // validate(['file' => ['fileSize:2*1024*1024']])->check(['file' => $file]);
  199. // } catch (ValidateException $e) {
  200. // $return['msg'] = $e->getMessage();
  201. //
  202. // }
  203. //新的文件名
  204. $new_name = $name;
  205. //文件在OSS上的URL
  206. $url=$path.$new_name;
  207. //抛出异常
  208. try{
  209. //相比传统的操作,其实就多了两个步骤
  210. //第一步是根据上面的参数实例化一个对象
  211. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  212. //这一步相当于之前把临时文件移入到设置的地址那步,只是以前的是自己的服务器上,现在是OSS;
  213. $result =$ossClient->uploadFile($bucket,$url,$tmp_name);
  214. //返回的参数
  215. if (isset($result['info']['http_code']) AND $result['info']['http_code']==200) {
  216. $arr=[
  217. 'fileName' => $new_name,
  218. 'fileUrl' => $result['info']['url']
  219. ];
  220. return $this->jsonSuccessData($arr);
  221. }else{
  222. return $this->jsonData(-1,'上传失败');
  223. }
  224. } catch(OssException $e) {
  225. $msg = $e->getMessage();
  226. return $this->jsonData(-1,$msg);
  227. }
  228. }
  229. /**
  230. * @return \think\response\Json
  231. * 删除任务 param:id
  232. */
  233. //删除任务
  234. public function delTask(){
  235. $data = $this->request->post();
  236. halt($data);
  237. $delarr = $data['delarr'];
  238. for($i=0;$i<count($delarr);$i++){
  239. $res=Db::name("ccjc")->where('id',$delarr[$i])->delete();
  240. }
  241. return $this->jsonSuccessData($res);
  242. }
  243. /**
  244. * @return \think\response\Json
  245. * @throws \think\db\exception\DbException
  246. * 发布和废止 param: id ispublic
  247. * 0:未发布 1:发布 2:废止
  248. */
  249. //更新任务发布状态
  250. public function isPublicTask(){
  251. $data = $this->request->post();
  252. $pubarr = $data['pubarr'];
  253. for($i=0;$i<count($pubarr);$i++) {
  254. $res = Db::name("ccjc")->where(['id'=>$pubarr[$i]['id']])->update(["ispublic" => $pubarr[$i]["ispublic"]]);
  255. }
  256. return $this->jsonSuccessData($res);
  257. }
  258. /**
  259. * 获取检测单位列表 param:pageNum pageSize
  260. */
  261. //获取检测单位列表
  262. public function getTestList(){
  263. $data = $this->request->post();
  264. $pageNum=$data['pageNum'];
  265. $pageSize=$data['pageSize'];
  266. $res['rows'] = Db::name("jcdw")->page($pageNum,$pageSize)->select();
  267. $res['total'] = Db::name("jcdw")->count();
  268. return $this->jsonSuccessData($res);
  269. }
  270. /**
  271. * 获取检测单位所属任务接口:param: pageNum pageSize
  272. */
  273. 获取牵头单位所属任务
  274. public function getTaskListTest(){
  275. $data = $this->request->post();
  276. $unit_id=$this->request->user['unit_id']->getValue();
  277. $pageNum=$data['pageNum'];
  278. $pageSize=$data['pageSize'];
  279. $map1=[];
  280. $map2=[];
  281. $map3=[];
  282. $map4=[];
  283. if(isset($data['year'])){
  284. $map1=[
  285. ['t_ccjc.year','=',$data['year']]
  286. ];
  287. }
  288. if(isset($data['ispublic'])){
  289. $map2=[
  290. ['t_ccjc.ispublic','=',$data['ispublic']]
  291. ];
  292. }
  293. if(isset($data['task_name'])){
  294. $map4=[
  295. ['t_ccjc.task_name','LIKE','%'.$data['task_name'].'%']
  296. ];
  297. }
  298. if(isset($data['starttime'])&&isset($data['endtime'])){
  299. $map3=[
  300. ['t_ccjc.starttime','>=',$data['starttime']],
  301. ['t_ccjc.endtime','<=',$data['endtime']]
  302. ];
  303. }
  304. $sumMap=[];
  305. if (count($map1)!=0){
  306. $sumMap[]=$map1;
  307. }
  308. if (count($map2)!=0){
  309. $sumMap[]=$map2;
  310. }
  311. if (count($map3)!=0){
  312. $sumMap[]=$map3;
  313. }if (count($map4)!=0){
  314. $sumMap[]=$map4;
  315. }
  316. $sumMap[]=['t_orgs.unit_id','=',$unit_id];
  317. $sumMap[]=['t_ccjc.task_class','=',$data['task_class']];
  318. $sumMap[]=['t_ccjc.ispublic','<>',0];
  319. // $sumMap[]=['t_ccjc.ispublic','<>',0];
  320. if ($map4||$map1||$map2||$map3){
  321. $res['rows'] =
  322. Db::name("orgs")
  323. ->Join('t_ccjc','t_orgs.task_id=t_ccjc.id')
  324. ->where($sumMap)
  325. ->page($pageNum,$pageSize)
  326. ->select();
  327. $res['total'] = Db::name("orgs")
  328. ->Join('t_ccjc','t_orgs.task_id=t_ccjc.id')
  329. ->where($sumMap)
  330. ->count();
  331. }else{
  332. $res['rows'] = Db::name("orgs")
  333. ->Join('t_ccjc','t_orgs.task_id=t_ccjc.id')
  334. ->where($sumMap)
  335. ->page($pageNum,$pageSize)
  336. ->select();
  337. $res['total'] = Db::name("orgs")->Join('t_ccjc','t_orgs.task_id=t_ccjc.id')->where($sumMap)->count();
  338. }
  339. return $this->jsonSuccessData($res);
  340. }
  341. public function getTaskcd(){
  342. $data = $this->request->post();
  343. $pageNum=$data['pageNum'];
  344. $pageSize=$data['pageSize'];
  345. $map1=[];
  346. $map2=[];
  347. $map3=[];
  348. $map4=[];
  349. if(isset($data['year'])){
  350. $map1=[
  351. ['year','=',$data['year']],
  352. ['task_class','=',$data['task_class']]
  353. ];
  354. }
  355. if(isset($data['ispublic'])){
  356. $map2=[
  357. ['ispublic','=',$data['ispublic']],
  358. ['task_class','=',$data['task_class']]
  359. ];
  360. }
  361. if(isset($data['task_name'])){
  362. $map4=[
  363. ['task_name','LIKE','%'.$data['task_name'].'%'],
  364. ['task_class','=',$data['task_class']]
  365. ];
  366. }
  367. if(isset($data['starttime'])&&isset($data['endtime'])){
  368. $map3=[
  369. ['starttime','>=',$data['starttime']],
  370. ['endtime','<=',$data['endtime']],
  371. ['task_class','=',$data['task_class']]
  372. ];
  373. }
  374. $sumMap=[];
  375. if (count($map1)!=0){
  376. $sumMap[]=$map1;
  377. }
  378. if (count($map2)!=0){
  379. $sumMap[]=$map2;
  380. }
  381. if (count($map3)!=0){
  382. $sumMap[]=$map3;
  383. }if (count($map4)!=0){
  384. $sumMap[]=$map4;
  385. }else{
  386. $sumMap[]=[
  387. ['task_class','=',$data['task_class']],
  388. ];
  389. }
  390. $unit_id=$this->request->user['unit_id']->getValue();
  391. $sumMap[]=['t_undertake.bear_id','=',$unit_id];
  392. $sumMap[]=['t_ccjc.ispublic','<>',0];
  393. $sumMap[]=['t_ccjc.ispublic','<>',1];
  394. if (count($sumMap)!=0){
  395. $res['rows'] = Db::name("undertake")
  396. ->Join('t_ccjc','t_undertake.task_id=t_ccjc.id')
  397. ->where($sumMap)
  398. ->select();
  399. // $res['rows'] = Db::name("ccjc")->where($sumMap)->page($pageNum,$pageSize)->select();
  400. $res['total'] = Db::name("undertake")
  401. ->Join('t_ccjc','t_undertake.task_id=t_ccjc.id')
  402. ->where($sumMap)
  403. ->count();
  404. // $res['total'] = Db::name("ccjc")->where($sumMap)->count();
  405. }else{
  406. $res['rows'] = Db::name("undertake")
  407. ->Join('t_ccjc','t_undertake.task_id=t_ccjc.id')
  408. ->where($sumMap)
  409. ->page($pageNum,$pageSize)
  410. ->select();
  411. $res['total'] = Db::name("undertake")
  412. ->Join('t_ccjc','t_undertake.task_id=t_ccjc.id')
  413. ->where($sumMap)
  414. ->count();
  415. }
  416. return $this->jsonSuccessData($res);
  417. }
  418. /**
  419. * @return \think\response\Json
  420. * @throws \think\db\exception\DataNotFoundException
  421. * @throws \think\db\exception\DbException
  422. * @throws \think\db\exception\ModelNotFoundException
  423. */
  424. //1.在任务里添加承担单位 2.绑定关系undertake
  425. public function AddUnderTask(){
  426. $data=$this->request->post();
  427. $data['userid']=$this->request->user['unit_id']->getValue();
  428. $bear_name = Db::name('jcdw')->field('name')->where('id',$data['bear_id'])->find();
  429. // dump($bear_name);die;
  430. $report_name = Db::name('jcdw')->field('name')->where('id',$data['report_id'])->find();
  431. $check_name = Db::name('jcdw')->field('name')->where('id',$data['check_id'])->find();
  432. $map =[
  433. 'bear_name' => $bear_name['name'],
  434. 'report_name' => $report_name['name'],
  435. 'sample_number' => $data['sample_number'],
  436. 'address' => $data['address'],
  437. 'uploadtime' => $data['uploadtime'],
  438. 'task_id'=> $data['task_id'],
  439. 'unit_id'=> $data['userid'],
  440. 'bear_id'=> $data['bear_id'],
  441. 'report_id' => $data['report_id'],
  442. 'check_id' => $data['check_id'],
  443. 'check_name' => $check_name['name']
  444. ];
  445. $res = Db::name('undertake')->strict(false)->insert($map);
  446. return $this->jsonSuccessData($res);
  447. }
  448. //牵头单位发布任务
  449. public function qtRelease(){
  450. $data=$this->request->post();
  451. $res = Db::name('ccjc')->where('id',$data['task_id'])->update(['ispublic' => 5]);
  452. return $this->jsonSuccessData($res);
  453. }
  454. //渲染所属牵头单位下承担单位的信息
  455. public function getUnderTask(){
  456. $data=$this->request->post();
  457. $data['userid']=$this->request->user['unit_id']->getValue();
  458. $map = [['task_id','=',$data['task_id']],['unit_id','=',$data['userid']]];
  459. $res['rows'] = Db::name('undertake')->where($map)->select();
  460. $res['total'] = Db::name('undertake')->where($map)->count();
  461. return $this->jsonSuccessData($res);
  462. }
  463. //获取模型列表
  464. public function getModelList(){
  465. $data = $this->request->post();
  466. $data['userid']=$this->request->user['unit_id']->getValue();
  467. $pageNum=$data['pageNum'];
  468. $pageSize=$data['pageSize'];
  469. if($data['type_id']!=''){
  470. $res['rows'] = Db::name('test_model')
  471. ->field('t_test_model.id,t_test_model.name,t_test_model.type_id,t_test_model.creattime,t_test_model.updatetime')
  472. ->where([['t_test_model.userid','=',$data['userid']],['t_test_model.type_id','=',$data['type_id']]])
  473. ->leftJoin('t_test_product','t_test_model.id=t_test_product.test_model_id')
  474. ->leftjoin('t_test_pesticides','t_test_model.id=t_test_pesticides.test_model_id')
  475. ->distinct(true)
  476. ->page($pageNum,$pageSize)
  477. ->select();
  478. $res['total'] = count(Db::name('test_model')->field('t_test_model.id,t_test_model.name,t_test_model.type_id')->where([['t_test_model.userid','=',$data['userid']],['t_test_model.type_id','=',$data['type_id']]])->leftJoin('t_test_product','t_test_model.id=t_test_product.test_model_id')->leftjoin('t_test_pesticides','t_test_model.id=t_test_pesticides.test_model_id')->distinct(true)->select());
  479. }else {
  480. $res['rows'] = Db::name('test_model')
  481. ->field('t_test_model.id,t_test_model.name,t_test_model.type_id,t_test_model.creattime,t_test_model.updatetime')
  482. ->where('t_test_model.userid', $data['userid'])
  483. ->leftJoin('t_test_product', 't_test_model.id=t_test_product.test_model_id')
  484. ->leftjoin('t_test_pesticides', 't_test_model.id=t_test_pesticides.test_model_id')
  485. ->distinct(true)->page($pageNum, $pageSize)
  486. ->select();
  487. $res['total'] = count(Db::name('test_model')->field('t_test_model.id,t_test_model.name,t_test_model.type_id')->where('t_test_model.userid', $data['userid'])->leftJoin('t_test_product', 't_test_model.id=t_test_product.test_model_id')->leftjoin('t_test_pesticides', 't_test_model.id=t_test_pesticides.test_model_id')->distinct(true)->select());
  488. }
  489. return $this->jsonSuccessData($res);
  490. }
  491. //查看模型详情
  492. public function getModeldetail(){
  493. $data = $this->request->post();
  494. $res['name'] = Db::name('test_model')->where([
  495. ['id','=',$data['id']]
  496. ])->field('id,name,type_id')
  497. ->find();
  498. $res['pesticides'] = Db::name('test_pesticides')->where([
  499. ['test_model_id','=',$data['id']]
  500. ])->field('test_name as name,test_id as id')
  501. ->select();
  502. $res['product'] = Db::name('test_product')->where([
  503. ['test_model_id','=',$data['id']]
  504. ])->field('product_name as name,product_id as id')
  505. ->select();
  506. return $this->jsonSuccessData($res);
  507. }
  508. /**
  509. *
  510. */
  511. public function test()
  512. {
  513. $data = $this->request->post();
  514. $map = [['type_id','=',$data['type_id']],['parent_id','=','0'.$data['type_id']]];
  515. $res=Db::name('product_class')->field('id,parent_id,product_code,name,alias')->where($map)->select()->toArray();
  516. for ($i=0;$i<count($res);$i++){
  517. $res[$i]['children']=Db::name('product_class')->field('id,parent_id,product_code,name,alias')->distinct(true)->where('parent_id','=',$res[$i]['product_code'])->select()->toArray();
  518. for($j=0;$j<count($res[$i]['children']);$j++){
  519. $res[$i]['children'][$j]['children']=Db::name('product_class')->field('id,parent_id,product_code,name,alias')->distinct(true)->where('parent_id','=',$res[$i]['children'][$j]['product_code'])->select()->toArray();
  520. }
  521. }
  522. return $this->jsonSuccessData($res);
  523. }
  524. function tree($res,$parent_id){
  525. $arr=[];
  526. foreach ($res as $k=> $v){
  527. if($v['parent_id']==$parent_id){
  528. unset($res[$k]);
  529. $v['children']=$this->tree($res, $v['product_code']);
  530. $arr[]=$v;
  531. }
  532. }
  533. return $arr;
  534. }
  535. public function getProductList(){
  536. $data = $this->request->post();
  537. $map = [['type_id','=',$data['type_id']]];
  538. $res=Db::name('product_class')->field('id,parent_id,product_code,name,alias')->where($map)->select()->toArray();
  539. $arr_res = $this->tree($res,'0'.$data['type_id']);
  540. return $this->jsonSuccessData($arr_res);
  541. }
  542. public function getPesticidesList(){
  543. $data = $this->request->post();
  544. $pageNum=$data['pageNum'];
  545. $pageSize=$data['pageSize'];
  546. if(isset($data['keyword'])){
  547. $res['rows'] = Db::name("pesticides")->where('name','like','%'.$data['keyword'].'%')->page($pageNum,$pageSize)->select();
  548. $res['total'] = Db::name("pesticides")->where('name','like','%'.$data['keyword'].'%')->count();
  549. }else{
  550. $res['rows'] = Db::name("pesticides")->page($pageNum,$pageSize)->select();
  551. $res['total'] = Db::name("pesticides")->count();
  552. }
  553. return $this->jsonSuccessData($res);
  554. }
  555. /**
  556. *
  557. * 农药残留表格
  558. */
  559. // 新增模型
  560. public function saveModel(){
  561. $data=$this->request->post();
  562. $data['userid']=$this->request->user['unit_id']->getValue();
  563. $res['model']=Db::name('test_model')->strict(false)->insertGetId($data);
  564. $product=[];
  565. foreach ($data['product'] as $k=>$v){
  566. $j=[];
  567. $j['test_model_id']=$res['model'];
  568. $j['product_id']=$v['id'];
  569. $j['product_name']=$v['name'];
  570. $product[]=$j;
  571. }
  572. $pesticides=[];
  573. foreach ($data['pesticides'] as $i=>$o){
  574. $p=[];
  575. $p['test_model_id']=$res['model'];
  576. $p['test_name']=$o['name'];
  577. $p['test_id']=$o['id'];
  578. $pesticides[]=$p;
  579. }
  580. $res['product']=Db::name('test_product')->strict(false)->insertAll($product);
  581. $res['pesticides']=Db::name('test_pesticides')->strict(false)->insertAll($pesticides);
  582. return $this->jsonSuccessData($res);
  583. }
  584. //修改模型
  585. public function DoEditModelInfo(){
  586. $data = $this->request->post();
  587. $condition = ['id'=>$data['id']];
  588. Db::name('test_model')
  589. ->where($condition)
  590. ->data(['name' => $data['name'],'type_id' =>$data['type_id']])
  591. ->update();
  592. $res['id']=$data['id'];
  593. $res['type_id']=$data['type_id'];
  594. Db::name('test_product')
  595. ->where("test_model_id",$data['id'])
  596. ->delete();
  597. Db::name('test_pesticides')
  598. ->where("test_model_id",$data['id'])
  599. ->delete();
  600. $product=[];
  601. foreach ($data['product'] as $k=>$v){
  602. $j=[];
  603. $j['test_model_id']=$res['id'];
  604. $j['product_id']=$v['id'];
  605. $j['product_name']=$v['name'];
  606. $product[]=$j;
  607. }
  608. $pesticides=[];
  609. foreach ($data['pesticides'] as $i=>$o){
  610. $p=[];
  611. $p['test_model_id']=$res['id'];
  612. $p['test_name']=$o['name'];
  613. $p['test_id']=$o['id'];
  614. $pesticides[]=$p;
  615. }
  616. $res['product']=Db::name('test_product')->strict(false)->insertAll($product);
  617. $res['pesticides']=Db::name('test_pesticides')->strict(false)->insertAll($pesticides);
  618. return $this->jsonSuccessData($res);
  619. }
  620. // 删除模型
  621. public function delModel(){
  622. $data = $this->request->post();
  623. $delarr = $data['delarr'];
  624. $id = '';
  625. for($j = 0; $j<count($delarr) ; $j++){
  626. if ($j == 0){
  627. $id = $delarr[$j]['id'];
  628. }else{
  629. $id = $delarr[$j]['id'].",".$id;
  630. }
  631. }
  632. $map=[];
  633. $map=[['test_model_id','in',$id]];
  634. $map1 = [['id','in',$id]];
  635. $result1 = Db::name("test_product")->where($map)->delete();
  636. $result2 = Db::name("test_pesticides")->where($map)->delete();
  637. $result3 = Db::name("test_model")->where($map1)->delete();
  638. if($result1 && $result2 && $result3){
  639. return $this->jsonData(0,"信息删除成功");
  640. }else{
  641. return $this->jsonData(-1,"信息删除失败");
  642. }
  643. return $this->jsonSuccessData($id);
  644. }
  645. public function bindModel(){
  646. $data = $this->request->post();
  647. $res = Db::name('ccjc')->where('id',$data['id'])->update(['is_divide'=>$data['is_divide'],'test_model_id'=>$data['test_model_id'],'endtime_cydsb'=>$data['endtime_cydsb']]);
  648. return $this->jsonSuccessData($res);
  649. }
  650. public function getTestObj(){
  651. $data=$this->request->post();
  652. $model = Db::name('ccjc')->where('id',$data['id'])->field('test_model_id')->find();
  653. $res = Db::name('test_model')->where('t_test_model.id',$model['test_model_id'])->join('t_test_product','t_test_model.id=t_test_product.test_model_id')->field('t_test_product.product_id,t_test_product.product_name')->select();
  654. return $this->jsonSuccessData($res);
  655. }
  656. public function saveSample(){
  657. $data=$this->request->post();
  658. $data['units_id']=Db::name('units_test')->strict(false)->insertGetId($data['units']);
  659. $data['product_unit']=Db::name('production')->strict(false)->insertGetId($data['production']);
  660. $data['sample_unit']=$this->request->user['unit_id']->getValue();
  661. $res=Db::name('cyd')->strict(false)->insert($data);
  662. return $this->jsonSuccessData($res);
  663. }
  664. public function getSampleList(){
  665. $data = $this->request->post();
  666. $pageNum=$data['pageNum'];
  667. $pageSize=$data['pageSize'];
  668. $data['sample_unit']=$this->request->user['unit_id']->getValue();
  669. $map=[['t_cyd.sample_unit','=',$data['sample_unit']],['t_cyd.task_id','=',$data['task_id']]];
  670. $res['rows'] = Db::name('cyd')->where($map)->field('t_cyd.id,t_cyd.sample_name,t_cyd.sample_id,t_cyd.sample_date,t_production.production_name,t_cyd.place,t_cyd.status,t_units_test.units_name,t_cyd.sample_unit_name,t_cyd.test_result,t_cyd.test_status,t_cyd.jc_value_id')->leftJoin('t_production','t_cyd.product_unit=t_production.id')->leftJoin('t_units_test','t_cyd.units_id=t_units_test.id')->page($pageNum,$pageSize)->select();
  671. $res['total'] = Db::name('cyd')->where($map)->leftJoin('t_production','t_cyd.product_unit=t_production.id')->leftJoin('t_units_test','t_cyd.units_id=t_units_test.id')->count();
  672. return $this->jsonSuccessData($res);
  673. }
  674. public function getSampleId(){
  675. $data=$this->request->post();
  676. $task = Db::name('ccjc')->where('id',$data['id'])->field('task_class')->find();
  677. $count = Db::name('cyd')->whereDay('createtime','today')->count();
  678. if($task['task_class']=="例行监测"){
  679. $id['id'] = 'LXJC'.date('Y').date('m').date('d').date('H').date('m').str_pad(($count+1),4,0,STR_PAD_LEFT);
  680. }else{
  681. $id['id'] = 'ZXJC'.date('Y').date('m').date('d').date('H').date('m').str_pad(($count+1),4,0,STR_PAD_LEFT);
  682. }
  683. return $this->jsonSuccessData($id);
  684. }
  685. public function setSampleStatus(){
  686. $data = $this->request->post();
  687. foreach ($data as $k =>$v) {
  688. $res = Db::name('cyd')->where('sample_id', $v['sample_id'])->update(['status' => 1]);
  689. }
  690. return $this->jsonSuccessData($res);
  691. }
  692. public function getTaskDetail(){
  693. $data=$this->request->post();
  694. $data['userid']=$this->request->user['unit_id']->getValue();
  695. $res = Db::name('ccjc')->where('id',$data['task_id'])->find();
  696. $res['releaserInfo'] = Db::name('jgry')->where('rymc',$res['releaser'])->field('groupname,rymc')->find();
  697. $res['product']=Db::name('test_model')->field('t_test_product.product_name,t_test_model.type_id')
  698. ->leftJoin('t_test_product','t_test_model.id=t_test_product.test_model_id')
  699. ->where('t_test_model.id',$res['test_model_id'])->select();
  700. $res['pesticides']=Db::name('test_model')->field('t_test_pesticides.test_name,t_test_model.type_id')
  701. ->leftJoin('t_test_pesticides','t_test_model.id=t_test_pesticides.test_model_id')
  702. ->where('t_test_model.id',$res['test_model_id'])->select();
  703. $res['name']=Db::name('undertake')->where([['task_id','=',$data['task_id']],['unit_id','=',$data['userid']]])->select();
  704. return $this->jsonSuccessData($res);
  705. }
  706. public function delSample(){
  707. $data = $this->request->post();
  708. foreach ($data as $k =>$v){
  709. $unit = Db::name('cyd')->where('sample_id',$v['sample_id'])->find();
  710. $res1=Db::name('units_test')->where('id',$unit['units_id'])->delete();
  711. $res2=Db::name('production')->where('id',$unit['product_unit'])->delete();
  712. $res = Db::name('cyd')->where('sample_id',$v['sample_id'])->delete();
  713. }
  714. return $this->jsonSuccessData($res);
  715. }
  716. public function getSampleDetail(){
  717. $data=$this->request->post();
  718. $map=[['t_cyd.sample_id','=',$data['sample_id']]];
  719. $res = Db::name('cyd')->where($map)->Join('t_production','t_cyd.product_unit=t_production.id')->leftJoin('t_units_test','t_cyd.units_id=t_units_test.id')->find();
  720. return $this->jsonSuccessData($res);
  721. }
  722. public function getTestPesticides(){
  723. $data=$this->request->post();
  724. $sample = Db::name('cyd')->where('sample_id',$data['sample_id'])->field('task_id')->find();
  725. $model = Db::name('ccjc')->where('id',$sample['task_id'])->field('test_model_id')->find();
  726. $test_model = Db::name('test_pesticides')->where('test_model_id',$model['test_model_id'])->field('test_name,test_id,test_model_id')->select();
  727. return $this->jsonSuccessData($test_model);
  728. }
  729. public function saveSamplePesticides(){
  730. $data=$this->request->post();
  731. $id = '';
  732. $test_result=1;
  733. $islog = Db::name('sample_pesticides')->where('sample_id',$data['sample_id'])->select();
  734. if (count($islog)!=0){
  735. return $this->jsonResponse(-1,'不要重复提交检测信息',$islog);
  736. }
  737. foreach ($data['pesticides'] as $k=>$v){
  738. $v['sample_id']=$data['sample_id'];
  739. $idv=Db::name('sample_pesticides')->insertGetId($v);
  740. $id.=$idv.',';
  741. if ($v['test_result']=='不合格'){
  742. $test_result=0;
  743. }
  744. }
  745. $res=Db::name('cyd')->where('sample_id',$data['sample_id'])->update(['jc_value_id'=>$id,'test_result'=>$test_result]);
  746. return $this->jsonSuccessData($res);
  747. }
  748. public function editSamplePesticides(){
  749. $data=$this->request->post();
  750. foreach ($data as $k=>$v){
  751. $id=Db::name('sample_pesticides')->where('id',$v['id'])->update($v);
  752. }
  753. return $this->jsonSuccessData($id);
  754. }
  755. public function getTestValue(){
  756. $data=$this->request->post();
  757. $res['result_detail']=Db::name('sample_pesticides')->where('sample_id',$data['sample_id'])->select();
  758. $res['result']=Db::name('cyd')->where('sample_id',$data['sample_id'])->find();
  759. return $this->jsonSuccessData($res);
  760. }
  761. public function saveTestFile(){
  762. $data=$this->request->post();
  763. $data['unit_id']=$this->request->user['unit_id']->getValue();
  764. $task = Db::name('cyd')->where('sample_id')->find();
  765. $data['task_id']=$task['task_id'];
  766. $res = Db::name('jcbg')->strict(false)->insertGetId($data);
  767. return $this->jsonSuccessData($res);
  768. }
  769. public function getUnitsFileList(){
  770. $data = $this->request->post();
  771. $pageNum=$data['pageNum'];
  772. $pageSize=$data['pageSize'];
  773. $data['unit_id']=$this->request->user['unit_id']->getValue();
  774. $res['rows'] = Db::name('jcbg')->where('unit_id',$data['unit_id'])->page($pageNum,$pageSize)->select();
  775. $res['total'] = Db::name('jcbg')->where('unit_id',$data['unit_id'])->count();
  776. return $this->jsonSuccessData($res);
  777. }
  778. public function getGovFileList(){
  779. $data = $this->request->post();
  780. $pageNum=$data['pageNum'];
  781. $pageSize=$data['pageSize'];
  782. // $data['unit_id']=$this->request->user['unit_id']->getValue();
  783. $res['rows'] = Db::name('jcbg')->field('t_jcbg.file_name,t_jcbg.file_url,t_ccjc.task_name,t_ccjc.year,t_cyd.sample_name,t_cyd.test_result')->join('t_ccjc','t_jcbg.task_id=t_ccjc.id')->join('t_cyd','t_jcbg.sample_id=t_cyd.id')->page($pageNum,$pageSize)->select();
  784. $res['total'] = Db::name('jcbg')->count();
  785. return $this->jsonSuccessData($res);
  786. }
  787. }