package com.weiqi.mis.impl;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;

import javax.annotation.Resource;

import com.weiqi.mis.DateUtil;
import com.weiqi.mis.ReturnValue;
import com.weiqi.mis.domain.*;
import com.weiqi.mis.mapper.WxXrPemsOrderMapper;
import com.weiqi.mis.mapper.WxXrPemsOrderPushLogMapper;
import com.weiqi.vo.OrderInfoVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.weiqi.mis.*;

@Service
public class MsgServiceImpl implements MsgService {

    private Logger logger = LoggerFactory.getLogger(getClass());



    // 记录上次处理时间
    Map<String, Long> currTimeMap = new ConcurrentHashMap<>();

    @Autowired
    private WxXrPemsOrderMapper wxXrPemsOrderMapper;
    @Autowired
    private WxXrPemsOrderPushLogMapper wxXrPemsOrderPushLogMapper;

    @Resource(name = "threadPool")
    Executor executor;
    public Object getinfo(){

        return "";
    }








    /**
     * 查询所有appid
     *
     * @return
     */
    public ReturnValue getAppId() {


        return ReturnValue.buildSuccessdata(null, 1);

    }

    /**
     * 查询所有sp
     *
     * @return
     */
    public ReturnValue getsp() {

        return ReturnValue.buildSuccessdata(null, 1);

    }

    @Override
    public Object usableGetsp() {

        return ReturnValue.buildSuccessdata(null, 1);
    }



    @Override
    public Object getOrderinfo(OrderInfoVo records) {

        WxXrPemsOrderExample example1 = new WxXrPemsOrderExample();
        WxXrPemsOrderExample.Criteria cri1 = example1.createCriteria();


        int offset = 0;
        int pagesize = records.getLimit();
        int pageNumber = records.getPage();

        if (records.getStatus() != null && !records.getStatus().equals("null")
                && records.getStatus().toString().length() > 0) {
            cri1.andStatusEqualTo(records.getStatus());
        }

        if (records.getPlatenumber() != null && !records.getPlatenumber().equals("null")
                && records.getPlatenumber().toString().length() > 0) {
            cri1.andPlatenumberLike("%"+records.getPlatenumber()+"%");
        }
        offset = (pageNumber - 1) * pagesize;
        example1.setOrderByClause(" create_time desc   limit " + offset + " ," + pagesize);

        if (records.getEndTime().length() > 0 && records.getBeginTime().length() > 0) {
            cri1.andCreateTimeBetween(DateUtil.string2Date(records.getBeginTime(), "yyyy-MM-dd HH:mm:ss"),
                    DateUtil.string2Date(records.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
        } else {
            cri1.andCreateTimeBetween(DateUtil.getDayBegin(), DateUtil.getDayEnd());

        }
        int num = wxXrPemsOrderMapper.countByExample(example1);
        List<WxXrPemsOrder> wxXrPemsOrders = wxXrPemsOrderMapper.selectByExample(example1);

        return  ReturnValue.buildSuccessdata(wxXrPemsOrders, num);
    }
    @Override
    public Object getOrderinfoLog(OrderInfoVo records) {

        WxXrPemsOrderPushLogExample example1 = new WxXrPemsOrderPushLogExample();
        WxXrPemsOrderPushLogExample.Criteria cri1 = example1.createCriteria();


        int offset = 0;
        int pagesize = records.getLimit();
        int pageNumber = records.getPage();

        if (records.getStatus() != null && !records.getStatus().equals("null")
                && records.getStatus().toString().length() > 0) {
            cri1.andStatusEqualTo(records.getStatus());
        }


        offset = (pageNumber - 1) * pagesize;
        example1.setOrderByClause(" create_time desc   limit " + offset + " ," + pagesize);

        if (records.getEndTime().length() > 0 && records.getBeginTime().length() > 0) {
            cri1.andCreateTimeBetween(DateUtil.string2Date(records.getBeginTime(), "yyyy-MM-dd HH:mm:ss"),
                    DateUtil.string2Date(records.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
        } else {
            cri1.andCreateTimeBetween(DateUtil.getDayBegin(), DateUtil.getDayEnd());

        }

        int num = wxXrPemsOrderPushLogMapper.countByExample(example1);
        List<WxXrPemsOrderPushLog> wxXrPemsOrders = wxXrPemsOrderPushLogMapper.selectByExample(example1);

        return  ReturnValue.buildSuccessdata(wxXrPemsOrders, num);
    }

}
