package com.mortals.xhx.modules.supplement.service.impl;

import com.alibaba.excel.EasyExcel;
import com.mortals.xhx.common.pdu.ApiRespPdu;
import com.mortals.xhx.modules.arameter.model.ArameterEntity;
import com.mortals.xhx.modules.arameter.service.ArameterService;
import com.mortals.xhx.modules.implementlist.service.ImplementlistService;
import com.mortals.xhx.modules.information.dao.InformationDao;
import com.mortals.xhx.modules.information.model.InformationQuery;
import com.mortals.xhx.modules.supplement.dao.SupplementDao;
import com.mortals.xhx.modules.supplement.listener.InformationDataConversionListener;
import com.mortals.xhx.modules.supplement.model.SupplementEntity;
import com.mortals.xhx.modules.supplement.service.SupplementService;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;


/**
 * <p>Title: 办件补录表</p>
 * <p>Description: SupplementService service接口 </p>
 * <p>Copyright: Copyright &reg;  </p>
 *
 * @version 1.0.0
 */
@Service("supplementService")
public class SupplementServiceImpl extends AbstractCRUDServiceImpl<SupplementDao, SupplementEntity, Long> implements SupplementService {

    @Autowired
    private SupplementDao supplementDao;

    @Autowired
    private ImplementlistService implementlistService;

    @Autowired
    private InformationDao informationDao;

    @Autowired
    private ArameterService arameterService;

    @SneakyThrows
    @Override
    public ApiRespPdu importOfficeInformation(BufferedInputStream file, String sysCode) {
//        //1.导入之前，查询当前系统编码的阈值，以及多少办件量,并判断今天能否再次推送
        ArameterEntity arameterEntity = new ArameterEntity();
        arameterEntity.setSysCode(sysCode);
        List<ArameterEntity> arameterEntities = arameterService.find(arameterEntity, null);
        //地区编码
        String areaCode = arameterEntities.get(0).getAreaCode();
//        String areaCode = "513237000000";
        //当前系统每天的固定办件量
//        Integer limitTheNumberOfDocuments = Integer.valueOf(arameterEntities.get(0).getThresholdValue());
        Integer limitTheNumberOfDocuments = Integer.valueOf("5000000");
        InformationQuery informationQuery = new InformationQuery();
        //设置查询条件，查询当天的办件量
        informationQuery.setSystemCode(sysCode);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //当前时间毫秒数
        long current=System.currentTimeMillis();
        //今天零点零分零秒的毫秒数
        long zero=current/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset();
        //今天23点59分59秒的毫秒数
        long twelve=zero+24*60*60*1000-1;
        informationQuery.setQueryBeginTime(simpleDateFormat.format(new Date(zero)));
        informationQuery.setQueryEndTime(simpleDateFormat.format(new Date(twelve)));
        //当前系统（当天）已推送的办件量
        int count = informationDao.getCount(informationQuery);
        ApiRespPdu<Object> objectApiRespPdu = new ApiRespPdu<>();
        if (count > limitTheNumberOfDocuments) {
            objectApiRespPdu.setCode(500);
            objectApiRespPdu.setMsg("数据上传失败，失败原因: 今日推送量已超过: "+limitTheNumberOfDocuments+" ，请明天再试");
            return objectApiRespPdu;
        } else {
            try {
                InformationDataConversionListener informationDataConversionListener = new InformationDataConversionListener(supplementDao,implementlistService);
                informationDataConversionListener.theAmountOfDeliverablesThatCanBePushed(limitTheNumberOfDocuments,count);
                informationDataConversionListener.currentSystemCode(sysCode,areaCode);
                EasyExcel.read(file, SupplementEntity.class, informationDataConversionListener)
                        .sheet(0)
                        .headRowNumber(3)
                        .doRead();
                objectApiRespPdu.setCode(200);
                objectApiRespPdu.setMsg("数据上传成功");
                return objectApiRespPdu;
            } catch (Exception e) {
                e.printStackTrace();
                objectApiRespPdu.setCode(500);
                objectApiRespPdu.setMsg("数据上传失败，失败原因:" + e.getMessage()+"目前剩余容量:"+String.valueOf(limitTheNumberOfDocuments-count));
                return objectApiRespPdu;
            }
        }
    }


}