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.HospitalMealPlan;
import org.dromara.web.domain.vo.HospitalMealPlanQueryVo;
import org.dromara.web.domain.vo.HospitalMealPlanVo;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 院内膳食主Mapper接口
 *
 * @author Lion Li
 * @date 2025-07-23
 */
@Repository
public interface HospitalMealPlanMapper extends BaseMapperPlus<HospitalMealPlan, HospitalMealPlanVo> {

    @Select("<script>" +
        "SELECT  " +
        "    MIN(hmr.id) AS id, " +
        "    hmp.settlement_id AS settlementId, " +
        "    GROUP_CONCAT(DISTINCT hmr.id) AS idStr, " +
        "    GROUP_CONCAT(DISTINCT hmr.food_name) AS productName, " +
        "    MIN(hmr.create_time) AS prescriptionDate, " +
        "    hmr.recipe_no AS recipeNo, " +
        "    MIN(hmr.execute_date) AS executeDate, " +
        "    MIN(hmr.meal_time) AS mealTime, " +
        "    hmr.eat_time AS eatTime, " +
        "    MIN(hmr.execute_time) AS executeTime, " +
        "    MIN(se.payment_status) AS paymentStatus, " +
        "    MIN(hmr.execute_status) AS executeStatus, " +
        "    MIN(hmr.make_status) AS makeStatus, " +
        "    MIN(hmr.make_time) AS makeTime, " +
        "    MIN(hmr.make_by) AS makeBy, " +
        "    MIN(hmr.execute_by) AS executeBy, " +
        "    MIN(hmr.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 makeByName, " +
        "    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(hmp.recommend_start_date) AS recommendStartDate, " +
        "    MIN(hmp.recommend_end_date) AS recommendEndDate, " +
        "    MIN(hmr.del_flag) AS delFlag " +
        "FROM hospital_meal_recipe hmr " +
        "LEFT JOIN hospital_meal_plan hmp ON hmr.plan_id = hmp.id AND IFNULL(hmp.del_Flag, '0') != '1' " +
        "LEFT JOIN settlement se ON hmp.settlement_id = se.id AND IFNULL(se.del_Flag, '0') != '1' " +
        "LEFT JOIN sys_user su ON hmr.make_by = su.user_id AND IFNULL(su.del_Flag, '0') != '1' " +
        "LEFT JOIN sys_user syu ON hmr.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 se.charge_type = #{prescriptionType} </if> " +
        "<if test='visitType != null and visitType != \"\"'> and se.visit_type = #{visitType} </if> " +
        "<if test='makeStatus != null and makeStatus != \"\"'> and hmr.make_status = #{makeStatus} </if> " +
        "<if test='paymentStatus != null and paymentStatus != \"\"'> and se.payment_status = #{paymentStatus} </if> " +
        "<if test='executeStatus != null and executeStatus != \"\"'> and hmr.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 hmp.settlement_id like concat('%', #{searchValue}, '%')) </if> " +
        "<if test='beginTime != null'> and hmr.create_time <![CDATA[>=]]> #{beginTime} </if> " +
        "<if test='endTime != null'> and hmr.create_time <![CDATA[<=]]> #{endTime} </if> " +
        "<if test='exeBeginTime != null'> and hmr.execute_date <![CDATA[>=]]> #{exeBeginTime} </if> " +
        "<if test='exeEndTime != null'> and hmr.execute_date <![CDATA[<=]]> #{exeEndTime} </if> " +
        "GROUP BY hmr.plan_id, hmr.recipe_no, hmp.settlement_id, hmr.eat_time " +
        "ORDER BY MIN(hmr.create_time) DESC " +
        "<if test='offset != null and rows != null'>LIMIT #{offset}, #{rows}</if>" +
        "</script>")
    List<HospitalMealPlanQueryVo> queryPageVoList(@Param("prescriptionType") String prescriptionType, @Param("visitType") String visitType, @Param("makeStatus") String makeStatus,
                                                  @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 hospital_meal_recipe hmr " +
        "LEFT JOIN hospital_meal_plan hmp ON hmr.plan_id = hmp.id AND IFNULL(hmp.del_Flag, '0') != '1' " +
        "LEFT JOIN settlement se ON hmp.settlement_id = se.id AND IFNULL(se.del_Flag, '0') != '1' " +
        "LEFT JOIN sys_user su ON hmr.make_by = su.user_id AND IFNULL(su.del_Flag, '0') != '1' " +
        "LEFT JOIN sys_user syu ON hmr.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 se.charge_type = #{prescriptionType} </if> " +
        "<if test='visitType != null and visitType != \"\"'> and se.visit_type = #{visitType} </if> " +
        "<if test='makeStatus != null and makeStatus != \"\"'> and hmr.make_status = #{makeStatus} </if> " +
        "<if test='paymentStatus != null and paymentStatus != \"\"'> and se.payment_status = #{paymentStatus} </if> " +
        "<if test='executeStatus != null and executeStatus != \"\"'> and hmr.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 hmp.settlement_id like concat('%', #{searchValue}, '%')) </if> " +
        "<if test='beginTime != null'> and hmr.create_time <![CDATA[>=]]> #{beginTime} </if>" +
        "<if test='endTime != null'> and hmr.create_time <![CDATA[<=]]> #{endTime} </if>" +
        "<if test='exeBeginTime != null'> and hmr.execute_date <![CDATA[>=]]> #{exeBeginTime} </if>" +
        "<if test='exeEndTime != null'> and hmr.execute_date <![CDATA[<=]]> #{exeEndTime} </if>" +
        "GROUP BY hmr.plan_id, hmr.recipe_no,hmp.settlement_id, hmr.eat_time ) t" +
        "</script>")
    Integer queryPageVoCount(@Param("prescriptionType") String prescriptionType, @Param("visitType") String visitType, @Param("makeStatus") String makeStatus,
                             @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);

//    @Select(
//        """
//        SELECT
//            `hospital_meal_plan`.`id` AS `id`,
//            `settlement`.`create_time` AS `tradeDate`,
//            `hospital_meal_plan`.`create_time` AS `calculationDate`,
//            `hospital_meal_plan`.`type` AS `consultationType`,
//            `doctor_dept`.`dept_name` AS `calculationDoctorDepartment`,
//            `sys_user`.`user_name` AS `calculationDoctor`,
//            `patient_dept`.`dept_name` AS `patientDepartment`,
//            `treatment_user`.`treat_name` AS `patientName`,
//            `settlement`.`payment_status` AS `payStatus`,
//            JSON_ARRAYAGG(
//                JSON_OBJECT(
//                    `mealName`, `hospital_meal_recipe`.`food_name`,
//                    `cookbooks`, JSON_ARRAYAGG(
//                        JSON_OBJECT(
//                            `cookbookName`, ``
//                        )
//                    )
//                )
//            ) AS `mealPeriods`
//        FROM `hospital_meal_plan`
//        LEFT JOIN `settlement`
//            ON `settlement`.`id` = `hospital_meal_plan`.`settlement_id` AND
//               `settlement`.`create_time` >= #{tradeDateEarliest} AND
//               `settlement`.`create_time` <= #{tradeDateLatest}
//        LEFT JOIN `sys_dept` `patient_dept`
//            ON `patient_dept`.`dept_id` = `hospital_meal_plan`.`dept_id`
//        LEFT JOIN `sys_dept` `doctor_dept`
//            ON `doctor_dept`.`dept_id` = `hospital_meal_plan`.`create_dept`
//        LEFT JOIN `sys_user`
//            ON `sys_user`.`user_id` = `hospital_meal_plan`.`create_by`
//        LEFT JOIN `treatment_user`
//            ON `treatment_user`.`id` = `hospital_meal_plan`.`patient_id`
//        LEFT JOIN `hospital_meal_recipe`
//            ON `hospital_meal_recipe`.`plan_id` = `hospital_meal_plan`.`id`
//        LEFT JOIN `sys_recipe`
//            ON `hospital_meal_recipe`.`food_id` = `sys_recipe`.`recipe_id`
//        WHERE
//            `hospital_meal_plan`.`type` = #{consultationType}
//        AND `hospital_meal_plan`.`dept_id` = #{patientDepartmentId}
//        AND `hospital_meal_plan`.`create_time` >= #{calculationDateEarliest}
//        AND `hospital_meal_plan`.`create_time` <= #{calculationDateLatest}
//        AND `hospital_meal_plan`.`create_dept` = #{doctorDepartmentId}
//        AND `hospital_meal_plan`.`settlement_id` is not null
//        LIMIT #{pageNum * pageSize}, #{pageSize};
//        """
//    )
//    DietaryPrescriptionVo listPageMealPlan(
//        @Param("tradeDateEarliest") Date tradeDateEarliest, @Param("tradeDateLatest") Date tradeDateLatest,
//        @Param("calculationDateEarliest") Date calculationDateEarliest, @Param("calculationDateLatest") Date calculationDateLatest,
//        @Param("doctorDepartmentId") Long doctorDepartmentId, @Param("patientDepartmentId") Long patientDepartmentId,
//        @Param("name") String name, @Param("consultationType") Integer consultationType,
//        @Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize
//    );
}
