Index.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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")->Join('t_ccjc','t_undertake.task_id=t_ccjc.id')->where($sumMap)->count();
  400. // $res['total'] = Db::name("ccjc")->where($sumMap)->count();
  401. }else{
  402. $res['rows'] = Db::name("undertake")->Join('t_ccjc','t_undertake.task_id=t_ccjc.id')->where($sumMap)->page($pageNum,$pageSize)->select();
  403. $res['total'] = Db::name("undertake")->Join('t_ccjc','t_undertake.task_id=t_ccjc.id')->where($sumMap)->count();
  404. }
  405. return $this->jsonSuccessData($res);
  406. }
  407. /**
  408. * @return \think\response\Json
  409. * @throws \think\db\exception\DataNotFoundException
  410. * @throws \think\db\exception\DbException
  411. * @throws \think\db\exception\ModelNotFoundException
  412. */
  413. //1.在任务里添加承担单位 2.绑定关系undertake
  414. public function AddUnderTask(){
  415. $data=$this->request->post();
  416. $data['userid']=$this->request->user['unit_id']->getValue();
  417. $bear_name = Db::name('jcdw')->field('name')->where('id',$data['bear_id'])->find();
  418. // dump($bear_name);die;
  419. $report_name = Db::name('jcdw')->field('name')->where('id',$data['report_id'])->find();
  420. $check_name = Db::name('jcdw')->field('name')->where('id',$data['check_id'])->find();
  421. $map =[
  422. 'bear_name' => $bear_name['name'],
  423. 'report_name' => $report_name['name'],
  424. 'sample_number' => $data['sample_number'],
  425. 'address' => $data['address'],
  426. 'uploadtime' => $data['uploadtime'],
  427. 'task_id'=> $data['task_id'],
  428. 'unit_id'=> $data['userid'],
  429. 'bear_id'=> $data['bear_id'],
  430. 'report_id' => $data['report_id'],
  431. 'check_id' => $data['check_id'],
  432. 'check_name' => $check_name['name']
  433. ];
  434. $res = Db::name('undertake')->strict(false)->insert($map);
  435. return $this->jsonSuccessData($res);
  436. }
  437. //牵头单位发布任务
  438. public function qtRelease(){
  439. $data=$this->request->post();
  440. $res = Db::name('ccjc')->where('id',$data['task_id'])->update(['ispublic' => 5]);
  441. return $this->jsonSuccessData($res);
  442. }
  443. //渲染所属牵头单位下承担单位的信息
  444. public function getUnderTask(){
  445. $data=$this->request->post();
  446. $data['userid']=$this->request->user['unit_id']->getValue();
  447. $map = [['task_id','=',$data['task_id']],['unit_id','=',$data['userid']]];
  448. $res['rows'] = Db::name('undertake')->where($map)->select();
  449. $res['total'] = Db::name('undertake')->where($map)->count();
  450. return $this->jsonSuccessData($res);
  451. }
  452. //获取模型列表
  453. public function getModelList(){
  454. $data = $this->request->post();
  455. $data['userid']=$this->request->user['unit_id']->getValue();
  456. $pageNum=$data['pageNum'];
  457. $pageSize=$data['pageSize'];
  458. if($data['type_id']!=''){
  459. $res['rows'] = Db::name('test_model')
  460. ->field('t_test_model.id,t_test_model.name,t_test_model.type_id,t_test_model.creattime,t_test_model.updatetime')
  461. ->where([['t_test_model.userid','=',$data['userid']],['t_test_model.type_id','=',$data['type_id']]])
  462. ->leftJoin('t_test_product','t_test_model.id=t_test_product.test_model_id')
  463. ->leftjoin('t_test_pesticides','t_test_model.id=t_test_pesticides.test_model_id')
  464. ->distinct(true)
  465. ->page($pageNum,$pageSize)
  466. ->select();
  467. $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());
  468. }else {
  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'])
  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)->page($pageNum, $pageSize)
  475. ->select();
  476. $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());
  477. }
  478. return $this->jsonSuccessData($res);
  479. }
  480. //查看模型详情
  481. public function getModeldetail(){
  482. $data = $this->request->post();
  483. $res['name'] = Db::name('test_model')->where([
  484. ['id','=',$data['id']]
  485. ])->field('id,name,type_id')
  486. ->find();
  487. $res['pesticides'] = Db::name('test_pesticides')->where([
  488. ['test_model_id','=',$data['id']]
  489. ])->field('test_name as name,test_id as id')
  490. ->select();
  491. $res['product'] = Db::name('test_product')->where([
  492. ['test_model_id','=',$data['id']]
  493. ])->field('product_name as name,product_id as id')
  494. ->select();
  495. return $this->jsonSuccessData($res);
  496. }
  497. /**
  498. *
  499. */
  500. public function test()
  501. {
  502. $data = $this->request->post();
  503. $map = [['type_id','=',$data['type_id']],['parent_id','=','0'.$data['type_id']]];
  504. $res=Db::name('product_class')->field('id,parent_id,product_code,name,alias')->where($map)->select()->toArray();
  505. for ($i=0;$i<count($res);$i++){
  506. $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();
  507. for($j=0;$j<count($res[$i]['children']);$j++){
  508. $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();
  509. }
  510. }
  511. return $this->jsonSuccessData($res);
  512. }
  513. function tree($res,$parent_id){
  514. $arr=[];
  515. foreach ($res as $k=> $v){
  516. if($v['parent_id']==$parent_id){
  517. unset($res[$k]);
  518. $v['children']=$this->tree($res, $v['product_code']);
  519. $arr[]=$v;
  520. }
  521. }
  522. return $arr;
  523. }
  524. public function getProductList(){
  525. $data = $this->request->post();
  526. $map = [['type_id','=',$data['type_id']]];
  527. $res=Db::name('product_class')->field('id,parent_id,product_code,name,alias')->where($map)->select()->toArray();
  528. $arr_res = $this->tree($res,'0'.$data['type_id']);
  529. return $this->jsonSuccessData($arr_res);
  530. }
  531. public function getPesticidesList(){
  532. $data = $this->request->post();
  533. $pageNum=$data['pageNum'];
  534. $pageSize=$data['pageSize'];
  535. if(isset($data['keyword'])){
  536. $res['rows'] = Db::name("pesticides")->where('name','like','%'.$data['keyword'].'%')->page($pageNum,$pageSize)->select();
  537. $res['total'] = Db::name("pesticides")->where('name','like','%'.$data['keyword'].'%')->count();
  538. }else{
  539. $res['rows'] = Db::name("pesticides")->page($pageNum,$pageSize)->select();
  540. $res['total'] = Db::name("pesticides")->count();
  541. }
  542. return $this->jsonSuccessData($res);
  543. }
  544. /**
  545. *
  546. * 农药残留表格
  547. */
  548. // 新增模型
  549. public function saveModel(){
  550. $data=$this->request->post();
  551. $data['userid']=$this->request->user['unit_id']->getValue();
  552. $res['model']=Db::name('test_model')->strict(false)->insertGetId($data);
  553. $product=[];
  554. foreach ($data['product'] as $k=>$v){
  555. $j=[];
  556. $j['test_model_id']=$res['model'];
  557. $j['product_id']=$v['id'];
  558. $j['product_name']=$v['name'];
  559. $product[]=$j;
  560. }
  561. $pesticides=[];
  562. foreach ($data['pesticides'] as $i=>$o){
  563. $p=[];
  564. $p['test_model_id']=$res['model'];
  565. $p['test_name']=$o['name'];
  566. $p['test_id']=$o['id'];
  567. $pesticides[]=$p;
  568. }
  569. $res['product']=Db::name('test_product')->strict(false)->insertAll($product);
  570. $res['pesticides']=Db::name('test_pesticides')->strict(false)->insertAll($pesticides);
  571. return $this->jsonSuccessData($res);
  572. }
  573. //修改模型
  574. public function DoEditModelInfo(){
  575. $data = $this->request->post();
  576. $condition = ['id'=>$data['id']];
  577. Db::name('test_model')
  578. ->where($condition)
  579. ->data(['name' => $data['name'],'type_id' =>$data['type_id']])
  580. ->update();
  581. $res['id']=$data['id'];
  582. $res['type_id']=$data['type_id'];
  583. Db::name('test_product')
  584. ->where("test_model_id",$data['id'])
  585. ->delete();
  586. Db::name('test_pesticides')
  587. ->where("test_model_id",$data['id'])
  588. ->delete();
  589. $product=[];
  590. foreach ($data['product'] as $k=>$v){
  591. $j=[];
  592. $j['test_model_id']=$res['id'];
  593. $j['product_id']=$v['id'];
  594. $j['product_name']=$v['name'];
  595. $product[]=$j;
  596. }
  597. $pesticides=[];
  598. foreach ($data['pesticides'] as $i=>$o){
  599. $p=[];
  600. $p['test_model_id']=$res['id'];
  601. $p['test_name']=$o['name'];
  602. $p['test_id']=$o['id'];
  603. $pesticides[]=$p;
  604. }
  605. $res['product']=Db::name('test_product')->strict(false)->insertAll($product);
  606. $res['pesticides']=Db::name('test_pesticides')->strict(false)->insertAll($pesticides);
  607. return $this->jsonSuccessData($res);
  608. }
  609. // 删除模型
  610. public function delModel(){
  611. $data = $this->request->post();
  612. $delarr = $data['delarr'];
  613. $id = '';
  614. for($j = 0; $j<count($delarr) ; $j++){
  615. if ($j == 0){
  616. $id = $delarr[$j]['id'];
  617. }else{
  618. $id = $delarr[$j]['id'].",".$id;
  619. }
  620. }
  621. $map=[];
  622. $map=[['test_model_id','in',$id]];
  623. $map1 = [['id','in',$id]];
  624. $result1 = Db::name("test_product")->where($map)->delete();
  625. $result2 = Db::name("test_pesticides")->where($map)->delete();
  626. $result3 = Db::name("test_model")->where($map1)->delete();
  627. if($result1 && $result2 && $result3){
  628. return $this->jsonData(0,"信息删除成功");
  629. }else{
  630. return $this->jsonData(-1,"信息删除失败");
  631. }
  632. return $this->jsonSuccessData($id);
  633. }
  634. public function bindModel(){
  635. $data = $this->request->post();
  636. $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']]);
  637. return $this->jsonSuccessData($res);
  638. }
  639. public function getTestObj(){
  640. $data=$this->request->post();
  641. $model = Db::name('ccjc')->where('id',$data['id'])->field('test_model_id')->find();
  642. $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();
  643. return $this->jsonSuccessData($res);
  644. }
  645. public function saveSample(){
  646. $data=$this->request->post();
  647. $data['units_id']=Db::name('units_test')->strict(false)->insertGetId($data['units']);
  648. $data['product_unit']=Db::name('production')->strict(false)->insertGetId($data['production']);
  649. $data['sample_unit']=$this->request->user['unit_id']->getValue();
  650. $res=Db::name('cyd')->strict(false)->insert($data);
  651. return $this->jsonSuccessData($res);
  652. }
  653. public function getSampleList(){
  654. $data = $this->request->post();
  655. $pageNum=$data['pageNum'];
  656. $pageSize=$data['pageSize'];
  657. $data['sample_unit']=$this->request->user['unit_id']->getValue();
  658. $map=[['t_cyd.sample_unit','=',$data['sample_unit']],['t_cyd.task_id','=',$data['task_id']]];
  659. $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();
  660. $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();
  661. return $this->jsonSuccessData($res);
  662. }
  663. public function getSampleId(){
  664. $data=$this->request->post();
  665. $task = Db::name('ccjc')->where('id',$data['id'])->field('task_class')->find();
  666. $count = Db::name('cyd')->whereDay('createtime','today')->count();
  667. if($task['task_class']=="例行监测"){
  668. $id['id'] = 'LXJC'.date('Y').date('m').date('d').date('H').date('m').str_pad(($count+1),4,0,STR_PAD_LEFT);
  669. }else{
  670. $id['id'] = 'ZXJC'.date('Y').date('m').date('d').date('H').date('m').str_pad(($count+1),4,0,STR_PAD_LEFT);
  671. }
  672. return $this->jsonSuccessData($id);
  673. }
  674. public function setSampleStatus(){
  675. $data = $this->request->post();
  676. foreach ($data as $k =>$v) {
  677. $res = Db::name('cyd')->where('sample_id', $v['sample_id'])->update(['status' => 1]);
  678. }
  679. return $this->jsonSuccessData($res);
  680. }
  681. public function getTaskDetail(){
  682. $data=$this->request->post();
  683. $data['userid']=$this->request->user['unit_id']->getValue();
  684. $res = Db::name('ccjc')->where('id',$data['task_id'])->find();
  685. $res['releaserInfo'] = Db::name('jgry')->where('rymc',$res['releaser'])->field('groupname,rymc')->find();
  686. $res['product']=Db::name('test_model')->field('t_test_product.product_name,t_test_model.type_id')
  687. ->leftJoin('t_test_product','t_test_model.id=t_test_product.test_model_id')
  688. ->where('t_test_model.id',$res['test_model_id'])->select();
  689. $res['pesticides']=Db::name('test_model')->field('t_test_pesticides.test_name,t_test_model.type_id')
  690. ->leftJoin('t_test_pesticides','t_test_model.id=t_test_pesticides.test_model_id')
  691. ->where('t_test_model.id',$res['test_model_id'])->select();
  692. $res['name']=Db::name('undertake')->where([['task_id','=',$data['task_id']],['unit_id','=',$data['userid']]])->select();
  693. return $this->jsonSuccessData($res);
  694. }
  695. public function delSample(){
  696. $data = $this->request->post();
  697. foreach ($data as $k =>$v){
  698. $unit = Db::name('cyd')->where('sample_id',$v['sample_id'])->find();
  699. $res1=Db::name('units_test')->where('id',$unit['units_id'])->delete();
  700. $res2=Db::name('production')->where('id',$unit['product_unit'])->delete();
  701. $res = Db::name('cyd')->where('sample_id',$v['sample_id'])->delete();
  702. }
  703. return $this->jsonSuccessData($res);
  704. }
  705. public function getSampleDetail(){
  706. $data=$this->request->post();
  707. $map=[['t_cyd.sample_id','=',$data['sample_id']]];
  708. $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();
  709. return $this->jsonSuccessData($res);
  710. }
  711. public function getTestPesticides(){
  712. $data=$this->request->post();
  713. $sample = Db::name('cyd')->where('sample_id',$data['sample_id'])->field('task_id')->find();
  714. $model = Db::name('ccjc')->where('id',$sample['task_id'])->field('test_model_id')->find();
  715. $test_model = Db::name('test_pesticides')->where('test_model_id',$model['test_model_id'])->field('test_name,test_id,test_model_id')->select();
  716. return $this->jsonSuccessData($test_model);
  717. }
  718. public function saveSamplePesticides(){
  719. $data=$this->request->post();
  720. $id = '';
  721. $test_result=1;
  722. $islog = Db::name('sample_pesticides')->where('sample_id',$data['sample_id'])->select();
  723. if (count($islog)!=0){
  724. return $this->jsonResponse(-1,'不要重复提交检测信息',$islog);
  725. }
  726. foreach ($data['pesticides'] as $k=>$v){
  727. $v['sample_id']=$data['sample_id'];
  728. $idv=Db::name('sample_pesticides')->insertGetId($v);
  729. $id.=$idv.',';
  730. if ($v['test_result']=='不合格'){
  731. $test_result=0;
  732. }
  733. }
  734. $res=Db::name('cyd')->where('sample_id',$data['sample_id'])->update(['jc_value_id'=>$id,'test_result'=>$test_result]);
  735. return $this->jsonSuccessData($res);
  736. }
  737. public function editSamplePesticides(){
  738. $data=$this->request->post();
  739. foreach ($data as $k=>$v){
  740. $id=Db::name('sample_pesticides')->where('id',$v['id'])->update($v);
  741. }
  742. return $this->jsonSuccessData($id);
  743. }
  744. public function getTestValue(){
  745. $data=$this->request->post();
  746. $res['result_detail']=Db::name('sample_pesticides')->where('sample_id',$data['sample_id'])->select();
  747. $res['result']=Db::name('cyd')->where('sample_id',$data['sample_id'])->find();
  748. return $this->jsonSuccessData($res);
  749. }
  750. public function saveTestFile(){
  751. $data=$this->request->post();
  752. $data['unit_id']=$this->request->user['unit_id']->getValue();
  753. $task = Db::name('cyd')->where('sample_id')->find();
  754. $data['task_id']=$task['task_id'];
  755. $res = Db::name('jcbg')->strict(false)->insertGetId($data);
  756. return $this->jsonSuccessData($res);
  757. }
  758. public function getUnitsFileList(){
  759. $data = $this->request->post();
  760. $pageNum=$data['pageNum'];
  761. $pageSize=$data['pageSize'];
  762. $data['unit_id']=$this->request->user['unit_id']->getValue();
  763. $res['rows'] = Db::name('jcbg')->where('unit_id',$data['unit_id'])->page($pageNum,$pageSize)->select();
  764. $res['total'] = Db::name('jcbg')->where('unit_id',$data['unit_id'])->count();
  765. return $this->jsonSuccessData($res);
  766. }
  767. public function getGovFileList(){
  768. $data = $this->request->post();
  769. $pageNum=$data['pageNum'];
  770. $pageSize=$data['pageSize'];
  771. // $data['unit_id']=$this->request->user['unit_id']->getValue();
  772. $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();
  773. $res['total'] = Db::name('jcbg')->count();
  774. return $this->jsonSuccessData($res);
  775. }
  776. }