package com.org.web.person.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.org.web.person.domain.BcPersonSupport;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface BcPersonSupportMapper extends BaseMapper<BcPersonSupport> {

    @Select("<script>" +
            "select t.* from (" +

            "select *, " +
            "       TIMESTAMPDIFF(YEAR, STR_TO_DATE(SUBSTRING(id_card_no, 7, 8), '%Y%m%d'), CURDATE()) AS age " +
            "from bc_person_support " +
            "where is_delete = 0 "+
            " <if test = 'type != null'> " +
            "  and type = #{type} " +
            " </if> " +

            " <if test = 'name != null and name != \"\"'> " +
            "  and name like concat('%', #{name}, '%') " +
            " </if> " +
            " <if test = 'idCardNo != null and idCardNo != \"\"'> " +
            "  and id_card_no like concat('%', #{idCardNo}, '%') " +
            " </if> " +
            " <if test = 'phone != null and phone != \"\"'> " +
            "  and phone like concat('%', #{phone}, '%') " +
            " </if> " +

            " <if test = 'village != null'> " +
            "  and village = #{village} " +
            " </if> " +

            " <if test = 'supportType != null'> " +
            "  and support_type = #{supportType} " +
            " </if> " +

            " <if test = 'gender != null'> " +
            "  and sex = #{gender} " +
            " </if> " +

            " <if test = 'nursingLevel != null'> " +
            "  and nursing_level = #{nursingLevel} " +
            " </if> " +

            " <if test = 'isInHospital != null'> " +
            "  and is_in_hospital = #{isInHospital} " +
            " </if> " +

            " <if test = 'ifHelpPoor != null'> " +
            "  and if_help_poor = #{ifHelpPoor} " +
            " </if> " +

            " order by create_time desc " +

            ") t " +

            "where 1 = 1 " +

            "       <if test = 'ageRange != null'> " +
            "           <choose> " +
            "               <when test=\"ageRange == 1\"> " +
            "                 and age &lt; 50 " +
            "               </when> " +
            "               <when test=\"ageRange == 2\"> " +
            "                 and age BETWEEN 50 AND 59  " +
            "               </when> " +
            "               <when test=\"ageRange == 3\"> " +
            "                 and age BETWEEN 60 AND 69  " +
            "               </when> " +
            "               <when test=\"ageRange == 4\"> " +
            "                 and age BETWEEN 70 AND 79 " +
            "               </when> " +
            "               <when test=\"ageRange == 5\"> " +
            "                 and age BETWEEN 80 AND 89 " +
            "               </when> " +
            "               <when test=\"ageRange == 6\"> " +
            "                 and age >= 90 " +
            "               </when> " +
            "               <otherwise> " +
            "                 and 1=1 " +
            "               </otherwise> " +
            "           </choose>" +
            "       </if> " +

            "</script>" )
    List<BcPersonSupport> queryList(BcPersonSupport person);

}
