﻿CREATE OR REPLACE  VIEW          "AICTASK" ("RANINSTASKID","RANINSPLANID","RANINSTASKNAME","RANINSTYPE","RANINSITEM","RANINSOBJSCOPE","EXTRACTAUTH","EXTRACTFROM","EXTRACTTO","RANINSOBJNUM","RANINSOBJPROP","SLDINSOBJNUM","INSPNUM","INSDATEFROM","INSDATETO","INSAUTH","REMARK","DSTATE","ADDTASKID","ADDDEPID","ADDDEPNAME","LEADINGDEPID","CREATETIME","UPDATETIME","TASKASSIGNDEPT","VERIFYSTATE","GROUPNUM","DIVISION","EXTRACTAUTHCODE","INSAUTHCODE","RANINSPLANID1","PLANTYPE1")  
AS 
SELECT t."RANINSTASKID",t."RANINSPLANID",t."RANINSTASKNAME",t."RANINSTYPE",t."RANINSITEM",t."RANINSOBJSCOPE",t."EXTRACTAUTH",t."EXTRACTFROM",t."EXTRACTTO",t."RANINSOBJNUM",t."RANINSOBJPROP",t."SLDINSOBJNUM",t."INSPNUM",t."INSDATEFROM",t."INSDATETO",t."INSAUTH",t."REMARK",t."DSTATE",t."ADDTASKID",t."ADDDEPID",t."ADDDEPNAME",t."LEADINGDEPID",t."CREATETIME",t."UPDATETIME",t."TASKASSIGNDEPT",t."VERIFYSTATE",t."GROUPNUM",t."DIVISION",t."EXTRACTAUTHCODE",t."INSAUTHCODE" , p1.RANINSPLANID AS RANINSPLANID1 , p1.PLANTYPE AS PLANTYPE1
FROM RANINSTASK t LEFT JOIN RANINSPLAN p1 ON t.RANINSPLANID = p1.RANINSPLANID
WHERE p1.PLANTYPE ='1' 
AND ( p1.DEPNAME  LIKE '%市场和质量%' or p1.DEPNAME  LIKE '%机场分局%' 
			or p1.DEPNAME  LIKE '%工商行政%' ) 
UNION
-- 跨部门任务
SELECT t."RANINSTASKID",t."RANINSPLANID",t."RANINSTASKNAME",t."RANINSTYPE",t."RANINSITEM",t."RANINSOBJSCOPE",t."EXTRACTAUTH",t."EXTRACTFROM",t."EXTRACTTO",t."RANINSOBJNUM",t."RANINSOBJPROP",t."SLDINSOBJNUM",t."INSPNUM",t."INSDATEFROM",t."INSDATETO",t."INSAUTH",t."REMARK",t."DSTATE",t."ADDTASKID",t."ADDDEPID",t."ADDDEPNAME",t."LEADINGDEPID",t."CREATETIME",t."UPDATETIME",t."TASKASSIGNDEPT",t."VERIFYSTATE",t."GROUPNUM",t."DIVISION",t."EXTRACTAUTHCODE",t."INSAUTHCODE" , p.RANINSPLANID AS RANINSPLANID , p.PLANTYPE  AS PLANTYPE
FROM RANINSTASK t LEFT JOIN RANINSPLAN p ON t.RANINSPLANID = p.RANINSPLANID
WHERE  p.PLANTYPE ='2'  
AND t.RANINSTASKID IN (
SELECT i.RANINSTASKID FROM RANTASKISSUED i LEFT JOIN T_ORGUNITS o ON i.DEPID = o.ORGUNITID 
	WHERE i.RANINSTASKID  IN (
		SELECT RANINSTASKID FROM RANINSTASK WHERE RANINSPLANID IN (
		SELECT RANINSPLANID FROM RANINSPLAN WHERE PLANTYPE ='2') 
	) AND ( o.ORGUNITNAME LIKE '%市场和质量%' or o.ORGUNITNAME LIKE '%机场分局%' 
	or o.ORGUNITNAME LIKE '%工商行政%' )
);

CREATE OR REPLACE  VIEW          "CHECKQUARANTINE" ("USERCARDID","TASKNAME","INSTYPE","PLANTYPE","ENTNAME","CHECKTIME","STATUS","ID","REPID","RANINSTASKID")  
AS 
select oi.userCardId, -- 检查人身份证
t.taskName, -- 任务名
t.insType,  -- 抽查类型 1 定向 2不定向
t.planType, -- 计划类型 1  内部计划  2 联合计划
oi.entName, -- 企业名称
e.checkTime, --检查完成时间
e.status, -- 是否完成检查 1已完成 2 未完成（结果全部录入，才会已完成）
e.id, -- 抽查主体状态 主键
e.repId, -- 法人主键id（企业主键字段无效，法人主键就是企业主键
e.RanInsTaskID -- 抽查任务编号
from raninsObjInsp oi , taskInside t,RanInsEnterpriseDstate e;

CREATE OR REPLACE  VIEW          "GROUPINFO" ("GROUPID","RANINSTASKID","GROUPLEAD","LOCDISTRICT","DEPTYPE")  
AS 
select g.groupid,
    g.RanInsTaskID as ranInsTaskId,
    r.ID as groupLead,
    r.LocDistrict as locDistrict ,
    u.orgUnitType as depType
    from RanInsGroup g,RanInsGroupInsp r,RanInsInspectors u
    where g.groupid = r.groupid and r.isLead = '1' and r.ID = u.ID;

CREATE OR REPLACE  VIEW          "INSWORKQUERY" ("RANINSPLANID","ENTNUM","REMARK","DEPNAME","PLANTYPE","SUBTIME","CREATETIME","RANINSPLANNAME","RANINSTASKID","RANINSTASKNAME","RANINSTYPE","EXTRACTFROM","EXTRACTTO","INSDATEFROM","INSDATETO","DEPID","USERIMPORT","ISIMPORT","ISSUED","EXECUTESTATE")  
AS 
(
select 
p.RANINSPLANID,p.ENTNUM,p.REMARK,p.DEPNAME,p.PLANTYPE,p.SUBTIME,p.CREATETIME,p.RANINSPLANNAME,
t.RANINSTASKID,t.RANINSTASKNAME,t.RANINSTYPE,t.EXTRACTFROM,t.EXTRACTTO, 
t.INSDATEFROM,t.INSDATETO,
i.DEPID ,i.USERIMPORT,i.ISIMPORT,i.ISSUED,
case when i.TIId is null then 0 when i.isFinish = '1' then 1 else 0 END executeState
from RanInsPlan p left join RanInsTask t on  p.RanInsPlanID = t.RanInsPlanID
left join RanTaskIssued i on i.RanInsTaskID = t.RanInsTaskID);

CREATE OR REPLACE  VIEW          "INSWORKQUERYTASK" ("RANINSTASKID","RANINSTASKNAME","RANINSTYPE","EXTRACTFROM","EXTRACTTO","DEPID","USERIMPORT","ISIMPORT","ISSUED","INSDATEFROM","INSDATETO","EXECUTESTATE")  
AS 
(
select t.RANINSTASKID,t.RANINSTASKNAME,t.RANINSTYPE,t.EXTRACTFROM,t.EXTRACTTO,
i.DEPID ,i.USERIMPORT,i.ISIMPORT,i.ISSUED, t.INSDATEFROM,t.INSDATETO,
case when i.TIId is null then 0 when i.isFinish = '1' then 1 else 0 END executeState
from  RanInsTask t  left join RanTaskIssued i on i.RanInsTaskID = t.RanInsTaskID);

CREATE OR REPLACE  VIEW          "KBM_USERTASK" ("RANINSTASKID","RANINSTASKNAME","RANINSPLANID","RANINSPLANNAME","EXTRACTFROM","EXTRACTTO","TASKASSIGNDEPT","TYPE","DEPID","PLANTYPE","LEADINGDEPID")  
AS 
(select t.RANINSTASKID,t.ranInsTaskName,p.RanInsPlanID,p.RanInsPlanName ,t.extractFrom,t.EXTRACTTO
,t.taskAssignDept,'1',t.LEADINGDEPID,p.planType,t.LEADINGDEPID
from RanInsTaskTemp t,raninsPlanTemp p
where t.ranInsPlanID = p.ranInsPlanID and t.VERIFYSTATE ='3' and t.cover='1'--审批通过
and  not exists(select 1 from RANTASKISSUED i where t.RANINSTASKID = i.RANINSTASKID )
UNION
--当parentID为空时，说明是主导部门分配的抽查任务，细化抽查任务列表
select rt.RANINSTASKID,rt.ranInsTaskName ,rp.RanInsPlanID,rp.RanInsPlanName
 ,rt.extractFrom,rt.EXTRACTTO,null,'2'  ,i.DEPID,rp.planType,rt.LEADINGDEPID
from RANTASKISSUED i ,RANINSTASK rt ,RANINSPLAN rp
where i.RANINSTASKID = rt.RANINSTASKID and rt.ranInsPlanID = rp.ranInsPlanID
and i.ISSUED = '0'and i.dstate !='2' and i.isFinish = '0' and i.PARENTID IS NULL
UNION
--汇总组长，如果不走下发流程，主导部门就直接去指定组长（99）
select rt.RANINSTASKID,rt.ranInsTaskName ,rp.RanInsPlanID,rp.RanInsPlanName
 ,rt.extractFrom,rt.EXTRACTTO,null,'99'  ,i.DEPID,rp.planType,rt.LEADINGDEPID
from RANTASKISSUED i ,RANINSTASK rt ,RANINSPLAN rp
where i.RANINSTASKID = rt.RANINSTASKID and rt.ranInsPlanID = rp.ranInsPlanID
and i.dstate !='2' and i.isFinish = '0' and i.USERIMPORT='99'
UNION
--汇总选取匹配人员，如果是主导部门，还需要指定组长
select rt.RANINSTASKID,rt.ranInsTaskName ,rp.RanInsPlanID,rp.RanInsPlanName
 ,rt.extractFrom,rt.EXTRACTTO,null,'51'  ,i.DEPID,rp.planType,rt.LEADINGDEPID
from RANTASKISSUED i ,RANINSTASK rt ,RANINSPLAN rp
where i.RANINSTASKID = rt.RANINSTASKID and rt.ranInsPlanID = rp.ranInsPlanID
and i.dstate !='2' and i.isFinish = '0' and i.USERIMPORT='51'
UNION
--汇总选取随机人员，如果是主导部门，还需要指定组长
select rt.RANINSTASKID,rt.ranInsTaskName ,rp.RanInsPlanID,rp.RanInsPlanName
 ,rt.extractFrom,rt.EXTRACTTO,null,'52'  ,i.DEPID,rp.planType,rt.LEADINGDEPID
from RANTASKISSUED i ,RANINSTASK rt ,RANINSPLAN rp
where i.RANINSTASKID = rt.RANINSTASKID and rt.ranInsPlanID = rp.ranInsPlanID
and i.dstate !='2' and i.isFinish = '0' and i.USERIMPORT='52'
UNION
--区级汇总选取匹配人员，提交名单
select rt.RANINSTASKID,rt.ranInsTaskName ,rp.RanInsPlanID,rp.RanInsPlanName
 ,rt.extractFrom,rt.EXTRACTTO,null,'61'  ,i.DEPID,rp.planType,rt.LEADINGDEPID
from RANTASKISSUED i ,RANINSTASK rt ,RANINSPLAN rp
where i.RANINSTASKID = rt.RANINSTASKID and rt.ranInsPlanID = rp.ranInsPlanID
and i.dstate !='2' and i.isFinish = '0' and i.USERIMPORT='61'
UNION
--区级汇总选取随机人员，提交名单
select rt.RANINSTASKID,rt.ranInsTaskName ,rp.RanInsPlanID,rp.RanInsPlanName
 ,rt.extractFrom,rt.EXTRACTTO,null,'62'  ,i.DEPID,rp.planType,rt.LEADINGDEPID
from RANTASKISSUED i ,RANINSTASK rt ,RANINSPLAN rp
where i.RANINSTASKID = rt.RANINSTASKID and rt.ranInsPlanID = rp.ranInsPlanID
and i.dstate !='2' and i.isFinish = '0' and i.USERIMPORT='62'
UNION
--汇总任务。
select rt.RANINSTASKID,rt.ranInsTaskName ,rp.RanInsPlanID,rp.RanInsPlanName
 ,rt.extractFrom,rt.EXTRACTTO,null,'4'  ,i.DEPID,rp.planType,rt.LEADINGDEPID
from RANTASKISSUED i ,RANINSTASK rt ,RANINSPLAN rp
where i.RANINSTASKID = rt.RANINSTASKID and rt.ranInsPlanID = rp.ranInsPlanID
and i.dstate !='2' and i.isFinish = '0' and i.FINALSTEP='1'
);

CREATE OR REPLACE  VIEW          "OAPPPERINFORMATION" ("APPPERINFORMATIONID","TASKID","ENTNO","REGNO","UNISCID","ENTNAME","ORGCODE","APPPERCARDID","APPPERNAME","APPPERCONTENT","PRINCIPAL","APPPERSTARTTIME","APPPERENDTIME","RELEASEAUTHORYKEY","RELEASEAUTHORYVALUE","ENTRYPEOPLE","ENTRYTIME","ACCESSORY","STATUS")  
AS 
select     LICID appPerInformationID,
   null taskID,
   PRIPID entNo,
   RegNO regNo,
UniSCID UniSCID,
   EntName entName,
   null orgCode,
   LicNO appPerCardID,
   LicName_CN appPerName,
   LicItem appPerContent,
   null principal,
ValFrom appPerStartTime,
   ValTo appPerEndTime,
   null releaseAuthoryKey,
   LicAnth releaseAuthoryValue,
   null entryPeople,
   null entryTime,    null accessory,    Status status
 from CPLICINFO_QT;

CREATE OR REPLACE  VIEW          "RANDOMRESULT" ("ENTNAME","GROUPNAME","NAME","MOBILE","RANINSID")  
AS 
SELECT
    e.entname,
    g.GROUPNAME,
     ins.NAME,
    ins.MOBTEL,
    oi.RanInsTaskID
FROM
    RANINSOBJINSP oi ,RANINSINSPECTORS ins 
    ,RANINSOBJGROUP og,RANINSGROUP g, RANINSENTERPRISEDSTATE e
WHERE 
oi.usercardid = ins.id and oi.repid = og.repid 
and og.GROUPID = g.GROUPID
and oi.repid = e.repid
and oi.RanInsTaskID = e.RanInsTaskID
and og.RanInsTaskID = g.RanInsTaskID
and oi.RanInsTaskID = og.RanInsTaskID;

CREATE or replace
	VIEW        SJZX_RANINSInspectors AS 
SELECT
ID,
NAME,
SEX,
MOBTEL,
LAWOFFICE,
LAWDEP,
LAWPOSITION,
LAWCERNO,
LAWISSAUTH,
DSTATE,
ORGUNITID,
ORGUNITTYPE,
CREATETIME,
UPDATETIME,
LOCDISTRICT,
REGIONID,
DEPARTMENTID,
CIVILSERVANT,
ISEEXPERT,
INSTYPE
FROM AICRIS.RANINSInspectors  sfc
WHERE  ORGUNITID IN 
(SELECT ORGUNITID FROM AICORG.T_ORGUNITS WHERE UNITTYPE='1' AND DELETED='0' 
START WITH ORGUNITID ='001' CONNECT BY PRIOR ORGUNITID=PARENTID);

CREATE or replace
	VIEW        SJZX_RANINSITEM AS 
SELECT 
RANINSITEM,
RANINSITEMNAME,
RANINSBASIS,
RANINSCONT,
USERID,
DEPID,
DEPNAME,
CREATETIME,
UPDATETIME,
DSTATE,
RANINSOBJTYPE,
INSCHECKTYPE,
DEPTYPE,
REGIONID,
ITEMOBJPROP,
ITEMCOUNT,
ITEMOBJ,
ITEMENTER,
ITEMMODE,
ITEMCATEGORY,
CHECKCATEGORY
FROM AICRIS.RANINSITEM  WHERE depid IN 
(SELECT ORGUNITID FROM AICORG.T_ORGUNITS WHERE UNITTYPE='1' AND DELETED='0' 
START WITH ORGUNITID ='001' CONNECT BY PRIOR ORGUNITID=PARENTID);

CREATE or replace
VIEW        SJZX_RANINSOBJINSP AS 
WITH TaskCTE AS (
    SELECT DISTINCT
        t.RANINSTASKID
    FROM
        AICRIS.RANINSTASK t
    INNER JOIN AICRIS.RANINSPLAN p ON (p.RANINSPLANID = t.RANINSPLANID AND p.DSTATE <> '2' AND p.PLANTYPE = '1')
    INNER JOIN AICRIS.RanInsEnterpriseDstate r ON (r.raninstaskid = t.raninstaskid AND r.verifyState = '0')
    WHERE
        t.DSTATE <> '2'
        AND t.createtime >= TO_DATE('2025-01-01')
        AND t.adddepid IN (
            SELECT o.ORGUNITID
            FROM AICORG.T_ORGUNITS o
            WHERE o.deleted = '0' AND o.unittype = '1'
            START WITH o.orgnumber = '44010000'
            CONNECT BY PRIOR o.ORGUNITID = o.PARENTID
        )
)
SELECT 
insp.ID,
insp.USERID,
insp.RANINSTASKID,
insp.ENTNO,
insp.ENTNAME,
insp.REPID,
insp.USERCARDID,
insp.REPTYPE,
insp.LOCDISTRICT,
insp.ORGNUMBER,
insp.CREATETIME,
insp.ASSIGNSTATE
FROM
    AICRIS.RANINSOBJINSP insp
INNER JOIN TaskCTE task ON (insp.RANINSTASKID = task.RANINSTASKID)
WHERE
    NOT EXISTS (
        SELECT 1
        FROM AICRIS.RANINSRESULT r
        WHERE
            r.pripid = insp.ENTNO
            AND r.raninstaskid = insp.raninstaskid
    );

CREATE OR REPLACE  VIEW          "TASKINSIDE" ("TASKID","TASKNAME","PLANID","PLANNAME","PLANTYPE","INSTYPE","SCOPEOBJ","PROP","INSPNUM","RANINSITEM","EXTRACTFROM","EXTRACTTO","SLDINSOBJNUM","REMARK","LEADINGDEPID","INSDATEFROM","INSDATETO")  
AS 
select
rt.ranInsTaskID taskId, -- 任务id
rt.ranInsTaskName taskName, --抽查任务名称
rp.RanInsPlanID planID, -- 抽查计划编号
rp.RanInsPlanName planName, --抽查计划名称
rp.planType planType, -- 计划类型 1  内部计划  2 联合计划
rt.ranInsType insType, -- 抽查类型 1 定向 2不定向
rt.ranInsObjScope scopeObj, --抽查范围
rt.ranInsObjProp prop, -- 抽查对象比例
rt.inspNum inspNum, --检查人员数量
rt.ranInsItem ranInsItem, -- 抽查事项描述
rt.extractFrom extractFrom,--抽取时间自
rt.extractTo extractTo,--抽取时间至
rt.sldInsObjNum sldInsObjNum, -- 应查对象总数
rt.remark remark, -- 备注
rt.leadingDepId leadingDepId, --牵头部门id
rt.insDateFrom insDateFrom, --  检查日期自
rt.insDateTo insDateTo -- 检查日期至
-- rt.isMatching isMatching --是否完成任务匹配 1 是 0 否
from RanInsTask rt left join RanInsPlan rp on rt.RanInsPlanID = rp.RanInsPlanID   where  rp.dstate!='2' and rt.dstate!='2';

CREATE OR REPLACE  VIEW          "TASKINSIDETEMP" ("TASKID","TASKNAME","PLANID","PLANNAME","PLANTYPE","INSTYPE","SCOPEOBJ","PROP","INSPNUM","RANINSITEM","EXTRACTFROM","EXTRACTTO","SLDINSOBJNUM","REMARK","LEADINGDEPID")  
AS 
select
rt.ranInsTaskID taskId, -- 任务id
rt.ranInsTaskName taskName, --抽查任务名称
rp.RanInsPlanID planID, -- 抽查计划编号
rp.RanInsPlanName planName, --抽查计划名称
rp.planType planType, -- 计划类型 1  内部计划  2 联合计划
rt.ranInsType insType, -- 抽查类型 1 定向 2不定向
rt.ranInsObjScope scopeObj, --抽查范围
rt.ranInsObjProp prop, -- 抽查对象比例
rt.inspNum inspNum, --检查人员数量
rt.ranInsItem ranInsItem, -- 抽查事项描述
rt.extractFrom extractFrom,--抽取时间自
rt.extractTo extractTo,--抽取时间至
rt.sldInsObjNum sldInsObjNum, -- 应查对象总数
rt.remark remark, -- 备注
rt.leadingDepId leadingDepId --牵头部门id
-- rt.isMatching isMatching --是否完成任务匹配 1 是 0 否
from RanInsTaskTemp rt left join RanInsPlanTemp rp on rt.RanInsPlanID = rp.RanInsPlanID   where  rp.dstate!='2' and rt.dstate!='2';

CREATE OR REPLACE  VIEW          "TASKOBJVERIFIED" ("RANINSTASKID","RANINSPLANID","RANINSTASKNAME","RANINSTYPE","RANINSITEM","RANINSOBJSCOPE","EXTRACTAUTH","EXTRACTFROM","EXTRACTTO","RANINSOBJNUM","RANINSOBJPROP","SLDINSOBJNUM","INSPNUM","INSDATEFROM","INSDATETO","INSAUTH","REMARK","DSTATE","ADDTASKID","ADDDEPID","ADDDEPNAME","LEADINGDEPID","CREATETIME","UPDATETIME","TASKASSIGNDEPT","VERIFYSTATE","GROUPNUM","DEPARTMENTID")  
AS 
select a."RANINSTASKID",a."RANINSPLANID",a."RANINSTASKNAME",a."RANINSTYPE",a."RANINSITEM",a."RANINSOBJSCOPE",a."EXTRACTAUTH",a."EXTRACTFROM",a."EXTRACTTO",a."RANINSOBJNUM",a."RANINSOBJPROP",a."SLDINSOBJNUM",a."INSPNUM",a."INSDATEFROM",a."INSDATETO",a."INSAUTH",a."REMARK",a."DSTATE",a."ADDTASKID",a."ADDDEPID",a."ADDDEPNAME",a."LEADINGDEPID",a."CREATETIME",a."UPDATETIME",a."TASKASSIGNDEPT",a."VERIFYSTATE",a."GROUPNUM",p."DEPARTMENTID"
 from RANINSTASK a,raninsplan p
 where p.RANINSPLANID = a.RANINSPLANID  and p.PLANTYPE ='1'
and  exists(
select 1 from RANTASKISSUED d where  a.raninstaskid = d.raninstaskid and d.dstate !='2' having count(d.raninstaskid)>1
)and not exists(
select 1 from RANTASKISSUED b , RANTASKISSUED c   where b.tiid = c.parentid and a.raninstaskid = b.raninstaskid
and b.parentid is null
and c.VERIFYSTATE != '3')
 ---随机出检查对象了，并且检查对象经过了两级领导的审核审批
UNION ALL 
 select  t."RANINSTASKID",t."RANINSPLANID",t."RANINSTASKNAME",t."RANINSTYPE",t."RANINSITEM",t."RANINSOBJSCOPE",t."EXTRACTAUTH",t."EXTRACTFROM",t."EXTRACTTO",t."RANINSOBJNUM",t."RANINSOBJPROP",t."SLDINSOBJNUM",t."INSPNUM",t."INSDATEFROM",t."INSDATETO",t."INSAUTH",t."REMARK",t."DSTATE",t."ADDTASKID",t."ADDDEPID",t."ADDDEPNAME",t."LEADINGDEPID",t."CREATETIME",t."UPDATETIME",t."TASKASSIGNDEPT",t."VERIFYSTATE",t."GROUPNUM",p."DEPARTMENTID"
from RANINSTASK t  , raninsplan p
WHERE p.RANINSPLANID = t.RANINSPLANID AND p.PLANTYPE ='2'
and exists(
    select 1 from RANTASKISSUED i where i.raninstaskid = t.raninstaskid and FINALSTEP='1'
    and i.VERIFYSTATE = '3'
);

CREATE OR REPLACE  VIEW          "TASKVERIFIED" ("RANINSTASKID","RANINSPLANID","RANINSTASKNAME","RANINSTYPE","RANINSITEM","RANINSOBJSCOPE","EXTRACTAUTH","EXTRACTFROM","EXTRACTTO","RANINSOBJNUM","RANINSOBJPROP","SLDINSOBJNUM","INSPNUM","INSDATEFROM","INSDATETO","INSAUTH","REMARK","DSTATE","ADDTASKID","ADDDEPID","ADDDEPNAME","LEADINGDEPID","CREATETIME","UPDATETIME","TASKASSIGNDEPT","VERIFYSTATE","GROUPNUM","DEPARTMENTID")  
AS 
select a."RANINSTASKID",a."RANINSPLANID",a."RANINSTASKNAME",a."RANINSTYPE",a."RANINSITEM",a."RANINSOBJSCOPE",a."EXTRACTAUTH",a."EXTRACTFROM",a."EXTRACTTO",a."RANINSOBJNUM",a."RANINSOBJPROP",a."SLDINSOBJNUM",a."INSPNUM",a."INSDATEFROM",a."INSDATETO",a."INSAUTH",a."REMARK",a."DSTATE",a."ADDTASKID",a."ADDDEPID",a."ADDDEPNAME",a."LEADINGDEPID",a."CREATETIME",a."UPDATETIME",a."TASKASSIGNDEPT",a."VERIFYSTATE",a."GROUPNUM",p."DEPARTMENTID"
 from RANINSTASK a,raninsplan p
 where p.RANINSPLANID = a.RANINSPLANID  and p.PLANTYPE ='1'
and  a.VERIFYSTATE = '3'
UNION ALL 
 select  t."RANINSTASKID",t."RANINSPLANID",t."RANINSTASKNAME",t."RANINSTYPE",t."RANINSITEM",t."RANINSOBJSCOPE",t."EXTRACTAUTH",t."EXTRACTFROM",t."EXTRACTTO",t."RANINSOBJNUM",t."RANINSOBJPROP",t."SLDINSOBJNUM",t."INSPNUM",t."INSDATEFROM",t."INSDATETO",t."INSAUTH",t."REMARK",t."DSTATE",t."ADDTASKID",t."ADDDEPID",t."ADDDEPNAME",t."LEADINGDEPID",t."CREATETIME",t."UPDATETIME",t."TASKASSIGNDEPT",t."VERIFYSTATE",t."GROUPNUM",p."DEPARTMENTID"
        from RANINSTASK t  , raninsplan p
        WHERE p.RANINSPLANID = t.RANINSPLANID AND p.PLANTYPE ='2'
        and t.VERIFYSTATE ='3';

CREATE OR REPLACE  VIEW          "TB_DSM_SPOT_CHECK_RESULT" ("SAUTOID","SCHECKID","SENTID","SRESULTTYPECODE","SRESULTTYPENAME","SBOOKCODE","DTCORRECTIONDATE","SCORRECTIONFLAG","SFLAG")  
AS 
select RESULTID as SAUTOID,RANINSTASKID as SCHECKID ,REPID as SENTID ,RANINSCHECKRES as SRESULTTYPECODE,RANINSITEMNAME as SRESULTTYPENAME,null as SBOOKCODE,
					null as DTCORRECTIONDATE,null as SCORRECTIONFLAG ,'0' as SFLAG 
					from RANINSRESULT t;

CREATE OR REPLACE  VIEW          "TEST_AICTASK" ("RANINSTASKID","RANINSPLANID","RANINSTASKNAME","RANINSTYPE","RANINSITEM","RANINSOBJSCOPE","EXTRACTAUTH","EXTRACTFROM","EXTRACTTO","RANINSOBJNUM","RANINSOBJPROP","SLDINSOBJNUM","INSPNUM","INSDATEFROM","INSDATETO","INSAUTH","REMARK","DSTATE","ADDTASKID","ADDDEPID","ADDDEPNAME","LEADINGDEPID","CREATETIME","UPDATETIME","TASKASSIGNDEPT","VERIFYSTATE","GROUPNUM","DIVISION","EXTRACTAUTHCODE","INSAUTHCODE","RANINSPLANID1","PLANTYPE1")  
AS 
SELECT t."RANINSTASKID",t."RANINSPLANID",t."RANINSTASKNAME",t."RANINSTYPE",t."RANINSITEM",t."RANINSOBJSCOPE",t."EXTRACTAUTH",t."EXTRACTFROM",t."EXTRACTTO",t."RANINSOBJNUM",t."RANINSOBJPROP",t."SLDINSOBJNUM",t."INSPNUM",t."INSDATEFROM",t."INSDATETO",t."INSAUTH",t."REMARK",t."DSTATE",t."ADDTASKID",t."ADDDEPID",t."ADDDEPNAME",t."LEADINGDEPID",t."CREATETIME",t."UPDATETIME",t."TASKASSIGNDEPT",t."VERIFYSTATE",t."GROUPNUM",t."DIVISION",t."EXTRACTAUTHCODE",t."INSAUTHCODE" , p1.RANINSPLANID AS RANINSPLANID1 , p1.PLANTYPE AS PLANTYPE1
FROM RANINSTASK t LEFT JOIN RANINSPLAN p1 ON t.RANINSPLANID = p1.RANINSPLANID
WHERE p1.PLANTYPE ='1' AND t.DSTATE!='2' AND p1.DSTATE!='2'
AND ( p1.DEPNAME  LIKE '%市场和质量%' or p1.DEPNAME  LIKE '%机场分局%' 
			or p1.DEPNAME  LIKE '%工商行政%' ) 
UNION
-- 跨部门任务
SELECT t."RANINSTASKID",t."RANINSPLANID",t."RANINSTASKNAME",t."RANINSTYPE",t."RANINSITEM",t."RANINSOBJSCOPE",t."EXTRACTAUTH",t."EXTRACTFROM",t."EXTRACTTO",t."RANINSOBJNUM",t."RANINSOBJPROP",t."SLDINSOBJNUM",t."INSPNUM",t."INSDATEFROM",t."INSDATETO",t."INSAUTH",t."REMARK",t."DSTATE",t."ADDTASKID",t."ADDDEPID",t."ADDDEPNAME",t."LEADINGDEPID",t."CREATETIME",t."UPDATETIME",t."TASKASSIGNDEPT",t."VERIFYSTATE",t."GROUPNUM",t."DIVISION",t."EXTRACTAUTHCODE",t."INSAUTHCODE" , p.RANINSPLANID AS RANINSPLANID , p.PLANTYPE  AS PLANTYPE
FROM RANINSTASK t LEFT JOIN RANINSPLAN p ON t.RANINSPLANID = p.RANINSPLANID
WHERE  p.PLANTYPE ='2' AND t.DSTATE!='2' AND p.DSTATE!='2'
AND t.RANINSTASKID IN (
SELECT i.RANINSTASKID FROM RANTASKISSUED i LEFT JOIN T_ORGUNITS o ON i.DEPID = o.ORGUNITID 
	WHERE i.RANINSTASKID  IN (
		SELECT RANINSTASKID FROM RANINSTASK WHERE RANINSPLANID IN (
		SELECT RANINSPLANID FROM RANINSPLAN WHERE PLANTYPE ='2') 
	) AND ( o.ORGUNITNAME LIKE '%市场和质量%' or o.ORGUNITNAME LIKE '%机场分局%' 
	or o.ORGUNITNAME LIKE '%工商行政%' )
);

CREATE OR REPLACE  VIEW          "TRENTPRISETYPE" ("ENTTYPEID","DISPLAYVALUE","PRINTVALUE","PENTTYPEID","ECOPROPERTY","CORPORATION","STANDARD","PARTYTYPE")  
AS 
SELECT
    ENTTYPEID AS ENTTYPEID ,
    DISPLAYVALUE ,
    PRINTVALUE ,
    PENTTYPEID AS PENTTYPEID ,
    ECOPROPERTY   AS ECOPROPERTY ,
    CORPORATION ,
    STANDARD,
    ' ' AS PARTYTYPE
FROM
    EnterpriseTypes;

CREATE OR REPLACE  VIEW          "USERTASK" ("RANINSTASKID","RANINSTASKNAME","RANINSPLANID","RANINSPLANNAME","EXTRACTFROM","EXTRACTTO","TASKASSIGNDEPT",
	"TYPE","DEPID","PLANTYPE","LEADINGDEPID","CREATETIME","ISZHCYCTYPE")  
AS 
(select t.RANINSTASKID,t.ranInsTaskName,p.RanInsPlanID,p.RanInsPlanName ,t.extractFrom,t.EXTRACTTO
,t.taskAssignDept,'1',null,p.planType ,t.LEADINGDEPID,t.CREATETIME,t.isZhcycType
from RanInsTaskTemp t,raninsPlanTemp p
where t.ranInsPlanID = p.ranInsPlanID and t.VERIFYSTATE ='3' and t.cover='1'--审批通过
and t.dstate!='2' and p.dstate!='2'
and  not exists(select 1 from RANTASKISSUED i where t.RANINSTASKID = i.RANINSTASKID and i.dstate !='2')
UNION
--正式表 用 用户所在机关id找
select rt.RANINSTASKID,rt.ranInsTaskName ,rp.RanInsPlanID,rp.RanInsPlanName
 ,rt.extractFrom,rt.EXTRACTTO,null,'2'  ,i.DEPID,rp.planType ,rt.LEADINGDEPID,rt.CREATETIME,rt.isZhcycType
from RANTASKISSUED i ,RANINSTASK rt ,RANINSPLAN rp
where i.RANINSTASKID = rt.RANINSTASKID and rt.ranInsPlanID = rp.ranInsPlanID
and i.ISSUED = '0' and i.dstate !='2' and rt.dstate !='2' and rp.dstate !='2' 
and i.isFinish = '0' and i.isImport in ('3')
---查询审核完的任务，退回的情况
UNION 
SELECT t.RANINSTASKID,t.ranInsTaskName,p.RanInsPlanID,p.RanInsPlanName ,t.extractFrom,t.EXTRACTTO
,t.taskAssignDept,'1',null,p.planType ,t.LEADINGDEPID,t.CREATETIME,t.isZhcycType
from RanInsTaskTemp t,raninsPlanTemp p
where   t.ranInsPlanID = p.ranInsPlanID and t.dstate!='2' and p.dstate!='2' 
AND exists(select 1 from RanInsTask t1 where  t.raninstaskid = t1.raninstaskid and t1.dstate !='2'  having count(t1.raninstaskid)>0)
AND t.VERIFYSTATE ='0'
);

