<?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.bonus.boot.manager.basic.dao.PhyManagementDao">
    <insert id="addInfo">
        insert into pm_phy_combin(id,combin_name,combin_type,combin_content,combin_price,is_active,selectId)
        values (#{id},#{combinName}, #{combinType}, #{contentList},#{combinPrice},'1',#{selectId});
    </insert>
    <insert id="insertBasicData" keyProperty="id" useGeneratedKeys="true">
        insert into pm_base_combination(comb_name,comb_type,comb_sex,person_type,if_highee,check_id,is_active)
        values(#{combinName}, #{combinType}, #{combinSex},#{personType},#{isHighPosition},#{checkType},'1')
    </insert>
    <update id="updateInfo">
        update pm_base_combination set
            comb_name = #{combinName},
            comb_type = #{combinType},
            comb_sex=#{combinSex},
            person_type = #{personType},
            if_highee = #{isHighPosition},
            check_id = #{checkType}
        where id = #{id}
    </update>
    <update id="delById">
        update pm_base_combination set is_active = '0' where id = #{id};
        update pm_phy_combin set is_active = '0' where id = #{id}
    </update>
    <delete id="delete">
        delete from pm_phy_combin where id = #{id}
    </delete>

    <select id="getList" resultType="com.bonus.boot.manager.basic.entity.PhyCombinBean">
        SELECT
        pbp.combin_content,
        pbc.id,
        pbc.comb_name as combinName,
        case when pbtp.type_name = '1' then '标准套餐' when pbtp.type_name = '2' then '个性化套餐' when pbtp.type_name = '3' then '职业健康套餐' end as combinType,
        pbc.comb_type as combinTypeId,
        case when pbc.comb_sex = '0' then '男' when pbc.comb_sex = '1' then '女' when pbc.comb_sex = '3' then '' end as combinSex,
        case when pbc.if_highee = '0' then '否' when pbc.if_highee = '1' then '是' when pbc.if_highee = '3' then '' end as isHighPosition,
        sdd.name as personType,
        pbc.person_type as personTypeId,
        GROUP_CONCAT(pbp.combin_content) as combinContent,
        GROUP_CONCAT(pbp.selectId) as selectId,
        sum(pbp.combin_price) as combinPrice
        FROM pm_base_combination pbc
        left join pm_base_type_package pbtp on pbc.comb_type = pbtp.id  and pbtp.is_active = '1'
        left join pm_phy_combin pbp on pbp.id = pbc.id  and pbp.is_active = '1'
        left join sys_dic_detail sdd on sdd.id = pbc.person_type
        where pbc.is_active = '1'
        group by pbc.id
        HAVING 1=1
        <if test="combinName != null and combinName != ''">
            AND  pbc.comb_name LIKE concat ('%',#{combinName},'%')
        </if>
        <if test="combinType != null and combinType != ''">
            AND pbc.comb_type = #{combinType}
        </if>
        <if test="combinContent != null and combinContent != ''">
            AND pbp.combin_content LIKE concat ('%',#{combinContent},'%')
        </if>
        <if test="keyWord != null and keyWord != '' ">
            and (
            pbc.comb_name like concat ('%',#{keyWord},'%') or
            pbp.combin_content like concat ('%',#{keyWord},'%')
            )
        </if>
    </select>
    <select id="getListById" resultType="com.bonus.boot.manager.basic.entity.PhyCombinBean">
        SELECT
            pbc.id,
            pbc.comb_name as combinName,
            pbtp.type_name as combinType,
            pbc.comb_type as combinTypeId,
            pbc.comb_sex as  combinSex,
            pbc.if_highee as isHighPosition,
            sdd.name as personType,
            pbc.person_type as personTypeId,
            GROUP_CONCAT(pbp.combin_name) as combinContent,
            GROUP_CONCAT( pbp.selectId ) AS selectId,
            sum(pbp.combin_price) as combinPrice,
            pbc.check_id as checkType
        FROM pm_base_combination pbc
                 left join pm_base_type_package pbtp on pbc.comb_type = pbtp.id  and pbtp.is_active = '1'
                 left join pm_phy_combin pbp on pbp.id = pbc.id  and pbp.is_active = '1'
                 left join sys_dic_detail sdd on sdd.id = pbc.person_type
        where pbc.is_active = '1' and pbc.id = #{id}
    </select>
    <select id="determineIfExists" resultType="java.lang.Integer">
        select count(1) from pm_base_combination where is_active = '1' and comb_sex = #{combinSex} and if_highee = #{isHighPosition} and person_type = #{personType} and check_id = #{checkType}
        <if test="id != null and id != ''">
            and id != #{id}
        </if>
        limit 1
    </select>
</mapper>

