interval day to second和interval…

本文详细介绍了Oracle数据库中时间间隔类型INTERVAL YEAR TO MONTH及INTERVAL DAY TO SECOND的使用方法,包括数据类型定义、示例及相关的函数介绍。
在9i 版本以前,Oracle 没有内置的方式来记录时间的流逝。DATE型数据用来记录单独的时间点;但是要表达一个时间量(也就是一个间隔), 数据库的设计者就必须把时间间隔转换成原始单位秒,然后用一个NUMBER列来保存它。

虽然NUMBER这个数据类型可以以秒为单位准确地表示时间,但是它使得时间的计算变得很困难。比如,60秒是1分钟,60分钟是1个小时,24个小时等于1天——这些数字在以十进制为基础的数字系统中都是非常蹩脚的。

在Oracle 9i中,按照 SQL 99标准,增加了时间间隔型数据 INTERVAL YEAR TO MONTH 和 INTERVAL DAY TO SECOND,它们和其他几种数据类型一起使得对时间的处理更加准确。TIMESTAMP、TIMESTAMP WITH TIME ZONE和TIMESTAMP WITH LOCAL TIME ZONE等数据类型都把时间的表达精确到了若干分之一秒,而且后面两种还解决了地理位置造成的时间变化。

在SQL和PL/SQL中,你都可以用时间间隔型数据,它们都是用同一种方式规定的:

INTERVAL YEAR[(year_precision)] TO MONTH
INTERVAL DAY[(day_precision)] TO SECOND[(fractional_seconds_precision)]
对于精确数值,规定有缺省值:年和日是两位数,若干分之一秒是六位数。

时间间隔的大小由INTERVAL来表示,后面紧接一个放在单引号中的表达式,以及用来解释该表达式的文字。用YEAR TO MONTH表示时间间隔大小时要在年和月之间用一个连字符(-) 连接。而DAY TO SECOND表示时间间隔大小时要在日和时间之间用一个空格连接。
举个例子来说,下面是2年6个月的时间间隔的表示方法:
INTERVAL "2-6" YEAR TO MONTH

下面的例子表示3天12个小时30分钟6.7秒:
INTERVAL "3 12:30:06.7" DAY TO SECOND(1)

时间间隔可以为正,也可以为负。它们可以从各种TIMESTAMP数据类型中加上或者减去,从而得到一个新的TIMESTAMP数据类型。它们之间也可以做加减运算得到新的时间间隔。

列表A说明了怎样创建一个表格来记录一个事件的开始时间和持续时间,如实验等。数据被收集以后,SQL中内置的摘要函数不需要与原始单位秒进行相互转换,就可以报告总的持续时间和平均持续时间。

列表A

CREATE TABLE experiment
(experiment_id NUMBER(9),
experiment_desc VARCHAR2(80),
experiment_start TIMESTAMP,
experiment_duration INTERVAL DAY(1) TO SECOND(4)
);

Table created.

INSERT INTO experiment
VALUES (
1, "Busted urban myth", "01-JUN-2006 02:00:00 PM",
INTERVAL "1 2:31:15.1250" DAY(1) TO SECOND(4)
);

1 row created.

col experiment_desc format a40
col experiment_start format a30
col experiment_duration format a20

SELECT * FROM experiment;

EXPERIMENT_ID EXPERIMENT_DESC ------------- ---------------------------------------- EXPERIMENT_START EXPERIMENT_DURATION ------------------------------ -------------------- 1 Busted urban myth 01-JUN-06 02.00.00.000000 PM 1 02:31:15.1250
-- Now compute the experiment"s ending time

SELECT experiment_id, experiment_start,
experiment_start experiment_durationexperiment_end
FROM experiment;

EXPERIMENT_ID EXPERIMENT_START ------------- ------------------------------ EXPERIMENT_END --------------------------------------------------------------------------- 1 01-JUN-06 02.00.00.000000 PM 02-JUN-06 04.31.15.125000000 PM
但遗憾的是, TO_CHAR函数中没有包括任何能够映射到各个时间间隔数据类型片段的格式模型。但是,你可以用新的EXTRACT函数来提取和合并这些片段。格式如下:

EXTRACT(timepart FROM interval_expression)
列表B给出了一个运用这种方法的例子。

列表B

SELECT EXTRACT(DAY FROM experiment_duration) ||
" days, " || EXTRACT (HOUR FROM experiment_duration) ||
" hours, " || EXTRACT (MINUTE FROM experiment_duration) ||
" minutes" Duration
FROM experiment;

DURATION
--------------------------------------------------------------------------------
1 days, 2 hours, 31 minutes
首先,从experiment_duration列中将天数提取出来,文字“Days”是与之相联的。对于实验持续时间中的小时和分钟部分,操作与上述方法一样。


 

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

SQL> create table train
  (
    train_no         varchar2(32),
    station_name     varchar2(32),
    arrival_time     date,
    acc_run_duration interval day to second
  )
  ;

Table created
 
insert into train t values('K279','beijing',to_date('10:00','hh24:mi'),interval '0' second);

insert into train t values('K279','shijiazhuang',to_date('12:00','hh24:mi'),interval '2' hour);

select * from train t

    TRAIN_NO STATION_NAME ARRIVAL_TIME ACC_RUN_DURATION
K279 beijing 2009-3-1 10:00:00 +00 00:00:00.000000
K279 shijiazhuang 2009-3-1 12:00:00 +00 02:00:00.000000

select t.*,extract(hour from t.acc_run_duration) from train t

    TRAIN_NO STATION_NAME ARRIVAL_TIME ACC_RUN_DURATION EXTRACT(HOURFROMT.ACC_RUN_DURA
K279 beijing 2009-3-1 10:00:00 +00 00:00:00.000000 0
K279 shijiazhuang 2009-3-1 12:00:00 +00 02:00:00.000000 2
Over。这里有3点,1。interval day to second是oracle的内部类型;2,interval '2' hour是一个常量2小时的表示法;3,extract函数抽取该类型的分解值

Oracle语法:
INTERVAL '{ integer | integer time_expr | time_expr }'
{ { DAY | HOUR | MINUTE } [ ( leading_precision ) ]
| SECOND [ ( leading_precision [, fractional_seconds_precision ] ) ] }
[ TO { DAY | HOUR | MINUTE | SECOND [ (fractional_seconds_precision) ] } ]

leading_precision值的范围是0到9, 默认是2. time_expr的格式为:HH[:MI[:SS[.n]]] or MI[:SS[.n]] or SS[.n], n表示微秒.
该类型与INTERVAL YEAR TO MONTH有很多相似的地方,建议先看INTERVAL YEAR TO MONTH再看该文.

范围值:
HOUR:    0 to 23
MINUTE: 0 to 59
SECOND: 0 to 59.999999999

eg:
INTERVAL '4 5:12:10.222' DAY TO SECOND(3)
表示: 4天5小时12分10.222秒

INTERVAL '4 5:12' DAY TO MINUTE
表示: 4天5小时12分

INTERVAL '400 5' DAY(3) TO HOUR
表示: 400天5小时, 400为3为精度,所以"DAY(3)", 注意默认值为2.

INTERVAL '400' DAY(3)
表示: 400天

INTERVAL '11:12:10.2222222' HOUR TO SECOND(7)
表示: 11小时12分10.2222222秒

INTERVAL '11:20' HOUR TO MINUTE
表示: 11小时20分

INTERVAL '10' HOUR
表示: 10小时

INTERVAL '10:22' MINUTE TO SECOND
表示: 10分22秒

INTERVAL '10' MINUTE
表示: 10分

INTERVAL '4' DAY
表示: 4天

INTERVAL '25' HOUR
表示: 25小时

INTERVAL '40' MINUTE
表示: 40分

INTERVAL '120' HOUR(3)
表示: 120小时

INTERVAL '30.12345' SECOND(2,4)     
表示: 30.1235秒, 因为该地方秒的后面精度设置为4, 要进行四舍五入.

INTERVAL '20' DAY - INTERVAL '240' HOUR = INTERVAL '10-0' DAY TO SECOND
表示: 20天 - 240小时 = 10天0秒

 

Oracle语法:
INTERVAL 'integer [- integer]' {YEAR | MONTH} [(precision)][TO {YEAR | MONTH}]

该数据类型常用来表示一段时间差, 注意时间差只精确到年和月. precision为年或月的精确域, 有效范围是0到9, 默认值为2.

eg:
INTERVAL '123-2' YEAR(3) TO MONTH      
表示: 123年2个月, "YEAR(3)" 表示年的精度为3, 可见"123"刚好为3为有效数值, 如果该处YEAR(n), n<3就会出错, 注意默认是2.

INTERVAL '123' YEAR(3)
表示: 123年0个月

INTERVAL '300' MONTH(3)
表示: 300个月, 注意该处MONTH的精度是3啊.

INTERVAL '4' YEAR      
表示: 4年, 同 INTERVAL '4-0' YEAR TO MONTH 是一样的

INTERVAL '50' MONTH      
表示: 50个月, 同 INTERVAL '4-2' YEAR TO MONTH 是一样

INTERVAL '123' YEAR      
表示: 该处表示有错误, 123精度是3了, 但系统默认是2, 所以该处应该写成 INTERVAL '123' YEAR(3) 或"3"改成大于3小于等于9的数值都可以的

INTERVAL '5-3' YEAR TO MONTH + INTERVAL '20' MONTH =
INTERVAL '6-11' YEAR TO MONTH
表示: 5年3个月 + 20个月 = 6年11个月

与该类型相关的函数:
NUMTODSINTERVAL(n, 'interval_unit')
将n转换成interval_unit所指定的值, interval_unit可以为: DAY, HOUR, MINUTE, SECOND
注意该函数不可以转换成YEAR和MONTH的.

NUMTOYMINTERVAL(n, 'interval_unit')
interval_unit可以为: YEAR, MONTH

eg: (Oracle Version 9204, RedHat Linux 9.0)
SQL> select numtodsinterval(100,'DAY') from dual;

NUMTODSINTERVAL(100,'DAY')                                                                                                         
---------------------------------------------------------------------------       
+000000100 00:00:00.000000000                                                                                                   

SQL> c/DAY/SECOND
  1* select numtodsinterval(100,'SECOND') from dual
SQL> /

NUMTODSINTERVAL(100,'SECOND')                                                                                                   
---------------------------------------------------------------------------       
+000000000 00:01:40.000000000                                                                                                   

SQL> c/SECOND/MINUTE
  1* select numtodsinterval(100,'MINUTE') from dual
SQL> /

NUMTODSINTERVAL(100,'MINUTE')                                                                                                   
---------------------------------------------------------------------------       
+000000000 01:40:00.000000000                                                                                                   

SQL> c/MINUTE/HOUR
  1* select numtodsinterval(100,'HOUR') from dual
SQL> /

NUMTODSINTERVAL(100,'HOUR')                                                                                                       
---------------------------------------------------------------------------       
+000000004 04:00:00.000000000                                                                                                   

SQL> c/HOUR/YEAR
  1* select numtodsinterval(100,'YEAR') from dual
SQL> /
select numtodsinterval(100,'YEAR') from dual
                                                    *
ERROR at line 1:
ORA-01760: illegal argument for function

SQL> select numtoyminterval(100,'year') from dual;

NUMTOYMINTERVAL(100,'YEAR')                                                                                                       
---------------------------------------------------------------------------       
+000000100-00                                                                                                                                   

SQL> c/year/month
  1* select numtoyminterval(100,'month') from dual
SQL> /

NUMTOYMINTERVAL(100,'MONTH')                                                                                                     
---------------------------------------------------------------------------       
+000000008-04                                                                                                                                   


时间的计算:
SQL> select to_date('1999-12-12','yyyy-mm-dd') - to_date('1999-12-01','yyyy-mm-dd') from dual;

TO_DATE('1999-12-12','YYYY-MM-DD')-TO_DATE('1999-12-01','YYYY-MM-DD')                   
---------------------------------------------------------------------                   
                                                                                                                                    11                   
-- 可以相减的结果为天.

SQL> c/1999-12-12/1999-01-12
  1* select to_date('1999-01-12','yyyy-mm-dd') - to_date('1999-12-01','yyyy-mm-dd') from dual
SQL> /

TO_DATE('1999-01-12','YYYY-MM-DD')-TO_DATE('1999-12-01','YYYY-MM-DD')                   
---------------------------------------------------------------------                   
                                                                                                                                -323                   
-- 也可以为负数的

SQL> c/1999-01-12/2999-10-12
  1* select to_date('2999-10-12','yyyy-mm-dd') - to_date('1999-12-01','yyyy-mm-dd') from dual
SQL> /

TO_DATE('2999-10-12','YYYY-MM-DD')-TO_DATE('1999-12-01','YYYY-MM-DD')                   
---------------------------------------------------------------------                   
                                                                                                                            365193                   

下面看看INTERVAL YEAR TO MONTH怎么用.
SQL> create table bb(a date, b date, c interval year(9) to month);

Table created.

SQL> desc bb;
  Name                                                                          Null?      Type
  ----------------------------------------- -------- ----------------------------
                                                                                                  DATE
                                                                                                  DATE
                                                                                                  INTERVAL YEAR(9) TO MONTH

SQL> insert into bb values(to_date('1985-12-12', 'yyyy-mm-dd'), to_date('1984-12-01','yyyy-mm-dd'), null)

1 row created.

SQL> select * from bb;

                                                                                                                                                   
--------- ---------                                                                                                                       
                                                                                                                                                         
---------------------------------------------------------------------------       
12-DEC-85 01-DEC-84                                                                                                                       
                                                                                                                                                             
                                                                                                                                                             
SQL> update bb set c = numtoyminterval(a-b, 'year');

1 row updated.

SQL> select * from bb;

                                                                                                                                                   
--------- ---------                                                                                                                       
                                                                                                                                                         
---------------------------------------------------------------------------       
12-DEC-85 01-DEC-84                                                                                                                       
+000000376-00                                                                                                                                   
                                                                                                                                                             
-- 直接将相减的天变成年了, 因为我指定变成年的
SQL> select a-b, c from bb;

            A-B                                                                                                                                         
----------                                                                                                                                         
                                                                                                                                                         
---------------------------------------------------------------------------       
            376                                                                                                                                         
+000000376-00                                                                                                                                   
                                                                                                                                                             

SQL> insert into bb values(null,null,numtoyminterval(376,'month'));

1 row created.

SQL> select * from bb;

                                                                                                                                               
--------- ---------      --------------------------------------------       
12-DEC-85 01-DEC-84      +000000376-00                                                                                                                                   
                                                +000000031-04                                                                                 

SQL> insert into bb values ( null,null, numtoyminterval(999999999,'year'));

1 row created.

SQL> select * from bb;

                                                                                                   
---------    ---------        ---------------------------------------------------------------------       
12-DEC-85    01-DEC-84    +000000376-00                                                                                                                                   
                                                  +000000031-04
                                                  +999999999-00                                                                                                                               
 
========================
今天来添加点新的东西![2008-07-26] 这部分东东来源: http://www.oraclefans.cn/forum/showblog.jsp?rootid=139

INTERVAL YEAR TO MONTH类型2个TIMESTAMP类型的时间差别。内部类型是182,长度是5。其中4个字节存储年份差异,存储的时候在差异上加了一个0X80000000的偏移量。一个字节存储月份的差异,这个差异加了60的偏移量。

SQL> ALTER TABLE TestTimeStamp ADD E INTERVAL YEAR TO MONTH;
SQL> update testTimeStamp set e=(select interval '5' year + interval '10' month year  from dual);

已更新3行。

SQL> commit;

提交完成。

SQL> select dump(e,16) from testTimeStamp;

DUMP(E,16)

---------------------------------------------

Typ=182 Len=5: 80,0,0,5,46

Typ=182 Len=5: 80,0,0,5,46

Typ=182 Len=5: 80,0,0,5,46

年:0X80000005-0X80000000=5

月:0x46-60=10
 

 

 

 


Oracle语法:
INTERVAL '{ integer | integer time_expr | time_expr }'
{ { DAY | HOUR | MINUTE } [ ( leading_precision ) ]
| SECOND [ ( leading_precision [, fractional_seconds_precision ] ) ] }
[ TO { DAY | HOUR | MINUTE | SECOND [ (fractional_seconds_precision) ] } ]

leading_precision值的范围是0到9, 默认是2. time_expr的格式为:HH[:MI[:SS[.n]]] or MI[:SS[.n]] or SS[.n], n表示微秒.
该类型与INTERVAL YEAR TO MONTH有很多相似的地方,建议先看INTERVAL YEAR TO MONTH再看该文.

范围值:
HOUR:      0 to 23
MINUTE: 0 to 59
SECOND: 0 to 59.999999999

eg:
INTERVAL '4 5:12:10.222' DAY TO SECOND(3)
表示: 4天5小时12分10.222秒

INTERVAL '4 5:12' DAY TO MINUTE
表示: 4天5小时12分

INTERVAL '400 5' DAY(3) TO HOUR
表示: 400天5小时, 400为3为精度,所以"DAY(3)", 注意默认值为2.

INTERVAL '400' DAY(3)
表示: 400天

INTERVAL '11:12:10.2222222' HOUR TO SECOND(7)
表示: 11小时12分10.2222222秒

INTERVAL '11:20' HOUR TO MINUTE
表示: 11小时20分

INTERVAL '10' HOUR
表示: 10小时

INTERVAL '10:22' MINUTE TO SECOND
表示: 10分22秒

INTERVAL '10' MINUTE
表示: 10分

INTERVAL '4' DAY
表示: 4天

INTERVAL '25' HOUR
表示: 25小时

INTERVAL '40' MINUTE
表示: 40分

INTERVAL '120' HOUR(3)
表示: 120小时

INTERVAL '30.12345' SECOND(2,4)      
表示: 30.1235秒, 因为该地方秒的后面精度设置为4, 要进行四舍五入.

INTERVAL '20' DAY - INTERVAL '240' HOUR = INTERVAL '10-0' DAY TO SECOND
表示: 20天 - 240小时 = 10天0秒

==================
该部分来源: http://www.oraclefans.cn/forum/showblog.jsp?rootid=140
INTERVAL DAY TO SECOND类型存储两个TIMESTAMP之间的时间差异,用日期、小时、分钟、秒钟形式表示。该数据类型的内部代码是183,长度位11字节:

              4个字节表示天数(增加0X80000000偏移量)
              小时、分钟、秒钟各用一个字节表示(增加60偏移量)
              4个字节表示秒钟的小时差异(增加0X80000000偏移量)

以下是一个例子:

SQL> alter table testTimeStamp add f interval day to second ;

表已更改。

SQL> update testTimeStamp set f=(select interval '5' day + interval '10' second from dual);

已更新3行。

SQL> commit;

提交完成。

SQL> select dump(f,16) from testTimeStamp;

DUMP(F,16)

-------------------------------------------------------------------------------- 

Typ=183 Len=11: 80,0,0,5,3c,3c,46,80,0,0,0
Typ=183 Len=11: 80,0,0,5,3c,3c,46,80,0,0,0
Typ=183 Len=11: 80,0,0,5,3c,3c,46,80,0,0,0

日期:0X80000005-0X80000000=5

小时:60-60=0
分钟:60-60=0
秒钟:70-60=10
秒钟小数部分:0X80000000-0X80000000=0

  网友评论
  本站网友时间:2006-12-11 10:09:10 IP地址:218.249.3.★
CREATE TABLE tint_test (
msg        VARCHAR2(25),
start_date TIMESTAMP WITH TIME ZONE,
end_date   TIMESTAMP WITH TIME ZONE,
duration_1 INTERVAL DAY(5) TO SECOND,
duration_2 INTERVAL YEAR TO MONTH);

INSERT INTO tint_test
(msg, start_date, end_date)
VALUES
('my plane ride',
timestamp'2004-08-08 17:02:32.212 US/Eastern',
timestamp'2004-08-08 19:10:12.235 US/Pacific');

INSERT INTO tint_test
(msg, start_date, end_date)
VALUES
('my vacation',
timestamp'2004-07-27 06:00:00',
timestamp'2004-08-04 18:00:00');

INSERT INTO tint_test
(msg, start_date, end_date)
VALUES
('my life',
timestamp'1950-01-15 02:00:00',
current_timestamp);

SELECT FROM tint_test;

UPDATE tint_test
SET duration_1 (end_date start_date) DAY(5) TO SECOND,
duration_2 (end_date start_date) YEAR TO MONTH;

SELECT msg, duration_1, duration_2 FROM tint_test;

SELECT t.*, end_date start_date FROM tint_test t;

<template> <e-layout> <!-- 搜索 --> <template v-slot:search-bar> <el-form :inline="true" :model="searchForm" @keyup.enter.native="search" @submit.native.prevent> <el-form-item> <el-date-picker v-model="searchForm.date" type="datetimerange" :picker-options="$constants.datePickerOptions" :range-separator="$t('Common.searchForm.datePicker.separator')" :start-placeholder="$t('Common.searchForm.datePicker.startTime')" :end-placeholder="$t('Common.searchForm.datePicker.endTime')" value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:00', '23:59:59']" align="right"> </el-date-picker> </el-form-item> <el-form-item> <el-input v-model="searchForm.ani" :placeholder="$t('OutboundCallJob.searchForm.ani')" clearable></el-input> <!-- 主叫号码 --> </el-form-item> <el-form-item> <el-input v-model="searchForm.dnis" :placeholder="$t('OutboundCallJob.searchForm.dnis')" clearable></el-input> <!-- 被叫号码 --> </el-form-item> <el-form-item> <template> <el-select v-model="searchForm.status" :placeholder="$t('OutboundCallJob.searchForm.status')" clearable> <el-option :label="$t('Common.enum.status.enabled')" value="enabled"></el-option> <!-- 启用 --> <el-option :label="$t('Common.enum.status.disabled')" value="disabled"></el-option> <!-- 停用 --> </el-select> </template> </el-form-item> <el-form-item> <el-button type="primary" size="mini" @click="search" :loading="loading" icon="el-icon-search">{{ $t('Common.button.search') }}</el-button> <!-- 搜索 --> </el-form-item> </el-form> </template> <!-- 按钮 --> <template v-slot:btn-bar> <el-row> <el-button type="success" size="mini" @click="toAdd" icon="el-icon-plus" v-has-permission="'monitor:outbound-call-task:addCycle'">{{ $t('Common.button.new') }}</el-button> <!-- 新建 --> </el-row> </template> <!-- 表格 --> <template v-slot:main-content> <el-table :data="tableData" height="100%" highlight-current-row ref="clearTableData"> <template slot="empty"> <el-empty :description="$t('Common.table.emptyDescription')" /> <!-- 暂无数据 --> </template> <el-table-column type="index" :label="$t('Common.table.index')" width="50" fixed="left" align='center'/> <!-- 序号 --> <el-table-column prop="createTime" :label="$t('OutboundCallJob.table.createTime')" min-width="150" align='center'/> <!-- 创建时间 --> <el-table-column prop="userName" :label="$t('OutboundCallJob.table.userName')" min-width="120" align='left'/> <!-- 创建人 --> <el-table-column prop="ani" :label="$t('OutboundCallJob.table.ani')" min-width="150" align='left'/> <!-- 主叫号码 --> <el-table-column prop="dnis" :label="$t('OutboundCallJob.table.dnis')" min-width="150" align='left'/> <!-- 被叫号码 --> <el-table-column prop="status" :label="$t('OutboundCallJob.table.status')" min-width="100" align='center'> <!-- 状态 --> <template v-slot="scope"> <el-tag v-if="scope.row.status === 'disabled'" type="info" effect="plain">{{ $t('Common.enum.status.disabled') }}</el-tag> <!-- 停用 --> <el-tag v-else-if="scope.row.status === 'enabled'" type="success" effect="plain">{{ $t('Common.enum.status.enabled') }}</el-tag> <!-- 启用 --> </template> </el-table-column> <el-table-column prop="text" :label="$t('OutboundCallJob.table.text')" show-overflow-tooltip min-width="300" align='left'/> <!-- 文本内容 --> <el-table-column prop="cron" :label="$t('OutboundCallJob.table.cron')" min-width="100" align='left'/> <!-- cron表达式 --> <el-table-column :label="$t('Common.table.operate')" min-width="200" class-name="small-padding fixed-width" fixed="right" > <!-- 操作 --> <template v-slot="scope"> <span> <el-button type='text' class="row-button" @click.stop="toEdit(scope.row)" v-has-permission="'outbound:cycle:update'"><i class="el-icon-edit"> </i>{{ $t('Common.button.edit') }}</el-button> <!-- 编辑 --> <el-button type="text" class="row-button" @click.stop="toDelete(scope.row)" v-has-permission="'outbound:cycle:delete'"><i class="el-icon-delete"> </i>{{ $t('Common.button.delete') }}</el-button> <!-- 删除 --> <el-button type="text" class="row-button" @click.stop="viewDetail(scope.row)"><i class="el-icon-document"> </i>{{ $t('OutboundCallTask.detailLabel') }}</el-button> <!-- 执行详情 --> </span> </template> </el-table-column> </el-table> </template> <!--分页工具条--> <template v-slot:pagination-bar> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pagination.currentPage" :page-size.sync="pagination.pageSize" :page-sizes="pagination.pageSizes" :total="pagination.total" layout="prev, pager, next, total, sizes, jumper" background > </el-pagination> </template> <!-- 新建弹窗 --> <el-dialog :title="dialogTitle" :visible.sync="isShowDialog" :close-on-click-modal="false" width="600px"> <el-form ref="addForm" label-width="90px" :model="form" :rules="addFormRules" > <!-- 执行周期 --> <el-form-item :label="$t('OutboundCallJob.form.label.scheduleInterval')" prop="scheduleInterval"> <!-- 执行周期 --> <cron-picker :cron="form.cron" @change="onChange" :locale="en" /> </el-form-item> <!-- Cron表达式 --> <el-form-item :label="$t('OutboundCallJob.form.label.cron')" prop="cron" style="margin-top: -15px"> <!-- Cron表达式 --> <el-input v-model="form.cron" disabled></el-input> </el-form-item> <!-- 主叫号码 --> <el-form-item :label="$t('OutboundCallJob.form.label.ani')" prop="ani"> <!-- 主叫号码 --> <el-input v-model="form.ani" :placeholder="$t('OutboundCallJob.form.placeholder.ani')"></el-input> </el-form-item> <!-- 被叫号码 --> <el-form-item :label="$t('OutboundCallJob.form.label.dnis')" prop="dnis"> <!-- 被叫号码 --> <div style="display: flex; justify-content: space-between"> <el-input v-model="form.dnis" :placeholder="$t('OutboundCallJob.form.placeholder.dnis')" style="margin-right: 10px"></el-input> <el-button type="success" icon="el-icon-share" @click.prevent="fillMyMobile">{{ $t('Common.button.copy') }}</el-button> <!-- 我的号码 --> </div> </el-form-item> <!-- 文本内容 --> <el-form-item :label="$t('OutboundCallJob.form.label.text')" prop="text" :autosize="{ minRows: 4, maxRows: 8}"> <el-input type="textarea" v-model="form.text" :placeholder="$t('OutboundCallJob.form.placeholder.text')"></el-input> </el-form-item> <!-- 状态 --> <el-form-item :label="$t('OutboundCallJob.form.label.status')"> <template> <el-radio-group v-model="form.status"> <el-radio label="enabled">{{ $t('Common.enum.status.enabled') }}</el-radio> <!-- 启用 --> <el-radio label="disabled">{{ $t('Common.enum.status.disabled') }}</el-radio> <!-- 停用 --> </el-radio-group> </template> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="isShowDialog = false" icon="el-icon-close">{{ $t('Common.dialog.button.cancel') }}</el-button> <!-- 取 消 --> <el-button type="primary" @click="createCallJob" icon="el-icon-check">{{ $t('Common.dialog.button.save') }}</el-button> <!-- 保 存 --> </div> </el-dialog> </e-layout> </template> <script> import CronPicker from 'cron-picker-vue' import expiringStorage from '@/utils/expiringStorage' export default { components: { CronPicker }, data () { return { tableData: [], dialogTitle: '', isShowDialog: false, searchForm: { status: '', result: '', ani: '', dnis: '', date: [] }, form: { scheduleInterval: 'day', // 调度周期 cron: '', // Cron 表达式 ani: '', dnis: '', status: '', text: '' }, // 表单验证 addFormRules: { cron: [ { required: true, message: this.$t('OutboundCallJob.rules.addFormRules.cron.required'), trigger: 'blur' } // 执行周期不能为空 ], dnis: [ { required: true, message: this.$t('OutboundCallJob.rules.addFormRules.dnis.required'), trigger: 'blur' } // 被叫号码不能为空 ], text: [ { required: true, message: this.$t('OutboundCallJob.rules.addFormRules.text.required'), trigger: 'blur' } // 文本内容不能为空 ] }, loading: false, pagination: { currentPage: 1, pageSize: 20, total: 0, pageSizes: [20, 40, 60, 80, 100, 200, 500] } } }, computed: { cronLocale () { return { everyText: this.$t('CronPicker.every'), minutesText: this.$t('CronPicker.minutes'), hoursText: this.$t('CronPicker.hours'), daysText: this.$t('CronPicker.days'), weeksText: this.$t('CronPicker.weeks'), monthsText: this.$t('CronPicker.months'), yearsText: this.$t('CronPicker.years'), specificWeekdayText: this.$t('CronPicker.specificWeekday'), specificDateText: this.$t('CronPicker.specificDate'), specificTimeText: this.$t('CronPicker.specificTime'), specificMinuteText: this.$t('CronPicker.specificMinute'), specificHourText: this.$t('CronPicker.specificHour'), specificDayText: this.$t('CronPicker.specificDay'), specificMonthText: this.$t('CronPicker.specificMonth'), specificYearText: this.$t('CronPicker.specificYear'), atText: this.$t('CronPicker.at'), andText: this.$t('CronPicker.and'), onText: this.$t('CronPicker.on'), ofText: this.$t('CronPicker.of'), theText: this.$t('CronPicker.the'), lastText: this.$t('CronPicker.last'), firstText: this.$t('CronPicker.first'), secondText: this.$t('CronPicker.second'), thirdText: this.$t('CronPicker.third'), fourthText: this.$t('CronPicker.fourth'), fifthText: this.$t('CronPicker.fifth'), sixthText: this.$t('CronPicker.sixth'), seventhText: this.$t('CronPicker.seventh'), eighthText: this.$t('CronPicker.eighth'), ninthText: this.$t('CronPicker.ninth'), tenthText: this.$t('CronPicker.tenth'), eleventhText: this.$t('CronPicker.eleventh'), twelfthText: this.$t('CronPicker.twelfth'), thirteenthText: this.$t('CronPicker.thirteenth'), fourteenthText: this.$t('CronPicker.fourteenth'), fifteenthText: this.$t('CronPicker.fifteenth'), sixteenthText: this.$t('CronPicker.sixteenth'), seventeenthText: this.$t('CronPicker.seventeenth'), eighteenthText: this.$t('CronPicker.eighteenth'), nineteenthText: this.$t('CronPicker.nineteenth'), twentiethText: this.$t('CronPicker.twentieth'), twentyFirstText: this.$t('CronPicker.twentyFirst'), twentySecondText: this.$t('CronPicker.twentySecond'), twentyThirdText: this.$t('CronPicker.twentyThird'), twentyFourthText: this.$t('CronPicker.twentyFourth'), twentyFifthText: this.$t('CronPicker.twentyFifth'), twentySixthText: this.$t('CronPicker.twentySixth'), twentySeventhText: this.$t('CronPicker.twentySeventh'), twentyEighthText: this.$t('CronPicker.twentyEighth'), twentyNinthText: this.$t('CronPicker.twentyNinth'), thirtiethText: this.$t('CronPicker.thirtieth'), thirtyFirstText: this.$t('CronPicker.thirtyFirst'), sundayText: this.$t('CronPicker.sunday'), mondayText: this.$t('CronPicker.monday'), tuesdayText: this.$t('CronPicker.tuesday'), wednesdayText: this.$t('CronPicker.wednesday'), thursdayText: this.$t('CronPicker.thursday'), fridayText: this.$t('CronPicker.friday'), saturdayText: this.$t('CronPicker.saturday') } } }, created () { this.search() }, methods: { toAdd () { this.reset() this.dialogTitle = this.$t('OutboundCallJob.dialog.title.new') // 新建 this.isShowDialog = true }, toEdit (row) { this.reset() this.dialogTitle = this.$t('OutboundCallJob.dialog.title.edit') // 编辑 this.isShowDialog = true Object.assign(this.form, row) this.form.id = row.id }, // 创建确认 createCallJob () { let fromData = new FormData() fromData.append('ani', this.form.ani) fromData.append('dnis', this.form.dnis) fromData.append('cron', this.form.cron) fromData.append('status', this.form.status) fromData.append('scheduleInterval', this.form.scheduleInterval) fromData.append('text', this.form.text) this.$refs['addForm'].validate(valid => { // 判断输入校验是否成功 if (!valid) { return } // 新建 if (!this.form.id) { this.$http.post(this.$apiUrl('callJobCreate'), fromData, { headers: { 'Content-Type': 'application/json' } } ).then(response => { this.$message.success(this.$t('OutboundCallJob.message.createSuccess')) // 创建成功 this.isShowDialog = false this.search() }) // 编辑 } else { fromData.append('id', this.form.id) this.$http.put(this.$apiUrl('callJobUpdate'), fromData, { headers: { 'Content-Type': 'application/json' } } ).then(response => { this.$message.success(this.$t('OutboundCallJob.message.updateSuccess')) // 修改成功 this.isShowDialog = false this.search() }).catch((error) => { console.dir(error) this.$message.error(error.message) }) } }) }, // 删除 toDelete (row) { this.$confirm( this.$t('OutboundCallJob.confirm.delete'), // 此操作将删除该任务,是否继续? this.$t('OutboundCallJob.confirm.title'), // 提示 { confirmButtonText: this.$t('OutboundCallJob.confirm.confirmButtonText'), // 确定 cancelButtonText: this.$t('OutboundCallJob.confirm.cancelButtonText'), // 取消 type: 'warning' }).then(() => { this.delete(row.id) }) }, delete (id) { this.$http.delete(this.$apiUrl('callJobDelete', [id])) .then(response => { this.$message.success(this.$t('OutboundCallJob.message.deleteSuccess')) // 删除成功! this.search() }) .catch((error) => { if (error.response) { this.$message.error(`${this.$t('OutboundCallJob.message.deleteFail')}: ${error.response.data.message}`) // 删除失败! ${error.response.data.message} } else { this.$message.error(`${this.$t('OutboundCallJob.message.deleteFail')}: ${error.message}`) // 删除失败! ${error.message} } }) }, // 列表搜索 search () { let startTime = '' let endTime = '' if (this.searchForm.date && this.searchForm.date.length > 0) { startTime = this.searchForm.date[0] endTime = this.searchForm.date[1] } let form = { startTime: startTime, endTime: endTime, page: this.pagination.currentPage, size: this.pagination.pageSize, ...this.searchForm } let url = this.$apiUrl('callJobSearch') + '?' + this.$qs.stringify(form) this.$http.get(url).then(response => { this.tableData = response.data.data.records this.pagination.total = response.data.data.total }).catch((error) => { this.$message.error(error.message) }) }, // 重置表单 reset () { this.form = { scheduleInterval: 'day', // 调度周期 cron: '', // Cron 表达式 ani: '', dnis: '', status: 'enabled', text: '' } this.$nextTick(() => { this.$refs['addForm'].clearValidate() }) }, fillMyMobile () { let user = expiringStorage.get('user') console.log(user) if (user.mobile) { this.form.dnis = user.mobile } }, viewDetail (callJob) { this.$emit('navigateToDetail', callJob.id) }, // change 事件会返回新的 interval cron onChange (cron) { console.log(cron) this.form.cron = cron }, handleSizeChange (val) { this.pagination.pageSize = val this.search() }, handleCurrentChange (val) { this.pagination.currentPage = val this.search() } } } </script> <style scoped lang="scss"> ::v-deep .cron-picker .el-select { margin-bottom: 15px; } </style>
最新发布
08-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值