package com.fs.store.mapper;

import com.fs.store.domain.FsFollow;
import com.fs.store.domain.FsStoreOrder;
import com.fs.store.param.FsFollowListDParam;
import com.fs.store.param.FsFollowListUParam;
import com.fs.store.param.FsFollowParam;
import com.fs.store.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 随访管理Mapper接口
 *
 * @author fs
 * @date 2023-07-14
 */
public interface FsFollowMapper
{
    /**
     * 查询随访管理
     *
     * @param followId 随访管理主键
     * @return 随访管理
     */
    public FsFollow selectFsFollowByFollowId(Long followId);

    /**
     * 查询随访管理列表
     *
     * @param fsFollow 随访管理
     * @return 随访管理集合
     */
    public List<FsFollow> selectFsFollowList(FsFollow fsFollow);

    /**
     * 新增随访管理
     *
     * @param fsFollow 随访管理
     * @return 结果
     */
    public int insertFsFollow(FsFollow fsFollow);

    /**
     * 修改随访管理
     *
     * @param fsFollow 随访管理
     * @return 结果
     */
    public int updateFsFollow(FsFollow fsFollow);

    /**
     * 删除随访管理
     *
     * @param followId 随访管理主键
     * @return 结果
     */
    public int deleteFsFollowByFollowId(Long followId);

    /**
     * 批量删除随访管理
     *
     * @param followIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsFollowByFollowIds(Long[] followIds);
    @Select({"<script> " +
            "select ff.*,fd.doctor_name,fd.account,fu.nick_name ,fu.phone,fft.`name`,fso.order_code,c.company_name,cu.nick_name as company_user_name FROM fs_follow ff " +
            " LEFT JOIN fs_doctor fd ON fd.doctor_id=ff.doctor_id " +
            " LEFT JOIN fs_user fu ON fu.user_id=ff.user_id " +
            " LEFT JOIN company c ON c.company_id =ff.company_id " +
            " LEFT JOIN company_user cu ON cu.user_id =ff.company_user_id " +
            " LEFT JOIN fs_follow_temp fft ON fft.temp_id=ff.temp_id " +
            " LEFT JOIN fs_store_order fso ON fso.order_id=ff.store_order_id"+
            "<where>  \n" +
            "            <if test=\"patientName != null  and patientName != ''\"> and ff.patient_name like concat('%', #{patientName}, '%')</if>\n" +
            "            <if test=\"patientPhone != null  and patientPhone != ''\"> and ff.patient_phone like concat('%', #{patientPhone}, '%')</if>\n" +
            "            <if test=\"orderCode != null  \"> and fso.order_code = #{orderCode}</if>\n" +
            "            <if test=\"userId != null  \"> and ff.user_id = #{userId}</if>\n" +
            "            <if test=\"companyUserName != null  \"> and cu.nick_name = #{companyUserName}</if>\n" +
            "            <if test=\"doctorAccount != null  \"> and fd.account = #{doctorAccount}</if>\n" +
            "            <if test=\"pushStatus != null \"> and ff.push_status = #{pushStatus}</if>\n" +
            "            <if test=\"companyId != null \"> and ff.company_id = #{companyId}</if>\n" +
            "            <if test=\"companyUserId != null \"> and ff.company_user_id = #{companyUserId}</if>\n" +
            "            <if test=\"writeStatus != null \"> and ff.write_status = #{writeStatus}</if>\n" +
//            "            <if test=\"planTime != null \"> and date(ff.plan_time)= date(#{planTime})</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(ff.plan_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(ff.plan_time) &lt;= DATE(#{eTime})</if>\n" +
            "        </where>"+
            "order by follow_id desc"+
            "</script>"})
    List<FsFollowListVO> selectFsFollowListVO(FsFollowParam fsFollow);
    @Select("select ff.*,fd.doctor_name,fu.nick_name ,fu.phone,fft.`name`,fso.order_code FROM fs_follow ff LEFT JOIN fs_doctor fd ON fd.doctor_id=ff.doctor_id LEFT JOIN fs_user fu ON fu.user_id=ff.user_id LEFT JOIN fs_follow_temp fft ON fft.temp_id=ff.temp_id LEFT JOIN fs_store_order fso ON fso.order_id=ff.store_order_id where ff.follow_id=#{followId}")
    FsFollowVO selectFsFollowVOByFollowId(Long followId);
    @Select({"<script> " +
            "select f.*,t.name as temp_name  from fs_follow f left join fs_follow_temp t on t.temp_id=f.temp_id   " +
            "where f.push_status=1  " +
            "<if test = 'maps.userId != null     '> " +
            "and f.user_id = #{maps.userId} " +
            "</if>" +
            "<if test = 'maps.writeStatus != null    '> " +
            "and f.write_status = #{maps.writeStatus} " +
            "</if>" +
            " order by f.follow_id desc "+
            "</script>"})
    List<FsFollowListUVO> selectFsFollowListUVO(@Param("maps")FsFollowListUParam param);
    @Select({"<script> " +
            "select f.*,t.name as temp_name  from fs_follow f left join fs_follow_temp t on t.temp_id=f.temp_id   " +
            "where f.push_status=1  " +
            "<if test = 'maps.doctorId != null     '> " +
            "and f.doctor_id = #{maps.doctorId} " +
            "</if>" +
            "<if test = 'maps.writeStatus != null    '> " +
            "and f.write_status = #{maps.writeStatus} " +
            "</if>" +
            "<if test = 'maps.patientName != null and maps.patientName != \"\"    '> " +
            "and f.patient_name like concat('%', #{maps.patientName}, '%') " +
            "</if>" +
            "<if test=' maps.beginTime != null and maps.beginTime != \"\" '>" +
            "AND date_format(f.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d')  " +
            "</if>" +
            "<if test='maps.endTime != null and maps.endTime != \"\" '>" +
            "AND date_format(f.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')  " +
            "</if>" +
            " order by f.follow_id desc "+
            "</script>"})
    List<FsFollowListDVO> selectFsFollowListDVO(@Param("maps")FsFollowListDParam param);

    @Select({"<script> " +
            "select ff.*,fd.doctor_name,fu.nick_name ,fu.phone,fft.`name`,fso.order_code,fp.prescribe_code,c.company_name FROM fs_follow ff " +
            " LEFT JOIN fs_doctor fd ON fd.doctor_id=ff.doctor_id " +
            " LEFT JOIN fs_user fu ON fu.user_id=ff.user_id " +
            " LEFT JOIN fs_follow_temp fft ON fft.temp_id=ff.temp_id " +
            " LEFT JOIN fs_store_order fso ON fso.order_id=ff.store_order_id " +
            " LEFT JOIN fs_prescribe fp ON fp.prescribe_id=fso.prescribe_id "+
            " LEFT JOIN company c ON ff.company_id=c.company_id " +
            " LEFT JOIN company_user cu ON ff.company_user_id=cu.user_id " +
            "<where>  \n" +
            "            <if test=\"patientName != null  and patientName != ''\"> and ff.patient_name like concat('%', #{patientName}, '%')</if>\n" +
            "            <if test=\"patientPhone != null  and patientPhone != ''\"> and ff.patient_phone like concat('%', #{patientPhone}, '%')</if>\n" +
            "            <if test=\"orderCode != null  \"> and fso.order_code = #{orderCode}</if>\n" +
            "            <if test=\"doctorAccount != null  \"> and fd.account = #{doctorAccount}</if>\n" +
            "            <if test=\"userId != null  \"> and ff.user_id = #{userId}</if>\n" +
            "            <if test=\"companyUserName != null  \"> and cu.nick_name = #{companyUserName}</if>\n" +
            "            <if test=\"pushStatus != null \"> and ff.push_status = #{pushStatus}</if>\n" +
            "            <if test=\"companyId != null \"> and ff.company_id = #{companyId}</if>\n" +
            "            <if test=\"companyUserId != null \"> and ff.company_user_id = #{companyUserId}</if>\n" +
            "            <if test=\"writeStatus != null \"> and ff.write_status = #{writeStatus}</if>\n" +
//            "            <if test=\"planTime != null \"> and date(ff.plan_time)= date(#{planTime})</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(ff.plan_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(ff.plan_time) &lt;= DATE(#{eTime})</if>\n" +
            "        </where>"+
            "order by follow_id desc"+
            "</script>"})
    List<FsFollowExcelListVO> selectFsFollowListExcelVO(FsFollowParam fsFollow);
    @Select("select order_id,user_id,follow_doctor_id from fs_store_order where  follow_doctor_id IN (125,118) AND  date(follow_end_time) >'2024-04-09'")
    List<FsStoreOrder> selectFsStoreListByFollow();
    @Select("select COUNT(follow_id) from fs_follow where user_id=#{userId} and doctor_id =#{doctorId}")
    int selectFsStoreByUserId(@Param("userId") Long userId, @Param("doctorId")Long doctorId);
    @Select("select follow_id from fs_follow where store_order_id=#{orderId} ORDER BY follow_id desc LIMIT 1")
    Long selectFsFollowByStoreOrderId(Long orderId);

    @Select({"<script> " +
            "SELECT \n" +
            "    so.order_code,\n" +
            "    COUNT(ff.store_order_id) AS num,\n" +
            "    so.follow_num+1 as follow_num,\n" +
            "\t\tCONCAT(YEAR(NOW()), '-', LPAD(MONTH(NOW()), 2, '0')) AS current_year_month,\n" +
            "\t\tMAX(d.doctor_name) AS doctor_name,\n" +
            "\t\tMAX(c.company_name) AS company_name,\n" +
            "\t\tMAX(ff.patient_name) AS patient_name,\n" +
            "\t\tMAX(ff.create_time) AS create_time\n" +
            "FROM\n" +
            "    fs_follow ff\n" +
            "LEFT JOIN fs_doctor d ON d.doctor_id = ff.doctor_id\n" +
            "LEFT JOIN company c ON c.company_id = ff.company_id\n" +
            "LEFT JOIN fs_store_order so ON ff.store_order_id = so.order_id\n" +
            "WHERE\n" +
            "    YEAR(ff.create_time) = YEAR(NOW()) AND MONTH(ff.create_time) = MONTH(NOW())\n" +
            "<if test=\"doctorAccount != null  \"> and d.account = #{doctorAccount}</if>\n" +
            "<if test=\"companyId != null \"> and ff.company_id = #{companyId}</if>\n" +
            "GROUP BY\n" +
            "    ff.store_order_id\n" +
            "ORDER BY MAX(ff.create_time); "+
            "</script>"})
    List<FsFollowExportListVO> selectFsFollowExportListVO(FsFollowParam fsFollow);

}
