package io.renren.modules.admin.dao;

import io.renren.modules.admin.entity.AttendersEntity;

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

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-21 15:10:27
 */
@Mapper
public interface AttendersDao extends BaseMapper<AttendersEntity> {
    @Select("SELECT a.id, m.truename as name, m.organization, m.phone, a.intention, a.room, a.transfer, a.note, a.modify_time, a.servicer, e.truename as servicerName, a.type_id as typeId, (select t.name from types_of_attenders t WHERE t.id=a.type_id) as typeName, (SELECT p.is_pay FROM  payment p WHERE p.attenders_id=a.id) as  isPay  FROM attenders a JOIN member m ON a.member_id=m.id LEFT JOIN employee e ON e.id=a.servicer  WHERE a.is_del=0 AND a.meeting_id=#{meetingId} AND (m.truename Like '%${truename}%' OR m.organization LIKE '%${organization}%') limit #{page},#{limit};")
    List<Map<String, Object>> getMapList(Long meetingId, String truename, String organization, int page, int limit);

    @Select("SELECT count(*) FROM attenders a JOIN member m ON a.member_id=m.id LEFT JOIN employee e ON e.id=a.servicer  WHERE a.is_del=0 AND a.meeting_id=#{meetingId} AND (m.truename Like '%${truename}%' OR m.organization LIKE '%${organization}%');")
    int getCounts(Long meetingId, String truename, String organization);

    @Select("SELECT a.id, m.truename, m.organization, m.position, m.job_title as jobTitle, m.phone, m.email, (select t.name from types_of_attenders t where t.id=a.type_id) as typeName, (SELECT p.is_ispay FROM  payment p WHERE p.attenders.id=a.id) as isPay  FROM attenders a JOIN member m ON a.member_id=m.id WHERE a.is_del=0 AND a.meeting_id=#{meetingId}")
    List<Map<String, Object>> getMapList(Long meetingId);

    @Select("SELECT a.id as aid,a.meeting_id,m.name_cn FROM attenders a JOIN meeting m ON a.meeting_id=m.id WHERE a.member_id=#{memberId} ORDER BY a.id;")
    List<Map<String, Object>> getMeetingListByMemberId(Long memberId);

    @Select("SELECT a.id,m.truename as name FROM attenders a JOIN member m ON a.member_id=m.id where a.is_del=0 AND a.id=#{aid}")
    Map<String, Object> getNameById(Long aid);

    @Select("SELECT a.id,m.truename,m.organization,m.position,m.job_title as jobTitle,m.phone,m.email,m.officephone,a.type_id typeId,(select t.name from types_of_attenders t where t.id=a.type_id) as typeName FROM attenders a JOIN member m ON a.member_id=m.id where a.is_del=0 AND a.id=#{aid}")
    Map<String, Object> getMapById(Long aid);

    @Update("UPDATE attenders set `${column}`=#{value} WHERE id=#{id}")
    int changeColumnValue(String column, Integer value, Long id);
}
