package thyyxxk.webserver.dao.his.settings;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
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.login.UserInfo;
import thyyxxk.webserver.entity.settings.deptphones.DeptPhones;
import thyyxxk.webserver.entity.settings.permissions.Role;
import thyyxxk.webserver.entity.settings.users.WorkIntegrationPlatformAdvice;

import java.util.List;
import java.util.Map;

/**
 * @author dj
 */
@Mapper
public interface SettingsDao {

    @Select("select rtrim(code) as code, " +
            "rtrim(code_rs) codeRs, " +
            "rtrim(name) as name, " +
            "deptCode=rtrim(dept_code), " +
            "title=(select rtrim(name) from zd_emp_title where code=emp_tit_code), " +
            "position=(select rtrim(name) from zd_emp_position where code=emp_po_code), " +
            "deptName=(select rtrim(name) from zd_unit_code where code=dept_code) " +
            "from a_employee_mi where isnull(del_flag,0)=0 and code=#{code}")
    UserInfo getUserInfo(@Param("code") String code);

    @Select("select password from dj_user_base where code=#{code}")
    String getPassword(@Param("code") String code);

    @Update("update dj_user_base set password=#{password} where code=#{code}")
    void updatePassword(@Param("code") String code, @Param("password") String password);

    @Select("select role_id from dj_user_role where user_code=#{code}")
    List<Integer> getUserRoles(@Param("code") String code);

    @Select("select rtrim(code) code, rtrim(name) name from zd_unit_code where isnull(del_flag,0)!=1 order by code")
    List<CodeName> selectAllDepts();

    @Select("select rtrim(code) code, rtrim(name) name from zd_unit_code where code=(" +
            "select rtrim(dept_code) from a_employee_mi where code=#{code})")
    List<CodeName> selectDeptsByUserCode(@Param("code") String code);

    @Select("select                    rtrim(code)         as code, " +
            "                          rtrim(code_rs)      as codeRs, " +
            "                          rtrim(name)         as name, " +
            "                          rtrim(yb_name)         as ybName, " +
            "                          rtrim(referral_code)         as referralCode, " +
            "                          rtrim(isnull(dept_code,''))    as deptCode, " +
            "    deptName=(select rtrim(name) " +
            "              from zd_unit_code " +
            "              where code = dept_code), " +
            "                          rtrim(emp_po_code) as emp_po_code, " +
            "    position=(select rtrim(name) " +
            "              from zd_emp_position " +
            "              where code = emp_po_code), " +
            "                          rtrim(mark) as mark, " +
            "    mark_name =           (select rtrim(name) from zd_emp_inmark b where b.code = a.mark), " +
            "                          rtrim(ifcadre) as ifcadre, " +
            "    ifcadre_name =        (select rtrim(name) from zd_ifcadre b where a.ifcadre = b.code), " +
            "                          rtrim(py_code) as py_code, " +
            "                          rtrim(d_code) as d_code, " +
            "                          rtrim(order_yn) as order_yn /*医生处方权 1 普通处方权 2 毒麻药处方权*/, " +
            "                          rtrim(ysjb) as ysjb, " +
            "                          rtrim(emp_tit_code) as emp_tit_code, " +
            "    title=(select rtrim(name) " +
            "           from zd_emp_title " +
            "           where code = emp_tit_code), " +
            "                          rtrim(gh_charge_type) as gh_charge_type, " +
            "    gh_charge_type_name = (select rtrim(name) from mzy_zd_charge_type b where b.code = gh_charge_type), " +
            "                          rtrim(doctor_xz_yp) as doctor_xz_yp /* 0 非限制使用药品 1 限制使用药品 2 特殊使用药品  */, " +
            "                          rtrim(isnull(nullif(login_flag,''),2)) as login_flag /*病区允许就诊 1 允许 2 不允许*/, " +
            "                          isnull(del_flag, 0) as del_flag, " +
            "                          rtrim(social_no) as social_no /*身份证*/, " +
            "                          rtrim(phone_no) as phone_no /* 手机 号码*/," +
            "                          rtrim(yb_code) as yb_code," +
            "                          partTimeDeptCode =        STUFF((select ',' + zy_part_time_dept.dept_code " +
            "                                                     from zy_part_time_dept " +
            "                                                     where zy_part_time_dept.code = a.code " +
            "                                                     for xml path('')), 1, 1, ''), " +
            "                          partTimeDeptName =    STUFF((select ',' + (select rtrim(name) " +
            "                                                                   from zd_unit_code " +
            "                                                                   where zy_part_time_dept.dept_code = zd_unit_code.code) " +
            "                                                     from zy_part_time_dept " +
            "                                                     where zy_part_time_dept.code = a.code " +
            "                                                     for xml path('')), 1, 1, ''), doctor_zy, doctor_yp_zl, dualchnl_flag,practice_certificate,isnull(auto_daily,'0') auto_daily " +
            "from a_employee_mi a  " +
            " ${ew.customSqlSegment} ")
    IPage<UserInfo> selectAllUsers(IPage<UserInfo> page,
                                   @Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select rtrim(code) as code, rtrim(code_rs) as codeRs, rtrim(name) as name, " +
            "rtrim(dept_code) as deptCode, deptName=(select rtrim(name) from zd_unit_code where code=dept_code), " +
            "title=(select rtrim(name) from zd_emp_title where code=emp_tit_code), " +
            "position=(select rtrim(name) from zd_emp_position where code=emp_po_code) " +
            "from a_employee_mi where dept_code=(select dept_code from a_employee_mi where code=#{code}) " +
            "and isnull(del_flag,0)!=1 ")
    List<UserInfo> getDeptUsersByUserCode(@Param("code") String code);

    @Select("select id,name,create_datetime,createStaff=(select rtrim(name) " +
            "from a_employee_mi where code=create_staff) from dj_dict_role")
    List<Role> getAllRoles();

    @Select("select id,name,create_datetime,createStaff=(select rtrim(name) " +
            "from a_employee_mi where code=create_staff) from dj_dict_role where id>2")
    List<Role> getAllRolesExceptAdmin();

    @Delete("delete from dj_user_role where user_code=#{code}")
    void deleteUserRoles(@Param("code") String code);

    @Insert("<script>" +
            "insert into dj_user_role (user_code, role_id) VALUES " +
            "<foreach collection='roles' item='role' separator=',' open='' close=''>" +
            "(#{code}, #{role.id})" +
            "</foreach>" +
            "</script>")
    void insertUserRoles(@Param("code") String code, @Param("roles") List<Role> roles);

    @Select("select count(1) from dj_dict_role where name=#{name}")
    Integer isRoleExist(@Param("name") String name);

    @Insert("insert into dj_dict_role (name,create_datetime,create_staff) values " +
            "(#{name},getdate(),#{createStaff})")
    void addNewRole(Role role);

    @Select("select * from dj_dict_dept_phones")
    List<DeptPhones> getDeptPhones();

    @Update("update dj_dict_dept_phones set in_phone=#{inPhone}, out_phone=#{outPhone} where id=#{id}")
    void saveDeptPhone(DeptPhones param);

    @Select("select * from t_work_integration_platform_advice order by status_flag")
    List<WorkIntegrationPlatformAdvice> selectAllAdvices();

    @Select("select * from t_work_integration_platform_advice where submit_staff=#{code} order by user_badge desc")
    List<WorkIntegrationPlatformAdvice> selectMyAdvices(@Param("code") String code);

    @Insert("insert into t_work_integration_platform_advice (submit_staff, submit_datetime, submit_content, status_flag) " +
            "values (#{staff},getdate(),#{content},0)")
    void insertNewAdvice(@Param("staff") String staff, @Param("content") String content);

    @Select("select user_code from dj_user_role where role_id=1")
    List<String> selectAdmins();

    @Update("update t_work_integration_platform_advice set status_flag=1 where id=#{id}")
    void checkAdvice(@Param("id") Integer id);

    @Update("update t_work_integration_platform_advice set user_badge=0 where id=#{id}")
    void dismissUserBadge(@Param("id") Integer id);

    @Update("update t_work_integration_platform_advice set reply=#{reply},reply_staff=#{staff}, " +
            "reply_datetime=getdate(), status_flag=2, user_badge=1 where id=#{id}")
    void updateReply(@Param("id") Integer id, @Param("reply") String reply, @Param("staff") String staff);

    @Select("select id from t_intergration_menu where type=2 and id " +
            "in (select menu_id from t_intergration_platform_role_menu where role_id=#{id})")
    List<Integer> getRoleVueMenus(@Param("id") Integer id);

    @Delete("delete from t_intergration_platform_role_menu where role_id=#{id}")
    void deleteRoleVueMenus(@Param("id") Integer id);

    @Insert("<script>" +
            "insert into t_intergration_platform_role_menu (role_id, menu_id) VALUES " +
            "<foreach collection='menus' item='menu' separator=',' open='' close=''>" +
            "(#{id}, #{menu})" +
            "</foreach>" +
            "</script>")
    void insertRoleVueMenus(@Param("id") Integer id, @Param("menus") List<Integer> menus);

    @Insert("insert into t_socket_message (id, level, sender, title, content) " +
            "values (#{id}, 1, '00000', '系统通知', #{content} )")
    void insertNewMessage(@Param("id") String id, @Param("content") String content);


    @Update("update a_employee_mi " +
            "set ${sql} where code = #{code}")
    void modifyPersonnelInformation(@Param("sql") String sql,
                                    @Param("code") String code);

    @Update("update a_employee_mi " +
            "set doctor_yp_zl = null where code = #{code}")
    void setDoctorYpZlNull(@Param("code") String code);

    @Select("select count(1) from a_employee_mi where code_rs = #{codeRs}")
    int doesThePersonnelNumberExist(String codeRs);

    @Insert("insert into a_employee_mi (code, name, yb_name, code_rs, emp_tit_code, emp_po_code, yb_code, " +
            "                           dept_code, mark, ifcadre, order_yn, ysjb, " +
            "                           gh_charge_type, login_flag, doctor_xz_yp, del_flag, " +
            "                           social_no, phone_no,  py_code, d_code,practice_certificate) " +
            "values (#{code}, #{name}, #{ybName}, #{codeRs}, #{empTitCode}, #{empPoCode}, #{ybCode}, #{deptCode}, #{mark}, #{ifcadre}, " +
            "        #{orderYn}, #{ysjb}, #{ghChargeType}, #{loginFlag}, #{doctorXzYp}, #{delFlag}, #{socialNo}, #{phoneNo}, " +
            "        #{pyCode}, #{dCode}, #{practiceCertificate})")
    void saveEmployeeInfo(UserInfo info);

    @Delete("delete zy_part_time_dept where code = #{code} ")
    void delPartTimeDeptByCode(String code);

    @Insert("<script>" +
            "insert into zy_part_time_dept (code, dept_code) values " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{code},#{item})" +
            "</foreach>" +
            "</script>")
    void insertPartTimeDept(@Param("code") String code,
                            @Param("list") List<String> list);

    //    @Update("update dj_user_base set password = '706cd5d552991d02d80356a30c9cf23d' , input_err_frequency = 0 ,next_input_times = #{nextDate} where code = #{code} ")
    @Update("update dj_user_base set password = '706cd5d552991d02d80356a30c9cf23d' where code = #{code} ")
    void resetPassword(String code);

    @Select(" select * from a_employee_image where code = #{code} ")
    Map<String, Object> selectAutographImageByCode(@Param("code") String code);

    @Update(" update a_employee_image set autograph_image = #{img} where code = #{code} ")
    int updateAutographImage(@Param("code") String code, @Param("img") String img);

    @Select("select autograph_image from  a_employee_image where code = #{code}  ")
    String getAutographImage(@Param("code") String code);

    @Insert(" insert into a_employee_image(code, image_comment, comment_type, image_base64, sex, autograph_image) " +
            " values (#{code}, null, '2', null, #{sex}, #{img}) ")
    int intoAutographImage(@Param("code") String code, @Param("img") String img, @Param("sex") String sex);

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

    @Update("update a_employee_mi set autograph_image = #{url} where  code = #{code} ")
    int putAutographImage(String code, String url);

    @Select("select top 1 code from a_employee_mi where code_rs = '${codeRs}'  ")
    String selectCodeByCodeRs(String codeRs);

    @Select("update  dj_user_base set user_config = #{config} where code = '${userCode}' ")
    void updateUserConfig(@Param("config") String config,
                          @Param("userCode") String userCode);


}
