package com.tofly.xrtygis.custom.service.call;

import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.pdf.PRAcroForm;
import com.tofly.common.core.util.Date8Util;
import com.tofly.common.core.util.DateUtil;
import com.tofly.common.core.util.StringUtil;
import com.tofly.xrtygis.custom.entity.enums.IVRReturnInfo;
import com.tofly.xrtygis.custom.entity.enums.IpAddress;
import com.tofly.xrtygis.custom.entity.gs.IVRContent;
import com.tofly.xrtygis.custom.entity.gs.IVRReturn;
import com.tofly.xrtygis.custom.entity.webserverbean.GetReadAndFees;
import com.tofly.xrtygis.custom.util.CallAPIUtil;
import com.tofly.xrtygis.custom.util.Date7Utils;
import com.tofly.xrtygis.custom.util.NumberParser;
import com.tofly.xrtygis.entity.Qfkhjbxx;
import com.tofly.xrtygis.mapper.RxsmsmessageMapper;
import com.tofly.xrtygis.mapper.RxyjfwMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 语音播报获取信息数据
 *
 * @author FlashWJL
 * @date 2021/7/13 19:58
 */
@Service
@AllArgsConstructor
public class IVRServiceImpl implements IVRService {

    private final RxsmsmessageMapper messageMapper;
    private final RxyjfwMapper rxyjfwMapper;


    @Override
    public IVRReturn getArrearageState(String yhbh) {
        //获取抄表欠费是获取上月的欠费数据
        //获取上月年月
        Date currentMonth = Date8Util.getCurrentDate();
        Date lastMonth = Date7Utils.addNextPeriod(-1, currentMonth);
        String lastMonthStr = DateUtil.parseDateToStr("yyyyMM", lastMonth);

        GetReadAndFees readAndFees = new GetReadAndFees();
        readAndFees.setYHBH(yhbh);
        readAndFees.setCBYF_START(lastMonthStr);
        readAndFees.setCBYF_END(lastMonthStr);
//        readAndFees.setCBYF_START("202107");
//        readAndFees.setCBYF_END("202109");
        readAndFees.setACTION("query");
        IVRReturn ivrReturn = new IVRReturn();
        try {
            Object info = CallAPIUtil.callAPIUtil(readAndFees, IpAddress.GetReadAndFees.getIpAddress()).get("data");
            List<IVRContent> ivrContentList = JSONObject.parseArray(JSONObject.toJSONString(info), IVRContent.class);
            //没有对应用户，直接返回
            if (StringUtil.isNull(ivrContentList) && ivrContentList.size() > 0){
                ivrReturn.setResultCode(IVRReturnInfo.IVR_RETURN_NO.getIVRCode());
                ivrReturn.setErrInfo(IVRReturnInfo.IVR_RETURN_NO.getIVRContent());
                return ivrReturn;
            }
            IVRContent ivrContent = ivrContentList.get(0);
            Integer isSf = ivrContent.getSfbz();
            ivrReturn.setResultCode(IVRReturnInfo.IVR_RETURN_SUCCESS.getIVRCode());
            ivrReturn.setErrInfo(IVRReturnInfo.IVR_RETURN_SUCCESS.getIVRContent());
            //由于森鑫炬没有给我们提供接口；没有预存金额值；所以目前只能设置为0元
            ivrReturn.setPresBalance(0.0f);
            //预存金额
            ivrReturn.setPresBalanceEndlish("ZERO");
            //0表示未缴费，欠费
            if (0==isSf) {
                Float ysje = ivrContent.getYsje();
                String waterValue = ivrContent.getCbsl();
                //转英语
                String ysjeEnglish = NumberParser.parse(ysje.toString());
                //保留小数点后两位
                ivrReturn.setGasArrears(ysje);
                ivrReturn.setGasArrearsEnglish(ysjeEnglish);
                ivrReturn.setWaterValue(waterValue);
                return ivrReturn;
            } else {
                return ivrReturn;
            }
        } catch (Exception e) {
            ivrReturn.setResultCode(IVRReturnInfo.IVR_RETURN_FAILED.getIVRCode());
            ivrReturn.setErrInfo(IVRReturnInfo.IVR_RETURN_FAILED.getIVRContent());
            return ivrReturn;
        }
    }

    @Override
    public String getStopWaterInfo(String mobile) {
        //查询当天用户是否有停水通知短信
        String currentDateStr = DateUtil.parseDateToStr("yyyyMMdd" , Date8Util.getCurrentDate());
        List<String> stopNoteInfoList = messageMapper.getStopWaterInfo(mobile , currentDateStr);
        if (stopNoteInfoList.size() > 0){
            //可能存在操作人员失误，导致停水通知短信一天之内发送了多次 ； 所以只取第一个就行了
            String stopWaterInfo = stopNoteInfoList.get(0);
            if (StringUtil.isNotEmpty(stopWaterInfo)){
                return stopWaterInfo;
            }
        }
        return "没有停水信息" ;
    }

    @Override
    public IVRReturn getStopWaterInfoTwo() {
        //查询当天用户是否有停水通知的应急记录
        String currentDateStr = DateUtil.parseDateToStr("yyyyMMdd" , Date8Util.getCurrentDate());
        List<String> stopWaterInform = rxyjfwMapper.getStopWater(currentDateStr);
        IVRReturn ivrReturn = new IVRReturn();
        if (stopWaterInform.size()>0){
            ivrReturn.setResultCode(IVRReturnInfo.IVR_RETURN_SUCCESS.getIVRCode());
            ivrReturn.setErrInfo(IVRReturnInfo.IVR_RETURN_SUCCESS.getIVRContent());
            ivrReturn.setContent(stopWaterInform);
            return ivrReturn;
        }else {
            ivrReturn.setResultCode(IVRReturnInfo.IVR_RETURN_SUCCESS.getIVRCode());
            ivrReturn.setErrInfo(IVRReturnInfo.IVR_RETURN_SUCCESS.getIVRContent());
            ivrReturn.setContent(null);
            return ivrReturn;
        }
    }


}
