package com.fs.his.mapper;

import java.util.List;
import com.fs.his.domain.FsPrescribe;
import com.fs.his.param.FsPrescribeListDParam;
import com.fs.his.param.FsPrescribeListUParam;
import com.fs.his.param.FsPrescribeParam;
import com.fs.his.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 处方Mapper接口
 *
 * @author fs
 * @date 2023-06-13
 */
public interface FsPrescribeMapper
{
    /**
     * 查询处方
     *
     * @param prescribeId 处方主键
     * @return 处方
     */
    public FsPrescribe selectFsPrescribeByPrescribeId(Long prescribeId);

    /**
     * 查询处方列表
     *
     * @param fsPrescribe 处方
     * @return 处方集合
     */
    public List<FsPrescribe> selectFsPrescribeList(FsPrescribe fsPrescribe);

    /**
     * 新增处方
     *
     * @param fsPrescribe 处方
     * @return 结果
     */
    public int insertFsPrescribe(FsPrescribe fsPrescribe);

    /**
     * 修改处方
     *
     * @param fsPrescribe 处方
     * @return 结果
     */
    public int updateFsPrescribe(FsPrescribe fsPrescribe);

    /**
     * 删除处方
     *
     * @param prescribeId 处方主键
     * @return 结果
     */
    public int deleteFsPrescribeByPrescribeId(Long prescribeId);

    /**
     * 批量删除处方
     *
     * @param prescribeIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsPrescribeByPrescribeIds(Long[] prescribeIds);
    @Select({"<script> " +
            "select so.*,us.nick_name,dc.doctor_name,dp.doctor_name doctor_drug_name,fso.order_code,fso.`status` order_status,fse.store_name FROM fs_prescribe so  LEFT JOIN fs_user us ON us.user_id=so.user_id LEFT JOIN fs_doctor dc ON dc.doctor_id = so.doctor_id LEFT JOIN fs_doctor dp ON dp.doctor_id =so.drug_doctor_id LEFT JOIN fs_store_order fso ON fso.order_id = so.store_order_id LEFT JOIN fs_store fse ON fse.store_id = so.store_id "+
            "<where>\n" +
            "        <if test=\"companyId != null \"> and fso.company_id = #{companyId}</if>\n" +
            "        <if test=\"companyUserId != null \"> and fso.company_user_id = #{companyUserId}</if>\n" +
            "        <if test=\"prescribeType != null \"> and so.prescribe_type = #{prescribeType}</if>\n" +
            "        <if test=\"inquiryOrderId != null \"> and so.inquiry_order_id = #{inquiryOrderId}</if>\n" +
            "        <if test=\"storeOrderId != null \"> and so.store_order_id = #{storeOrderId}</if>\n" +
            "        <if test=\"userId != null \"> and so.user_id = #{userId}</if>\n" +
            "        <if test=\"prescribeCode != null  and prescribeCode != ''\"> and so.prescribe_code = #{prescribeCode}</if>\n" +
            "        <if test=\"patientName != null  and patientName != ''\"> and so.patient_name like concat('%', #{patientName}, '%')</if>\n" +
            "        <if test=\"doctorName != null  and doctorName != ''\"> and dc.doctor_name like concat('%', #{doctorName}, '%')</if>\n" +
            "        <if test=\"patientTel != null  and patientTel != ''\"> and so.patient_tel = #{patientTel}</if>\n" +
            "        <if test=\"doctorId != null  and doctorId != ''\"> and so.doctor_id = #{doctorId}</if>\n" +
            "        <if test=\"status != null \"> and so.status = #{status}</if>\n" +
            "        <if test=\"sTime != null \">  and DATE(so.create_time) &gt;= DATE(#{sTime})</if>\n" +
            "        <if test=\"eTime != null \">  and DATE(so.create_time) &lt;= DATE(#{eTime})</if>\n" +
            "        <if test=\"auditSTime != null \">  and DATE(so.audit_time) &gt;= DATE(#{auditSTime})</if>\n" +
            "        <if test=\"auditETime != null \">  and DATE(so.audit_time) &lt;= DATE(#{auditETime})</if>\n" +
            "        <if test=\"auditTime != null \"> and so.audit_time = #{auditTime}</if>\n" +
            "        <if test=\"orderCode != null \"> and fso.order_code = #{orderCode}</if>\n" +
            "        <if test=\"orderStatus != null \"> and fso.`status` = #{orderStatus}</if>\n" +
            "        <if test=\"storeId != null \"> and so.store_id = #{storeId}</if>\n" +
            "    </where>\n" +
            "    order by prescribe_id desc"+
            "</script>"})
    List<FsPrescribeListVO> selectFsPrescribeListVO(FsPrescribeParam fsPrescribe);
    @Select({"<script> " +
            "select p.*  from fs_prescribe p    " +
            "where 1=1  " +
            "<if test = ' maps.patientName!=null and maps.patientName != \"\"  '> " +
            "and p.patient_name like concat('%', #{maps.patientName}, '%') " +
            "</if>" +
            "<if test = ' maps.prescribeType!=null  '> " +
            "and p.prescribe_type = #{maps.prescribeType} " +
            "</if>" +
            "<if test = ' maps.prescribeCode!=null and maps.prescribeCode!= \"\"  '> " +
            "and p.prescribe_code = #{maps.prescribeCode} " +
            "</if>" +
            "<if test = ' maps.type==1  '> " +
            "and p.doctor_id = #{maps.doctorId} " +
            "</if>" +
            "<if test = ' maps.type==2 and maps.status != null and maps.status == 1  '> " +
            "and p.status = 0 " +
            "</if>" +
            "<if test = ' maps.type==2 and maps.status != null and maps.status == 2  '> " +
            "and (p.status = 1 || p.status = 2) and p.drug_doctor_id = #{maps.doctorId} " +
            "</if>" +
            "<if test=' maps.beginTime != null and maps.beginTime != \"\" '>" +
            "AND date_format(p.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(p.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')  " +
            "</if>" +
            " order by p.create_time desc "+
            "</script>"})
    List<FsPrescribeListDVO> selectFsPrescribeListDVO(@Param("maps") FsPrescribeListDParam param);
    @Select({"<script> " +
            "select p.*  from fs_prescribe p    " +
            "where 1=1  " +
            "<if test = ' maps.userId != null '> " +
            "and p.user_id = #{maps.userId} " +
            "</if>" +
            "<if test = 'maps.status != null    '> " +
            "and p.status = #{maps.status} " +
            "</if>" +
            " order by p.create_time desc "+
            "</script>"})
    List<FsPrescribeListUVO> selectFsPrescribeListUVO(@Param("maps")FsPrescribeListUParam param);


    @Select("select so.*,ffff.doctor_name prescribeDoctorName, us.nick_name ,dc.doctor_name,dp.doctor_name doctor_drug_name,fso.order_code,fso.`status` order_status,fse.store_name FROM fs_prescribe so  LEFT JOIN fs_user us ON us.user_id=so.user_id LEFT JOIN fs_doctor dc ON dc.doctor_id = so.doctor_id LEFT JOIN fs_doctor dp ON dp.doctor_id =so.drug_doctor_id LEFT JOIN fs_store_order fso ON fso.order_id = so.store_order_id LEFT JOIN fs_store fse ON fse.store_id = so.store_id  LEFT JOIN fs_doctor ffff ON ffff.doctor_id=so.prescribe_doctor_id  where so.prescribe_id=#{prescribeId}")
    FsPrescribeVO selectFsPrescribeByPrescribeIdVO(Long prescribeId);
    @Select({"<script> " +
            "select so.*,us.nick_name,dc.doctor_name,dp.doctor_name doctor_drug_name,fso.order_code,fso.`status` order_status,fse.store_name,fio.order_sn FROM fs_prescribe so  LEFT JOIN fs_user us ON us.user_id=so.user_id LEFT JOIN fs_doctor dc ON dc.doctor_id = so.doctor_id LEFT JOIN fs_doctor dp ON dp.doctor_id =so.drug_doctor_id LEFT JOIN fs_store_order fso ON fso.order_id = so.store_order_id LEFT JOIN fs_store fse ON fse.store_id = so.store_id LEFT JOIN fs_inquiry_order fio ON fio.order_id=so.inquiry_order_id "+
            "<where>\n" +
            "        <if test=\"companyId != null \"> and fso.company_id = #{companyId}</if>\n" +
            "        <if test=\"companyUserId != null \"> and fso.company_user_id = #{companyUserId}</if>\n" +
            "        <if test=\"prescribeType != null \"> and so.prescribe_type = #{prescribeType}</if>\n" +
            "        <if test=\"inquiryOrderId != null \"> and so.inquiry_order_id = #{inquiryOrderId}</if>\n" +
            "        <if test=\"storeOrderId != null \"> and so.store_order_id = #{storeOrderId}</if>\n" +
            "        <if test=\"userId != null \"> and so.user_id = #{userId}</if>\n" +
            "        <if test=\"patientId != null \"> and so.patient_id = #{patientId}</if>\n" +
            "        <if test=\"prescribeCode != null  and prescribeCode != ''\"> and so.prescribe_code = #{prescribeCode}</if>\n" +
            "        <if test=\"patientDescs != null  and patientDescs != ''\"> and so.patient_descs = #{patientDescs}</if>\n" +
            "        <if test=\"patientAge != null  and patientAge != ''\"> and so.patient_age = #{patientAge}</if>\n" +
            "        <if test=\"patientName != null  and patientName != ''\"> and so.patient_name like concat('%', #{patientName}, '%')</if>\n" +
            "        <if test=\"doctorName != null  and doctorName != ''\"> and dc.doctor_name like concat('%', #{doctorName}, '%')</if>\n" +
            "        <if test=\"patientTel != null  and patientTel != ''\"> and so.patient_tel = #{patientTel}</if>\n" +
            "        <if test=\"patientGender != null  and patientGender != ''\"> and so.patient_gender = #{patientGender}</if>\n" +
            "        <if test=\"recordPic != null  and recordPic != ''\"> and so.record_pic = #{recordPic}</if>\n" +
            "        <if test=\"auditReason != null  and auditReason != ''\"> and so.audit_reason = #{auditReason}</if>\n" +
            "        <if test=\"diagnose != null  and diagnose != ''\"> and so.diagnose = #{diagnose}</if>\n" +
            "        <if test=\"doctorId != null  and doctorId != ''\"> and so.doctor_id = #{doctorId}</if>\n" +
            "        <if test=\"status != null \"> and so.status = #{status}</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(so.create_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(so.create_time) &lt;= DATE(#{eTime})</if>\n" +
            "            <if test=\"auditSTime != null \">  and DATE(so.audit_time) &gt;= DATE(#{auditSTime})</if>\n" +
            "            <if test=\"auditETime != null \">  and DATE(so.audit_time) &lt;= DATE(#{auditETime})</if>\n" +
            "        <if test=\"auditTime != null \"> and so.audit_time = #{auditTime}</if>\n" +
            "        <if test=\"orderCode != null \"> and fso.order_code = #{orderCode}</if>\n" +
            "        <if test=\"orderStatus != null \"> and fso.`status` = #{orderStatus}</if>\n" +
            "        <if test=\"storeId != null \"> and so.store_id = #{storeId}</if>\n" +
            "    </where>\n" +
            "    order by prescribe_id desc"+
            "</script>"})
    List<FsPrescribeExcelListVO> selectFsPrescribeExcelListVO(FsPrescribeParam fsPrescribe);

    @Select({"<script> " +
            "select so.*,dc.doctor_name,cu.nick_name as company_user_name,c.company_name FROM fs_prescribe so  " +
            " LEFT JOIN fs_user us ON us.user_id=so.user_id " +
            " LEFT JOIN fs_doctor dc ON dc.doctor_id = so.doctor_id " +
            " LEFT JOIN fs_doctor dp ON dp.doctor_id =so.drug_doctor_id " +
            " LEFT JOIN fs_store_order fso ON fso.order_id = so.store_order_id " +
            " LEFT JOIN fs_store fse ON fse.store_id = so.store_id " +
            " LEFT JOIN fs_inquiry_order fio ON fio.order_id=so.inquiry_order_id " +
            " left join company_user cu on fso.company_user_id = cu.user_id "+
            " left join company c on fso.company_id = c.company_id "+
            "<where>\n" +
            "        <if test=\"companyId != null \"> and fso.company_id = #{companyId}</if>\n" +
            "        <if test=\"companyUserId != null \"> and fso.company_user_id = #{companyUserId}</if>\n" +
            "        <if test=\"prescribeType != null \"> and so.prescribe_type = #{prescribeType}</if>\n" +
            "        <if test=\"inquiryOrderId != null \"> and so.inquiry_order_id = #{inquiryOrderId}</if>\n" +
            "        <if test=\"storeOrderId != null \"> and so.store_order_id = #{storeOrderId}</if>\n" +
            "        <if test=\"userId != null \"> and so.user_id = #{userId}</if>\n" +
            "        <if test=\"patientId != null \"> and so.patient_id = #{patientId}</if>\n" +
            "        <if test=\"prescribeCode != null  and prescribeCode != ''\"> and so.prescribe_code = #{prescribeCode}</if>\n" +
            "        <if test=\"patientDescs != null  and patientDescs != ''\"> and so.patient_descs = #{patientDescs}</if>\n" +
            "        <if test=\"patientAge != null  and patientAge != ''\"> and so.patient_age = #{patientAge}</if>\n" +
            "        <if test=\"patientName != null  and patientName != ''\"> and so.patient_name like concat('%', #{patientName}, '%')</if>\n" +
            "        <if test=\"doctorName != null  and doctorName != ''\"> and dc.doctor_name like concat('%', #{doctorName}, '%')</if>\n" +
            "        <if test=\"patientTel != null  and patientTel != ''\"> and so.patient_tel = #{patientTel}</if>\n" +
            "        <if test=\"patientGender != null  and patientGender != ''\"> and so.patient_gender = #{patientGender}</if>\n" +
            "        <if test=\"recordPic != null  and recordPic != ''\"> and so.record_pic = #{recordPic}</if>\n" +
            "        <if test=\"auditReason != null  and auditReason != ''\"> and so.audit_reason = #{auditReason}</if>\n" +
            "        <if test=\"diagnose != null  and diagnose != ''\"> and so.diagnose = #{diagnose}</if>\n" +
            "        <if test=\"doctorId != null  and doctorId != ''\"> and so.doctor_id = #{doctorId}</if>\n" +
            "        <if test=\"status != null \"> and so.status = #{status}</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(so.create_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(so.create_time) &lt;= DATE(#{eTime})</if>\n" +
            "            <if test=\"auditSTime != null \">  and DATE(so.audit_time) &gt;= DATE(#{auditSTime})</if>\n" +
            "            <if test=\"auditETime != null \">  and DATE(so.audit_time) &lt;= DATE(#{auditETime})</if>\n" +
            "        <if test=\"auditTime != null \"> and so.audit_time = #{auditTime}</if>\n" +
            "        <if test=\"orderCode != null \"> and fso.order_code = #{orderCode}</if>\n" +
            "        <if test=\"orderStatus != null \"> and fso.`status` = #{orderStatus}</if>\n" +
            "        <if test=\"storeId != null \"> and so.store_id = #{storeId}</if>\n" +
            "    </where>\n" +
            "    order by prescribe_id desc"+
            "</script>"})
    List<FsMessageFeedbackExportListVO> selectFsMessageFeedbackExportListVO(FsPrescribeParam fsPrescribe);
    @Select("select * from fs_prescribe where prescribe_img_url is not NULL and prescribe_img_url LIKE '%htj-1258038825.cos.ap-beijing.myqcloud.com%' \n" +
            "OR doctor_sign_url LIKE '%htj-1258038825.cos.ap-beijing.myqcloud.com%' \n" +
            "OR prescribe_doctor_sign_url LIKE '%htj-1258038825.cos.ap-beijing.myqcloud.com%' \n" +
            "OR drug_doctor_sign_url LIKE '%htj-1258038825.cos.ap-beijing.myqcloud.com%' \n" +
            "order by  prescribe_id desc ")
    List<FsPrescribe> selectFsPrescribeByPrescribeByimg();
    @Select("select prescribe_code from fs_prescribe where store_order_id=#{orderId}")
    String selectFsPrescribeCodeByOrderId(String  orderId);
    @Select("select prescribe_code from fs_prescribe where store_order_id=(select order_id from fs_store_order where order_code=#{businessCode} )")
    String selectFsPrescribeCodeByOrderCode(String businessCode);
    @Select({"<script> " +
            "select count(1) FROM fs_prescribe so  LEFT JOIN fs_user us ON us.user_id=so.user_id LEFT JOIN fs_doctor dc ON dc.doctor_id = so.doctor_id LEFT JOIN fs_doctor dp ON dp.doctor_id =so.drug_doctor_id LEFT JOIN fs_store_order fso ON fso.order_id = so.store_order_id LEFT JOIN fs_store fse ON fse.store_id = so.store_id LEFT JOIN fs_inquiry_order fio ON fio.order_id=so.inquiry_order_id "+
            "<where>\n" +
            "        <if test=\"companyId != null \"> and fso.company_id = #{companyId}</if>\n" +
            "        <if test=\"companyUserId != null \"> and fso.company_user_id = #{companyUserId}</if>\n" +
            "        <if test=\"prescribeType != null \"> and so.prescribe_type = #{prescribeType}</if>\n" +
            "        <if test=\"inquiryOrderId != null \"> and so.inquiry_order_id = #{inquiryOrderId}</if>\n" +
            "        <if test=\"storeOrderId != null \"> and so.store_order_id = #{storeOrderId}</if>\n" +
            "        <if test=\"userId != null \"> and so.user_id = #{userId}</if>\n" +
            "        <if test=\"patientId != null \"> and so.patient_id = #{patientId}</if>\n" +
            "        <if test=\"prescribeCode != null  and prescribeCode != ''\"> and so.prescribe_code = #{prescribeCode}</if>\n" +
            "        <if test=\"patientDescs != null  and patientDescs != ''\"> and so.patient_descs = #{patientDescs}</if>\n" +
            "        <if test=\"patientAge != null  and patientAge != ''\"> and so.patient_age = #{patientAge}</if>\n" +
            "        <if test=\"patientName != null  and patientName != ''\"> and so.patient_name like concat('%', #{patientName}, '%')</if>\n" +
            "        <if test=\"doctorName != null  and doctorName != ''\"> and dc.doctor_name like concat('%', #{doctorName}, '%')</if>\n" +
            "        <if test=\"patientTel != null  and patientTel != ''\"> and so.patient_tel = #{patientTel}</if>\n" +
            "        <if test=\"patientGender != null  and patientGender != ''\"> and so.patient_gender = #{patientGender}</if>\n" +
            "        <if test=\"recordPic != null  and recordPic != ''\"> and so.record_pic = #{recordPic}</if>\n" +
            "        <if test=\"auditReason != null  and auditReason != ''\"> and so.audit_reason = #{auditReason}</if>\n" +
            "        <if test=\"diagnose != null  and diagnose != ''\"> and so.diagnose = #{diagnose}</if>\n" +
            "        <if test=\"doctorId != null  and doctorId != ''\"> and so.doctor_id = #{doctorId}</if>\n" +
            "        <if test=\"status != null \"> and so.status = #{status}</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(so.create_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(so.create_time) &lt;= DATE(#{eTime})</if>\n" +
            "            <if test=\"auditSTime != null \">  and DATE(so.audit_time) &gt;= DATE(#{auditSTime})</if>\n" +
            "            <if test=\"auditETime != null \">  and DATE(so.audit_time) &lt;= DATE(#{auditETime})</if>\n" +
            "        <if test=\"auditTime != null \"> and so.audit_time = #{auditTime}</if>\n" +
            "        <if test=\"orderCode != null \"> and fso.order_code = #{orderCode}</if>\n" +
            "        <if test=\"orderStatus != null \"> and fso.`status` = #{orderStatus}</if>\n" +
            "        <if test=\"storeId != null \"> and so.store_id = #{storeId}</if>\n" +
            "    </where>\n" +
            "   "+
            "</script>"})
    Long selectFsPrescribeExcelListVOCount(FsPrescribeParam fsPrescribe);

    @Select("select p.prescribe_id from fs_store_order o LEFT JOIN fs_prescribe p on p.store_order_id =o.order_id where o.status=2 and  o.order_type=2 and p.prescribe_img_store_url is null ")
    List<Long> selectFsPrescribeByPrescribeIdByOrderType();

}
