<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~
  ~      Copyright (c) 2018-2025, hnqz All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the pig4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: hnqz
  ~
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.qunzhixinxi.hnqz.admin.mapper.WmScorePackageMapper">

	<resultMap id="wmScorePackageMap" type="com.qunzhixinxi.hnqz.admin.entity.WmScorePackage">
		<id property="id" column="id"/>
		<result property="taskRuleId" column="task_rule_id"/>
		<result property="drugEntId" column="drug_ent_id"/>
		<result property="scoreId" column="score_id"/>
		<result property="scoreName" column="score_name"/>
		<result property="relationScoreId" column="relation_score_id"/>
		<result property="score" column="score"/>
		<result property="packageStatus" column="package_status"/>
		<result property="packageFinishStatus" column="package_finish_status"/>
		<result property="packageAuditStatus" column="package_audit_status"/>
		<result property="sendPackageDeptId" column="send_package_dept_id"/>
		<result property="agentTypeId" column="agent_type_id"/>
		<result property="level1Id" column="level1_id"/>
		<result property="level2Id" column="level2_id"/>
		<result property="zbId" column="zb_id"/>
		<result property="xxdbId" column="xxdb_id"/>
		<result property="typeid" column="typeid"/>
		<result property="deptId" column="dept_id"/>
		<result property="delFlag" column="del_flag"/>
		<result property="enableFlag" column="enable_flag"/>
		<result property="tenantId" column="tenant_id"/>
		<result property="createTime" column="create_time"/>
		<result property="createUser" column="create_user"/>
		<result property="updateTime" column="update_time"/>
		<result property="settlementDate" column="settlement_date"/>
		<result property="updateUser" column="update_user"/>
		<result property="isConduct" column="is_conduct"/>
		<result property="receiveDate" column="receive_date"/>
		<result property="settleAmount" column="settle_amount"/>
		<result property="discount" column="discount"/>
		<result property="settleStatus" column="settle_status"/>
		<result property="invoiceCategory" column="invoice_category"/>
		<result property="remark" column="remark"/>
		<result property="certStatus" column="cert_status"/>
		<result property="belongDate" column="belong_date"/>
		<result property="allocationDate" column="allocation_date"/>
		<result property="payRate" column="pay_rate"/>
		<result property="payScore" column="pay_score"/>
		<result property="acceptSug" column="accept_sug"/>
		<result property="finishRate" column="finish_rate"/>
		<result property="ysh" column="ysh"/>
		<result property="shtg" column="shtg"/>
		<result property="shbtg" column="shbtg"/>
		<result property="shtgnum" column="shtgnum"/>
		<result property="shbtgnum" column="shbtgnum"/>
		<result property="shtgtt" column="shtgtt"/>
		<result property="shbtgtt" column="shbtgtt"/>
		<result property="packageType1" column="package_type1"/>
		<result property="description" column="description"/>
		<result property="kfpjf" column="kfpjf"/>
		<result property="relatedService" column="related_service"/>
		<result property="scorePackageName" column="score_package_name"/>
		<result property="endLabelDate" column="end_label_date"  />
		<result property="packageIsConduct" column="package_is_conduct"  />
		<result property="ruleId" column="rule_id"  />
		<result property="packageFinishTime" column="package_finish_time"/>
		<result property="packageType2" column="package_type2"/>
		<result property="userNum" column="user_num"/>
		<result property="taskNum" column="task_num"/>
		<result property="packageUserScope" column="package_user_scope"/>
		<result property="userScore" column="user_score"/>
		<result property="enableType" column="enable_type"/>
		<result property="isReceive" column="is_receive"/>
		<result property="settleFlag" column="settle_flag"/>
		<result property="oldPackage" column="old_package"/>
		<result property="scorePackageStatus" column="score_package_status"/>
		<result property="subType" column="sub_type"/>
		<result property="drugProducerList" column="drug_producer"/>

	</resultMap>

	<select id="listWmScorePackageAndTask" resultMap="wmScorePackageMap">
		select id, task_rule_id, drug_ent_id, score_id, score_name, score, agent_type_id, level1_id, level2_id, zb_id,
		xxdb_id, typeid, package_status, dept_id, del_flag, enable_flag, tenant_id, create_time, create_user,settlement_date,
		update_time, update_user
		,bb.ytj,bb.ysh,bb.dsh from wm_score_package,(
		select score_package_id,sum(if(task_status>0,score,0)) ytj,sum(if(task_status=3,score,0))
		ysh,sum(if(task_status=2,score,0)) dsh from wm_task where score_package_id is not null and wm_task.del_flag=0
		and wm_task.enable_flag=0 and wm_task.real_flag = '0' group by score_package_id)
		bb
		<where>
			wm_score_package.id =bb.score_package_id
			and wm_score_package.del_flag='0' and wm_score_package.enable_flag='0'
			<if test="query.taskRuleId != null and query.taskRuleId != ''">
				AND wm_score_package.task_rule_id= #{query.taskRuleId}
			</if>
			<if test="query.deptId != null and query.deptId != ''">
				AND wm_score_package.dept_id= #{query.deptId}
			</if>
		</where>
	</select>

	<select id="selectByRelationScoreIdList" resultMap="wmScorePackageMap">
		select
		*
		from wm_score_package
		where
		relation_score_id = #{relationScoreId,jdbcType=VARCHAR}
	</select>

	<select id="listWmScorePackageAndTaskExt" resultMap="wmScorePackageMap">
		select id, task_rule_id, drug_ent_id, score_id, score_name, score, agent_type_id, level1_id, level2_id, zb_id,score_package_status,settlement_date,
		xxdb_id, typeid, package_finish_status, dept_id, del_flag, enable_flag, tenant_id, create_time, create_user,send_package_dept_id,old_package,
		update_time, update_user,belong_date,allocation_date,score_package_name,package_type1,relation_score_id,ywctt,settle_status,finish_rate
		,bb.ytj,bb.ysh,bb.dsh from wm_score_package LEFT JOIN (
		select score_package_id,sum(if(task_status>0,score,0)) ytj,sum(if(task_status=3,score,0))
		ysh,sum(if(task_status=2,score,0)) dsh from wm_task where score_package_id is not null and wm_task.del_flag=0
		and wm_task.enable_flag=0 group by score_package_id)
		bb
		on wm_score_package.id =bb.score_package_id
		<where>
			wm_score_package.del_flag='0' and wm_score_package.enable_flag='0'
			and wm_score_package.package_type1='1'
			and wm_score_package.old_package!='1'
			<if test="query.taskRuleId != null and query.taskRuleId != ''">
				AND wm_score_package.task_rule_id= #{query.taskRuleId}
			</if>
			<if test="query.drugEntId != null and query.drugEntId != ''">
				AND wm_score_package.drug_ent_id= #{query.drugEntId}
			</if>
			<if test="query.agentTypeId != null and query.agentTypeId != ''">
				AND wm_score_package.agent_type_id= #{query.agentTypeId}
			</if>

			<if test="query.level1Id != null and query.level1Id != ''">
				AND wm_score_package.level1_id= #{query.level1Id}
			</if>
			<if test="query.deptId != null and query.deptId != ''">
				AND wm_score_package.dept_id= #{query.deptId}
			</if>

			<if test="query.typeid != null and query.typeid != ''">
				AND wm_score_package.typeid=#{query.typeid}
			</if>

			<if test="query.id != null and query.id != ''">
				AND wm_score_package.id like CONCAT('%',#{query.id},'%')
			</if>
			<if test="query.scorePackageStatus != null and query.scorePackageStatus != ''">
				AND wm_score_package.score_package_status like CONCAT('%',#{query.scorePackageStatus},'%')
			</if>
			<if test="query.scorePackageName != null and query.scorePackageName != ''">
				AND wm_score_package.score_package_name like CONCAT('%',#{query.scorePackageName},'%')
			</if>

			<if test="query.packageStatus != null and query.packageStatus != ''">
				AND wm_score_package.package_status like CONCAT('%',#{query.packageStatus},'%')
			</if>

			<if test="query.sendPackageDeptId != null and query.sendPackageDeptId != ''">
				AND wm_score_package.send_package_dept_id like CONCAT('%',#{query.sendPackageDeptId},'%')
			</if>

			<if test="query.packageFinishStatus != null and query.packageFinishStatus != ''">
				AND wm_score_package.package_finish_status=#{query.packageFinishStatus}
			</if>

			order by id desc
		</where>
	</select>


	<select id="listWmScorePackageAndTaskExtOld" resultMap="wmScorePackageMap">
		select id, task_rule_id, drug_ent_id, score_id, score_name, score, agent_type_id, level1_id, level2_id, zb_id,score_package_status,settlement_date,
		xxdb_id, typeid, package_finish_status, dept_id, del_flag, enable_flag, tenant_id, create_time, create_user,send_package_dept_id,old_package,
		update_time, update_user,belong_date,allocation_date,score_package_name,package_type1,relation_score_id,ywctt,settle_status,finish_rate
		,bb.ytj,bb.ysh,bb.dsh from wm_score_package LEFT JOIN (
		select score_package_drug_id,sum(if(task_status>0,score,0)) ytj,sum(if(task_status=3,score,0))
		ysh,sum(if(task_status=2,score,0)) dsh from wm_task where score_package_id is not null and wm_task.del_flag=0
		and wm_task.enable_flag=0 group by score_package_drug_id)
		bb
		on wm_score_package.id =bb.score_package_drug_id
		<where>
			wm_score_package.del_flag='0' and wm_score_package.enable_flag='0'
			and wm_score_package.package_type1='1'
			and wm_score_package.old_package='1'
			<if test="query.taskRuleId != null and query.taskRuleId != ''">
				AND wm_score_package.task_rule_id= #{query.taskRuleId}
			</if>
			<if test="query.drugEntId != null and query.drugEntId != ''">
				AND wm_score_package.drug_ent_id= #{query.drugEntId}
			</if>
			<if test="query.agentTypeId != null and query.agentTypeId != ''">
				AND wm_score_package.agent_type_id= #{query.agentTypeId}
			</if>

			<if test="query.level1Id != null and query.level1Id != ''">
				AND wm_score_package.level1_id= #{query.level1Id}
			</if>
			<if test="query.deptId != null and query.deptId != ''">
				AND wm_score_package.dept_id= #{query.deptId}
			</if>

			<if test="query.typeid != null and query.typeid != ''">
				AND wm_score_package.typeid=#{query.typeid}
			</if>

			<if test="query.id != null and query.id != ''">
				AND wm_score_package.id like CONCAT('%',#{query.id},'%')
			</if>
			<if test="query.scorePackageStatus != null and query.scorePackageStatus != ''">
				AND wm_score_package.score_package_status like CONCAT('%',#{query.scorePackageStatus},'%')
			</if>
			<if test="query.scorePackageName != null and query.scorePackageName != ''">
				AND wm_score_package.score_package_name like CONCAT('%',#{query.scorePackageName},'%')
			</if>

			<if test="query.packageStatus != null and query.packageStatus != ''">
				AND wm_score_package.package_status like CONCAT('%',#{query.packageStatus},'%')
			</if>

			<if test="query.sendPackageDeptId != null and query.sendPackageDeptId != ''">
				AND wm_score_package.send_package_dept_id like CONCAT('%',#{query.sendPackageDeptId},'%')
			</if>

			<if test="query.packageFinishStatus != null and query.packageFinishStatus != ''">
				AND wm_score_package.package_finish_status=#{query.packageFinishStatus}
			</if>

			order by id desc
		</where>
	</select>



	<select id="listWmScorePackageAndTaskExt2" resultMap="wmScorePackageMap">
		select id, task_rule_id, drug_ent_id, score_id, score_name, score, agent_type_id, level1_id, level2_id, zb_id,score_package_status,settlement_date,
		xxdb_id, typeid, package_finish_status, dept_id, del_flag, enable_flag, tenant_id, create_time, create_user,send_package_dept_id,old_package,
		update_time, update_user,belong_date,allocation_date,score_package_name,package_type1,relation_score_id,ywctt,settle_status,finish_rate
		,bb.ytj,bb.ysh,bb.dsh from wm_score_package LEFT JOIN (
		select score_package_id,sum(if(task_status>0,score,0)) ytj,sum(if(task_status=3,score,0))
		ysh,sum(if(task_status=2,score,0)) dsh from wm_task where score_package_id is not null and wm_task.del_flag=0
		and wm_task.enable_flag=0 and wm_task.real_flag = '0' group by score_package_id)
		bb
		on wm_score_package.id =bb.score_package_id
		<where>
			wm_score_package.del_flag='0' and wm_score_package.enable_flag='0'
			and wm_score_package.package_type1='1'
			and wm_score_package.old_package!='1'
			<if test="query.taskRuleId != null and query.taskRuleId != ''">
				AND wm_score_package.task_rule_id= #{query.taskRuleId}
			</if>
			<if test="query.drugEntId != null and query.drugEntId != ''">
				AND wm_score_package.drug_ent_id= #{query.drugEntId}
			</if>
			<if test="query.agentTypeId != null and query.agentTypeId != ''">
				AND wm_score_package.agent_type_id= #{query.agentTypeId}
			</if>
			<if test="query.typeid != null and query.typeid != ''">
				AND wm_score_package.typeid=#{query.typeid}
			</if>
			<if test="query.level1Id != null and query.level1Id != ''">
				AND wm_score_package.level1_id= #{query.level1Id}
			</if>
			<if test="query.deptId != null and query.deptId != ''">
				AND wm_score_package.dept_id= #{query.deptId}
			</if>
			<if test="query.level2Id != null and query.level2Id != ''">
				AND wm_score_package.level2_id= #{query.level2Id}
			</if>
			<if test="query.scoreId != null and query.scoreId != ''">
				AND wm_score_package.score_id like CONCAT('%',#{query.scoreId},'%')
			</if>
			<if test="query.scorePackageStatus != null and query.scorePackageStatus != ''">
				AND wm_score_package.score_package_status like CONCAT('%',#{query.scorePackageStatus},'%')
			</if>
			<if test="query.packageStatus != null and query.packageStatus != ''">
				AND wm_score_package.package_status like CONCAT('%',#{query.packageStatus},'%')
			</if>

			<if test="query.sendPackageDeptId != null and query.sendPackageDeptId != ''">
				AND wm_score_package.send_package_dept_id like CONCAT('%',#{query.sendPackageDeptId},'%')
			</if>

		</where>
		order by id desc
	</select>


	<select id="listWmScorePackageAndTaskExt2Old" resultMap="wmScorePackageMap">
		select id, task_rule_id, drug_ent_id, score_id, score_name, score, agent_type_id, level1_id, level2_id, zb_id,score_package_status,settlement_date,
		xxdb_id, typeid, package_finish_status, dept_id, del_flag, enable_flag, tenant_id, create_time, create_user,send_package_dept_id,old_package,
		update_time, update_user,belong_date,allocation_date,score_package_name,package_type1,relation_score_id,ywctt,settle_status,finish_rate
		,bb.ytj,bb.ysh,bb.dsh from wm_score_package LEFT JOIN (
		select score_package_id,sum(if(task_status>0,score,0)) ytj,sum(if(task_status=3,score,0))
		ysh,sum(if(task_status=2,score,0)) dsh from wm_task where score_package_id is not null and wm_task.del_flag=0
		and wm_task.enable_flag=0 and wm_task.real_flag = '0' group by score_package_id)
		bb
		on wm_score_package.id =bb.score_package_id
		<where>
			wm_score_package.del_flag='0' and wm_score_package.enable_flag='0'
			and wm_score_package.package_type1='1'
			and wm_score_package.old_package='1'
			<if test="query.taskRuleId != null and query.taskRuleId != ''">
				AND wm_score_package.task_rule_id= #{query.taskRuleId}
			</if>
			<if test="query.drugEntId != null and query.drugEntId != ''">
				AND wm_score_package.drug_ent_id= #{query.drugEntId}
			</if>
			<if test="query.agentTypeId != null and query.agentTypeId != ''">
				AND wm_score_package.agent_type_id= #{query.agentTypeId}
			</if>
			<if test="query.typeid != null and query.typeid != ''">
				AND wm_score_package.typeid=#{query.typeid}
			</if>
			<if test="query.level1Id != null and query.level1Id != ''">
				AND wm_score_package.level1_id= #{query.level1Id}
			</if>
			<if test="query.deptId != null and query.deptId != ''">
				AND wm_score_package.dept_id= #{query.deptId}
			</if>
			<if test="query.level2Id != null and query.level2Id != ''">
				AND wm_score_package.level2_id= #{query.level2Id}
			</if>
			<if test="query.scoreId != null and query.scoreId != ''">
				AND wm_score_package.score_id like CONCAT('%',#{query.scoreId},'%')
			</if>
			<if test="query.scorePackageStatus != null and query.scorePackageStatus != ''">
				AND wm_score_package.score_package_status like CONCAT('%',#{query.scorePackageStatus},'%')
			</if>
			<if test="query.packageStatus != null and query.packageStatus != ''">
				AND wm_score_package.package_status like CONCAT('%',#{query.packageStatus},'%')
			</if>

			<if test="query.sendPackageDeptId != null and query.sendPackageDeptId != ''">
				AND wm_score_package.send_package_dept_id like CONCAT('%',#{query.sendPackageDeptId},'%')
			</if>

		</where>
	</select>


	<select id="listWmScorePackageAndTaskLevel1" resultMap="wmScorePackageMap">
		select id, task_rule_id, drug_ent_id, score_id, score_name, score, agent_type_id, level1_id, level2_id, zb_id,
		xxdb_id, typeid, package_status, dept_id, del_flag, enable_flag, tenant_id, create_time, create_user,settlement_date,
		update_time, update_user
		,bb.ytj,bb.ysh,bb.dsh from wm_score_package,(
		select score_package_id,sum(if(task_status>0,score,0)) ytj,sum(if(task_status=3,score,0))
		ysh,sum(if(task_status=2,score,0)) dsh from wm_task where score_package_id is not null and wm_task.del_flag=0
		and wm_task.enable_flag=0 and wm_task.real_flag = '0' group by score_package_id)
		bb
		<where>
			wm_score_package.id =bb.score_package_id
			and wm_score_package.del_flag='0' and wm_score_package.enable_flag='0'
			<if test="query.taskRuleId != null and query.taskRuleId != ''">
				AND wm_score_package.task_rule_id= #{query.taskRuleId}
			</if>
			<if test="query.drugEntId != null and query.drugEntId != ''">
				AND wm_score_package.drug_ent_id= #{query.drugEntId}
			</if>
			<if test="query.agentTypeId != null and query.agentTypeId != ''">
				AND wm_score_package.agent_type_id= #{query.agentTypeId}
			</if>
			<if test="query.level1Id != null and query.level1Id != ''">
				AND wm_score_package.level1_id= #{query.level1Id}
			</if>
		</where>
	</select>


	<select id="listWmScorePackageAndTaskLevel3" resultMap="wmScorePackageMap">
		select id, task_rule_id, drug_ent_id, score_id, score_name, score, agent_type_id, level1_id, level2_id, zb_id,
		xxdb_id, typeid, package_status, dept_id, del_flag, enable_flag, tenant_id, create_time, create_user,settlement_date,
		update_time, update_user
		,bb.ytj,bb.ysh,bb.dsh from wm_score_package,(
		select score_package_id,sum(if(task_status>0,score,0)) ytj,sum(if(task_status=3,score,0))
		ysh,sum(if(task_status=2,score,0)) dsh from wm_task where score_package_id is not null
		and wm_task.del_flag=0 and wm_task.real_flag = '0'
		and wm_task.enable_flag=0 group by score_package_id)
		bb
		<where>
			wm_score_package.id =bb.score_package_id
			and wm_score_package.del_flag='0' and wm_score_package.enable_flag='0'
			<if test="query.taskRuleId != null and query.taskRuleId != ''">
				AND wm_score_package.task_rule_id= #{query.taskRuleId}
			</if>
			<if test="query.drugEntId != null and query.drugEntId != ''">
				AND wm_score_package.drug_ent_id= #{query.drugEntId}
			</if>
			<if test="query.agentTypeId != null and query.agentTypeId != ''">
				AND wm_score_package.agent_type_id= #{query.agentTypeId}
			</if>
			<if test="query.level1Id != null and query.level1Id != ''">
				AND wm_score_package.level1_id= #{query.level1Id}
			</if>
		</where>

		union

		select id, task_rule_id, drug_ent_id, score_id, score_name, score, agent_type_id, level1_id, level2_id, zb_id,
		xxdb_id, typeid, package_status, dept_id, del_flag, enable_flag, tenant_id, create_time, create_user,settlement_date,
		update_time, update_user
		,bb.ytj,bb.ysh,bb.dsh from wm_score_package,(
		select score_package_level1_id,sum(if(task_status>0,score,0)) ytj,sum(if(task_status=3,score,0))
		ysh,sum(if(task_status=2,score,0)) dsh from wm_task where score_package_level1_id is not null and
		wm_task.del_flag=0 and wm_task.real_flag = '0' and wm_task.enable_flag=0 group by score_package_level1_id)
		bb
		<where>
			wm_score_package.id =bb.score_package_level1_id
			and wm_score_package.del_flag='0' and wm_score_package.enable_flag='0'
			<if test="query.taskRuleId != null and query.taskRuleId != ''">
				AND wm_score_package.task_rule_id= #{query.taskRuleId}
			</if>
			<if test="query.drugEntId != null and query.drugEntId != ''">
				AND wm_score_package.drug_ent_id= #{query.drugEntId}
			</if>
			<if test="query.agentTypeId != null and query.agentTypeId != ''">
				AND wm_score_package.agent_type_id= #{query.agentTypeId}
			</if>
			<if test="query.level1Id != null and query.level1Id != ''">
				AND wm_score_package.level1_id= #{query.level1Id}
			</if>
		</where>
	</select>

	<select id="getExportPackageTotalList1" resultMap="wmScorePackageMap">
		select bb.ysh,p.* from wm_score_package p
		left join (
		select score_package_id,sum(if(task_status=3,score,0)) ysh from wm_task
		where score_package_id is not null and wm_task.del_flag=0 and wm_task.enable_flag=0
		group by score_package_id)
		bb on p.id =bb.score_package_id
		where p.del_flag='0' and p.enable_flag='0'
		and p.package_type2='1'
		<if test="query.sendPackageDeptId != null and query.sendPackageDeptId != ''">
			AND p.send_package_dept_id= #{query.sendPackageDeptId}
		</if>
		<if test="query.drugEntId != null and query.drugEntId != ''">
			AND p.drug_ent_id= #{query.drugEntId}
		</if>
		<if test="query.scorePackageName != null and query.scorePackageName != ''">
			AND p.score_package_name LIKE CONCAT('%',#{query.scorePackageName},'%')
		</if>
		<if test="query.typeid != null and query.typeid != ''">
			AND p.typeid= #{query.typeid}
		</if>
		<if test="query.packageFinishStatus != null and query.packageFinishStatus != ''">
			AND p.package_finish_status= #{query.packageFinishStatus}
		</if>
		order by p.create_time desc

	</select>

	<select id="getExportPackageTotalList" resultMap="wmScorePackageMap">
		select * from view_wm_socre_package_total
		<where>
			<if test="query.drugsIds != null and query.drugsIds.size()>0">
				AND view_wm_socre_package_total.send_package_dept_id in(
				<foreach collection="query.drugsIds" item="pid" index="index" separator=",">
					#{pid}
				</foreach>
				)
			</if>
			<if test="query.taskRuleId != null and query.taskRuleId != ''">
				AND view_wm_socre_package_total.task_rule_id= #{query.taskRuleId}
			</if>
			<if test="query.drugEntId != null and query.drugEntId != ''">
				AND view_wm_socre_package_total.drug_ent_id= #{query.drugEntId}
			</if>

			<if test="query.scoreId != null and query.scoreId != ''">
				AND view_wm_socre_package_total.score_id LIKE CONCAT('%',#{query.scoreId},'%')
			</if>

			<if test="query.scoreName != null and query.scoreName != ''">
				AND view_wm_socre_package_total.score_name LIKE CONCAT('%',#{query.scoreName},'%')
			</if>

			<if test="query.agentTypeId != null and query.agentTypeId != ''">
				AND view_wm_socre_package_total.agent_type_id= #{query.agentTypeId}
			</if>

			<if test="query.typeid != null and query.typeid != ''">
				AND view_wm_socre_package_total.typeid= #{query.typeid}
			</if>
			<if test="query.scoreName != null and query.scoreName != ''">
				AND view_wm_socre_package_total.score_name LIKE CONCAT('%',#{query.scoreName},'%')
			</if>

			<if test="query.updateUser != null and query.updateUser != ''">
				AND view_wm_socre_package_total.t_type=#{query.updateUser}
			</if>

			<if test="query.zbId != null and query.zbId != ''">
				AND view_wm_socre_package_total.zb_id=#{query.zbId}
			</if>
		</where>
	</select>


	<select id="getPlatTotalData" resultType="java.util.HashMap">
		<![CDATA[
	SELECT
	sum(if(typeid=1 and agent_type_id=1,score,0)) score_drugent,
	sum(IF( (agent_type_id = 2 AND ( level2_id IS NULL OR level2_id = '' )) or (agent_type_id = 2 AND typeid=2), score, 0 )) score_agent,
	sum(if(typeid=3 and agent_type_id=2 and (level2_id is null or level2_id =''),score,0)) score_yydb,
	sum(if(typeid=3 and agent_type_id=2 and (level2_id is null or level2_id =''),ytj,0)) score_yydb_finish,
	sum(if(typeid=4 and agent_type_id=2 and (level2_id is null or level2_id =''),score,0)) score_zbdb,
	sum(if(typeid=4 and agent_type_id=2 and (level2_id is null or level2_id =''),ytj,0)) score_zbdb_finish,
	sum(if(agent_type_id=2 and typeid=2 and (level2_id is not null and level2_id <>''),score,0)) score_csoxf,
	sum(if(agent_type_id=2 and typeid=2 and (level2_id is not null and level2_id <>''),ytj,0)) score_edwc,
	(select count(DISTINCT sys_user.user_id) from sys_user_role,sys_user where sys_user_role.user_id=sys_user.user_id and  sys_user_role.role_id=5 and sys_user.del_flag=0	]]>
		<if test="query.drugEntId != null and query.drugEntId != ''">
			AND sys_user.drug_ent_id= #{query.drugEntId}
		</if>

		<if test="query.level1Id != null and query.level1Id != ''">
			AND sys_user.dept_id in (select sys_dept.dept_id from sys_dept where level2_id=#{query.level1Id} and
			`level`=3)
		</if>

		<if test="query.delFlag != null">
			AND sys_user.create_time>= #{query.delFlag}
		</if>

		<if test="query.enableFlag != null">
			<![CDATA[ AND sys_user.create_time<= #{query.enableFlag}]]>
		</if>
		) user_xxdb,
		(select count(DISTINCT sys_user.user_id) from sys_user_role,sys_user where
		sys_user_role.user_id=sys_user.user_id and sys_user_role.role_id=6 and sys_user.del_flag=0
		<if test="query.drugEntId != null and query.drugEntId != ''">
			AND sys_user.drug_ent_id= #{query.drugEntId}
		</if>

		<if test="query.level1Id != null and query.level1Id != ''">
			AND sys_user.dept_id in (select sys_dept.dept_id from sys_dept where level2_id=#{query.level1Id} and
			`level`=3)
		</if>

		<if test="query.delFlag != null">
			AND sys_user.create_time>= #{query.delFlag}
		</if>

		<if test="query.enableFlag != null">
			<![CDATA[ AND sys_user.create_time<= #{query.enableFlag}]]>
		</if>
		) user_zbdb
		FROM
		view_wm_socre_package_total

		<where>
			<if test="query.drugEntId != null and query.drugEntId != ''">
				AND view_wm_socre_package_total.drug_ent_id= #{query.drugEntId}
			</if>

			<if test="query.level1Id != null and query.level1Id != ''">
				AND view_wm_socre_package_total.level1_id= #{query.level1Id}
			</if>

			<if test="query.delFlag != null">
				AND view_wm_socre_package_total.create_time>= #{query.delFlag}
			</if>

			<if test="query.enableFlag != null">
				<![CDATA[ AND view_wm_socre_package_total.create_time<= #{query.enableFlag}]]>
			</if>
		</where>
	</select>

	<select id="selectWmScorePackageList" resultMap="wmScorePackageMap">
		select v.*,s.cert_status from view_wm_socre_package_total v
		LEFT JOIN sys_user s
		ON v.zb_id = s.user_id

		<where>
			<if test="query.drugsIds != null and query.drugsIds.size()>0">
				AND v.send_package_dept_id in(
				<foreach collection="query.drugsIds" item="pid" index="index" separator=",">
					#{pid}
				</foreach>
				)
			</if>
			<if test="query.taskRuleId != null and query.taskRuleId != ''">
				AND v.task_rule_id= #{query.taskRuleId}
			</if>
			<if test="query.drugEntId != null and query.drugEntId != ''">
				AND v.drug_ent_id= #{query.drugEntId}
			</if>

			<if test="query.scoreId != null and query.scoreId != ''">
				AND v.score_id LIKE CONCAT('%',#{query.scoreId},'%')
			</if>

			<if test="query.scoreName != null and query.scoreName != ''">
				AND v.score_name LIKE CONCAT('%',#{query.scoreName},'%')
			</if>

			<if test="query.agentTypeId != null and query.agentTypeId != ''">
				AND v.agent_type_id= #{query.agentTypeId}
			</if>

			<if test="query.typeid != null and query.typeid != ''">
				AND v.typeid= #{query.typeid}
			</if>

			<if test="query.level2Id != null and query.level2Id != ''">
				AND v.level2_id= #{query.level2Id}
			</if>

			<if test="query.level1Id != null and query.level1Id != ''">
				AND v.level1_id= #{query.level1Id}
			</if>

			<if test="query.scoreName != null and query.scoreName != ''">
				AND v.score_name LIKE CONCAT('%',#{query.scoreName},'%')
			</if>

			<if test="query.updateUser != null and query.updateUser != ''">
				AND v.t_type=#{query.updateUser}
			</if>
			<if test="query.zbId != null and query.zbId != ''">
				AND v.zb_id=#{query.zbId}
			</if>
			AND package_finish_status=1
			ORDER BY v.id DESC
		</where>
	</select>

	<select id="selectWmScorePackageByCode" resultMap="wmScorePackageMap" parameterType="java.util.HashMap">
		select
		score_id,score_package_name,relation_score_id
		from wm_score_package where 1=1 and enable_flag='0'
		<if test="id != null">
			AND id != #{id,jdbcType=BIGINT}
		</if>
		<if test="scoreId != null and scoreId !=''">
			AND score_id = #{scoreId,jdbcType=VARCHAR}
		</if>
		<if test="scorePackageName != null and scorePackageName !=''">
			AND score_package_name = #{scorePackageName,jdbcType=VARCHAR}
		</if>
		<if test="relationScoreId != null and relationScoreId!=''">
			AND relation_score_id = #{relationScoreId,jdbcType=VARCHAR}
		</if>
		order by id desc
	</select>

	<select id="selectByScoreId" resultMap="wmScorePackageMap" parameterType="java.lang.String" >
		select o.*,u.name as acceptSug
		from wm_score_package o
		left join  sys_dept u on u.dept_id=o.accept_sug
		where o.id = #{id,jdbcType=INTEGER}
	</select>

	<select id="selectByScoreIds" resultMap="wmScorePackageMap" parameterType="java.lang.String" >
		select
		*
		from wm_score_package
		where relation_score_id = #{relationScoreId,jdbcType=VARCHAR}
	</select>

	<select id="selectByName" resultMap="wmScorePackageMap" parameterType="java.lang.String" >
		select
		*
		from wm_score_package
		where
		score_package_name = #{scorePackageName,jdbcType=VARCHAR}
		and
		dept_id = #{deptId,jdbcType=VARCHAR}
	</select>

	<select id="selectWmScorePackageLists" resultMap="wmScorePackageMap" parameterType="java.util.HashMap">
		select o.*,u.name as acceptSug
		from wm_score_package o
		left join  sys_dept u on u.dept_id=o.accept_sug
		where 1 = 1 and o.typeid not in (3,4)
		<if test="query.id != null">
			AND o.id != #{query.id,jdbcType=INTEGER}
		</if>
		<if test="query.scoreId != null and query.scoreId !=''">
			AND o.score_id = #{query.scoreId,jdbcType=VARCHAR}
		</if>
		<if test="query.scoreName != null and query.scoreName!=''">
			AND o.score_name = #{query.scoreName,jdbcType=VARCHAR}
		</if>
		<if test="query.scorePackageName != null and query.scorePackageName !=''">
			AND o.score_package_name like CONCAT('%',#{query.scorePackageName},'%')
		</if>
		<if test="query.relationScoreId != null and query.relationScoreId!=''">
			AND o.relation_score_id = #{query.relationScoreId,jdbcType=VARCHAR}
		</if>
		<if test="query.score != null and query.score!=''">
			AND o.score = #{query.score,jdbcType=VARCHAR}
		</if>
		<if test="query.allocationDate != null and query.allocationDate!='' ">
			AND o.allocation_date <![CDATA[>=]]> #{query.allocationDate,jdbcType=TIMESTAMP}
		</if>
		<if test="query.endLabelDate != null and query.endLabelDate!=''">
			AND o.allocation_date <![CDATA[<=]]>#{query.endLabelDate,jdbcType=TIMESTAMP}
		</if>
		<if test="query.belongDate != null ">
			AND o.belong_date = #{query.belongDate,jdbcType=VARCHAR}
		</if>
		<if test="query.packageFinishStatus != null and query.packageFinishStatus!=''">
			AND o.package_finish_status = #{query.packageFinishStatus,jdbcType=VARCHAR}
		</if>
		<if test="query.ysh != null">
			AND o.ysh = #{query.ysh,jdbcType=INTEGER}
		</if>
		<if test="query.finishRate != null">
			AND o.finish_rate = #{query.finishRate,jdbcType=INTEGER}
		</if>
		<if test="query.payScore != null and query.payScore!=''">
			AND o.pay_score = #{query.payScore,jdbcType=INTEGER}
		</if>
		<if test="query.payRate != null">
			AND o.pay_rate = #{query.payRate,jdbcType=INTEGER}
		</if>
		<if test="query.sendPackageDeptId != null and query.sendPackageDeptId!=''">
			AND o.send_package_dept_id = #{query.sendPackageDeptId,jdbcType=VARCHAR}
		</if>
		<if test="query.acceptSug != null">
			AND o.accept_sug = #{query.acceptSug,jdbcType=VARCHAR}
		</if>
		<if test="query.deptId != null and query.deptId!=''">
			AND o.dept_id = #{query.deptId,jdbcType=VARCHAR}
		</if>
		<if test="query.packageType1 != null and query.packageType1!=''">
			AND o.package_type1 = #{query.packageType1,jdbcType=VARCHAR}
		</if>
		<if test="query.oldPackage != null and query.oldPackage!=''">
			AND o.old_package = #{query.oldPackage,jdbcType=VARCHAR}
		</if>
		order by id desc
	</select>


	<select id="listWmScorePackages" resultMap="wmScorePackageMap" parameterType="java.util.HashMap">
		select o.*,u.name as acceptSug
		from wm_score_package o
		left join  sys_dept u on u.dept_id=o.accept_sug
		where 1 = 1 and enable_flag='0'
		<if test="id != null">
			AND o.id != #{id,jdbcType=INTEGER}
		</if>
		<if test="scoreId != null and scoreId !=''">
			AND o.score_id = #{scoreId,jdbcType=VARCHAR}
		</if>
		<if test="scoreName != null and scoreName!=''">
			AND o.score_name = #{scoreName,jdbcType=VARCHAR}
		</if>
		<if test="scorePackageName != null and scorePackageName !=''">
			AND o.score_package_name = #{scorePackageName,jdbcType=VARCHAR}
		</if>
		<if test="relationScoreId != null and relationScoreId!=''">
			AND o.relation_score_id = #{relationScoreId,jdbcType=VARCHAR}
		</if>
		<if test="score != null and score!=''">
			AND o.score = #{score,jdbcType=VARCHAR}
		</if>
		<if test="allocationDate != null and allocationDate!='' ">
			AND o.allocation_date <![CDATA[>=]]> #{allocationDate,jdbcType=TIMESTAMP}
		</if>
		<if test="endLabelDate != null and endLabelDate!=''">
			AND o.allocation_date <![CDATA[<=]]>#{endLabelDate,jdbcType=TIMESTAMP}
		</if>
		<if test="belongDate != null ">
			AND o.belong_date = #{belongDate,jdbcType=VARCHAR}
		</if>
		<if test="packageFinishStatus != null and packageFinishStatus!=''">
			AND o.package_finish_status = #{packageFinishStatus,jdbcType=VARCHAR}
		</if>
		<if test="ysh != null">
			AND o.ysh = #{ysh,jdbcType=INTEGER}
		</if>
		<if test="finishRate != null">
			AND o.finish_rate = #{finishRate,jdbcType=INTEGER}
		</if>
		<if test="payScore != null and payScore!=''">
			AND o.pay_score = #{payScore,jdbcType=INTEGER}
		</if>
		<if test="payRate != null">
			AND o.pay_rate = #{payRate,jdbcType=INTEGER}
		</if>
		<if test="sendPackageDeptId != null and sendPackageDeptId!=''">
			AND o.send_package_dept_id = #{sendPackageDeptId,jdbcType=VARCHAR}
		</if>
		<if test="acceptSug != null">
			AND o.accept_sug = #{acceptSug,jdbcType=VARCHAR}
		</if>
		<if test="deptId != null and deptId!=''">
			AND o.dept_id = #{deptId,jdbcType=VARCHAR}
		</if>
		<if test="drugEntId != null and drugEntId!=''">
			AND o.drug_ent_id = #{drugEntId,jdbcType=VARCHAR}
		</if>
		<if test="level1Id != null and level1Id!=''">
			AND o.level1_id = #{level1Id,jdbcType=VARCHAR}
		</if>
		order by id desc
	</select>

	<select id="selectBywmId" resultMap="wmScorePackageMap" parameterType="java.lang.String" >
		select
		*
		from wm_score_package
		where id = #{id,jdbcType=VARCHAR}
	</select>

	<select id="selectByRelationScoreId" resultMap="wmScorePackageMap" parameterType="java.lang.String" >
		select
		*
		from wm_score_package
		where score_id = #{scoreId,jdbcType=VARCHAR}
	</select>

	<select id="selectRelationScoreId" resultMap="wmScorePackageMap" parameterType="java.lang.String" >
		select
		*
		from wm_score_package
		where id != #{id,jdbcType=VARCHAR} and enable_flag='0' and typeid not in(3,4)
		ORDER BY id DESC
	</select>
	<select id="selectByReRelatedService" resultMap="wmScorePackageMap" parameterType="java.lang.String" >
		select
		*
		from wm_score_package
		where 1=1 and typeid not in(3,4) and enable_flag='0'
		<if test="sendPackageDeptId != null and sendPackageDeptId !=''">
			AND send_package_dept_id = #{sendPackageDeptId,jdbcType=VARCHAR}
		</if>
		<if test="deptId != null and deptId !=''">
			AND dept_id = #{deptId,jdbcType=VARCHAR}
		</if>
		ORDER BY id DESC
	</select>



	<select id="selectByPackage" resultMap="wmScorePackageMap">
	select
	*
	from wm_score_package
	where id = #{id,jdbcType=VARCHAR}
	</select>

	<select id="selectListById" resultType="java.util.HashMap">
	select
	*
	from
	wm_score_package
	where
	id = #{query.id,jdbcType=VARCHAR}
	and
	settle_status = #{query.settleStatus,jdbcType=BIGINT}
	</select>

	<select id="selectLists" resultMap="wmScorePackageMap">
		select
		*
		from
		wm_score_package
		where
		relation_score_id = #{query.relationScoreId,jdbcType=VARCHAR}
	</select>
	<select id="finishRate" resultMap="wmScorePackageMap">
		select sum(ifnull(c.payScore,0)) pay_score ,sum(c.ysh) ysh
		from (
			select 0 as payScore, a.ysh  from (
				select score_package_id,sum(if(task_status=3,score,0)) ysh from wm_task where wm_task.score_package_id in
					(
						select id from wm_score_package where id=#{query.id}
						union all
						select id from wm_score_package where relation_score_id in (select id from wm_score_package where id=#{query.id})
						union all
						select id from wm_score_package   where relation_score_id in (
							select id from wm_score_package where relation_score_id in (select id from wm_score_package where id=#{query.id})
						)
					)
				 and  wm_task.enable_flag=0 and wm_task.real_flag = '0' and score_package_id is not null and wm_task.del_flag=0
				 group by score_package_id
			 )a,wm_score_package b, wm_score_package c where a.score_package_id=b.id and b.relation_score_id=c.id and
				(b.relation_score_id = #{query.id} or c.relation_score_id = #{query.id})
			 union all
				select sum(score) as payScore,0 as ysh from wm_task  where
				    (report_drug_approval_opinion=1 or report_one_approval_opinion=1) and (report_one_id in (
				    	select id from wm_report where report_status=6 and score_id=#{query.id})
					or report_second_id in (select id from wm_report where report_status=6 and score_id=#{query.id})
					or report_drug_id in (select id from wm_report where report_status=6 and score_id=#{query.id})
				)
		) c
	</select>
	<select id="getScorePackagekfpjf" resultMap="wmScorePackageMap">
		select dd.* from (
		select p.id,p.score,p.score_package_name,p.score_id,p.package_type1,p.drug_producer,
		p.score-sum(if(p1.del_flag='0' and p1.enable_flag='0',p1.score,0)) as kfpjf
		from wm_score_package p
		left join wm_score_package p1 on p.id=p1.relation_score_id and p1.del_flag='0' and p1.enable_flag='0'
		<where>
			p.del_flag='0' and p.enable_flag='0' and p.old_package='0'
			<if test="query.deptId != null and query.deptId != ''">
				AND p.dept_id= #{query.deptId}
			</if>
			<if test="query.sendPackageDeptId != null and query.sendPackageDeptId != ''">
				AND p.send_package_dept_id= #{query.sendPackageDeptId}
			</if>
			<if test="query.typeid != null and query.typeid != ''">
				AND p.typeid IN(${query.typeid})
			</if>
			<if test="query.id != null and query.id != ''">
				AND p.id IN(${query.id})
			</if>
		</where>
		group by p.id,p.score,p.score_package_name,p.score_id,p.package_type1
		) dd where dd.kfpjf>0
		order by dd.id
	</select>


	<select id="getScorePackagekList" resultMap="wmScorePackageMap">
		select aa.id,bb.ysh,cc.yjd,cc.yjddsh,bb.dsh,bb.shtg,bb.shbtg,bb.shtgnum,bb.shbtgnum,
		bb.score_package_id as isTask, aa.is_receive,
		bb.ytj,
		CASE WHEN aa.package_type1 = 1 THEN( bb.ysh * 100 / aa.score )
		else ( bb.yshnum * 100 / (aa.user_num*aa.task_num) )END AS yshtt,
		CASE WHEN aa.package_type1 = 1 THEN( bb.shtg * 100 / aa.score )
		else ( bb.shtgnum * 100 / (aa.user_num*aa.task_num) )END AS shtgtt,
		CASE WHEN aa.package_type1 = 1 THEN( bb.shbtg * 100 / aa.score )
		else ( bb.shbtgnum * 100 / (aa.user_num*aa.task_num) )END AS shbtgtt,
		d1.label as package_type1,d2.label as package_type2,
		aa.task_rule_id, aa.drug_ent_id, aa.score_id, aa.score_name,
		aa.score,  aa.zb_id, aa.xxdb_id, aa.typeid,aa.settlement_date,
		aa.package_status, aa.dept_id, aa.del_flag, aa.enable_flag, aa.tenant_id, aa.create_time,
		aa.create_user, aa.update_time, aa.update_user,aa.send_package_dept_id,aa.settle_status,
		aa.score_package_name,p.score_package_name as relation_score_id,aa.package_user_scope,
		aa.user_num,aa.task_num,aa.related_service,aa.package_finish_status,aa.user_score,
		aa.enable_type,aa.settle_flag,aa.score_package_status,aa.belong_date,aa.description,
		aa.sub_type, aa.drug_producer
		from wm_score_package aa left join (
		select score_package_id,
		sum(if(report_drug_approval_opinion =1 or report_one_approval_opinion = 1, score, 0))   shtg,
		sum(if(report_drug_approval_opinion =2 or report_one_approval_opinion = 2, score, 0))   shbtg,
		sum(if(report_drug_approval_opinion =1 or report_one_approval_opinion = 1, 1, 0))   shtgnum,
		sum(if(report_drug_approval_opinion =2 or report_one_approval_opinion = 2, 1, 0))   shbtgnum,
		sum(if(task_status in (1,2,3),score,0)) ytj,sum(if(task_status=3,score,0)) ysh,
		sum(IF( task_status = 3, 1, 0 )) yshnum,sum(if(task_status=2,score,0)) dsh
		from wm_task
		where score_package_id is not null
		and wm_task.del_flag=0 and wm_task.enable_flag=0
		and wm_task.real_flag=0
		  group by score_package_id)
		bb on aa.id =bb.score_package_id
		left join  (select sum(if(`status` IN ('1', '2'), 1, 0)) as yjd,sum(if(`status`=1, 1, 0)) as yjddsh,
		package_id, user_id from wm_score_package_status
		where wm_score_package_status.del_flag='0' and wm_score_package_status.enable_flag='0' group by package_id)
		cc on aa.id=cc.package_id
		left join sys_dict_item d1  on d1.type='package_type1' and d1.`value`=aa.package_type1
		left join sys_dict_item d2  on d2.type='package_type2' and d2.`value`=aa.package_type2
		left join wm_score_package p  on  p.id=aa.relation_score_id
		<where>
			aa.del_flag='0'
			--and aa.enable_flag='0'
			<if test="query.taskRuleId != null and query.taskRuleId != ''">
				AND aa.task_rule_id= #{query.taskRuleId}
			</if>
			<if test="query.deptId != null and query.deptId != ''">
				AND aa.dept_id= #{query.deptId}
			</if>
			<if test="query.sendPackageDeptId != null and query.sendPackageDeptId != ''">
				AND aa.send_package_dept_id= #{query.sendPackageDeptId}
			</if>
			<if test="query.typeid != null and query.typeid != ''">
				AND aa.typeid in (${query.typeid})
			</if>
			<if test="query.packageFinishStatus != null and query.packageFinishStatus != ''">
				AND aa.package_finish_status= #{query.packageFinishStatus}
			</if>
			<if test="query.scorePackageName != null and query.scorePackageName != ''">
				AND aa.score_package_name like CONCAT('%',#{query.scorePackageName},'%')
			</if>
			<if test="query.packageType1 != null and query.packageType1 != ''">
				AND aa.package_type1= #{query.packageType1}
			</if>
			<if test="query.packageType2 != null and query.packageType2 != ''">
				AND aa.package_type2= #{query.packageType2}
			</if>
			<if test="query.scorePackageStatus != null and query.scorePackageStatus != ''">
				AND aa.score_package_status= #{query.scorePackageStatus}
			</if>
			<if test="query.userIdList != null and query.userIdList.size() > 0">
				AND cc.user_id in
				<foreach collection="query.userIdList" item="userId" index="index" open="(" separator="," close=")">
					#{userId}
				</foreach>
			</if>
			<if test="query.sourceType != null and query.sourceType != ''">
				AND aa.source_type = #{query.sourceType}
			</if>
		</where>
		order by aa.create_time desc
	</select>
	<select id="selectRelationScoreIdList" resultMap="wmScorePackageMap">
		select *
		from wm_score_package
		where
		relation_score_id= #{relationScoreId}
	</select>


	<select id="getScorePackagePageById" resultMap="wmScorePackageMap">
	select (b.kfpjf)as kfpjf,a.* from wm_score_package a
			left join
			(select p.id,
			p.score-sum(if(p1.del_flag='0' and p1.enable_flag='0',p1.score,0)) as kfpjf
			from wm_score_package p
			left join wm_score_package p1 on p.id=p1.relation_score_id and p1.del_flag='0' and p1.enable_flag='0'
			where
				p.del_flag='0' and p.enable_flag='0'
			group by p.id) b
			on b.id=a.relation_score_id
			where a.id=#{id}

	</select>
	<select id="getPackageListForReceive" resultType="com.qunzhixinxi.hnqz.admin.entity.input.WmScorePackageApiOutput">
		SELECT p.id, p.score_package_name, p.send_package_dept_id, p.task_add_flag,
		p.create_time, p.package_type1, p.user_num, p.task_num, p.description, p.belong_date,
		p.score, d.name packageEntName
		from wm_score_package p, sys_dept d
		where (p.send_package_dept_id = d.dept_id and p.package_user_scope = '1'
		and p.is_receive = '0' and p.enable_flag = '0' and p.enable_type != '2'
		<if test="query.scorePackageName != null and query.scorePackageName != ''">
			AND p.score_package_name like CONCAT('%',#{query.scorePackageName},'%')
		</if>
		<if test="query.belongDate != null and query.belongDate != ''">
			AND p.belong_date= #{query.belongDate}
		</if>
		<if test="query.deptId != null and query.deptId != ''">
			AND p.dept_id= #{query.deptId}
		</if>
		<if test="query.typeid != null and query.typeid != ''">
			AND p.typeid= #{query.typeid}
		</if>
		<if test="query.packageType2 != null and query.packageType2 != ''">
			AND p.package_type2= #{query.packageType2}
		</if>
		and p.id not in (
		select s.package_id from wm_score_package_status s where s.user_id = #{query.zbId} and s.status != 3
		)
		)
		or
		(p.send_package_dept_id = d.dept_id and p.package_user_scope = '2'
		and p.is_receive = '0' and p.enable_flag = '0' and p.enable_type != '2'
		<if test="query.scorePackageName != null and query.scorePackageName != ''">
			AND p.score_package_name like CONCAT('%',#{query.scorePackageName},'%')
		</if>
		<if test="query.belongDate != null and query.belongDate != ''">
			AND p.belong_date= #{query.belongDate}
		</if>
		<if test="query.typeid != null and query.typeid != ''">
			AND p.typeid= #{query.typeid}
		</if>
		<if test="query.packageType2 != null and query.packageType2 != ''">
			AND p.package_type2= #{query.packageType2}
		</if>
		and p.id not in (
		select s.package_id from wm_score_package_status s where s.user_id = #{query.zbId} and s.status != 3
		)
		)
		order  by p.create_time desc
	</select>

	<select id="listPackageSettleList2" resultType="com.qunzhixinxi.hnqz.admin.entity.input.WmScorePackageSettleOutput">
		SELECT p.id, d. NAME packageEntName, p.score_package_name, p.create_time, p.package_status, p.package_finish_status,
		p.settle_status,p.invoice_category, p.settle_amount ,p.package_type1, p.score, wk.ytj, wk.ysh, wk.dsh
		FROM wm_score_package p, sys_dept d,
		(
		SELECT
		t.score_package_id,
		sum(

		IF (t.task_status > 0, t.score, 0)
		) AS ytj,
		sum(

		IF (t.task_status = 3, t.score, 0)
		) AS ysh,
		sum(

		IF (t.task_status = 2, t.score, 0)
		) AS dsh
		FROM
		wm_task t
		WHERE
		t.score_package_id IS NOT NULL
		AND t.real_flag = '0'
		GROUP BY
		t.score_package_id
		) wk
		WHERE
		p.send_package_dept_id = d.dept_id
		AND p.id = wk.score_package_id
		<if test="query.sendPackageDeptId != null and query.sendPackageDeptId != ''">
			AND p.send_package_dept_id= #{query.sendPackageDeptId}
		</if>
		<if test="query.scorePackageName != null and query.scorePackageName != ''">
			AND p.score_package_name like CONCAT('%',#{query.scorePackageName},'%')
		</if>
		<if test="query.belongDate != null and query.belongDate != ''">
			AND p.belong_date= #{query.belongDate}
		</if>
		<if test="query.typeid != null and query.typeid != ''">
			AND p.typeid= #{query.typeid}
		</if>
		<if test="query.packageType1 != null and query.packageType1 != ''">
			AND p.package_type1= #{query.packageType1}
		</if>
		<if test="query.packageFinishStatus != null and query.packageFinishStatus != ''">
			AND p.package_finish_status= #{query.packageFinishStatus}
		</if>
		<if test="query.packageType2 != null and query.packageType2 != ''">
			AND p.package_type2= #{query.packageType2}
		</if>
		ORDER BY
		p.create_time DESC
	</select>

	<!-- 积分包结算（分页） -->
	<select id="listPackageSettleList" resultType="com.qunzhixinxi.hnqz.admin.entity.input.WmScorePackageSettleOutput">
		SELECT p.id, d.name packageEntName, p.score_package_name, p.create_time, p.package_status, p.package_finish_status,
		p.settle_status,p.invoice_category, p.settle_amount ,p.package_type1, p.score, p.package_type2, p.sub_type
		FROM wm_score_package p, sys_dept d
		WHERE
		p.send_package_dept_id = d.dept_id
		<if test="query.sendPackageDeptId != null and query.sendPackageDeptId != ''">
			AND p.send_package_dept_id= #{query.sendPackageDeptId}
		</if>
		<if test="query.scorePackageName != null and query.scorePackageName != ''">
			AND p.score_package_name like CONCAT('%',#{query.scorePackageName},'%')
		</if>
		<if test="query.belongDate != null and query.belongDate != ''">
			AND p.belong_date= #{query.belongDate}
		</if>
		<if test="query.typeid != null and query.typeid != ''">
			AND p.typeid= #{query.typeid}
		</if>
		<if test="query.packageType1 != null and query.packageType1 != ''">
			AND p.package_type1= #{query.packageType1}
		</if>
		<if test="query.packageFinishStatus != null and query.packageFinishStatus != ''">
			AND p.package_finish_status= #{query.packageFinishStatus}
		</if>
		<if test="query.packageType2 != null and query.packageType2 != ''">
			AND p.package_type2= #{query.packageType2}
		</if>
		<if test="query.settleFlag != null and query.settleFlag != ''">
			AND p.settle_flag= #{query.settleFlag}
		</if>
		<if test="query.subType != null and query.subType != ''">
			AND p.sub_type= #{query.subType}
		</if>
		ORDER BY
		p.create_time DESC
	</select>

	<!-- 积分包结算列表 -->
	<select id="listAllPackageSettle" resultType="com.qunzhixinxi.hnqz.admin.entity.input.WmScorePackageSettleOutput">
		SELECT p.id, d.name packageEntName, p.score_package_name, p.create_time, p.package_status, p.package_finish_status,
		p.settle_status, p.invoice_category, p.settle_amount ,p.package_type1, p.score, p.package_type2, p.sub_type
		FROM wm_score_package p, sys_dept d
		WHERE
		p.send_package_dept_id = d.dept_id
		<if test="query.sendPackageDeptId != null and query.sendPackageDeptId != ''">
			AND p.send_package_dept_id= #{query.sendPackageDeptId}
		</if>
		<if test="query.scorePackageName != null and query.scorePackageName != ''">
			AND p.score_package_name like CONCAT('%',#{query.scorePackageName},'%')
		</if>
		<if test="query.belongDate != null and query.belongDate != ''">
			AND p.belong_date= #{query.belongDate}
		</if>
		<if test="query.typeid != null and query.typeid != ''">
			AND p.typeid= #{query.typeid}
		</if>
		<if test="query.packageType1 != null and query.packageType1 != ''">
			AND p.package_type1= #{query.packageType1}
		</if>
		<if test="query.packageFinishStatus != null and query.packageFinishStatus != ''">
			AND p.package_finish_status= #{query.packageFinishStatus}
		</if>
		<if test="query.packageType2 != null and query.packageType2 != ''">
			AND p.package_type2= #{query.packageType2}
		</if>
		<if test="query.settleFlag != null and query.settleFlag != ''">
			AND p.settle_flag= #{query.settleFlag}
		</if>
		ORDER BY
		p.create_time DESC
	</select>
</mapper>
