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