MachineProductionReportAdd.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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()->table('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()->table('db_sczladd')
  36. ->field('LEFT(sys_rq, 10) as date, rtrim(sys_id) as sys_id, COUNT(sys_id) as count')
  37. ->where('sys_rq','>=',$rows[59]['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()->table('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()->table('db_sczladd')->where($where)->count();
  88. $sb = db()->table('设备_基本资料')->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()->table('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()->table('设备_基本资料')
  141. ->field('rtrim(使用部门) as 使用部门, rtrim(设备编号) as jtbh, rtrim(设备名称) as 设备名称')
  142. ->where('设备编号|设备名称','like','%'.$sys_mc.'%')
  143. ->order('设备编号 asc')
  144. ->select();
  145. $arr = ['胶印车间', '凹印车间', '丝印车间', '烫模车间', '检验车间', '研发中心'];
  146. $data = [];
  147. $data['使用部门'] = $arr;
  148. foreach ($arr as $value) {
  149. $data[$value] = [];
  150. foreach ($rows as $row){
  151. if($row['使用部门']==$value){
  152. unset($row['使用部门']);
  153. array_push($data[$value], $row);
  154. }
  155. }
  156. }
  157. $this->success('成功',[$data]);
  158. }
  159. /**
  160. * 新增
  161. * @ApiMethod (POST)
  162. * @param string
  163. */
  164. public function add()
  165. {
  166. if(!$this->request->isPost()){
  167. $this->error('请求方式错误');
  168. }
  169. $req = $this->request->param();
  170. $arr = [
  171. 'sys_id','sczl_rq','sczl_jtbh',
  172. 'sczl_bzdh','sczl_设备运行工时',
  173. 'sczl_desc'
  174. ];
  175. $data = [];
  176. foreach ($arr as $key => $value){
  177. if (!(isset($req[$value]))){
  178. $this->error('参数错误',$value,$key+1);
  179. }
  180. $data[$value] = $req[$value];
  181. }
  182. $data['sys_rq'] = date('Y-m-d H:i:s');
  183. //开启事务
  184. db()->startTrans();
  185. try{
  186. $sql = db()->table('db_sczladd')->fetchSql(true)->insert($data);
  187. $bool = db()->query($sql);
  188. // 提交事务
  189. db()->commit();
  190. } catch (\Exception $e) {
  191. // 回滚事务
  192. db()->rollback();
  193. $this->error($e->getMessage());
  194. }
  195. if($bool===false) $this->error('失败');
  196. $this->success('成功');
  197. }
  198. /**
  199. * 新增
  200. * @ApiMethod (POST)
  201. * @param string
  202. */
  203. public function edit()
  204. {
  205. if(!$this->request->isPost()){
  206. $this->error('请求方式错误');
  207. }
  208. $req = $this->request->param();
  209. $arr = [
  210. 'sys_id','sczl_rq','sczl_jtbh',
  211. 'sczl_bzdh','sczl_设备运行工时',
  212. 'sczl_desc'
  213. ];
  214. $data = [];
  215. foreach ($arr as $key => $value){
  216. if (!(isset($req[$value]))){
  217. continue;
  218. }
  219. $data[$value] = $req[$value];
  220. }
  221. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  222. $this->error('参数错误','UniqId',100);
  223. }
  224. $data['mod_rq'] = date('Y-m-d H:i:s');
  225. //开启事务
  226. db()->startTrans();
  227. try{
  228. $sql = db()->table('db_sczladd')->where('UniqId',$req['UniqId'])->fetchSql(true)->update($data);
  229. $bool = db()->query($sql);
  230. // 提交事务
  231. db()->commit();
  232. } catch (\Exception $e) {
  233. // 回滚事务
  234. db()->rollback();
  235. $this->error($e->getMessage());
  236. }
  237. if($bool===false) $this->error('失败');
  238. $this->success('成功');
  239. }
  240. /**
  241. * 修改
  242. * @ApiMethod (POST)
  243. * @param string 'UniqId'
  244. */
  245. public function del()
  246. {
  247. if(!$this->request->isPost()){
  248. $this->error('请求方式错误');
  249. }
  250. $req = $this->request->param();
  251. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  252. $this->error('参数错误','UniqId',100);
  253. }
  254. //开启事务
  255. db()->startTrans();
  256. try{
  257. $bool = db()->table('db_sczladd')->where('UniqId',$req['UniqId'])->delete();
  258. // 提交事务
  259. db()->commit();
  260. } catch (\Exception $e) {
  261. // 回滚事务
  262. db()->rollback();
  263. $this->error($e->getMessage());
  264. }
  265. if($bool===false) $this->error('失败');
  266. $this->success('成功');
  267. }
  268. /**
  269. * excel导出
  270. * @ApiMethod (GET)
  271. * @param string file_name
  272. * @param string date
  273. * @param string sys_id
  274. * @param array fields
  275. */
  276. public function export(){
  277. if(!$this->request->isGet()){
  278. $this->error('请求方式错误');
  279. }
  280. $req = $this->request->param();
  281. $where = [];
  282. if (!isset($req['date']) || empty($req['date'])) $this->error('参数缺失');
  283. if (!(isset($req['fields']) && count($req['fields'])!=0)){
  284. $this->error('参数错误','fields',100);
  285. }
  286. if(strpos($req['date'],'-')){
  287. $where = ['a.sys_rq'=>['like','%'.$req['date'].'%']];
  288. }else{
  289. $where = ['a.sczl_gdbh'=>$req['date']];
  290. }
  291. if (isset($req['sys_id']) && !empty($req['sys_id'])){
  292. $where['a.sys_id'] = ['LIKE','%'.$req['sys_id'].'%'];
  293. }
  294. $field = '';
  295. $arr = [
  296. 'sczl_rq' =>['生产日期','LEFT(a.sczl_rq, 10) as sczl_rq'],
  297. 'sczl_jtbh' =>['设备编号','a.sczl_jtbh'],
  298. '设备名称' =>['设备名称','rtrim(b.设备名称) as 设备名称'],
  299. 'sczl_bzdh' =>['班组','rtrim(a.sczl_bzdh) as sczl_bzdh'],
  300. 'sczl_设备运行工时' =>['排班工时','a.sczl_设备运行工时'],
  301. 'sczl_desc' =>['备注','rtrim(a.sczl_desc) as sczl_desc']
  302. ];
  303. $data[0] = [];
  304. foreach ($req['fields'] as $k=>$v){
  305. if(array_key_exists($v,$arr)){
  306. if ($k==0){
  307. $field .= $arr[$v][1];
  308. }else{
  309. $field .= ','.$arr[$v][1];
  310. }
  311. array_push($data[0],$arr[$v][0]);
  312. }
  313. }
  314. $rows = db()->table('db_sczladd')->alias('a')
  315. ->join('设备_基本资料 b','a.sczl_jtbh = b.设备编号','left')
  316. ->field($field)
  317. ->where($where)
  318. ->order('a.UniqId asc')
  319. ->select();
  320. foreach ($rows as $key=>$value) {
  321. if (isset($value['sczl_设备运行工时'])) $value['sczl_设备运行工时'] = floatval($value['sczl_设备运行工时']);
  322. $subArray = [];
  323. foreach ($req['fields'] as $k=>$v){
  324. if(array_key_exists($v,$arr)){
  325. if($value[$v]){
  326. array_push($subArray,$value[$v]);
  327. }else{
  328. array_push($subArray,'');
  329. }
  330. }
  331. }
  332. array_push($data,$subArray);
  333. }
  334. $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
  335. }
  336. }