MachineProductionReportAdd.php 11 KB

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