package io.renren.modules.admin.service.impl;

import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.admin.dao.PaymentDao;
import io.renren.modules.admin.entity.PaymentEntity;
import io.renren.modules.admin.service.PaymentService;


@Service("paymentService")
public class PaymentServiceImpl extends ServiceImpl<PaymentDao, PaymentEntity> implements PaymentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PaymentEntity> page = this.page(
                new Query<PaymentEntity>().getPage(params),
                new QueryWrapper<PaymentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils myQueryPage(Map<String, Object> params) {
        Long meetingId = Long.valueOf(params.get("meetingId").toString());
        String key = (String) params.get("key");

        int page = params.containsKey("page") ? Integer.valueOf((String) params.get("page")) - 1 : 0;
        if (page < 0) {
            page = 0;
        }
        int limit = params.containsKey("limit") ? Integer.valueOf((String) params.get("limit")) : 10;
        List<Map<String, Object>> list = baseMapper.myQueryPage(meetingId, key, key, page, limit); 
        int total = baseMapper.myQueryPageCounts(meetingId, key, key);

        return new PageUtils(list, total, limit, page);
    }

    @Override
    public void savePayment(PaymentEntity payment) {
        payment.setCreateTime(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String nowtime = sdf.format(new Date());
        // System.out.println(time);
        Integer fourRandom = Integer.valueOf((int)((Math.random()*9+1)*1000));
        payment.setOrderId("FY" + nowtime + fourRandom);
        this.save(payment);
    }

    @Override
    public List<Map<String, Object>> getMapList(Map<String, Object> params) {
        Long meetingId = Long.valueOf(params.get("meetingId").toString());
        String key = (String) params.get("key");
        return baseMapper.getMapList(meetingId, key, key);
    }

    @Override
    public int getTotal(Long meetingId) {
        return baseMapper.getTotal(meetingId);
    }

    @Override
    public BigDecimal getTotalMoney(Long meetingId, String where) {
        return baseMapper.getTotalMoney(meetingId, where);
    }

    @Override
    public int updatePayStatus(Long id, Integer status) {
       return baseMapper.updatePayStatus(id, status);
    }
}