package thyyxxk.webserver.dao.his.dictionary.clinicalPathway;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.dto.ClinicalPathwayOrder;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.vo.CreateOrderVo;

import java.util.List;

@Mapper
public interface ClinicalPathwayOrderDao extends BaseMapper<ClinicalPathwayOrder> {


    @Select("select *,\n" +
            "       supply_code_name = case\n" +
            "                              when type = 1 then\n" +
            "                                      (select rtrim(name) from jy_zd_sample where code = supply_code)\n" +
            "                              else (select rtrim(supply_name)\n" +
            "                                    from yz_supply_type\n" +
            "                                    where yz_supply_type.supply_code = clinical_pathway_order.supply_code) end,\n" +
            "       mini_unit_name   = (select rtrim(name)\n" +
            "                           from yp_zd_unit\n" +
            "                           where code = mini_unit),\n" +
            "       dose_unit_name   = (select rtrim(name)\n" +
            "                           from yp_zd_unit\n" +
            "                           where code = dose_unit),\n" +
            "       exec_unit_name   = (select rtrim(name)\n" +
            "                           from zd_unit_code\n" +
            "                           where code = exec_unit) " +
            "from clinical_pathway_order where work_id = #{workId}")
    List<CreateOrderVo> getOrderTemplate(String workId);


    @Select("select *,\n" +
            "       supply_code_name = case\n" +
            "                              when type = 1 then\n" +
            "                                      (select rtrim(name) from jy_zd_sample where code = supply_code)\n" +
            "                              else (select rtrim(supply_name)\n" +
            "                                    from yz_supply_type\n" +
            "                                    where yz_supply_type.supply_code = clinical_pathway_order.supply_code) end,\n" +
            "       mini_unit_name   = (select rtrim(name)\n" +
            "                           from yp_zd_unit\n" +
            "                           where code = mini_unit),\n" +
            "       dose_unit_name   = (select rtrim(name)\n" +
            "                           from yp_zd_unit\n" +
            "                           where code = dose_unit),\n" +
            "       exec_unit_name   = (select rtrim(name)\n" +
            "                           from zd_unit_code\n" +
            "                           where code = exec_unit)," +
            "exec_type " +
            "from clinical_pathway_day a\n" +
            "         inner join clinical_pathway_work b on (a.id = b.day_id)\n" +
            "         inner join clinical_pathway_order c on (b.id = c.work_id)\n" +
            "where a.id = #{dayId}")
    List<CreateOrderVo> getOrderTemplateByDayId(String dayId);


    @Delete("delete clinical_pathway_order where work_id = #{workId}")
    void deleteOrderTemplateByWorkId(String workId);
}
