WaterMaterialScience.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\controller\water;
  8. use app\admin\controller\AuthController;
  9. use crmeb\services\{ExpressService,
  10. JsonService,
  11. JsonService as Json,
  12. MiniProgramService,
  13. upload\Upload,
  14. WechatService,
  15. FormBuilder as Form,
  16. CacheService,
  17. UtilService as Util};
  18. use think\facade\Env;
  19. use think\facade\Route as Url;
  20. use think\facade\Validate;
  21. Use app\admin\model\water\WaterMaterialScience as model;
  22. use function GuzzleHttp\Psr7\str;
  23. /**
  24. * 订单管理控制器 同一个订单表放在一个控制器
  25. * Class StoreOrder
  26. * @package app\admin\controller\store
  27. */
  28. class WaterMaterialScience extends AuthController
  29. {
  30. /**
  31. * @return mixed
  32. */
  33. public function roof()
  34. {
  35. $this->assign('role', 'roof');
  36. return $this->fetch();
  37. }
  38. public function side_plate()
  39. {
  40. $this->assign('role', 'side_plate');
  41. return $this->fetch();
  42. }
  43. public function lacing()
  44. {
  45. $this->assign('role', 'lacing');
  46. return $this->fetch();
  47. }
  48. public function floor()
  49. {
  50. $this->assign('role', 'floor');
  51. return $this->fetch();
  52. }
  53. public function column()
  54. {
  55. $this->assign('role', 'column');
  56. return $this->fetch();
  57. }
  58. public function fula()
  59. {
  60. $this->assign('role', 'fula');
  61. return $this->fetch();
  62. }
  63. public function warm()
  64. {
  65. $this->assign('role', 'warm');
  66. return $this->fetch();
  67. }
  68. public function channel()
  69. {
  70. $this->assign('role', 'channel');
  71. return $this->fetch();
  72. }
  73. public function escalator()
  74. {
  75. $this->assign('role', 'escalator');
  76. return $this->fetch();
  77. }
  78. public function list()
  79. {
  80. $where = Util::getMore([
  81. ['page', 1],
  82. ['limit', 20],
  83. ['name', ''],
  84. ['role', '']
  85. ]);
  86. return Json::successlayui(model::list($where));
  87. }
  88. /**
  89. * 显示创建资源表单页.
  90. *
  91. * @return \think\Response
  92. */
  93. public function create($id = 0, $role = 0)
  94. {
  95. if ($role == 'roof'){
  96. $type = 1;
  97. }elseif ($role == 'side_plate'){
  98. $type = 2;
  99. }elseif ($role == 'floor'){
  100. $type = 4;
  101. }else{
  102. return Json::fail('参数错误');
  103. }
  104. $f = [];
  105. $f[] = Form::input('name', '名称')->col(12);
  106. $f[] = Form::hidden('type', $type);
  107. $f[] = Form::select('th_id', '选择厚度', '')->options(
  108. array_merge([['value' => 0, 'label' => '请选择厚度']], \app\admin\model\water\WaterThickness::order('id DESC')->field('id as value,name as label')->select()->toArray())
  109. )->filterable(true);
  110. $f[] = Form::select('cate_id', '选择分类', '')->options(
  111. array_merge([['value' => 0, 'label' => '请选择分类']], \app\admin\model\water\WaterCate::order('id DESC')->where('type', $type)->field('id as value,name as label')->select()->toArray())
  112. )->filterable(true);
  113. $f[] = Form::input('weight', '重量(kg)');
  114. $f[] = Form::input('unit_price', '单价');
  115. $f[] = Form::input('company', '单位');
  116. $f[] = Form::input('long', '长');
  117. $f[] = Form::input('wide', '宽');
  118. // $f[] = Form::radio('is_gc', '公差', 0)->options([['value' => 0, 'label' => '正常'], ['value' => 1, 'label' => '大公差']]);
  119. $form = Form::make_post_form('添加', $f, Url::buildUrl('save'));
  120. $this->assign(compact('form'));
  121. return $this->fetch('public/form-builder');
  122. }
  123. /**
  124. * 显示创建资源表单页.
  125. *
  126. * @return \think\Response
  127. */
  128. public function creates($id = 0, $role = 0)
  129. {
  130. if ($role == 'column'){
  131. $type = 5;
  132. }elseif ($role == 'fula'){
  133. $type = 6;
  134. }elseif ($role == 'lacing'){
  135. $type = 3;
  136. }else{
  137. return Json::fail('参数错误');
  138. }
  139. $f = [];
  140. $f[] = Form::input('name', '名称')->col(12);
  141. $f[] = Form::hidden('type', $type);
  142. $f[] = Form::select('th_id', '选择厚度', '')->options(
  143. array_merge([['value' => 0, 'label' => '请选择厚度']], \app\admin\model\water\WaterThickness::order('id DESC')->field('id as value,name as label')->select()->toArray())
  144. )->filterable(true);
  145. $f[] = Form::select('cate_id', '选择分类', '')->options(
  146. array_merge([['value' => 0, 'label' => '请选择分类']], \app\admin\model\water\WaterCate::order('id DESC')->where('type', $type)->field('id as value,name as label')->select()->toArray())
  147. )->filterable(true);
  148. $f[] = Form::input('weight', '重量(kg)');
  149. $f[] = Form::input('unit_price', '单价');
  150. $f[] = Form::input('company', '单位');
  151. // $f[] = Form::radio('is_gc', '公差', 0)->options([['value' => 0, 'label' => '正常'], ['value' => 1, 'label' => '大公差']]);
  152. $form = Form::make_post_form('添加', $f, Url::buildUrl('save'));
  153. $this->assign(compact('form'));
  154. return $this->fetch('public/form-builder');
  155. }
  156. /**
  157. * 显示创建资源表单页.
  158. *
  159. * @return \think\Response
  160. */
  161. public function create_warm($id = 0, $role = 0)
  162. {
  163. if ($role == 'warm'){
  164. $type = 7;
  165. }else{
  166. return Json::fail('参数错误');
  167. }
  168. $f = [];
  169. $f[] = Form::input('name', '名称')->col(12);
  170. $f[] = Form::hidden('type', $type);
  171. $f[] = Form::select('cate_id', '选择分类', '')->options(
  172. array_merge([['value' => 0, 'label' => '请选择分类']], \app\admin\model\water\WaterCate::order('id DESC')->where('type', $type)->field('id as value,name as label')->select()->toArray())
  173. )->filterable(true);
  174. $f[] = Form::input('weight', '重量(kg)');
  175. $f[] = Form::input('unit_price', '单价');
  176. $f[] = Form::input('company', '单位');
  177. $f[] = Form::input('long', '长');
  178. $f[] = Form::input('wide', '宽');
  179. // $f[] = Form::radio('is_gc', '公差', 0)->options([['value' => 0, 'label' => '正常'], ['value' => 1, 'label' => '大公差']]);
  180. $form = Form::make_post_form('添加', $f, Url::buildUrl('save'));
  181. $this->assign(compact('form'));
  182. return $this->fetch('public/form-builder');
  183. }
  184. /**
  185. * 显示创建资源表单页.
  186. *
  187. * @return \think\Response
  188. */
  189. public function create_channel($id = 0, $role = 0)
  190. {
  191. if ($role == 'escalator'){
  192. $type = 9;
  193. }elseif ($role == 'channel'){
  194. $type = 8;
  195. }else{
  196. return Json::fail('参数错误');
  197. }
  198. $f = [];
  199. $f[] = Form::input('name', '名称')->col(12);
  200. $f[] = Form::hidden('type', $type);
  201. $f[] = Form::select('cate_id', '选择分类', '')->options(
  202. array_merge([['value' => 0, 'label' => '请选择分类']], \app\admin\model\water\WaterCate::order('id DESC')->where('type', $type)->field('id as value,name as label')->select()->toArray())
  203. )->filterable(true);
  204. $f[] = Form::input('weight', '重量(kg)');
  205. $f[] = Form::input('unit_price', '单价');
  206. $f[] = Form::input('company', '单位');
  207. // $f[] = Form::radio('is_gc', '公差', 0)->options([['value' => 0, 'label' => '正常'], ['value' => 1, 'label' => '大公差']]);
  208. $form = Form::make_post_form('添加', $f, Url::buildUrl('save'));
  209. $this->assign(compact('form'));
  210. return $this->fetch('public/form-builder');
  211. }
  212. public function save()
  213. {
  214. $model = new model;
  215. $data = Util::postMore([
  216. 'name',
  217. 'type',
  218. 'th_id',
  219. 'weight',
  220. 'unit_price',
  221. 'company',
  222. 'long',
  223. 'wide',
  224. // 'is_gc',
  225. 'cate_id',
  226. ]);
  227. $validate = Validate::rule('name', 'require')->rule([
  228. 'name' => 'require',
  229. 'type' => 'require',
  230. 'weight' => 'require',
  231. 'unit_price' => 'require',
  232. 'company' => 'require',
  233. 'cate_id' => 'require',
  234. ]);
  235. $validate->message([
  236. 'name.require' => '名称不能为空',
  237. 'type.require' => '请选择分类',
  238. 'weight.require' => '请填写重量',
  239. 'unit_price.require' => '请填写单价',
  240. 'company.require' => '请填写单位',
  241. 'cate_id.require' => '请选择分类',
  242. ]);
  243. if (!$validate->check($data)) {
  244. return Json::fail($validate->getError());
  245. }
  246. $res = $model->save($data);
  247. if ($res) return Json::successful('添加成功');
  248. return Json::fail('添加失败');
  249. }
  250. /**
  251. * 显示创建资源表单页.
  252. *
  253. * @return \think\Response
  254. */
  255. public function edit($id = 0)
  256. {
  257. $data = model::find($id);
  258. $f = [];
  259. $f[] = Form::input('name', '名称', $data['name'])->col(12);
  260. $f[] = Form::select('th_id', '选择厚度', (string)$data['th_id'])->options(
  261. array_merge([['value' => 0, 'label' => '请选择分类']], \app\admin\model\water\WaterThickness::order('id DESC')->field('id as value,name as label')->select()->toArray())
  262. )->filterable(true);
  263. $f[] = Form::select('cate_id', '选择分类', (string)$data['cate_id'])->options(
  264. array_merge([['value' => 0, 'label' => '请选择分类']], \app\admin\model\water\WaterCate::order('id DESC')->where('type', $data['type'])->field('id as value,name as label')->select()->toArray())
  265. )->filterable(true);
  266. $f[] = Form::input('weight', '重量(kg)', $data['weight']);
  267. $f[] = Form::input('unit_price', '单价', $data['unit_price']);
  268. $f[] = Form::input('company', '单位', $data['company']);
  269. $f[] = Form::input('long', '长', $data['long']);
  270. $f[] = Form::input('wide', '宽', $data['wide']);
  271. // $f[] = Form::radio('is_gc', '公差', $data['is_gc'])->options([['value' => 0, 'label' => '正常'], ['value' => 1, 'label' => '大公差']]);
  272. $f[] = Form::hidden('id', $id);
  273. $form = Form::make_post_form('修改', $f, Url::buildUrl('update'));
  274. $this->assign(compact('form'));
  275. return $this->fetch('public/form-builder');
  276. }
  277. /**
  278. * 显示创建资源表单页.
  279. *
  280. * @return \think\Response
  281. */
  282. public function edits($id = 0)
  283. {
  284. $data = model::find($id);
  285. $f = [];
  286. $f[] = Form::input('name', '名称', $data['name'])->col(12);
  287. $f[] = Form::select('th_id', '选择厚度', (string)$data['th_id'])->options(
  288. array_merge([['value' => 0, 'label' => '请选择分类']], \app\admin\model\water\WaterThickness::order('id DESC')->field('id as value,name as label')->select()->toArray())
  289. )->filterable(true);
  290. $f[] = Form::select('cate_id', '选择分类', (string)$data['cate_id'])->options(
  291. array_merge([['value' => 0, 'label' => '请选择分类']], \app\admin\model\water\WaterCate::order('id DESC')->where('type', $data['type'])->field('id as value,name as label')->select()->toArray())
  292. )->filterable(true);
  293. $f[] = Form::input('weight', '重量(kg)', $data['weight']);
  294. $f[] = Form::input('unit_price', '单价', $data['unit_price']);
  295. $f[] = Form::input('company', '单位', $data['company']);
  296. // $f[] = Form::radio('is_gc', '公差', $data['is_gc'])->options([['value' => 0, 'label' => '正常'], ['value' => 1, 'label' => '大公差']]);
  297. $f[] = Form::hidden('id', $id);
  298. $form = Form::make_post_form('修改', $f, Url::buildUrl('update'));
  299. $this->assign(compact('form'));
  300. return $this->fetch('public/form-builder');
  301. }
  302. /**
  303. * 显示创建资源表单页.
  304. *
  305. * @return \think\Response
  306. */
  307. public function edit_warm($id = 0)
  308. {
  309. $data = model::find($id);
  310. $f = [];
  311. $f[] = Form::input('name', '名称', $data['name'])->col(12);
  312. $f[] = Form::select('cate_id', '选择分类', (string)$data['cate_id'])->options(
  313. array_merge([['value' => 0, 'label' => '请选择分类']], \app\admin\model\water\WaterCate::order('id DESC')->where('type', $data['type'])->field('id as value,name as label')->select()->toArray())
  314. )->filterable(true);
  315. $f[] = Form::input('weight', '重量(kg)', $data['weight']);
  316. $f[] = Form::input('unit_price', '单价', $data['unit_price']);
  317. $f[] = Form::input('company', '单位', $data['company']);
  318. $f[] = Form::input('long', '长', $data['long']);
  319. $f[] = Form::input('wide', '宽', $data['wide']);
  320. // $f[] = Form::radio('is_gc', '公差', $data['is_gc'])->options([['value' => 0, 'label' => '正常'], ['value' => 1, 'label' => '大公差']]);
  321. $f[] = Form::hidden('id', $id);
  322. $form = Form::make_post_form('修改', $f, Url::buildUrl('update'));
  323. $this->assign(compact('form'));
  324. return $this->fetch('public/form-builder');
  325. }
  326. /**
  327. * 显示创建资源表单页.
  328. *
  329. * @return \think\Response
  330. */
  331. public function edit_channel($id = 0)
  332. {
  333. $data = model::find($id);
  334. $f = [];
  335. $f[] = Form::input('name', '名称', $data['name'])->col(12);
  336. $f[] = Form::select('cate_id', '选择分类', (string)$data['cate_id'])->options(
  337. array_merge([['value' => 0, 'label' => '请选择分类']], \app\admin\model\water\WaterCate::order('id DESC')->where('type', $data['type'])->field('id as value,name as label')->select()->toArray())
  338. )->filterable(true);
  339. $f[] = Form::input('weight', '重量(kg)', $data['weight']);
  340. $f[] = Form::input('unit_price', '单价', $data['unit_price']);
  341. $f[] = Form::input('company', '单位', $data['company']);
  342. // $f[] = Form::radio('is_gc', '公差', $data['is_gc'])->options([['value' => 0, 'label' => '正常'], ['value' => 1, 'label' => '大公差']]);
  343. $f[] = Form::hidden('id', $id);
  344. $form = Form::make_post_form('修改', $f, Url::buildUrl('update'));
  345. $this->assign(compact('form'));
  346. return $this->fetch('public/form-builder');
  347. }
  348. /**
  349. * 修改
  350. * @return void
  351. * @throws \think\db\exception\DataNotFoundException
  352. * @throws \think\db\exception\DbException
  353. * @throws \think\db\exception\ModelNotFoundException
  354. */
  355. public function update()
  356. {
  357. $model = new model;
  358. $data = Util::postMore([
  359. 'name',
  360. 'th_id',
  361. 'weight',
  362. 'unit_price',
  363. 'company',
  364. 'long',
  365. 'wide',
  366. 'id',
  367. // 'is_gc',
  368. 'cate_id'
  369. ]);
  370. $validate = Validate::rule('name', 'require')->rule([
  371. 'name' => 'require',
  372. 'weight' => 'require',
  373. 'unit_price' => 'require',
  374. 'company' => 'require',
  375. 'cate_id' => 'require',
  376. ]);
  377. $validate->message([
  378. 'name.require' => '名称不能为空',
  379. 'type.require' => '请选择分类',
  380. 'weight.require' => '请填写重量',
  381. 'unit_price.require' => '请填写单价',
  382. 'company.require' => '请填写单位',
  383. 'cate_id.require' => '请选择分类',
  384. ]);
  385. $details = $model->find($data['id']);
  386. $details['name'] = $data['name'];
  387. $details['th_id'] = $data['th_id'];
  388. $details['weight'] = $data['weight'];
  389. $details['unit_price'] = $data['unit_price'];
  390. $details['company'] = $data['company'];
  391. // $details['is_gc'] = $data['is_gc'];
  392. $details['long'] = $data['long'];
  393. $details['wide'] = $data['wide'];
  394. $details['cate_id'] = $data['cate_id'];
  395. $res = $details->save();
  396. if ($res) return Json::successful('修改成功');
  397. return Json::fail('修改失败');
  398. }
  399. public function excel()
  400. {
  401. $upload = new Upload('local');
  402. $res = $upload->to('config/file')->move($this->request->param('file', 'file'));
  403. $file = app()->getRootPath().'public'.$res->filePath;
  404. $PHPExcel = new \PHPExcel();//创建一个excel对象
  405. $PHPReader = new \PHPExcel_Reader_Excel2007(); //建立reader对象,excel—2007以后格式
  406. if (!$PHPReader->canRead($file)) {
  407. $PHPReader = new \PHPExcel_Reader_Excel5();//建立reader对象,excel—2007以前格式
  408. if (!$PHPReader->canRead($file)) {
  409. $msg = '不是excel格式文件';
  410. $this->apiReturn(303,$msg);
  411. }
  412. }
  413. $PHPExcel = $PHPReader->load($file); //加载excel对象
  414. $sheet = $PHPExcel->getSheet(0); //获取指定的sheet表
  415. $rows = $sheet->getHighestRow();//行数
  416. $cols = $sheet->getHighestColumn();//列数
  417. $data = array();
  418. for ($i = 1; $i <= $rows; $i++){ //行数是以第1行开始
  419. $count = 0;
  420. for ($j = 'A'; $j <= $cols; $j++) { //列数是以A列开始
  421. $value = (string)$sheet->getCell($j . $i)->getValue();
  422. if ($value) {
  423. $data[$i - 1][$count] = (string)$sheet->getCell($j . $i)->getValue();
  424. if ($sheet->getCell($j . $i)->getOldCalculatedValue()){
  425. if ($j == 'J') $data[$i - 1][7] = $sheet->getCell($j . $i)->getOldCalculatedValue();
  426. if ($j == 'K') $data[$i - 1][8] = $sheet->getCell($j . $i)->getOldCalculatedValue();
  427. }
  428. $count += 1;
  429. }
  430. }
  431. }
  432. foreach ($data as $k => $item){
  433. if (isset($item[3])){;
  434. if (!strstr($item[3], '双') and strstr($item[3], '/') and isset($item[8])){
  435. $array = explode('/', $item['3']);
  436. $name = $item[2].$array[1].'mm';
  437. if (strstr(explode('×',$array[2])[0], 'm')){
  438. $long = substr(explode('×',$array[2])[0],0,strrpos(explode('×',$array[2])[0],"m"));
  439. $wide = substr(explode('×',$array[2])[1],0,strrpos(explode('×',$array[2])[1],"m"));
  440. }else{
  441. $long = explode('×',$array[2])[0];
  442. $wide = explode('×',$array[2])[1];
  443. $name = $item[2].$array[1].'mm'.$long.'*'.$wide;
  444. }
  445. $cate = \app\admin\model\water\WaterCate::where('name', 'like', '%'.$name.'%')->select()->toArray();
  446. if (count($cate) > 0){
  447. foreach ($cate as $value){
  448. $details = \app\admin\model\water\WaterMaterialScience::where('number', $item['1'])->where('type', $value['type'])->find();
  449. if (!$details){
  450. \app\admin\model\water\WaterMaterialScience::create([
  451. 'name' => $item[2].$array[1].'mm',
  452. 'number' => $item[1],
  453. 'type' => $value['type'],
  454. 'cate_id' => $value['id'],
  455. 'th_id' => \app\admin\model\water\WaterThickness::where('thickness', $array[1])->value('id'),
  456. 'weight' => $item[6],
  457. 'unit_price' => $item[8],
  458. 'company' => $item[5],
  459. 'long' => $long,
  460. 'wide' => $wide,
  461. ]);
  462. }else{
  463. $details['unit_price'] = $item[8];
  464. $details['long'] = $long;
  465. $details['wide'] = $wide;
  466. $details['name'] = $name;
  467. $details->save();
  468. }
  469. }
  470. }
  471. }
  472. }
  473. }
  474. return Json::successful('导入成功!');
  475. }
  476. /**
  477. * 删除
  478. * @param $id
  479. * @return void
  480. * @throws \Exception
  481. */
  482. public function delete($id)
  483. {
  484. if (!$id) Json::fail('删除失败');
  485. $model = new model;
  486. $res = model::destroy($id);
  487. if ($res){
  488. return Json::success('删除成功!');
  489. }else{
  490. return Json::fail($model->getErrorInfo());
  491. }
  492. }
  493. }