package thyyxxk.webserver.dao.his.outpatient;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.outpatient.mangedoctorinfo.DoctorInfo;

import java.util.List;

/**
 * @author dj
 */
@Mapper
public interface DoctorManageDao {
    @Select("select rtrim(code) code, rtrim(name) name, rtrim(py_code) pyCode from zd_unit_code with(nolock) where mz_flag=1")
    List<CodeName> getAllMzDept();

    @Select("select titleName=(select rtrim(name) from zd_emp_title with(nolock) where code=a.emp_tit_code), " +
            "rtrim(a.emp_tit_code) titleCode, " +
            "rtrim(a.name) name, " +
            "isnull(a.sex_code, 9) sex, " +
            "rtrim(a.code) code, " +
            "isnull(a.wx_homepage_flag, 0) wxHomepageFlag, " +
            "rtrim(a.dept_code) deptCode, " +
            "deptName=(select rtrim(name) from zd_unit_code with(nolock) where code=a.dept_code) " +
            "from a_employee_mi a with(nolock) " +
            "where a.dept_code like #{deptCode} " +
            "and a.name like #{doctorName} " +
            "and a.code not in ('00000', '00026') " +
            "and isnull(a.del_flag,0)<>1 " +
            "and a.show_manage_flag=#{manageFlag}")
    IPage<DoctorInfo> getAllDoctors(IPage<DoctorInfo> iPage,
                                    @Param("deptCode") String deptCode,
                                    @Param("doctorName") String doctorName,
                                    @Param("manageFlag") int manageFlag);

    @Select("select rtrim(code) code, rtrim(name) name " +
            "from zd_emp_title with(nolock) where tit_grade=1 order by code")
    List<CodeName> getAllTitles();

    @Select("select name=rtrim(a.name), " +
            "sex=isnull(a.sex_code, 9)," +
            "rtrim(a.emp_tit_code) titleCode," +
            "titleName=(select rtrim(name) from zd_emp_title with(nolock) where code=a.emp_tit_code), " +
            "code=rtrim(a.code), " +
            "rtrim(a.dept_code) deptCode," +
            "deptName=(select rtrim(name) from zd_unit_code with(nolock) where code=a.dept_code), " +
            "a.portrait, " +
            "a.specialty, " +
            "a.introduction " +
            "from a_employee_mi a with(nolock) where a.code=#{code}")
    DoctorInfo getDoctorInfo(@Param("code") String code);

    @Update("update a_employee_mi set " +
            "name=#{name}, " +
            "sex_code=#{sex}, " +
            "dept_code=#{deptCode}, " +
            "emp_tit_code=#{titleCode}, " +
            "introduction=#{introduction}, " +
            "specialty=#{specialty} " +
            "where code=#{code}")
    void saveDoctorInfo(DoctorInfo param);

    @Update("update a_employee_mi set portrait=#{imgBase64} where code=#{code}")
    void updatePortraitUrl(@Param("imgBase64") String imgBase64, @Param("code") String code);

    @Update("update a_employee_mi set wx_homepage_flag=#{flag} where code=#{code}")
    void updateDoctorWxHomepageFlag(@Param("code") String code, @Param("flag") Integer flag);

    @Select("select " +
            "rtrim(b.name) titleName, " +
            "rtrim(a.name) name, " +
            "isnull(a.sex_code, 9) sex, " +
            "rtrim(a.code) code, " +
            "a.wx_homepage_flag," +
            "a.wx_homepage_order, " +
            "deptName=(select rtrim(name) from zd_unit_code with(nolock) where code=a.dept_code) " +
            "from a_employee_mi a with(nolock),zd_emp_title b with(nolock) " +
            "where a.emp_tit_code=b.code " +
            "and a.wx_homepage_flag=1 " +
            "and b.tit_grade=1 " +
            "and isnull(a.del_flag,0)<>1 " +
            "and a.show_manage_flag=1 " +
            "order by a.wx_homepage_order")
    List<DoctorInfo> getAllRecommendDoctors();

    @Update("update a_employee_mi set wx_homepage_order=#{order} where code=#{code}")
    void updateWxHomepageOrder(@Param("code") String code, @Param("order") Integer order);

    @Update("update a_employee_mi set wx_homepage_flag=0,show_manage_flag=0 where code=#{code}")
    void deleteDoctor(@Param("code") String code);

    @Update("update a_employee_mi set show_manage_flag=1 where code=#{code}")
    void recoverDoctor(@Param("code") String code);
}
