package org.dromara.web.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
import org.dromara.web.domain.EnteralNutrition;
import org.dromara.web.domain.vo.EnteralNutritionQueryOneVo;
import org.dromara.web.domain.vo.EnteralNutritionVo;

import java.util.List;

/**
 * 肠内营养Mapper接口
 *
 * @author Lion Li
 * @date 2025-07-22
 */
public interface EnteralNutritionMapper extends BaseMapperPlus<EnteralNutrition, EnteralNutritionVo> {

    @Select("<script>" +
        "SELECT " +
        "   MIN(en.id) AS id, " +
        "   en.settlement_id AS settlementId, " +
        "   GROUP_CONCAT(DISTINCT en.id) AS idStr, " +
        "   GROUP_CONCAT(DISTINCT en.nutrition_product) AS productName, " +
        "   MIN(en.prescription_date) AS prescriptionDate, " +
        "   en.group_no AS groupNo, " +
        "   MIN(en.execute_date) AS executeDate, " +
        "   MIN(en.meal_time) AS mealTime, " +
        "   MIN(en.execute_time) AS executeTime, " +
        "   MIN(se.payment_status) AS paymentStatus, " +
        "   MIN(en.execute_status) AS executeStatus, " +
        "   MIN(en.stop_date) AS stopDate, " +
        "   MIN(en.config_status) AS configStatus, " +
        "   MIN(en.config_time) AS configTime, " +
        "   MIN(en.config_by) AS configBy, " +
        "   MIN(en.execute_by) AS executeBy, " +
        "   MIN(en.tag_print_num) AS tagPrintNum, " +
        "   MIN(sd.dept_name) AS patientDepartment, " +
        "   MIN(se.visit_type) AS visitType, " +
        "   MIN(tu.id_card) AS idCard, " +
        "   MIN(tu.outpatient_no) AS patientNo, " +
        "   MIN(tu.bed_no) AS bedNo, " +
        "   MIN(tu.ward_name) AS wardName, " +
        "   MIN(tu.treat_num) AS treatNum, " +
        "   MIN(su.user_name) AS configByName, " +
        "   MIN(syu.user_name) AS executeByName, " +
        "   MIN(tu.treat_name) AS patientName, " +
        "   MIN(se.stop_date) AS stopDate, " +
        "   MIN(se.patient_id) AS patientId, " +
        "   MIN(en.del_flag) AS delFlag " +
        "FROM enteral_nutrition en " +
        "LEFT JOIN settlement se ON en.settlement_id = se.id AND IFNULL(se.del_Flag, '0') != '1' " +
        "LEFT JOIN sys_user su ON en.config_by = su.user_id AND IFNULL(su.del_Flag, '0') != '1' " +
        "LEFT JOIN sys_user syu ON en.execute_by = syu.user_id AND IFNULL(syu.del_Flag, '0') != '1' " +
        "LEFT JOIN treatment_user tu ON se.patient_id = tu.id AND IFNULL(tu.del_Flag, '0') != '1' " +
        "LEFT JOIN sys_dept sd ON se.door_id = sd.dept_id AND IFNULL(sd.del_Flag, '0') != '1' " +
        "WHERE 1 = 1 " +
        "<if test='prescriptionType != null and prescriptionType != \"\"'> and en.prescription_type = #{prescriptionType} </if> " +
        "<if test='visitType != null and visitType != \"\"'> and se.visit_type = #{visitType} </if> " +
        "<if test='configStatus != null and configStatus != \"\"'> and en.config_status = #{configStatus} </if> " +
        "<if test='paymentStatus != null and paymentStatus != \"\"'> and se.payment_status = #{paymentStatus} </if> " +
        "<if test='executeStatus != null and executeStatus != \"\"'> and en.execute_status = #{executeStatus} </if> " +
        "<if test='doorId != null '> and se.door_id = #{doorId} </if> " +
        "<if test='wardId != null '> and se.ward_id = #{wardId} </if> " +
        "<if test='searchValue != null and searchValue != \"\"'> and (tu.treat_num like concat('%', #{searchValue}, '%') " +
        "or tu.treat_name like concat('%', #{searchValue}, '%') or tu.id_card like concat('%', #{searchValue}, '%')" +
        "or tu.outpatient_no like concat('%', #{searchValue}, '%') or en.settlement_id like concat('%', #{searchValue}, '%')) </if> " +
        "<if test='beginTime != null'> and en.prescription_date <![CDATA[>=]]> #{beginTime} </if>" +
        "<if test='endTime != null'> and en.prescription_date <![CDATA[<=]]> #{endTime} </if>" +
        "<if test='exeBeginTime != null'> and en.execute_date <![CDATA[>=]]> #{exeBeginTime} </if>" +
        "<if test='exeEndTime != null'> and en.execute_date <![CDATA[<=]]> #{exeEndTime} </if>" +
        "GROUP BY en.settlement_id, en.group_no " +
        "ORDER BY MIN(en.create_time) DESC " +
        "<if test='offset != null and rows != null'>LIMIT #{offset}, #{rows}</if>" +
        "</script>")
    List<EnteralNutritionQueryOneVo> queryPageVoList(@Param("prescriptionType") String prescriptionType, @Param("visitType") String visitType, @Param("configStatus") String configStatus,
                                                     @Param("paymentStatus") String paymentStatus, @Param("executeStatus") String executeStatus, @Param("doorId") Long doorId,
                                                     @Param("wardId") Long wardId, @Param("searchValue") String searchValue, @Param("beginTime") String beginTime,
                                                     @Param("endTime") String endTime, @Param("exeBeginTime") String exeBeginTime,
                                                     @Param("exeEndTime") String exeEndTime, @Param("offset") Integer offset, @Param("rows") Integer rows);

    @Select("<script>" +
        "SELECT COUNT(*) FROM ( SELECT 1 FROM enteral_nutrition en " +
        "LEFT JOIN settlement se ON en.settlement_id = se.id AND IFNULL(se.del_Flag, '0') != '1' " +
        "LEFT JOIN sys_user su ON en.config_by = su.user_id AND IFNULL(su.del_Flag, '0') != '1' " +
        "LEFT JOIN sys_user syu ON en.execute_by = syu.user_id AND IFNULL(syu.del_Flag, '0') != '1' " +
        "LEFT JOIN treatment_user tu ON se.patient_id = tu.id AND IFNULL(tu.del_Flag, '0') != '1' " +
        "LEFT JOIN sys_dept sd ON se.door_id = sd.dept_id AND IFNULL(sd.del_Flag, '0') != '1' " +
        "WHERE 1 = 1 " +
        "<if test='prescriptionType != null and prescriptionType != \"\"'> and en.prescription_type = #{prescriptionType} </if> " +
        "<if test='visitType != null and visitType != \"\"'> and se.visit_type = #{visitType} </if> " +
        "<if test='configStatus != null and configStatus != \"\"'> and en.config_status = #{configStatus} </if> " +
        "<if test='paymentStatus != null and paymentStatus != \"\"'> and se.payment_status = #{paymentStatus} </if> " +
        "<if test='executeStatus != null and executeStatus != \"\"'> and en.execute_status = #{executeStatus} </if> " +
        "<if test='doorId != null '> and se.door_id = #{doorId} </if> " +
        "<if test='wardId != null '> and se.ward_id = #{wardId} </if> " +
        "<if test='searchValue != null and searchValue != \"\"'> and (tu.treat_num like concat('%', #{searchValue}, '%') " +
        "or tu.treat_name like concat('%', #{searchValue}, '%') or tu.id_card like concat('%', #{searchValue}, '%')" +
        "or tu.outpatient_no like concat('%', #{searchValue}, '%') or en.settlement_id like concat('%', #{searchValue}, '%')) </if> " +
        "<if test='beginTime != null'> and en.prescription_date <![CDATA[>=]]> #{beginTime} </if>" +
        "<if test='endTime != null'> and en.prescription_date <![CDATA[<=]]> #{endTime} </if>" +
        "<if test='exeBeginTime != null'> and en.execute_date <![CDATA[>=]]> #{exeBeginTime} </if>" +
        "<if test='exeEndTime != null'> and en.execute_date <![CDATA[<=]]> #{exeEndTime} </if>" +
        "GROUP BY en.settlement_id, en.group_no ) t" +
        "</script>")
    Integer queryPageVoCount(@Param("prescriptionType") String prescriptionType, @Param("visitType") String visitType, @Param("configStatus") String configStatus,
                             @Param("paymentStatus") String paymentStatus, @Param("executeStatus") String executeStatus, @Param("doorId") Long doorId,
                             @Param("wardId") Long wardId, @Param("searchValue") String searchValue, @Param("beginTime") String beginTime,
                             @Param("endTime") String endTime, @Param("exeBeginTime") String exeBeginTime,
                             @Param("exeEndTime") String exeEndTime);


}
