<?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.canteen.core.customer.mapper.CustInfoMapper">
    <!--根据人员id集合获取人员信息-->
    <select id="selectOne" resultType="com.bonus.canteen.core.customer.model.CustInfo">
        select cust_id, cust_name, cust_num, cust_photo_url, mobile, pwd
        from cust_info
        <where>
            cust_state = 1
            <if test="custId != null">
                and cust_id = #{custId}
            </if>
            <if test="custName != null and custName != ''">
                and cust_name = #{custName}
            </if>
            <if test="custNum != null and custNum != ''">
                and cust_num = #{custNum}
            </if>
            <if test="mobile != null and mobile != ''">
                and mobile = #{mobile}
            </if>
            <if test="idCard != null and idCard != ''">
                and id_card = #{idCard}
            </if>
        </where>
    </select>

    <update id="updateById">
        update cust_info
        set cust_name = #{custName}, cust_num = #{custNum}, mobile = #{mobile}, id_card = #{idCard}, pwd = #{pwd}
        where cust_id = #{custId}
    </update>

    <!-- 用户详细信息,返回字段 -->
    <sql id="detailInfo">
        ci.cust_id,
        ci.cust_num,
        ci.cust_name,
        ci.mobile,
        ci.email,
        ci.sex,
        --         ci.age,
        ci.id_card,
        ci.birthday,
        ci.cust_photo_url,
        ci.org_id,
        ci.cust_third_id,
        ci.org_num,
        ci.org_full_name,
        ci.place_id,
        place.place_num,
        place.place_full_name,
        ci.psn_type,
        ci.psn_type_name,
        ci.home_addr,
        ci.qq,
        ci.wechat,
        ci.company_wechat,
        ci.dingtalk,
        ci.emer_contact_person,
        ci.emer_contact_num,
        ci.emer_contact_card,
        ci.job,
        ci.job_rank,
        ci.job_level,
        ci.job_title,
        ci.job_title_rank,
        ci.auth_strength,
        ci.entry_date,
        ci.signing_date,
        ci.expiry_date,
        --         ci.cust_type,
        ci.birthday,
        cd.label_ids,
        cd.label_names,
        cd.taste_ids,
        cd.taste_names,
        ai.end_date accEndDate,
        ci.doctor_num,
        ci.hospital_num
    </sql>

    <select id="queryCustInfoDetail" resultType="com.bonus.canteen.core.customer.vo.CustInfoVo">
        select
        <include refid="detailInfo"/>
        ,
        ci.marital_status,
        ci.cost_center_id,
        A.name costCenterName,
        ci.ethnicity_code,
        ci.native_place,
        ci.position,
        ci.bank_card_num,
        ci.head_portrait_url,
        ci.extend_date,
        ci.extend_date_time,
        ci.extend_str1,
        ci.extend_str2,
        cc.openid
        from cust_info ci
        left join cust_diet_like cd on ci.cust_id = cd.cust_id and cd.if_del = 2
        left join acc_info ai on ci.cust_id = ai.cust_id
        LEFT JOIN cust_place place ON ci.place_id = place.place_id
        LEFT JOIN cust_cost_center A ON A.cost_center_id = ci.cost_center_id
        LEFT JOIN cust_casual cc on ci.cust_id = cc.cust_id
        where ci.cust_id = #{info.custId}
        and ci.cust_state = #{info.custState}
    </select>

    <!-- 查询用户详细信息 -->
    <select id="queryCustInfo" resultType="com.bonus.canteen.core.customer.vo.CustInfoVo">
        select ci.cust_limit_id,
        <if test="info.serialNum != null and info.serialNum != ''">
            ac.serial_num,
            ac.card_status,
        </if>
        ci.alipay_contract_num,
        <include refid="detailInfo"/>
        from cust_info ci
        left join cust_diet_like cd on ci.cust_id = cd.cust_id and cd.if_del = 2
        left join acc_info ai on ci.cust_id = ai.cust_id
        <if test="info.serialNum != null and info.serialNum != ''">
            left join acc_card ac on ci.cust_id = ac.cust_id and  ac.card_status in(1,4)
        </if>

        LEFT JOIN cust_place place ON ci.place_id = place.place_id
        where 1 = 1
        <if test="info.custId != null">
            and ci.cust_id = #{info.custId}
        </if>
        <if test="info.custLimitId != null">
            and ci.cust_limit_id = #{info.custLimitId}
        </if>
        <if test="info.serialNum != null and info.serialNum != ''">
            and ac.serial_num = #{info.serialNum}
        </if>
        <if test="info.custState != null">
            and ci.cust_state = #{info.custState}
        </if>
    </select>


    <select id="getCustInfoForRechargeByCustNum" resultType="com.bonus.canteen.core.customer.vo.CustInfoForRechargeVO">
        SELECT ci.cust_id,
        ci.cust_num,
        ci.cust_name,
        ci.mobile,
        ci.cust_third_id,
        ci.org_full_name,
        co.org_num
        FROM cust_info AS ci
        INNER JOIN cust_org AS co ON ci.org_id = co.org_id
        where ci.cust_num = #{custNum}
        and ci.cust_state = #{custState}
    </select>

    <select id="listCustInfoForRechargeByCustNums" resultType="com.bonus.canteen.core.customer.vo.CustInfoForRechargeVO">
        SELECT
        ci.cust_id,
        ci.cust_third_id,
        ci.cust_num,
        ci.cust_name,
        ci.mobile,
        co.org_num,
        co.org_name,
        co.org_full_name
        FROM
        cust_info AS ci
        LEFT JOIN cust_org AS co ON ci.org_id = co.org_id
        where ci.cust_num in
        <foreach collection="custNumList" item="custNum" separator="," open="(" close=")">
            #{custNum}
        </foreach>
        and ci.cust_state = #{custState}
    </select>

    <select id="queryOrgManagerCustIdByCustId" resultType="java.lang.Long">
        SELECT t2.org_head_id
        FROM cust_org t2
        INNER JOIN cust_info t1 ON t1.org_id = t2.org_id
        WHERE t1.cust_id = #{custId}
    </select>


    <select id="getOrgManagerCustIdByCustId" resultType="java.lang.Long">
        SELECT t2.org_head_id
        FROM cust_org t2
        WHERE EXISTS(
        SELECT 1
        FROM cust_info t1
        WHERE t1.cust_id = #{custId}
        AND t1.org_id = t2.org_id
        )
    </select>

    <select id="getCustPayVO" resultType="com.bonus.canteen.core.customer.vo.CustPayVO">
        SELECT a.cust_id,
        a.psn_type,
        a.psn_type_name,
        a.org_id,
        a.org_num,
        a.cust_third_id,
        a.alipay_contract_num,
        a.cust_state,
        a.cust_num,
        a.cust_name,
        a.org_full_name,
        a.cust_photo_url,
        a.head_portrait_url,
        a.mobile,
        b.serial_num,
        b.third_serial_num,
        b.card_status,
        b.validity_date
        FROM
        cust_info a
        LEFT JOIN acc_card b ON b.cust_id = a.cust_id and b.card_status in (1, 4)
        <if test="custPayDTO.openid != null and custPayDTO.openid != ''">
            LEFT JOIN cust_casual c ON c.cust_id = a.cust_id
        </if>
        <where>
            <if test="custPayDTO.serialNum != null and custPayDTO.serialNum != ''">
                AND b.serial_num = #{custPayDTO.serialNum}
            </if>
            <if test="custPayDTO.openid != null and custPayDTO.openid != ''">
                AND c.openid = #{custPayDTO.openid}
            </if>
            <if test="custPayDTO.custId != null">
                AND a.cust_id = #{custPayDTO.custId}
            </if>
        </where>
    </select>

    <select id="selectCustCasualByCustId" resultType="com.bonus.common.houqin.domain.CustCasual">
        select casual_id, openid from cust_casual where cust_id = #{custId} and source_type = #{sourceType} and if_del = 2
    </select>

    <insert id="addCustCasual">
        insert into cust_casual (casual_id, casual_name, cust_id, mobile, openid, source_type)
        values (#{casualId}, #{casualName}, #{custId}, #{mobile}, #{openid}, #{sourceType})
    </insert>

    <update id="updateCustCasual">
        update cust_casual
        set openid = #{openid}, mobile = #{mobile}
        where casual_id = #{casualId}
    </update>

    <select id="getCustomerNumByOrgIdList" resultType="com.bonus.canteen.core.customer.po.CustomerNumByOrgIdMapperPO">
        SELECT
        org_id,
        COUNT(*) customerNum
        FROM
        cust_info
        <where>
            cust_state = 1
            <if test="orgIdList != null and orgIdList.size() > 0">
                AND org_id in
                <foreach collection="orgIdList" item="orgId" separator="," open="(" close=")">
                    #{orgId}
                </foreach>
            </if>
        </where>
        GROUP BY org_id
    </select>

    <select id="pageCustInfoPhoto" resultType="com.bonus.canteen.core.customer.vo.PageCustInfoPhotoVO"
            parameterType="com.bonus.canteen.core.customer.dto.CustInfoParam">
        SELECT
        ci.id,
        ci.cust_id,
        ci.cust_num,
        ci.cust_name,
        ci.mobile,
        ci.email,
        ci.org_id,
        co.org_full_name,
        ci.cost_center_id,
        A.name costCenterName,
        ci.place_id,
        place.place_full_name,
        ci.sex,
        B.job_name job,
        ci.cust_third_id,
        ci.home_addr,
        ci.qq,
        ci.wechat,
        ci.psn_type,
        ci.psn_type_name,
        ci.dingtalk,
        ci.cust_state,
        ci.crtime,
        ci.uptime,
        --             ci.cust_type,
        ci.birthday,
        cp.photo_url,
        cp.features_build_status,
        cp.features_build_time,
        cp.error_msg,
        cp.expire_date,
        ac.card_status cardStatus,
        ac.serial_num serialNum,
        ac.crtime openCardTime,
        ci.cust_limit_id,
        ci.extend_date,
        ci.extend_date_time,
        ci.extend_str1,
        ci.extend_str2
        FROM cust_info ci
        LEFT JOIN (
        SELECT cust_id,max(features_build_status) features_build_status ,MAX(features_build_time) features_build_time ,MAX(photo_url) photo_url,MAX(error_msg) error_msg ,MAX(expire_date) expire_date from cust_photo WHERE photo_type = 1  GROUP BY cust_id ) cp ON ci.cust_id = cp.cust_id
        LEFT JOIN cust_org co ON co.org_id = ci.org_id
        LEFT JOIN
        (SELECT cust_id,
        GROUP_CONCAT(serial_num SEPARATOR ', ')                               as serial_num,
        GROUP_CONCAT(card_status SEPARATOR ', ')                              as card_status,
        GROUP_CONCAT(DATE_FORMAT(crtime, '%Y-%m-%d %H:%i:%s') SEPARATOR ', ') as crtime
        FROM acc_card where  card_status in(1,4)
        GROUP BY cust_id) ac   ON ac.cust_id = ci.cust_id
        LEFT JOIN cust_place place ON place.place_id = ci.place_id
        LEFT JOIN cust_cost_center A ON A.cost_center_id = ci.cost_center_id
        LEFT JOIN cust_job B ON B.job_code = ci.job AND B.job_type = 1
        <where>
            <if test="infoParam.custId != null">
                and ci.cust_id = #{infoParam.custId}
            </if>
            <if test="infoParam.custName != null and infoParam.custName != ''">
                and ci.cust_name_like LIKE CONCAT('%',#{infoParam.custName,typeHandler=com.bonus.canteen.core.common.encrypt.CipherQueryLikeHandler},'%')
            </if>
            <if test="infoParam.custNum != null and infoParam.custNum != ''">
                and ci.cust_num = #{infoParam.custNum}
            </if>

            <if test="infoParam.keyword != null and infoParam.keyword != ''">
                and (ci.cust_num LIKE CONCAT('%', #{infoParam.keyword},'%')
                or ci.cust_name = #{infoParam.keyword,typeHandler=com.bonus.canteen.core.common.encrypt.SM4EncDecHandler}
                or ci.cust_name_like LIKE CONCAT('%',#{infoParam.keyword,typeHandler=com.bonus.canteen.core.common.encrypt.CipherQueryLikeHandler},'%')
                or ci.mobile = #{infoParam.keyword,typeHandler=com.bonus.canteen.core.common.encrypt.SM4EncDecHandler}
                or ci.mobile_suffix = #{infoParam.keyword,typeHandler=com.bonus.canteen.core.common.encrypt.SM4EncDecHandler}
                )
            </if>

            <if test="infoParam.orgIdList != null and infoParam.orgIdList.size > 0">
                and ci.org_id in
                <foreach collection="infoParam.orgIdList" item="orgId" separator="," open="(" close=")">
                    #{orgId}
                </foreach>
            </if>
            <if test="infoParam.placeIdList != null and infoParam.placeIdList.size > 0">
                and ci.place_id in
                <foreach collection="infoParam.placeIdList" item="placeId" separator="," open="(" close=")">
                    #{placeId}
                </foreach>
            </if>
            <if test="infoParam.mobile != null and infoParam.mobile != ''">
                and ci.mobile = #{infoParam.mobile,typeHandler=com.bonus.canteen.core.common.encrypt.SM4EncDecHandler}
            </if>
            <if test="infoParam.sex != null">
                and ci.sex = #{infoParam.sex}
            </if>
            <if test="infoParam.psnType != null and infoParam.psnType != -99">
                and ci.psn_type = #{infoParam.psnType}
            </if>
            <if test="infoParam.psnType == -99">
                and ci.psn_type is null
            </if>
            <if test="infoParam.psnTypeList != null and infoParam.psnTypeList.size > 0">
                and ci.psn_type in
                <foreach collection="infoParam.psnTypeList" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="infoParam.custState != null">
                and ci.cust_state = #{infoParam.custState}
            </if>
            <if test="infoParam.featuresBuildStatus == 1">
                and (cp.features_build_status is null or cp.features_build_status = 1)
            </if>
            <if test="infoParam.featuresBuildStatus == 2 or infoParam.featuresBuildStatus == 3">
                and cp.features_build_status = #{infoParam.featuresBuildStatus}
            </if>
            <if test="infoParam.serialNum != null or (infoParam.cardStatusList != null and infoParam.cardStatusList.size > 0)  or infoParam.beginOpenCardTime != null">
                and  EXISTS (SELECT 1 FROM acc_card acc
                WHERE acc.cust_id = ci.cust_id and acc.card_status in(1,4)
                <if test="infoParam.serialNum != null">
                    AND acc.serial_num like concat('%', #{infoParam.serialNum},'%')
                </if>
                <if test="infoParam.cardStatusList != null and infoParam.cardStatusList.size > 0">
                    and acc.card_status in
                    <foreach collection="infoParam.cardStatusList" item="cardStatus" separator="," open="(" close=")">
                        #{cardStatus}
                    </foreach>
                </if>
                <if test="infoParam.beginOpenCardTime != null">
                    <![CDATA[
                and acc.crtime >= #{infoParam.beginOpenCardTime}
                ]]>
                </if>
                <if test="infoParam.endOpenCardTime != null">
                    <![CDATA[
                and acc.crtime <= #{infoParam.endOpenCardTime}
                ]]>
                </if>
                )
            </if>
            <if test="infoParam.birthdayStartTime">
                <![CDATA[
                and ci.birthday >= #{infoParam.birthdayStartTime}
                ]]>
            </if>
            <if test="infoParam.birthdayEndTime != null">
                <![CDATA[
                and ci.birthday <= #{infoParam.birthdayEndTime}
                ]]>
            </if>
            <if test="infoParam.costCenterIds != null and infoParam.costCenterIds.size > 0">
                and ci.cost_center_id in
                <foreach collection="infoParam.costCenterIds" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="infoParam.upTime != null">
                <![CDATA[
                and ci.uptime >= #{infoParam.upTime}
                ]]>
            </if>
        </where>
        order by ci.id desc
    </select>

    <!-- 分页查询人员及职位信息 -->
    <select id="pageCustJobInfo" resultType="com.bonus.canteen.core.customer.vo.PageCustJobInfoVO"
            parameterType="com.bonus.canteen.core.customer.dto.CustJobInfoParam">
        SELECT
        t1.id, t1.cust_id, t1.cust_num, t1.cust_name, t1.mobile, t1.job, t2.org_full_name
        FROM cust_info t1
        LEFT JOIN cust_org t2 ON t1.org_id = t2.org_id
        WHERE
        t1.cust_state = 1
        <if test="param.keyword != null and param.keyword != ''">
            and (t1.cust_num LIKE CONCAT('%', #{param.keyword},'%')
            or t1.cust_name = #{param.keyword,typeHandler=net.xnzn.core.common.encrypt.SM4EncDecHandler}
            or t1.cust_name_like LIKE CONCAT('%',#{param.keyword,typeHandler=net.xnzn.core.common.encrypt.CipherQueryLikeHandler},'%')
            or t1.mobile = #{param.keyword,typeHandler=net.xnzn.core.common.encrypt.SM4EncDecHandler}
            or t1.mobile_suffix = #{param.keyword,typeHandler=net.xnzn.core.common.encrypt.SM4EncDecHandler}
            )
        </if>

        <choose>
            <when test="param.job != null and param.job != ''">
                AND t1.job = #{param.job}
            </when>
            <when test="param.ifQueryJob == 1">
                AND t1.job IS NOT NULL AND t1.job != ''
            </when>
        </choose>
        order by t1.id desc
    </select>
</mapper>








































