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