package thyyxxk.webserver.dao.his.dictionary;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.dictionary.EmployeeMi;
import thyyxxk.webserver.entity.dictionary.EmployeeMiVo;

import java.util.List;

@Mapper
public interface EmployeeMiDao extends BaseMapper<EmployeeMi> {

    @Select("  select mi.code,   " +
            "         mi.dept_code, rtrim(unit.name) as dept_name,   " +
            "         rtrim(mi.name) as name,   " +
            "         mi.emp_po_code, rtrim(po.name) as emp_po_name,  " +
            "         mi.py_code,   " +
            "         mi.d_code,   " +
            "         mi.emp_tit_code, rtrim(tit.name) as emp_tit_name,  " +
            "         mi.mark, rtrim(mark.name) as mark_name,  " +
            "         mi.dept_code_2,   " +
            "         mi.prof_type,   " +
            "         mi.ifcadre, rtrim(ifcadre.name) as ifcadre_name,  " +
            "         mi.limited,   " +
            "         mi.order_yn,   " +
            "         mi.del_flag,  " +
            "         mi.n_code, " +
            "         '0' as new_flag,  " +
            "         mi.code_rs,  " +
            "         mi.ysjb, rtrim(ysjb.name) as ysjb_name, " +
            "         mi.gh_charge_type, rtrim(hb.name) as gh_charge_name, " +
            "         mi.rs_pr_sj,  " +
            "         mi.doctor_xz_yp,  " +
            "         mi.login_flag, " +
            "         isnull(mi.del_flag,'0') as del_flag_b, " +
            "         mi.social_no,  " +
            "         mi.phone_no,  " +
            "         mi.yb_code  " +
            "    from a_employee_mi as mi  " +
            "    left join zd_unit_code as unit " +
            "      on mi.dept_code = unit.code and isnull(unit.del_flag, '') <> '1' " +
            "    left join zd_emp_position as po  " +
            "      on mi.emp_po_code = po.code " +
            "    left join zd_emp_title as tit  " +
            "      on mi.emp_tit_code = tit.code " +
            "    left join zd_emp_inmark as mark  " +
            "      on mi.mark = mark.code " +
            "    left join zd_ifcadre as ifcadre  " +
            "      on mi.ifcadre = ifcadre.code " +
            "    left join ysh_zd_ysjb as ysjb " +
            "      on mi.ysjb = ysjb.code " +
            "    left join mzy_zd_charge_type as hb " +
            "      on mi.gh_charge_type = hb.code " +
            "   where isnull(mi.py_code,'') like ('%' + upper(#{text}) + '%')  " +
            "      or isnull(mi.d_code,'') like ('%' + upper(#{text}) + '%')  " +
            "      or (isnull(mi.code,'') like '%${text}%' or isnull(mi.n_code,'') like '${text}%') " +
            "      or isnull(mi.name,'') like '%${text}%' " +
            "   order by mi.code ")
    List<EmployeeMiVo> selectEmployeeMiAll(@Param("text") String text);

    @Select(" select mi.* from a_employee_mi mi where mi.code = #{code} ")
    EmployeeMi selectEmployeeMiByCode(@Param("code") String code);

    @Delete(" delete from a_employee_mi where code = #{code} ")
    int deleteEmployeeMiByCode(@Param("code") String code);

    @Update(" update a_employee_mi set del_flag = #{delFlag} where code = #{code} ")
    int updateEmployeeMiDelFlagByCode(@Param("code") String code, @Param("delFlag") String delFlag);

    /**
     * @Description 获取所有医生
     * @Author hsh
     * @Date 2024/1/25 11:06
     */
    @Select("select rtrim(code) as code, rtrim(name) as name, isnull(del_flag, '0') as statusFlag, " +
            " case when del_flag = '1' then 'true' else 'false' end as disabled " +
            "from a_employee_mi with(nolock) where isnull(del_flag, '0') = '0' order by code ")
    List<CodeName> selectEmployee();

    /**
     * @Description 根据text模糊查询，获取所有在职医生
     * @Author hsh
     * @Date 2024/1/25 11:29
     */
    @Select("select rtrim(code) as code, rtrim(name) as name, isnull(del_flag, '0') as statusFlag, " +
            " case when del_flag = '1' then 'true' else 'false' end as disabled " +
            "from a_employee_mi with(nolock) where isnull(del_flag, '0') = '0' " +
            " and (code like '%${text}%' or name like '%${text}%' " +
            "  or py_code like ('%' + upper(#{text}) + '%') " +
            "  or d_code like ('%' + upper(#{text}) + '%')) " +
            " order by code ")
    List<CodeName> selectEmployeeByCode(String text);
}
