HydEquipment.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. namespace catchAdmin\hydraulic\controller;
  3. error_reporting(1);
  4. use catchAdmin\equipment\model\EquipmentType;
  5. use catcher\base\CatchRequest as Request;
  6. use catcher\CatchResponse;
  7. use catcher\base\CatchController;
  8. use catchAdmin\hydraulic\model\HydEquipment as hydEquipmentModel;
  9. use catchAdmin\hydraulic\model\Hydraulic;
  10. use catchAdmin\hydraulic\model\Wrench;
  11. use catchAdmin\system\model\SysDictData;
  12. use catcher\base\CatchRequest;
  13. use PhpOffice\PhpSpreadsheet\IOFactory;
  14. use PhpParser\Node\Stmt\Catch_;
  15. use think\facade\Db;
  16. class HydEquipment extends CatchController
  17. {
  18. protected $hydEquipmentModel;
  19. public function __construct(HydEquipmentModel $hydEquipmentModel)
  20. {
  21. $this->hydEquipmentModel = $hydEquipmentModel;
  22. }
  23. /**
  24. * 列表
  25. * @time 2022年06月21日 11:02
  26. * @param Request $request
  27. */
  28. public function index(Request $request): \think\Response
  29. {
  30. return CatchResponse::paginate($this->hydEquipmentModel->getList());
  31. }
  32. /**
  33. * 保存信息
  34. * @time 2022年06月21日 11:02
  35. * @param Request $request
  36. */
  37. public function save(Request $request): \think\Response
  38. {
  39. $data = $request->post();
  40. $hydraulic = null;
  41. $wrench = null;
  42. $hydequ = [
  43. // 物料号
  44. 'material_number' => $data['material_number'],
  45. // 设备类别
  46. 'equipment_type' => $data['equipment_type'],
  47. // 工具名称
  48. 'name' => $data['name'],
  49. // 设备型号
  50. 'equipment_model' => $data['equipment_model'],
  51. // 发放单位
  52. 'issue_unit' => $data['issue_unit'],
  53. // 固定资产编号
  54. 'fixed_asset_number' => $data['fixed_asset_number'],
  55. // 类固定资产编号
  56. 'fixed_asset_number2' => $data['fixed_asset_number2'],
  57. // 序列号
  58. 'serial_number' => $data['serial_number'],
  59. // 出厂编号
  60. 'factory_number' => $data['factory_number'],
  61. // 上次校验时间
  62. 'check_last_time' => $data['check_last_time'],
  63. // 下次校验时间
  64. 'check_next_time' => $data['check_next_time'],
  65. // 校验状态
  66. 'check_status' => $data['check_status'],
  67. // 状态
  68. 'status' => $data['status'],
  69. 'address' => $data['address'],
  70. 'longitude' => $data['longitude'],
  71. 'latitude' => $data['latitude']
  72. ];
  73. Db::startTrans();
  74. try {
  75. $id = $this->hydEquipmentModel->storeBy($hydequ);
  76. if ($data['equipment_type'] == 2) {
  77. $hydraulic = [
  78. // 编号
  79. 'number' => $data['number'],
  80. // 所属部门
  81. 'department_id' => $data['department_id'],
  82. 'remark' => $data['remark'] ? $data['remark'] : '',
  83. 'max_pressure' => $data['max_pressure'],
  84. 'min_pressure' => $data['min_pressure'],
  85. 'imei' => $data['imei'],
  86. 'effective_period' => $data['effective_period'],
  87. //设备的id
  88. 'eq_id' => $id
  89. ];
  90. $hyd_exist = Hydraulic::where('imei', $data['imei'])->find();
  91. if ($hyd_exist) {
  92. Db::rollback();
  93. return CatchResponse::fail('imei号已存在');
  94. }
  95. Hydraulic::create($hydraulic);
  96. }
  97. //保存扳手
  98. if ($data['equipment_type'] == 3 || $data['equipment_type'] == 4) {
  99. $wrench = [
  100. // 编号
  101. 'number' => $data['number'],
  102. //设备id
  103. 'eq_id' => $id,
  104. // 所属部门
  105. 'department_id' => $data['department_id'],
  106. 'remark' => $data['remark'] ? $data['remark'] : '',
  107. 'min_pressure' => $data['min_pressure'],
  108. 'max_pressure' => $data['max_pressure'],
  109. 'min_torque' => $data['min_torque'],
  110. 'max_torque' => $data['max_torque'],
  111. 'angle_sensor' => $data['angle_sensor'],
  112. 'angular_resolution' => $data['angular_resolution']
  113. ];
  114. $wrench_exist = Wrench::where('number', $data['number'])->find();
  115. if ($wrench_exist) {
  116. Db::rollback();
  117. return CatchResponse::fail('扳手编号已存在');
  118. }
  119. Wrench::create($wrench);
  120. }
  121. } catch (\Exception $e) {
  122. Db::rollback();
  123. return CatchResponse::fail($e->getMessage());
  124. }
  125. Db::commit();
  126. return CatchResponse::success();
  127. }
  128. /**
  129. * 读取
  130. * @time 2022年06月21日 11:02
  131. * @param $id
  132. */
  133. public function read($id): \think\Response
  134. {
  135. return CatchResponse::success($this->hydEquipmentModel->findBy($id));
  136. }
  137. /**
  138. * 更新
  139. * @time 2022年06月21日 11:02
  140. * @param Request $request
  141. * @param $id
  142. */
  143. public function update(Request $request, $id): \think\Response
  144. {
  145. $data = $request->post();
  146. $hydraulic = null;
  147. $wrench = null;
  148. $hydequ = [
  149. // 物料号
  150. 'material_number' => $data['material_number'],
  151. // 设备类别
  152. 'equipment_type' => $data['equipment_type'],
  153. // 工具名称
  154. 'name' => $data['name'],
  155. // 设备型号
  156. 'equipment_model' => $data['equipment_model'],
  157. // 发放单位
  158. 'issue_unit' => $data['issue_unit'],
  159. // 固定资产编号
  160. 'fixed_asset_number' => $data['fixed_asset_number'],
  161. // 类固定资产编号
  162. 'fixed_asset_number2' => $data['fixed_asset_number2'],
  163. // 序列号
  164. 'serial_number' => $data['serial_number'],
  165. // 出厂编号
  166. 'factory_number' => $data['factory_number'],
  167. // 上次校验时间
  168. 'check_last_time' => $data['check_last_time'] ? strstr($data['check_last_time'], '-') ? strtotime($data['check_last_time']) : $data['check_last_time'] : '',
  169. // 下次校验时间
  170. 'check_next_time' => $data['check_next_time'] ? strstr($data['check_next_time'], '-') ? strtotime($data['check_next_time']) : $data['check_next_time'] : '',
  171. // 校验状态
  172. 'check_status' => $data['check_status'],
  173. // 状态
  174. 'status' => $data['status'],
  175. 'address' => $data['address'],
  176. 'longitude' => $data['longitude'],
  177. 'latitude' => $data['latitude']
  178. ];
  179. if ($data['equipment_type'] == 2) {
  180. $hydraulic = [
  181. // 编号
  182. 'number' => $data['number'],
  183. // 所属部门
  184. 'department_id' => $data['department_id'],
  185. //备注
  186. 'remark' => $data['remark'],
  187. 'max_pressure' => $data['max_pressure'],
  188. 'min_pressure' => $data['min_pressure'],
  189. 'imei' => $data['imei'],
  190. 'effective_period' => $data['effective_period'],
  191. //设备的id
  192. 'eq_id' => $id
  193. ];
  194. $hyd_exist = Hydraulic::where('imei', $data['imei'])->where('eq_id', '<>', $id)->find();
  195. if ($hyd_exist) {
  196. Db::rollback();
  197. return CatchResponse::fail('imei号已存在');
  198. }
  199. Hydraulic::where('eq_id', $id)->update($hydraulic);
  200. }
  201. //保存扳手
  202. if ($data['equipment_type'] == 3 || $data['equipment_type'] == 4) {
  203. $wrench = [
  204. 'id' => $data['list']['id'],
  205. // 编号
  206. 'number' => $data['number'],
  207. //设备id
  208. 'eq_id' => $id,
  209. // 所属部门
  210. 'department_id' => $data['department_id'],
  211. 'remark' => $data['remark'],
  212. 'min_pressure' => $data['min_pressure'],
  213. 'max_pressure' => $data['max_pressure'],
  214. 'min_torque' => $data['min_torque'],
  215. 'max_torque' => $data['max_torque'],
  216. 'angular_resolution' => $data['angular_resolution'],
  217. 'angle_sensor' => $data['angle_sensor'],
  218. // 扭矩
  219. ];
  220. $wrench_exist = Wrench::where('number', $data['number'])->where('id', '<>', $data['list']['id'])->find();
  221. if ($wrench_exist) {
  222. Db::rollback();
  223. return CatchResponse::fail('扳手编号已存在');
  224. }
  225. Wrench::update($wrench, ['id' => $data['list']['id']]);
  226. }
  227. return CatchResponse::success($this->hydEquipmentModel->updateBy($id, $hydequ));
  228. }
  229. /**
  230. * 删除
  231. * @time 2022年06月21日 11:02
  232. * @param $id
  233. */
  234. public function delete($id): \think\Response
  235. {
  236. $data = array();
  237. $where = [];
  238. if (is_array($id)) {
  239. $where[] = ['id', 'in', $id];
  240. $data = $this->hydEquipmentModel->where($where)->select()->toArray();
  241. } else {
  242. $list = $this->hydEquipmentModel->where('id', $id)->find();
  243. $data[] = $list;
  244. }
  245. Db::startTrans();
  246. try {
  247. foreach ($data as $item) {
  248. if ($item['equipment_type'] == 2) {
  249. Hydraulic::where('eq_id', '=', $item['id'])->delete();
  250. }
  251. if ($item['equipment_type'] == 3 || $item['equipment_type'] == 4) {
  252. $wrench = Wrench::where('eq_id', '=', $item['id'])->find();
  253. $wrench->delete();
  254. }
  255. }
  256. $this->hydEquipmentModel->deleteBy($id, true);
  257. } catch (\Exception $e) {
  258. Db::rollback();
  259. return CatchResponse::fail($e->getMessage());
  260. }
  261. Db::commit();
  262. return CatchResponse::success();
  263. }
  264. /**
  265. * @Descripttion: 获取设备状态和校验状态
  266. * @name: likang
  267. * @return {*}
  268. */
  269. public function getHydEquipmentType(Request $request)
  270. {
  271. $data = $request->get();
  272. $code = $data['code'];
  273. $sysDict = new SysDictData();
  274. $list = $sysDict->getTypesByCodeWithRemark($code, '');
  275. return CatchResponse::success($list);
  276. }
  277. /**
  278. * 根据设备类别统计每个设备的总数
  279. */
  280. public function getTotalByEquipmentType($id)
  281. {
  282. $list = EquipmentType::where('pid', $id)->order('order desc')->field('id,name')->limit(4)->select();
  283. foreach ($list as $key => $value) {
  284. $list[$key]['total'] = $this->hydEquipmentModel->where('equipment_type', $value['id'])->count();
  285. }
  286. return CatchResponse::success($list);
  287. }
  288. /**
  289. * @Descripttion: 获取不同类型的设备
  290. * @name: likang
  291. * @return {*}
  292. */
  293. public function getAllHydEqu(Request $request)
  294. {
  295. $data = $request->get();
  296. $type = $data['type'];
  297. $list = $this->hydEquipmentModel->where('equipment_type', $type)->field('id as value,name as text')->select();
  298. return CatchResponse::success($list);
  299. }
  300. public function importStores(Request $request)
  301. {
  302. $url = $request->post('url');
  303. if (!$url) {
  304. return CatchResponse::fail('请上传文件');
  305. }
  306. $brand_id = $request->post('brand_id');
  307. if (!$brand_id) {
  308. return CatchResponse::fail('请选择品牌');
  309. }
  310. $creator_id = $request->post('creator_id');
  311. //解析地址
  312. $parse_url = parse_url($url)['path'];
  313. //载入excel表格
  314. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  315. // var_dump($objPHPExcel);
  316. //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
  317. // $nameArr = $objPHPExcel->getSheetNames();
  318. // var_dump($nameArr);
  319. //获取表的数量
  320. $sheetCount = $objPHPExcel->getSheetCount();
  321. $fail = 0; //失败条数
  322. $success = 0; //成功条数
  323. $total = 0; //总导入数
  324. $excel_data = [];
  325. //循环读取每一张表
  326. for ($index = 0; $index < $sheetCount; $index++) {
  327. //设置当前要读取的表
  328. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  329. // var_dump($sheet);exit;
  330. $highestRow = $sheet->getHighestRow(); // 取得总行数
  331. // var_dump($highestRow);
  332. if ($highestRow <= 2) {
  333. continue;
  334. }
  335. $total += $highestRow - 3;
  336. for ($j = 4; $j <= $highestRow - 1; $j++) {
  337. $data = array(); //每条门店信息
  338. $name = trim($sheet->getCell("B" . $j)->getFormattedValue()); //门店名称
  339. $city_name = trim($sheet->getCell("C" . $j)->getFormattedValue()); //市区
  340. $area_name = trim($sheet->getCell("D" . $j)->getFormattedValue()); //县区
  341. $street = trim($sheet->getCell("E" . $j)->getFormattedValue()); //街道办
  342. $address = trim($sheet->getCell("F" . $j)->getFormattedValue()); //详细地址
  343. $longitude = trim($sheet->getCell("G" . $j)->getFormattedValue()); //经度
  344. $latitude = trim($sheet->getCell("H" . $j)->getFormattedValue()); //维度
  345. $category = trim($sheet->getCell("I" . $j)->getFormattedValue()); //门店类别
  346. if ($category == '直营店') {
  347. $mendian = '1';
  348. }
  349. if ($category == '经销商') {
  350. $mendian = '2';
  351. }
  352. if ($category == '加盟店') {
  353. $mendian = '3';
  354. }
  355. $principal = trim($sheet->getCell("J" . $j)->getFormattedValue()); //店长
  356. $mobile = trim($sheet->getCell("K" . $j)->getFormattedValue()); //电话
  357. if (!$name) {
  358. $fail++;
  359. $msg = '导入第' . $j . '行门店失败:门店名称不存在';
  360. $this->importFailLog($msg);
  361. continue;
  362. }
  363. $data = array(
  364. 'department_name' => $name,
  365. 'brand_id' => $brand_id,
  366. 'area_id' => Db::table('area')->whereLike('area_name', $area_name)->value('id'),
  367. 'street' => $street,
  368. 'address' => $address,
  369. 'status' => 1, //正常
  370. 'longitude' => $longitude,
  371. 'latitude' => $latitude,
  372. 'category' => $mendian,
  373. 'principal' => $principal,
  374. 'mobile' => $mobile,
  375. 'reserve_num' => 999, //预约剩余数
  376. 'creator_id' => $creator_id,
  377. 'created_at' => time(),
  378. 'updated_at' => time()
  379. );
  380. array_push($excel_data, $data);
  381. }
  382. }
  383. // var_dump($excel_data);
  384. // return CatchResponse::success();
  385. //防止Excel有重复,去重
  386. array_unique($excel_data, SORT_REGULAR);
  387. if ($count = $this->department->limit(100)->insertAll($excel_data)) {
  388. return CatchResponse::success('共' . $total . '条数据,成功' . $count . '条,失败' . $fail . '条');
  389. }
  390. return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
  391. // return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条');
  392. }
  393. }