| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- /**
- * 机台生产日报表维护附加接口
- */
- class MachineProductionReportAdd extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- /**
- * 首页
- *
- */
- public function index()
- {
- $this->success('请求成功');
- }
- /**
- * 获取机台生产日报表附加侧边栏
- * @ApiMethod (GET)
- */
- public function getTab()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $rows = db('db_sczladd')
- ->field('LEFT(sys_rq, 10) as date, COUNT(*) as counts')
- ->group('date')
- ->order('UniqId desc')
- ->limit(60)
- ->select();
- $arr = db('db_sczladd')
- ->field('LEFT(sys_rq, 10) as date, rtrim(sys_id) as sys_id, COUNT(*) as count')
- ->where('sys_rq','>=',$rows[count($rows)-1]['date'])
- ->group('date, sys_id')
- ->select();
- foreach($rows as $key=>$value){
- $rows[$key]['sys'] = [];
- foreach($arr as $k=>$v){
- if($value['date'] == $v['date']){
- unset($v['date']);
- array_push($rows[$key]['sys'],$v);
- unset($arr[$k]);
- }
- }
- $rows[$key]['date'] = str_replace('-', '.', $rows[$key]['date']);
- }
- $this->success('成功',$rows);
- }
- /**
- * 获取机台生产日报表附加列表
- * @ApiMethod (GET)
- * @param string $date 时间
- * @param string $sys_id 用户
- */
- public function getList()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- $page = 1;
- $limit = 15;
- if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
- if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
- $where = [];
- if (isset($req['date']) && !empty($req['date'])){
- $where['sys_rq'] = ['LIKE',$req['date'].'%'];
- }else{
- $this->error('参数错误');
- }
- if (isset($req['sys_id']) && !empty($req['sys_id'])) $where['sys_id'] = ['LIKE',$req['sys_id'].'%'];
- //来料异常 日定额 千件工价 补产标准
- //制程废 次品
- $rows = db('db_sczladd')
- ->field('LEFT(sczl_rq, 10) as sczl_rq, sczl_jtbh,
- sczl_bzdh, CAST(sczl_设备运行工时 AS SIGNED) as sczl_设备运行工时, sczl_desc,
- rtrim(sys_id) as sys_id, sys_rq, mod_rq, UniqId')
- ->where($where)
- ->order('UniqId desc')
- ->page($page,$limit)
- ->select();
- $total = db('db_sczladd')->where($where)->count();
- $sb = db('设备_基本资料')->column('设备编号, 设备名称');
- foreach ($rows as $key=>$value){
- $rows[$key]['mod_rq'] = $value['mod_rq']=='1900-01-01 00:00:00' ? '' :$value['mod_rq'];
- $rows[$key]['sczl_sbmc'] = trim($sb[$value['sczl_jtbh']]);
- $rows[$key]['sczl_设备运行工时'] = $value['sczl_设备运行工时'] == 0 ? '' : $value['sczl_设备运行工时'];
- }
- $data = [
- 'total' => $total,
- 'rows' => $rows,
- ];
- $this->success('成功',$data);
- }
- /**
- * 获取机台生产日报表附加信息
- * @ApiMethod (GET)
- * @param string $UniqId UniqId
- */
- public function getInfo()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (isset($req['UniqId']) && !empty($req['UniqId'])){
- $UniqId = $req['UniqId'];
- }else{
- $this->error('参数错误');
- }
- $rows = db('db_sczladd')->alias('d')
- ->field('LEFT(d.sczl_rq, 10) as sczl_rq, d.sczl_jtbh, rtrim(g.设备名称) as 设备名称, d.sczl_bzdh, d.sczl_设备运行工时, d.sczl_desc')
- ->join('设备_基本资料 g', 'd.sczl_jtbh=g.设备编号')
- ->where('d.UniqId',$UniqId)
- ->select();
- $this->success('成功',$rows);
- }
- /**
- * 查询机台
- * @ApiMethod (GET)
- */
- public function getJtbh()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (isset($req['sys_mc'])){
- $sys_mc = $req['sys_mc'];
- }else{
- $this->error('参数错误');
- }
- $rows = db('设备_基本资料')
- ->field('rtrim(使用部门) as 使用部门, rtrim(设备编号) as jtbh, rtrim(设备名称) as 设备名称')
- ->where('设备编号|设备名称','like','%'.$sys_mc.'%')
- ->order('设备编号 asc')
- ->select();
- $arr = array_unique(array_column($rows, '使用部门'));
- sort($arr);
- $data = [];
- $data['使用部门'] = [];
- foreach ($arr as $value) {
- $data[$value] = [];
- foreach ($rows as $row){
- if($row['使用部门']==$value){
- if (!in_array($row['使用部门'], $data['使用部门'])) {
- array_push($data['使用部门'],$row['使用部门']);
- }
- unset($row['使用部门']);
- array_push($data[$value], $row);
- }
- }
- }
- $this->success('成功',[$data]);
- }
- /**
- * 新增
- * @ApiMethod (POST)
- * @param string
- */
- public function add()
- {
- if(!$this->request->isPost()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- $arr = [
- 'sys_id','sczl_rq','sczl_jtbh',
- 'sczl_bzdh','sczl_设备运行工时',
- 'sczl_desc'
- ];
- $data = [];
- foreach ($arr as $key => $value){
- if (!(isset($req[$value]))){
- $this->error('参数错误',$value,$key+1);
- }
- $data[$value] = $req[$value];
- }
- $data['sys_rq'] = date('Y-m-d H:i:s');
- //查询UniqId
- $UniqId = db('db_sczladd')->max('UniqId');
- $data['UniqId'] = $UniqId < 10000000 ? 10000000 : $UniqId + 1;
- //开启事务
- db()->startTrans();
- try{
- $sql = db('db_sczladd')->fetchSql(true)->insert($data);
- $bool = db()->query($sql);
- // 提交事务
- db()->commit();
- } catch (\Exception $e) {
- // 回滚事务
- db()->rollback();
- $this->error($e->getMessage());
- }
- if($bool===false) $this->error('失败');
- $this->success('成功');
- }
- /**
- * 新增
- * @ApiMethod (POST)
- * @param string
- */
- public function edit()
- {
- if(!$this->request->isPost()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- $arr = [
- 'sys_id','sczl_rq','sczl_jtbh',
- 'sczl_bzdh','sczl_设备运行工时',
- 'sczl_desc'
- ];
- $data = [];
- foreach ($arr as $key => $value){
- if (!(isset($req[$value]))){
- continue;
- }
- $data[$value] = $req[$value];
- }
- if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
- $this->error('参数错误','UniqId',100);
- }
- $data['mod_rq'] = date('Y-m-d H:i:s');
- //开启事务
- db()->startTrans();
- try{
- $sql = db('db_sczladd')->where('UniqId',$req['UniqId'])->fetchSql(true)->update($data);
- $bool = db()->query($sql);
- // 提交事务
- db()->commit();
- } catch (\Exception $e) {
- // 回滚事务
- db()->rollback();
- $this->error($e->getMessage());
- }
- if($bool===false) $this->error('失败');
- $this->success('成功');
- }
- /**
- * 修改
- * @ApiMethod (POST)
- * @param string 'UniqId'
- */
- public function del()
- {
- if(!$this->request->isPost()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
- $this->error('参数错误','UniqId',100);
- }
- //开启事务
- db()->startTrans();
- try{
- $bool = db('db_sczladd')->where('UniqId',$req['UniqId'])->delete();
- // 提交事务
- db()->commit();
- } catch (\Exception $e) {
- // 回滚事务
- db()->rollback();
- $this->error($e->getMessage());
- }
- if($bool===false) $this->error('失败');
- $this->success('成功');
- }
- /**
- * excel导出
- * @ApiMethod (GET)
- * @param string file_name
- * @param string date
- * @param string sys_id
- * @param array fields
- */
- public function export(){
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- $where = [];
- if (!isset($req['date']) || empty($req['date'])) $this->error('参数缺失');
- if (!(isset($req['fields']) && count($req['fields'])!=0)){
- $this->error('参数错误','fields',100);
- }
- if(strpos($req['date'],'-')){
- $where = ['a.sys_rq'=>['like',$req['date'].'%']];
- }else{
- $where = ['a.sczl_gdbh'=>$req['date']];
- }
- if (isset($req['sys_id']) && !empty($req['sys_id'])){
- $where['a.sys_id'] = ['LIKE',$req['sys_id'].'%'];
- }
- $field = '';
- $arr = [
- 'sczl_rq' =>['生产日期','LEFT(a.sczl_rq, 10) as sczl_rq'],
- 'sczl_jtbh' =>['设备编号','a.sczl_jtbh'],
- '设备名称' =>['设备名称','rtrim(b.设备名称) as 设备名称'],
- 'sczl_bzdh' =>['班组','rtrim(a.sczl_bzdh) as sczl_bzdh'],
- 'sczl_设备运行工时' =>['排班工时','a.sczl_设备运行工时'],
- 'sczl_desc' =>['备注','rtrim(a.sczl_desc) as sczl_desc']
- ];
- $data[0] = [];
- foreach ($req['fields'] as $k=>$v){
- if(array_key_exists($v,$arr)){
- if ($k==0){
- $field .= $arr[$v][1];
- }else{
- $field .= ','.$arr[$v][1];
- }
- array_push($data[0],$arr[$v][0]);
- }
- }
- $rows = db('db_sczladd')->alias('a')
- ->join('设备_基本资料 b','a.sczl_jtbh = b.设备编号','left')
- ->field($field)
- ->where($where)
- ->order('a.UniqId asc')
- ->select();
- foreach ($rows as $key=>$value) {
- if (isset($value['sczl_设备运行工时'])) $value['sczl_设备运行工时'] = floatval($value['sczl_设备运行工时']);
- $subArray = [];
- foreach ($req['fields'] as $k=>$v){
- if(array_key_exists($v,$arr)){
- if($value[$v]){
- array_push($subArray,$value[$v]);
- }else{
- array_push($subArray,'');
- }
- }
- }
- array_push($data,$subArray);
- }
- $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
- }
- }
|