MachineProductionReportAdd.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. /**
  5. * 机台生产日报表维护附加接口
  6. */
  7. class MachineProductionReportAdd extends Api
  8. {
  9. protected $noNeedLogin = ['*'];
  10. protected $noNeedRight = ['*'];
  11. /**
  12. * 首页
  13. *
  14. */
  15. public function index()
  16. {
  17. $this->success('请求成功');
  18. }
  19. /**
  20. * 获取机台生产日报表附加侧边栏
  21. * @ApiMethod (GET)
  22. */
  23. public function getTab()
  24. {
  25. //get请求
  26. if(!$this->request->isGet()){
  27. $this->error('请求方式错误');
  28. }
  29. $rows = db('db_sczladd')
  30. ->field('LEFT(sys_rq, 10) as date, COUNT(*) as counts')
  31. ->group('date')
  32. ->order('UniqId desc')
  33. ->limit(60)
  34. ->select();
  35. $arr = db('db_sczladd')
  36. ->field('LEFT(sys_rq, 10) as date, rtrim(sys_id) as sys_id, COUNT(*) as count')
  37. ->where('sys_rq','>=',$rows[count($rows)-1]['date'])
  38. ->group('date, sys_id')
  39. ->select();
  40. foreach($rows as $key=>$value){
  41. $rows[$key]['sys'] = [];
  42. foreach($arr as $k=>$v){
  43. if($value['date'] == $v['date']){
  44. unset($v['date']);
  45. array_push($rows[$key]['sys'],$v);
  46. unset($arr[$k]);
  47. }
  48. }
  49. $rows[$key]['date'] = str_replace('-', '.', $rows[$key]['date']);
  50. }
  51. $this->success('成功',$rows);
  52. }
  53. /**
  54. * 获取机台生产日报表附加列表
  55. * @ApiMethod (GET)
  56. * @param string $date 时间
  57. * @param string $sys_id 用户
  58. */
  59. public function getList()
  60. {
  61. //get请求
  62. if(!$this->request->isGet()){
  63. $this->error('请求方式错误');
  64. }
  65. $req = $this->request->param();
  66. $page = 1;
  67. $limit = 15;
  68. if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
  69. if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
  70. $where = [];
  71. if (isset($req['date']) && !empty($req['date'])){
  72. $where['sys_rq'] = ['LIKE',$req['date'].'%'];
  73. }else{
  74. $this->error('参数错误');
  75. }
  76. if (isset($req['sys_id']) && !empty($req['sys_id'])) $where['sys_id'] = ['LIKE',$req['sys_id'].'%'];
  77. //来料异常 日定额 千件工价 补产标准
  78. //制程废 次品
  79. $rows = db('db_sczladd')
  80. ->field('LEFT(sczl_rq, 10) as sczl_rq, sczl_jtbh,
  81. sczl_bzdh, CAST(sczl_设备运行工时 AS SIGNED) as sczl_设备运行工时, sczl_desc,
  82. rtrim(sys_id) as sys_id, sys_rq, mod_rq, UniqId')
  83. ->where($where)
  84. ->order('UniqId desc')
  85. ->page($page,$limit)
  86. ->select();
  87. $total = db('db_sczladd')->where($where)->count();
  88. $sb = db('设备_基本资料')->column('设备编号, 设备名称');
  89. foreach ($rows as $key=>$value){
  90. $rows[$key]['mod_rq'] = $value['mod_rq']=='1900-01-01 00:00:00' ? '' :$value['mod_rq'];
  91. $rows[$key]['sczl_sbmc'] = trim($sb[$value['sczl_jtbh']]);
  92. $rows[$key]['sczl_设备运行工时'] = $value['sczl_设备运行工时'] == 0 ? '' : $value['sczl_设备运行工时'];
  93. }
  94. $data = [
  95. 'total' => $total,
  96. 'rows' => $rows,
  97. ];
  98. $this->success('成功',$data);
  99. }
  100. /**
  101. * 获取机台生产日报表附加信息
  102. * @ApiMethod (GET)
  103. * @param string $UniqId UniqId
  104. */
  105. public function getInfo()
  106. {
  107. //get请求
  108. if(!$this->request->isGet()){
  109. $this->error('请求方式错误');
  110. }
  111. $req = $this->request->param();
  112. if (isset($req['UniqId']) && !empty($req['UniqId'])){
  113. $UniqId = $req['UniqId'];
  114. }else{
  115. $this->error('参数错误');
  116. }
  117. $rows = db('db_sczladd')->alias('d')
  118. ->field('LEFT(d.sczl_rq, 10) as sczl_rq, d.sczl_jtbh, rtrim(g.设备名称) as 设备名称, d.sczl_bzdh, d.sczl_设备运行工时, d.sczl_desc')
  119. ->join('设备_基本资料 g', 'd.sczl_jtbh=g.设备编号')
  120. ->where('d.UniqId',$UniqId)
  121. ->select();
  122. $this->success('成功',$rows);
  123. }
  124. /**
  125. * 查询机台
  126. * @ApiMethod (GET)
  127. */
  128. public function getJtbh()
  129. {
  130. //get请求
  131. if(!$this->request->isGet()){
  132. $this->error('请求方式错误');
  133. }
  134. $req = $this->request->param();
  135. if (isset($req['sys_mc'])){
  136. $sys_mc = $req['sys_mc'];
  137. }else{
  138. $this->error('参数错误');
  139. }
  140. $rows = db('设备_基本资料')
  141. ->field('rtrim(使用部门) as 使用部门, rtrim(设备编号) as jtbh, rtrim(设备名称) as 设备名称')
  142. ->where('设备编号|设备名称','like','%'.$sys_mc.'%')
  143. ->order('设备编号 asc')
  144. ->select();
  145. $arr = array_unique(array_column($rows, '使用部门'));
  146. sort($arr);
  147. $data = [];
  148. $data['使用部门'] = [];
  149. foreach ($arr as $value) {
  150. $data[$value] = [];
  151. foreach ($rows as $row){
  152. if($row['使用部门']==$value){
  153. if (!in_array($row['使用部门'], $data['使用部门'])) {
  154. array_push($data['使用部门'],$row['使用部门']);
  155. }
  156. unset($row['使用部门']);
  157. array_push($data[$value], $row);
  158. }
  159. }
  160. }
  161. $this->success('成功',[$data]);
  162. }
  163. /**
  164. * 新增
  165. * @ApiMethod (POST)
  166. * @param string
  167. */
  168. public function add()
  169. {
  170. if(!$this->request->isPost()){
  171. $this->error('请求方式错误');
  172. }
  173. $req = $this->request->param();
  174. $arr = [
  175. 'sys_id','sczl_rq','sczl_jtbh',
  176. 'sczl_bzdh','sczl_设备运行工时',
  177. 'sczl_desc'
  178. ];
  179. $data = [];
  180. foreach ($arr as $key => $value){
  181. if (!(isset($req[$value]))){
  182. $this->error('参数错误',$value,$key+1);
  183. }
  184. $data[$value] = $req[$value];
  185. }
  186. $data['sys_rq'] = date('Y-m-d H:i:s');
  187. //查询UniqId
  188. $UniqId = db('db_sczladd')->max('UniqId');
  189. $data['UniqId'] = $UniqId < 10000000 ? 10000000 : $UniqId + 1;
  190. //开启事务
  191. db()->startTrans();
  192. try{
  193. $sql = db('db_sczladd')->fetchSql(true)->insert($data);
  194. $bool = db()->query($sql);
  195. // 提交事务
  196. db()->commit();
  197. } catch (\Exception $e) {
  198. // 回滚事务
  199. db()->rollback();
  200. $this->error($e->getMessage());
  201. }
  202. if($bool===false) $this->error('失败');
  203. $this->success('成功');
  204. }
  205. /**
  206. * 新增
  207. * @ApiMethod (POST)
  208. * @param string
  209. */
  210. public function edit()
  211. {
  212. if(!$this->request->isPost()){
  213. $this->error('请求方式错误');
  214. }
  215. $req = $this->request->param();
  216. $arr = [
  217. 'sys_id','sczl_rq','sczl_jtbh',
  218. 'sczl_bzdh','sczl_设备运行工时',
  219. 'sczl_desc'
  220. ];
  221. $data = [];
  222. foreach ($arr as $key => $value){
  223. if (!(isset($req[$value]))){
  224. continue;
  225. }
  226. $data[$value] = $req[$value];
  227. }
  228. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  229. $this->error('参数错误','UniqId',100);
  230. }
  231. $data['mod_rq'] = date('Y-m-d H:i:s');
  232. //开启事务
  233. db()->startTrans();
  234. try{
  235. $sql = db('db_sczladd')->where('UniqId',$req['UniqId'])->fetchSql(true)->update($data);
  236. $bool = db()->query($sql);
  237. // 提交事务
  238. db()->commit();
  239. } catch (\Exception $e) {
  240. // 回滚事务
  241. db()->rollback();
  242. $this->error($e->getMessage());
  243. }
  244. if($bool===false) $this->error('失败');
  245. $this->success('成功');
  246. }
  247. /**
  248. * 修改
  249. * @ApiMethod (POST)
  250. * @param string 'UniqId'
  251. */
  252. public function del()
  253. {
  254. if(!$this->request->isPost()){
  255. $this->error('请求方式错误');
  256. }
  257. $req = $this->request->param();
  258. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  259. $this->error('参数错误','UniqId',100);
  260. }
  261. //开启事务
  262. db()->startTrans();
  263. try{
  264. $bool = db('db_sczladd')->where('UniqId',$req['UniqId'])->delete();
  265. // 提交事务
  266. db()->commit();
  267. } catch (\Exception $e) {
  268. // 回滚事务
  269. db()->rollback();
  270. $this->error($e->getMessage());
  271. }
  272. if($bool===false) $this->error('失败');
  273. $this->success('成功');
  274. }
  275. /**
  276. * excel导出
  277. * @ApiMethod (GET)
  278. * @param string file_name
  279. * @param string date
  280. * @param string sys_id
  281. * @param array fields
  282. */
  283. public function export(){
  284. if(!$this->request->isGet()){
  285. $this->error('请求方式错误');
  286. }
  287. $req = $this->request->param();
  288. $where = [];
  289. if (!isset($req['date']) || empty($req['date'])) $this->error('参数缺失');
  290. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  291. $this->error('参数错误','fields',100);
  292. }
  293. if(strpos($req['date'],'-')){
  294. $where = ['a.sys_rq'=>['like',$req['date'].'%']];
  295. }else{
  296. $where = ['a.sczl_gdbh'=>$req['date']];
  297. }
  298. if (isset($req['sys_id']) && !empty($req['sys_id'])){
  299. $where['a.sys_id'] = ['LIKE',$req['sys_id'].'%'];
  300. }
  301. $field = '';
  302. $arr = [
  303. 'sczl_rq' =>['生产日期','LEFT(a.sczl_rq, 10) as sczl_rq'],
  304. 'sczl_jtbh' =>['设备编号','a.sczl_jtbh'],
  305. '设备名称' =>['设备名称','rtrim(b.设备名称) as 设备名称'],
  306. 'sczl_bzdh' =>['班组','rtrim(a.sczl_bzdh) as sczl_bzdh'],
  307. 'sczl_设备运行工时' =>['排班工时','a.sczl_设备运行工时'],
  308. 'sczl_desc' =>['备注','rtrim(a.sczl_desc) as sczl_desc']
  309. ];
  310. $data[0] = [];
  311. foreach ($req['fields'] as $k=>$v){
  312. if(array_key_exists($v,$arr)){
  313. if ($k==0){
  314. $field .= $arr[$v][1];
  315. }else{
  316. $field .= ','.$arr[$v][1];
  317. }
  318. array_push($data[0],$arr[$v][0]);
  319. }
  320. }
  321. $rows = db('db_sczladd')->alias('a')
  322. ->join('设备_基本资料 b','a.sczl_jtbh = b.设备编号','left')
  323. ->field($field)
  324. ->where($where)
  325. ->order('a.UniqId asc')
  326. ->select();
  327. foreach ($rows as $key=>$value) {
  328. if (isset($value['sczl_设备运行工时'])) $value['sczl_设备运行工时'] = floatval($value['sczl_设备运行工时']);
  329. $subArray = [];
  330. foreach ($req['fields'] as $k=>$v){
  331. if(array_key_exists($v,$arr)){
  332. if($value[$v]){
  333. array_push($subArray,$value[$v]);
  334. }else{
  335. array_push($subArray,'');
  336. }
  337. }
  338. }
  339. array_push($data,$subArray);
  340. }
  341. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  342. }
  343. }