<?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.yanzhu.jh.project.mapper.SurProjectInsuranceMapper">
    
    <resultMap type="SurProjectInsurance" id="SurProjectInsuranceResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="insuranceType"    column="insurance_type"    />
        <result property="insuranceNumber"    column="insurance_number"    />
        <result property="insuranceFile"    column="insurance_file"    />
        <result property="insuranceState"    column="insurance_state"    />
        <result property="beginDate"    column="begin_date"    />
        <result property="endDate"    column="end_date"    />
        <result property="companyName"    column="company_name"    />
        <result property="isDel"    column="is_del"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="insuranceTypeName"    column="insurance_type_name"    />
    </resultMap>

    <sql id="selectSurProjectInsuranceVo">
        select spi.id, spi.project_id, sp.projectName as project_name, spi.dept_id, d.dept_name, spi.insurance_type, sdd.dict_label as insurance_type_name, spi.insurance_number, spi.insurance_file, spi.insurance_state, spi.begin_date, spi.end_date, spi.company_name, spi.is_del, spi.create_by, spi.create_time, spi.update_by, spi.update_time, spi.remark from sur_project_insurance spi
        left join sur_project sp on sp.id=spi.project_id
        left JOIN sys_dict_data sdd ON sdd.dict_type = 'sur_project_insurance_type' AND spi.insurance_type = sdd.dict_value
        left join sys_dept d on d.dept_id = spi.dept_id
    </sql>

    <select id="selectProjectInsuranceListByProjectId" parameterType="Long" resultMap="SurProjectInsuranceResult">
        SELECT x.*,y.dict_label AS insurance_type_name FROM (
            SELECT * FROM sys_dict_data WHERE dict_type = 'sur_project_insurance_type' AND STATUS=0
        ) Y
            LEFT JOIN
        (
            SELECT a.*,b.projectName project_name,  c.dept_name  FROM sur_project_insurance a,sur_project b,sys_dept c
            WHERE a.project_id=b.id AND a.dept_id=c.dept_id  AND IF(a.is_del IS NULL ,0,a.is_del) =0 AND a.project_id=#{projectId}
        ) AS X

        ON x.insurance_type=y.dict_value

    </select>

    <select id="selectSurProjectInsuranceList" parameterType="SurProjectInsurance" resultMap="SurProjectInsuranceResult">
        SELECT
        spi.id,
        spi.project_id,
        sp.projectName as project_name,
        d.dept_name,
        spi.insurance_type,
        sdd.dict_label as insurance_type_name,
        spi.insurance_number,
        spi.insurance_file,
        CASE WHEN spi.insurance_state IS NULL THEN 0 ELSE 1 END AS insurance_state,
        spi.begin_date,
        spi.end_date,
        spi.company_name,
        spi.is_del,
        spi.create_by,
        spi.create_time,
        spi.update_by,
        spi.update_time,
        spi.remark
        FROM
        sur_project sp
        left join sur_project_unit_info spui on spui.projectId=sp.id and spui.unitType=2
        left join sys_dept d on d.dept_id = spui.unitId
        RIGHT JOIN sys_dict_data sdd ON sdd.dict_type = 'sur_project_insurance_type'
        LEFT JOIN sur_project_insurance spi ON spi.project_id = sp.id
        AND spi.insurance_type = sdd.dict_value
        AND spi.is_del = 0
        <!--普通用户查询项目人员-->
        <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> left join sur_project_userinfo spu on spu.project_id = sp.id</if>
        <where>
            and sp.isDel=0
            and spui.del_flag=0
            and d.del_flag=0
            and sdd.dict_type = 'sur_project_insurance_type'
            <if test="projectId != null "> and sp.id = #{projectId}</if>
            <if test="projectName != null and projectName != ''"> and sp.projectName like concat('%', #{projectName}, '%')</if>
            <if test="deptName != null and deptName != ''"> and d.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="insuranceNumber != null and insuranceNumber != ''"> and spi.insurance_number like concat('%', #{insuranceNumber}, '%')</if>
            <if test="companyName != null and companyName != ''"> and spi.company_name like concat('%', #{companyName}, '%')</if>
            <if test='insuranceState == "0"'>and spi.insurance_state IS NULL</if>
            <if test='insuranceState == "1"'>and spi.insurance_state IS NOT NULL</if>
            <if test="insuranceType != null and insuranceType != ''"> and spi.insurance_type = #{insuranceType}</if>
            <!-- 查询条件-项目部门 -->
            <if test="projectDeptId != null "> and sp.deptId = #{projectDeptId}</if>
            <!--子部门数据-->
            <if test='nowRole == "4"'> and sp.deptId = #{nowDept}</if>
            <!--监理单位/总包公司/分包单位查询当前关联数据-->
            <if test='nowRole == "5" or nowRole == "6" or nowRole == "7"'> and spui.unitId = #{nowDept}</if>
            <!--普通用户查询项目人员-->
            <if test='nowRole == "15" or nowRole == "16" or nowRole == "17" or nowRole == "99"'> and spu.user_id = #{nowUser} and spu.is_del=0</if>
        </where>
        ORDER BY
        sp.projectSort,
        sdd.dict_sort
    </select>

    <select id="selectSurProjectInsuranceListv2" parameterType="SurProjectInsurance" resultMap="SurProjectInsuranceResult">
        SELECT
            spi.id,
            spi.project_id,
            sp.projectName,
            sdd.dict_value,
            sdd.dict_label as insurance_type_name,
            spi.insurance_number,
            spi.insurance_file,
            CASE WHEN spi.insurance_state IS NULL THEN 0 ELSE 1 END AS insurance_state,
            spi.begin_date,
            spi.end_date,
            spi.company_name,
            spi.is_del,
            spi.create_by,
            spi.create_time,
            spi.update_by,
            spi.update_time,
            spi.remark
        FROM
            sur_project sp
            RIGHT JOIN sys_dict_data sdd ON sdd.dict_type = 'sur_project_insurance_type'
            LEFT JOIN sur_project_insurance spi ON spi.project_id = sp.id
            AND spi.insurance_type = sdd.dict_value
            AND spi.dept_id = #{deptId}
            AND spi.is_del = 0
        <where>
            and sdd.dict_type = 'sur_project_insurance_type'
            <if test="projectId != null "> and sp.id = #{projectId}</if>
        </where>
        ORDER BY
        sp.projectSort,
        sdd.dict_sort
    </select>

    <select id="findGroupCountByProjectId" parameterType="Long" resultType="map">
        SELECT
            spi.insurance_type as insuranceType,
            sdd.dict_label as insuranceTypeName,
            count(1) AS total
        FROM
            sur_project_insurance spi
            LEFT JOIN sys_dict_data sdd ON sdd.dict_type = 'sur_project_insurance_type' and spi.insurance_type = sdd.dict_value
        WHERE
            spi.is_del = 0
            AND spi.project_id = #{projectId}
        GROUP BY
            spi.insurance_type
        order by sdd.dict_sort
    </select>

    <!--查询项目保险统计列表-->
    <select id="selectBgscreenInsuranceList" parameterType="SurProjectInsurance" resultType="map">
        SELECT
            sp.id,
            sp.projectName,
            CASE
            WHEN azx.insurance_state IS NULL THEN
            '未办理'
            ELSE
            '已办理'
            END AS azxstate,
            CASE
            WHEN yqx.insurance_state IS NULL THEN
            '未办理'
            ELSE
            '已办理'
            END AS yqxstate
        FROM
        sur_project sp
        LEFT JOIN sur_project_insurance azx ON azx.project_id = sp.id
        AND azx.insurance_type = 2 and azx.is_del=0
        LEFT JOIN sur_project_insurance yqx ON yqx.project_id = sp.id
        AND yqx.insurance_type = 1 and yqx.is_del=0
        where sp.isDel=0
        <if test="nowDept != null and nowDept != ''"> and sp.deptId = #{nowDept}</if>
        <if test="projectId != null "> and sp.id = #{projectId}</if>
        <if test="prjIds !=null and prjIds.size()>0">
            and sp.id  in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        GROUP BY sp.id,sp.projectName
        ORDER BY sp.projectSort ASC
    </select>
    
    <select id="selectSurProjectInsuranceById" parameterType="Long" resultMap="SurProjectInsuranceResult">
        <include refid="selectSurProjectInsuranceVo"/>
        where spi.id = #{id}
    </select>
        
    <insert id="insertSurProjectInsurance" parameterType="SurProjectInsurance" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_insurance
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="insuranceType != null">insurance_type,</if>
            <if test="insuranceNumber != null">insurance_number,</if>
            <if test="insuranceFile != null">insurance_file,</if>
            <if test="insuranceState != null">insurance_state,</if>
            <if test="beginDate != null">begin_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="companyName != null">company_name,</if>
            <if test="isDel != null">is_del,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="insuranceType != null">#{insuranceType},</if>
            <if test="insuranceNumber != null">#{insuranceNumber},</if>
            <if test="insuranceFile != null">#{insuranceFile},</if>
            <if test="insuranceState != null">#{insuranceState},</if>
            <if test="beginDate != null">#{beginDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateSurProjectInsurance" parameterType="SurProjectInsurance">
        update sur_project_insurance
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="insuranceType != null">insurance_type = #{insuranceType},</if>
            <if test="insuranceNumber != null">insurance_number = #{insuranceNumber},</if>
            <if test="insuranceFile != null">insurance_file = #{insuranceFile},</if>
            <if test="insuranceState != null">insurance_state = #{insuranceState},</if>
            <if test="beginDate != null">begin_date = #{beginDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurProjectInsuranceById" parameterType="Long">
        delete from sur_project_insurance where id = #{id}
    </delete>

    <delete id="deleteSurProjectInsuranceByIds" parameterType="String">
        delete from sur_project_insurance where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="findInsuranceByParams" parameterType="SurProjectInsurance" resultType="int">
        select count(1) as total from sur_project_insurance where is_del=0 and project_id = #{projectId} and dept_id = #{deptId} and insurance_type = #{insuranceType}
    </select>
</mapper>