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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.config.WaterFeign;
import com.tofly.xrtygis.custom.entity.enums.IVRReturnInfo;
import com.tofly.xrtygis.custom.entity.gs.IVRReturn;
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 com.tofly.xrtygis.service.QfkhjbxxService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

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

    private final RxsmsmessageMapper messageMapper;
    private final RxyjfwMapper rxyjfwMapper;
    private final QfkhjbxxService qfkhjbxxService;
    private final WaterFeign waterFeign;


    @Override
    public IVRReturn getArrearageState(String yhbh) {
        //获取抄表欠费是获取上月的欠费数据
        //获取上月年月
//        Date currentMonth = Date8Util.getCurrentDate();
//        Date lastMonth = Date7Utils.addNextPeriod(-1, currentMonth);
//        String lastMonthStr = DateUtil.parseDateToStr("yyyyMM", lastMonth);
        IVRReturn ivrReturn = new IVRReturn();
        try {
            //查询客户档案信息
            Qfkhjbxx qfkhjbxx = qfkhjbxxService.getOne(Wrappers.<Qfkhjbxx>lambdaQuery().eq(Qfkhjbxx::getYhbh, yhbh));
            if (StringUtil.isNull(qfkhjbxx)) {
                ivrReturn.setResultCode(IVRReturnInfo.IVR_RETURN_NO.getIVRCode());
                ivrReturn.setErrInfo(IVRReturnInfo.IVR_RETURN_NO.getIVRContent());
                return ivrReturn;
            }
            //查询数据成功
            ivrReturn.setResultCode(IVRReturnInfo.IVR_RETURN_SUCCESS.getIVRCode());
            ivrReturn.setErrInfo(IVRReturnInfo.IVR_RETURN_SUCCESS.getIVRContent());
            if (StringUtil.isNotNull(qfkhjbxx.getZhye())) {
//                Float aaa = qfkhjbxx.getZhye().floatValue();
//                log.info("预存金额："+aaa);
                //预存金额
                ivrReturn.setPresBalance(qfkhjbxx.getZhye().floatValue());
//                String zhye = qfkhjbxx.getZhye().toString();
                //预存金额英语
                ivrReturn.setPresBalanceEndlish(NumberParser.parse(qfkhjbxx.getZhye().toString()));
            }
            ivrReturn.setIsArrearage(qfkhjbxx.getSfqf());
            //欠费金额
            if (StringUtil.isNotNull(qfkhjbxx.getQfje())) {
                ivrReturn.setGasArrears(qfkhjbxx.getQfje().floatValue());
                ivrReturn.setGasArrearsEnglish(NumberParser.parse(qfkhjbxx.getQfje().toString()));
                //先查询本月是否有抄表并且已经同步的数据
                String cbsl = waterFeign.getNewCbsl(yhbh);
                ivrReturn.setWaterValue(cbsl);
            } else {
                ivrReturn.setGasArrears(0.0f);
                ivrReturn.setGasArrearsEnglish("ZERO");
                ivrReturn.setWaterValue("0");
            }
            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;
        }
    }


}
