package io.renren.modules.admin.dao;

import io.renren.modules.admin.entity.PaymentEntity;
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;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-14 14:51:30
 */
@Mapper
public interface PaymentDao extends BaseMapper<PaymentEntity> {
    @Select("select p.id, p.order_id orderId, mem.truename, mem.organization, p.pay_type payType, mem.phone, mem.email, p.pay_time payTime,p.is_pay isPay from attenders join payment p on attenders.id=p.attenders_id join member mem on mem.id=attenders.member_id where p.meeting_id=#{meetingId} and attenders.is_del=0 and p.is_del=0 and (mem.truename like '%${name}%' or p.order_id like '%${order}%') limit #{page},#{limit};")
    List<Map<String, Object>> myQueryPage(Long meetingId, String name, String order, int page, int limit);

    @Select("select count(*) from attenders join payment p on attenders.id=p.attenders_id join member mem on mem.id=attenders.member_id where p.meeting_id=#{meetingId} and attenders.is_del=0 and p.is_del=0 and (mem.truename like '%${name}%' or p.order_id like '%${order}%');")
    int myQueryPageCounts(Long meetingId, String name, String order);

    @Select("select p.id, p.order_id orderId, mem.truename, mem.organization, p.pay_type payType, mem.phone, mem.email, p.pay_time payTime,p.is_pay isPay from attenders join payment p on attenders.id=p.attenders_id join member mem on mem.id=attenders.member_id where p.meeting_id=#{meetingId} and attenders.is_del=0 and p.is_del=0 and (mem.truename like '%${name}%' or p.order_id like '%${order}%');")
    List<Map<String, Object>> getMapList(Long meetingId, String name, String order);

    @Select("select count(*) from attenders join payment p on attenders.id=p.attenders_id join member mem on mem.id=attenders.member_id where p.meeting_id=#{meetingId} and attenders.is_del=0 and p.is_del=0;")
    int getTotal(Long meetingId);

    @Select("select sum(p.money) as totalMoney from attenders join payment p on attenders.id=p.attenders_id  where p.meeting_id=#{meetingId} and attenders.is_del=0 and p.is_del=0;")
    BigDecimal getTotalMoney(Long meetingId);

    @Select("select sum(p.money) as totalMoney from attenders join payment p on attenders.id=p.attenders_id  where p.meeting_id=#{meetingId} and attenders.is_del=0 and p.is_del=0 ${where};")
    BigDecimal getTotalMoney(Long meetingId, String where);

    @Update("UPDATE payment SET `is_pay`=#{status} where id=#{id}")
    int updatePayStatus(Long id, Integer status);
}
