<?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.yc.ship.module.trade.dal.mysql.insurance.InsuranceMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="queryPage" resultType="com.yc.ship.module.trade.controller.admin.insurance.vo.InsuranceRespVO">
        SELECT ti.*,t.order_no orderNo,GROUP_CONCAT(tv.credential_no) as insuredIdNo,tv.credential_type as insuredIdType,GROUP_CONCAT(tv.name) as insuredName
        FROM trade_insurance ti
        inner JOIN trade_order t on t.id = ti.order_id
        inner JOIN trade_detail td on td.order_id = ti.order_id
        inner JOIN trade_visitor tv on td.id = tv.detail_id
        where ti.deleted =0
        <if test="param.insuranceNo != null and param.insuranceNo !=''">
            AND ti.insurance_no like CONCAT('%', #{param.insuranceNo}, '%')
        </if>
        <if test="param.insuranceStatus != null ">
            and FIND_IN_SET(ti.insurance_status,#{param.insuranceStatus})
        </if>
        <if test="param.policyNo != null and param.policyNo !=''">
            AND ti.policy_no like CONCAT('%', #{param.policyNo}, '%')
        </if>
        <if test="param.otaName != null and param.otaName !=''">
            AND (t.source_name like CONCAT('%', #{param.otaName}, '%') or t.store_name like CONCAT('%', #{param.otaName}, '%'))
        </if>
        <if test="param.storeId != null and param.storeId !=''">
            AND t.store_id = #{param.storeId}
        </if>
        <if test="param.orderNo != null and param.orderNo !=''">
            AND t.order_no = #{param.orderNo}
        </if>
          <if test="param.holderNo != null and param.holderNo !=''">
            AND ti.holder_no = #{param.holderNo}
        </if>
        <if test="param.insuredName != null and param.insuredName !=''">
            AND tv.name like CONCAT('%', #{param.insuredName}, '%')
        </if>
        <if test="param.createTime != null">
            AND ti.create_time BETWEEN #{param.createTime[0]} AND #{param.createTime[1]}
        </if>
        GROUP BY ti.id
        order by ti.create_time desc , ti.update_time desc
    </select>
</mapper>