<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sict.excel.mapper.DownloadMapper">


	<!-- 筛查监控-血清学检查 -->
	<select id="getSerumStatistics"
		resultType="com.sict.excel.dto.SerumStatisticsDTO">
		SELECT
		COUNT(*) serumTestTotal,
		count(HP抗体(IGG)检测 = '阳性' OR
		NULL) iggPositive
		from(
		SELECT
		p.id 患者id,
		p.step step,
		CASE rel1.igg
		WHEN 1
		THEN
		'阳性'
		WHEN 0 THEN
		'阴性'
		ELSE
		''
		END HP抗体(IGG)检测,
		rel1.`status` 审核状态
		FROM
		tb_patient AS p
		INNER JOIN (
		SELECT
		patient_id,
		step,
		igg,
		`status`
		FROM
		tb_lab_inspect
		) rel1 ON rel1.patient_id = p.id
		INNER JOIN (SELECT
		`name`,number FROM tb_hosp )rel2 ON rel2.number = p.hosp_number
		WHERE
		rel1.`status` NOT
		IN (0, 5)
		)rel
	</select>


	<!-- 筛查排名-按血检人数排名 -->
	<select id="getScreeningRankProvinceDTOs"
		resultType="com.sict.excel.dto.ScreeningRankProvinceDTO">
		SELECT
		(@rownum := @rownum + 1) as
		id,hospname,province,actualserum
		FROM
		(SELECT
		医联体名称 hospname,
		省份 province,
		count( * ) actualserum
		FROM
		(
		SELECT
		p.id 患者id,
		rel1.number 医联体编号,
		rel1.`name` 医联体名称,
		rel2.step 实验室检查调查进度,
		rel2.`status` 审核状态,
		rel3.area 省份
		FROM
		tb_patient AS p
		INNER JOIN ( SELECT `name`, number FROM tb_hosp )
		rel1 ON rel1.number =
		p.hosp_number
		INNER JOIN ( SELECT patient_id,
		step,
		g17, pg1, pg2, igg, `status` FROM
		tb_lab_inspect ) rel2 ON
		rel2.patient_id = p.id
		INNER JOIN ( SELECT area, `name` FROM
		tmp_tb_hosp_area ) rel3 ON
		rel3.`name` = rel1.`name`
		WHERE
		rel2.`status`
		NOT IN ( 0, 5 )
		) rel
		GROUP BY
		医联体名称
		ORDER BY
		actualserum DESC )
		actual,(select @rownum :=0) result
	</select>

	<!-- 省份早癌排名 -->
	<select id="getEarlyCancerRankDTOs"
		resultType="com.sict.excel.dto.EarlyCancerRankDTO">
		SELECT province,ECamount
		FROM
		(
		SELECT
		省份 AS province,
		count(
		最终诊断详情 LIKE '%"b":1%'
		OR NULL
		) + count(
		最终诊断详情 LIKE '%"g":1%'
		OR NULL
		)
		ECamount
		FROM
		(
		SELECT
		p.id 患者id,
		rel3.area 省份,
		rel1.step 胃镜检查调查进度,
		rel1.diagnose_info 最终诊断详情,
		rel1. STATUS 审核状态
		FROM
		tb_patient p
		INNER JOIN
		(
		SELECT
		patient_id,
		step,
		inspect_way,
		precise_way,
		capsule,
		diagnose_result,
		diagnose_info,
		is_inspect,
		`status`
		FROM
		tb_gastro_inspect
		WHERE
		is_inspect = 1
		AND `status` NOT IN (0, 5)
		AND
		diagnose_result = 1
		) rel1 ON rel1.patient_id = p.id
		INNER JOIN (
		SELECT
		`name`,
		number
		FROM
		tb_hosp
		) rel2 ON rel2.number = p.hosp_number
		LEFT JOIN
		(
		SELECT
		area,
		`name`
		FROM
		tmp_tb_hosp_area
		) rel3 ON rel3.`name` =
		rel2.`name`
		) rel
		GROUP BY
		省份
		)result
		WHERE ECamount>0
		ORDER BY ECamount
		DESC
	</select>

	<!-- 运营分析-医院早癌 胃癌数 -->

	<select id="geteaCancerHospitals"
		resultType="com.sict.excel.dto.EarlyCancerHospitalDTO">
		SELECT (@rowNO := @rowNo+1) AS id,a.*
		FROM(
		SELECT
		hospName as
		hospname,
		area province,
		(
		count( gastroDetailInfo LIKE '%"a":1%' OR NULL
		) + count(
		gastroDetailInfo LIKE '%"b":1%' OR NULL ) + count(
		gastroDetailInfo
		LIKE '%"c":1%' OR NULL ) + count( gastroDetailInfo
		LIKE '%"d":1%' OR
		NULL ) + count( gastroDetailInfo LIKE '%"e":1%' OR
		NULL ) + count(
		gastroDetailInfo LIKE '%"f":1%' OR NULL ) + count(
		gastroDetailInfo
		LIKE '%"g":1%' OR NULL ) + count( gastroDetailInfo
		LIKE '%"h":1%' OR
		NULL ) + count( gastroDetailInfo LIKE '%"i":1%' OR
		NULL ) + count(
		gastroDetailInfo LIKE '%"j":1%' OR NULL ) + count(
		gastroDetailInfo
		LIKE '%"k":1%' OR NULL ) + count( gastroDetailInfo
		LIKE '%"l":1%' OR
		NULL ) + count( gastroDetailInfo LIKE '%"m":1%' OR
		NULL ) + count(
		gastroDetailInfo LIKE '%"n":%' OR NULL )
		) total,
		count(
		gastroDetailInfo LIKE '%"a":1%' OR NULL ) AS highEsophagealNeoplasia,
		count( gastroDetailInfo LIKE '%"b":1%' OR NULL ) AS
		earlyEsophagealCancer,
		count( gastroDetailInfo LIKE '%"c":1%' OR NULL )
		AS
		advancedEsophagealCancer,
		count( gastroDetailInfo LIKE '%"f":1%' OR
		NULL ) AS highGastricNeoplasia,
		count( gastroDetailInfo LIKE '%"g":1%'
		OR NULL ) AS
		earlyGastricCancer,
		count( gastroDetailInfo LIKE '%"h":1%'
		OR NULL ) AS advancedGastricCancer
		FROM
		(
		SELECT
		p.id patientId,
		rel2.number hospNumber,
		rel2.`name` hospName,
		rel1.step
		gastroStep,
		rel1.is_inspect isGastro,
		rel1.inspect_way gastroWay,
		rel1.capsule
		gastroResult,
		rel1.precise_way gastroDetailWay,
		rel1.diagnose_result
		gastroDetailResult,
		rel1.diagnose_info
		gastroDetailInfo,
		rel1.`status`
		auditStatus,
		rel3.area
		FROM
		tb_patient p
		INNER JOIN (
		SELECT
		patient_id,
		step,
		is_inspect,
		inspect_way,
		capsule,
		precise_way,
		diagnose_result,
		diagnose_info,
		`status`
		FROM
		tb_gastro_inspect
		WHERE
		is_inspect = 1
		AND
		`status` NOT IN ( 0, 5 )
		AND diagnose_result = 1
		) rel1 ON
		rel1.patient_id = p.id
		INNER JOIN ( SELECT `name`, number FROM
		tb_hosp )
		rel2 ON rel2.number = p.hosp_number
		INNER JOIN tmp_tb_hosp_area rel3 ON
		rel2.`name` = rel3.`name`
		) rel
		GROUP BY
		hospName
		ORDER BY
		earlyGastricCancer DESC
		) a,
		(select @rowNO :=0) b
	</select>

	<!-- 运营分析-省份数据 -->
	<select id="getAnalysisProvinceDTOs"
		resultType="com.sict.excel.dto.AnalysisProvinceDTO">
		SELECT
		provinceName AS province,
		SUM(applyHosp) applyHosp,
		SUM(openAccountHosp) openAccountHosp,
		SUM(applyAccountHosp)
		applyAccountHosp,
		SUM(passApplyHosp) passApplyHosp,
		SUM(platAccountApplyHosp) platAccountApplyHosp,
		SUM(provinceScreeningTotal) provinceScreeningTotal,
		SUM(earlyCancer)
		earlyCancer
		FROM
		(
		(
		SELECT
		provinceName,
		COUNT(NULL) applyHosp,
		COUNT(NULL)
		openAccountHosp,
		COUNT(NULL) applyAccountHosp,
		COUNT(NULL)
		passApplyHosp,
		COUNT(NULL) platAccountApplyHosp,
		count(*)
		provinceScreeningTotal,
		count(NULL) earlyCancer
		FROM
		(
		SELECT
		p.id
		patient_id,
		rel6.area provinceName
		FROM
		( SELECT id,step,hosp_number FROM
		tb_patient WHERE step=0) p
		INNER JOIN (
		SELECT
		number,
		`name`
		FROM
		tb_hosp
		)
		rel5 ON rel5.number = p.hosp_number
		LEFT JOIN (
		SELECT
		area,
		`name`
		FROM
		tmp_tb_hosp_area
		) rel6 ON rel6.`name` = rel5.`name`
		)rel
		GROUP BY
		provinceName
		)
		UNION
		(
		SELECT
		provinceName,
		COUNT(NULL) applyHosp,
		COUNT(NULL) openAccountHosp,
		COUNT(NULL) applyAccountHosp,
		COUNT(NULL)
		passApplyHosp,
		COUNT(NULL) platAccountApplyHosp,
		count(NULL)
		provinceScreeningTotal,
		count(
		gastroscopyDetailInfo LIKE '%"b":1%'
		OR
		NULL
		) + count(
		gastroscopyDetailInfo LIKE '%"g":1%'
		OR NULL
		) earlyCancer
		FROM
		(
		SELECT
		rel3.area provinceName,
		rel1.diagnose_info
		gastroscopyDetailInfo
		FROM
		tb_patient p
		INNER JOIN (
		SELECT
		patient_id,
		diagnose_info
		FROM
		tb_gastro_inspect
		WHERE
		is_inspect = 1
		AND `status` NOT
		IN (0, 5)
		AND diagnose_result = 1
		) rel1 ON rel1.patient_id = p.id
		INNER
		JOIN (
		SELECT
		`name`,
		number
		FROM
		tb_hosp
		) rel2 ON rel2.number =
		p.hosp_number
		LEFT JOIN (
		SELECT
		area,
		`name`
		FROM
		tmp_tb_hosp_area
		) rel3 ON
		rel3.`name` = rel2.`name`
		) rel
		GROUP BY
		provinceName
		)
		UNION
		(
		SELECT
		rel2.area provinceName,
		COUNT(NULL) applyHosp,
		COUNT(NULL)
		openAccountHosp,
		COUNT(NULL) applyAccountHosp,
		COUNT(rel2.area)
		passApplyHosp,
		COUNT(NULL) platAccountApplyHosp,
		count(NULL)
		provinceScreeningTotal,
		count(NULL) earlyCancer
		FROM
		tb_hosp rel1
		INNER
		JOIN tmp_tb_hosp_area rel2 ON rel1.`name` = rel2.`name`
		GROUP BY
		provinceName
		)
		UNION
		(
		SELECT
		provinceName,
		COUNT(NULL) applyHosp,
		COUNT(provinceName) openAccountHosp,
		COUNT(NULL) applyAccountHosp,
		COUNT(NULL) passApplyHosp,
		COUNT(NULL) platAccountApplyHosp,
		count(NULL) provinceScreeningTotal,
		count(NULL) earlyCancer
		FROM
		(
		SELECT
		DISTINCT
		rel3. NAME hospname,
		rel3.area provinceName
		FROM
		tb_hosp rel1
		INNER JOIN (SELECT area FROM tb_user) rel2 ON rel2.area LIKE
		CONCAT('%',
		rel1.number, '%')
		INNER JOIN (
		SELECT
		`name`,
		area
		FROM
		tmp_tb_hosp_area
		) rel3 ON rel1.`name` = rel3. NAME
		WHERE
		rel2.area IS
		NOT NULL
		) rel4
		GROUP BY
		provinceName
		)
		) result
		GROUP BY
		result.provinceName
	</select>

	<!-- 社区筛查排名-血检人数 体检筛查排名 门诊筛查排名 -->
	<select id="getScreeningRankDTOs"
		resultType="com.sict.excel.dto.ScreeningRankDTO">
		SELECT
		(@rownum := @rownum + 1) AS id,
		hospname,
		actualserum
		FROM
		(
		SELECT
		医联体名称 hospname,
		count(*) actualserum
		FROM
		(
		SELECT
		p.id 患者id,
		rel1.number 医联体编号,
		rel1.`name` 医联体名称,
		rel2.step 实验室检查调查进度,
		rel2.`status`
		审核状态
		FROM
		tb_patient AS p
		INNER JOIN (
		SELECT
		`name`,
		number
		FROM
		tb_hosp
		)
		rel1 ON rel1.number = p.hosp_number
		INNER JOIN (
		SELECT
		patient_id,
		step,
		g17,
		pg1,
		pg2,
		igg,
		`status`
		FROM
		tb_lab_inspect
		) rel2 ON rel2.patient_id =
		p.id
		WHERE
		rel2.`status` NOT IN (0, 5)
		) rel
		GROUP BY
		医联体名称
		ORDER BY
		actualserum DESC
		) actual,
		(SELECT @rownum := 0) result
	</select>

	<!-- 运营分析-图例数据 -->
	<select id="getAnalysisGraphicDTOs"
		resultType="com.sict.excel.dto.AnalysisGraphicDTO">
		SELECT
		sum(
		applyGastricCancerScreeningHosp
		)
		applyGastricCancerScreeningHosp,
		sum(passApplyHosp) passApplyHosp,
		sum(platAccountApplyHosp) platAccountApplyHosp,
		sum(openAccountHosp)
		openAccountHosp
		FROM
		(
		SELECT
		SUM(
		applyGastricCancerScreeningHosp
		)
		applyGastricCancerScreeningHosp,
		SUM(passApplyHosp) passApplyHosp,
		SUM(platAccountApplyHosp) platAccountApplyHosp,
		SUM(openAccountHosp)
		openAccountHosp
		FROM
		(
		(
		SELECT
		provinceName,
		COUNT(NULL)
		applyGastricCancerScreeningHosp,
		COUNT(NULL) passApplyHosp,
		COUNT(NULL)
		platAccountApplyHosp,
		COUNT(provinceName) openAccountHosp
		FROM
		(
		SELECT
		DISTINCT
		rel3. NAME hospname,
		rel3.area provinceName
		FROM
		tb_hosp rel1
		INNER JOIN (SELECT area FROM tb_user) rel2 ON rel2.area LIKE
		CONCAT('%',
		rel1.number, '%')
		INNER JOIN (
		SELECT
		`name`,
		area
		FROM
		tmp_tb_hosp_area
		) rel3 ON rel1.`name` = rel3. NAME
		WHERE
		rel2.area IS
		NOT NULL
		) rel4
		GROUP BY
		provinceName
		)
		UNION
		(
		SELECT
		rel2.area provinceName,
		COUNT(NULL) applyGastricCancerScreeningHosp,
		COUNT(rel2.area)
		passApplyHosp,
		COUNT(NULL) platAccountApplyHosp,
		COUNT(NULL)
		openAccountHosp
		FROM
		tb_hosp rel1
		INNER JOIN tmp_tb_hosp_area rel2 ON
		rel1.`name` = rel2.`name`
		GROUP BY
		provinceName
		)
		) result
		GROUP BY
		result.provinceName
		) result2
	</select>

	<!-- 筛查监控-胃镜检查 -->
	<select id="getGastroscopeTestDTOs"
		resultType="com.sict.excel.dto.GastroscopeTestDTO">
		SELECT
		sum(actual) actualGastroscope,
		sum(total)
		planGastroscope
		FROM
		(
		(
		SELECT
		COUNT(DISTINCT p2.patient_id) total,
		COUNT(NULL) actual
		FROM
		tb_patient p1 INNER JOIN tb_risk_assess p2
		ON p1.id = p2.patient_id RIGHT JOIN (SELECT `name`,number FROM tb_hosp
		)rel2 ON rel2.number = p1.hosp_number
		WHERE p2.grade > 1
		AND p2.step = 0
		)
		UNION
		(
		SELECT
		COUNT(NULL)
		total,
		COUNT(p2.capsule) actual
		FROM
		tb_patient
		p1 INNER JOIN tb_gastro_inspect p2
		on p1.id = p2.patient_id RIGHT JOIN
		(SELECT `name`,number FROM tb_hosp )rel2 ON rel2.number =
		p1.hosp_number
		WHERE p2.capsule = 1
		)
		) rell
	</select>

	<!-- 诊断结果 -->
	<select id="getDiagnosisDTO"
		resultType="com.sict.excel.dto.DiagnosisDTO">
		SELECT
		count( gastroDetailInfo LIKE '%"type_a":"a"%' OR
		gastroDetailInfo LIKE
		'%"type_f":"a"%' OR NULL ) AS higeLevel,
		count(
		gastroDetailInfo LIKE '%"type_a":"a"%' OR NULL ) AS highEsophageal,
		count( gastroDetailInfo LIKE '%"type_f":"a"%' OR NULL ) AS
		highStomach,
		count( gastroDetailInfo LIKE '%"type_a":"b"%' OR
		gastroDetailInfo LIKE
		'%"type_f":"b"%' OR NULL ) AS lowLevel,
		count(
		gastroDetailInfo LIKE '%"type_a":"b"%' OR NULL ) AS lowEsophageal,
		count( gastroDetailInfo LIKE '%"type_f":"b"%' OR NULL ) AS lowStomach,
		count( gastroDetailInfo LIKE '%"g":1%' OR NULL ) AS
		earlyGastricCancer,
		count( gastroDetailInfo LIKE '%"h":1%' OR NULL ) AS
		advancedGastricCancer,
		count( gastroDetailInfo LIKE '%"b":1%' OR NULL )
		AS
		earlyEsophagealCancer,
		count( gastroDetailInfo LIKE '%"c":1%' OR NULL
		) AS
		advancedEsophagealCancer,
		count( gastroDetailInfo LIKE '%"i":1%' OR
		NULL ) AS atrophicGastritis,
		count( gastroDetailInfo LIKE '%"j":1%' OR
		NULL ) AS gastricUlcer,
		count( gastroDetailInfo LIKE '%"k":1%' OR NULL
		) AS gastricPolyps
		FROM
		(
		SELECT
		p.id patientId,
		rel1.step gastroStep,
		rel1.is_inspect isGastro,
		rel1.inspect_way gastroWay,
		rel1.capsule
		gastroResult,
		rel1.precise_way
		gastroDetailWay,
		rel1.diagnose_result
		gastroDetailResult,
		rel1.diagnose_info gastroDetailInfo,
		rel1.`status`
		auditStatus
		FROM
		tb_patient p
		INNER JOIN (
		SELECT
		patient_id,
		step,
		is_inspect,
		inspect_way,
		capsule,
		precise_way,
		diagnose_result,
		diagnose_info,
		`status`
		FROM
		tb_gastro_inspect
		WHERE
		is_inspect = 1
		AND
		`status` NOT IN ( 0, 5 )
		AND diagnose_result = 1
		) rel1 ON
		rel1.patient_id = p.id
		INNER JOIN (SELECT `name`,number FROM tb_hosp )rel2 ON rel2.number =
		p.hosp_number
		) rel

	</select>

	<select id="getRiskDTOs" resultType="com.sict.excel.dto.RiskDTO">
		SELECT
		胃癌患病风险等级 as grade,
		count( * ) total
		FROM
		(
		SELECT
		p.id 患者id,
		rel1.score
		胃癌患病风险评分,
		CASE
		rel1.grade
		WHEN 1 THEN
		'low'
		WHEN 2 THEN
		'medium'
		WHEN 3 THEN
		'high' ELSE ''
		END 胃癌患病风险等级
		FROM
		tb_patient p
		INNER JOIN ( SELECT
		patient_id, score,
		grade FROM tb_risk_assess ) rel1 ON
		rel1.patient_id = p.id
		INNER JOIN (SELECT `name`,number FROM tb_hosp )rel2 ON rel2.number = p.hosp_number
		) rel
		GROUP BY
		胃癌患病风险等级

	</select>

</mapper>