package com.jkcredit.invoice.credit.interserver;

import cn.com.taiji.sdk.model.comm.protocol.tts.trade.service.CardTradeModel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jkcredit.invoice.common.DataResult;
import com.jkcredit.invoice.model.entity.customer.CustomerCarRec;
import com.jkcredit.invoice.model.entity.customer.CustomerRec;
import com.jkcredit.invoice.model.vo.CardChangeQueryVo;
import com.jkcredit.invoice.model.vo.CardChangeVo;
import com.jkcredit.invoice.service.customer.CustomerRecService;
import com.jkcredit.invoice.service.lowerservice.SelfCarCustService;
import com.jkcredit.invoice.service.lowerservice.vo.*;
import com.jkcredit.invoice.util.DateUtil;
import com.jkcredit.invoice.util.RespR;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.jkcredit.invoice.common.CommonConst.NOT_FOUND;
import static com.jkcredit.invoice.common.CommonConst.TWO_NUM;


/**
 * 自有车上游请求接口实现类
 * @author mumuxigua
 */
@Service("selfCarInterService")
@Slf4j
public class SelfCarInterServiceImpl implements SelfCarInterService {

    @Autowired
    SelfCarCustService selfCarService;

    @Autowired
    CustomerRecService customerRecMapper;

    /**
     * 自有车 用户卡列表查询接口
     *
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult customeretcquery(String appKey, String api, String data, String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.customerETCQuery-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            //企业编号 必输
            String companyNum = jsonObject.getString("companyNum");
            //卡号  非必输
            String cardId = jsonObject.getString("cardId");
            //车牌号 非必输
            String plateNum = jsonObject.getString("plateNum");
            //车牌颜色 非必输
            Integer plateColor = jsonObject.getInteger("plateColor");
            if (StringUtils.isEmpty(data) || null == jsonObject || StringUtils.isEmpty(companyNum)) {
                result.setData(3);
                result.setMsg("请输入必输项!");
                return result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(companyNum);
            customerRec.setInterType(0);
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            if (null == customerRec1) {
                result.setCode(200);
                result.setMsg("该企业编号在平台中不存在，请联系管理员核实!");
                return result;
            }

            EtcQueryVo etcQueryVo = new EtcQueryVo();
            //客户名称
            etcQueryVo.setCustomerName(appKey);
            //企业名称
            etcQueryVo.setCompanyName(customerRec1.getCompanyName());
            //车牌号 非必输
            etcQueryVo.setPlateNum(plateNum);
            //卡号 非必输
            etcQueryVo.setCardId(cardId);
            //车牌颜色 非必输
            etcQueryVo.setPlateColor(plateColor);
            RespR rs = selfCarService.getEtcInfo(etcQueryVo);
            long costtimeend = System.currentTimeMillis();
            log.info("[-SelfCarInterServiceImpl.customerETCQuery-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if (null != rs && rs.getCode() == 0) {
                result.setData(1);
                result.setCode(200);

                result.setMsg(rs.getData().toString());
                return result;
            } else {
                if (rs != null && rs.getCode() == TWO_NUM) {
                    result.setData(2);
                    result.setCode(200);
                    result.setMsg("未查得");
                    return result;
                }
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.customerETCQuery-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }


    /**
     * 自有车 卡信息查询接口
     *
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult customerQueryEtcInfo(String appKey, String api, String data, String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.customerQueryEtcInfo-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            //企业编号 必输
            String companyNum = jsonObject.getString("companyNum");
            //车牌的json字符串 必输
            JSONArray vehicleList = jsonObject.getJSONArray("vehicleList");
            if (StringUtils.isEmpty(data) || null == jsonObject || StringUtils.isEmpty(companyNum)
                    || null == vehicleList
            ) {
                result.setCode(200);
                result.setMsg("必传参数有为空情况，请核实后再试");
                return result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(companyNum);
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);

            if (null == customerRec1) {
                result.setMsg("该企业编号在平台中不存在，请联系管理员核实!");
                return result;
            }

            EtcBindVo etcQueryVo = new EtcBindVo();
            //客户名称
            etcQueryVo.setCustomerName(appKey);
            //企业名称
            etcQueryVo.setCompanyName(customerRec1.getCompanyName());

            List<CarVo> cards = new ArrayList<CarVo>();
            for (int i = 0; i < vehicleList.size(); i++) {
                JSONObject jb = vehicleList.getJSONObject(i);
                //车牌号
                String plateNum = jb.getString("plateNum");
                //车牌颜色
                Integer plateColor = jb.getInteger("plateColor");
                if (null == jb || StringUtils.isEmpty(plateNum)
                        || null == plateColor
                ) {
                    result.setMsg("参数格式错误");
                    return result;
                }

                CarVo carVo = new CarVo();
                carVo.setNum(plateNum);
                carVo.setColor(plateColor);
                cards.add(carVo);
            }
            etcQueryVo.setCards(cards);
            return getDataResult(data, costtimestart, result, etcQueryVo);
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.customerQueryEtcInfo-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }

    private DataResult getDataResult(String data, long costtimestart, DataResult result, EtcBindVo etcQueryVo) {
        RespR rs = selfCarService.queryEtcInfo(etcQueryVo);

        long costtimeend = System.currentTimeMillis();
        log.info("[-SelfCarInterServiceImpl.customerQueryEtcInfo-] result is "
                + rs.toString() + ", request is " + data + " ,costtime="
                + (costtimeend - costtimestart));
        if (null != rs && rs.getCode() == 0) {
            result.setData(1);
            result.setCode(200);
            result.setMsg(rs.getData().toString());
            return result;
        } else {
            result.setData(3);
            result.setCode(200);
            result.setMsg(rs.getMsg());
            if (NOT_FOUND.equals(rs.getMsg())) {
                result.setData(2);
            }
            return result;
        }
    }


    /**
     * 自有车下发短信通知接口
     *
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult customeretcrec(String appKey, String api, String data, String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.customerETCRec-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            //企业编号 必输
            String companyNum = jsonObject.getString("companyNum");
            //企业预留手机号 必输
            String mobile = jsonObject.getString("mobile");
            //ETC卡编号列表 必输
            JSONArray cardIdList = jsonObject.getJSONArray("cardIdList");
            if (StringUtils.isEmpty(data) || null == jsonObject || StringUtils.isEmpty(companyNum)
                    || StringUtils.isEmpty(mobile)
                    || null == cardIdList

            ) {
                result.setCode(200);
                result.setMsg("必传参数有为空情况，请核实后再试");
                return result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(companyNum);
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            if (null == customerRec1) {
                result.setCode(200);
                result.setMsg("该企业编号在平台中不存在，请联系管理员核实!");
                return result;
            }
            EtcBindVo etcQueryVo = new EtcBindVo();
            //客户名称
            etcQueryVo.setCustomerName(appKey);
            //企业名称
            etcQueryVo.setCompanyName(customerRec1.getCompanyName());
            //企业预留手机号
            etcQueryVo.setMobile(mobile);
            List<CarVo> cards = new ArrayList<CarVo>();
            for (int i = 0; i < cardIdList.size(); i++) {
                JSONObject jb = cardIdList.getJSONObject(i);
                //用户卡Id
                String cardId = jb.getString("cardId");

                if (null == jb || StringUtils.isEmpty(cardId)
                ) {
                    result.setCode(200);
                    result.setMsg("用户卡为空!");
                    return result;
                }


                CarVo carVo = new CarVo();
                carVo.setEtcNum(cardId);
                cards.add(carVo);
            }
            etcQueryVo.setCards(cards);
            RespR rs = selfCarService.customerEtcRec(etcQueryVo);
            long costtimeend = System.currentTimeMillis();
            log.info("[-SelfCarInterServiceImpl.customerETCRec-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if (null != rs && rs.getCode() == 0) {
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.customerETCRec-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }


    /**
     * 自有车 卡绑定接口 渠道调用此接口，上传用户收到的短信验证码
     *
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult customeretcrecvalid(String appKey, String api, String data, String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.customerETCRecValid-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            //企业编号 必输
            String companyNum = jsonObject.getString("companyNum");
            //企业预留手机号 必输
            String mobile = jsonObject.getString("mobile");
            //验证码 必输
            String validCode = jsonObject.getString("validCode");
            if (StringUtils.isEmpty(data) || null == jsonObject || StringUtils.isEmpty(companyNum)
                    || StringUtils.isEmpty(mobile)
                    || StringUtils.isEmpty(validCode)

            ) {
                result.setCode(200);
                result.setMsg("必传参数有为空情况，请核实后再试");
                return result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(companyNum);
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            if (null == customerRec1) {
                result.setCode(200);
                result.setMsg("该企业编号在平台中不存在，请联系管理员核实!");
                return result;
            }
            EtcValidVo etcValidVo = new EtcValidVo();
            etcValidVo.setCustomerName(appKey);
            etcValidVo.setCompanyName(customerRec1.getCompanyName());
            etcValidVo.setValidCode(validCode);
            etcValidVo.setMobile(mobile);
            //接口
            etcValidVo.setInterType(0);

            RespR rs = selfCarService.customerEtcRecValid(etcValidVo);
            long costtimeend = System.currentTimeMillis();
            log.info("[-SelfCarInterServiceImpl.customerETCRecValid-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if (null != rs && rs.getCode() == 0) {
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.customerETCRecValid-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }


    /**
     * 自有车 交易查询接口 渠道通过此接口可以查询单张卡连续90天内的交易（待开票、开票中、已开票）
     *
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult getTradeList(String appKey, String api, String data, String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.getTradeList-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            //企业编号 必输
            String companyNum = jsonObject.getString("companyNum");
            //卡号 必输
            String cardId = jsonObject.getString("cardId");
            //交易状态 必输
            Integer tradeStatus = jsonObject.getInteger("tradeStatus");
            //开始时间 必输
            String startExTime = jsonObject.getString("startExTime");
            //结束时间 必输
            String endExTime = jsonObject.getString("endExTime");
            if (StringUtils.isEmpty(data) || null == jsonObject || StringUtils.isEmpty(companyNum)
                    || StringUtils.isEmpty(cardId)
                    || null == tradeStatus
                    || StringUtils.isEmpty(startExTime)
                    || StringUtils.isEmpty(endExTime)

            ) {
                result.setCode(200);
                result.setMsg("必传参数有为空情况，请核实后再试");
                return result;
            }

            if (!DateUtil.isValidDate(startExTime)) {
                result.setMsg("日期格式有误");
                return result;
            }

            if (!DateUtil.isValidDate(endExTime)) {
                result.setMsg("日期格式有误");
                return result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(companyNum);
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            if (null == customerRec1) {
                result.setCode(200);
                result.setMsg("该企业编号在平台中不存在，请联系管理员核实!");
                return result;
            }
            TradeRequestVo tradeRequestVo = new TradeRequestVo();
            tradeRequestVo.setCustomerName(appKey);
            tradeRequestVo.setCompanyName(customerRec1.getCompanyName());
            tradeRequestVo.setStartTime(DateUtil.getDistanceHoursFormat(startExTime));
            tradeRequestVo.setEndTime(DateUtil.getDistanceHoursFormat(endExTime));
            tradeRequestVo.setEtcId(cardId);
            tradeRequestVo.setTradeStatus(tradeStatus);
            //0 接口
            tradeRequestVo.setInterType(0);

            if (tradeRequestVo.getTradeStatus() == null) {
                return getDataResult(result, tradeRequestVo);
            }
            return getDataResult(data, costtimestart, result, tradeRequestVo);
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.getTradeList-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }

    private DataResult getDataResult(DataResult result, TradeRequestVo tradeRequestVo) {
        tradeRequestVo.setTradeStatus(1);
        RespR<List<CardTradeModel>> respRbefore = selfCarService.getTradeList(tradeRequestVo);
        tradeRequestVo.setTradeStatus(2);
        RespR<List<CardTradeModel>> listRespR = selfCarService.getTradeList(tradeRequestVo);
        tradeRequestVo.setTradeStatus(3);
        RespR<List<CardTradeModel>> tradeList = selfCarService.getTradeList(tradeRequestVo);
        //合并展示
        List<CardTradeModel> cardTradeModels = new ArrayList<>();
        if (respRbefore.getCode() == 0) {
            cardTradeModels.addAll(respRbefore.getData());
        }
        if (listRespR.getCode() == 0) {
            cardTradeModels.addAll(listRespR.getData());
        }
        if (tradeList.getCode() == 0) {
            cardTradeModels.addAll(tradeList.getData());
        }

        result.setData(1);
        result.setCode(200);
        result.setMsg(cardTradeModels.toString());
        return result;
    }

    private DataResult getDataResult(String data, long costtimestart, DataResult result, TradeRequestVo tradeRequestVo) {
        RespR rs = selfCarService.getTradeList(tradeRequestVo);
        long costtimeend = System.currentTimeMillis();
        log.info("[-SelfCarInterServiceImpl.getTradeList-] result is "
                + rs.toString() + ", request is " + data + " ,costtime="
                + (costtimeend - costtimestart));
        if (null != rs && rs.getCode() == 0) {
            result.setData(1);
            result.setCode(200);
            result.setMsg(rs.getData().toString());
            return result;
        } else {
            result.setData(3);
            if (NOT_FOUND.equals(rs.getMsg())) {
                result.setData(2);
            }
            result.setCode(200);
            result.setMsg(rs.getMsg());
            return result;
        }
    }


    /**
     * 自有车 交易查询接口 渠道通过此接口可以查询单张卡连续90天内的交易（待开票、开票中、已开票）
     *
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult applInvoice(String appKey, String api, String data, String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.applInvoice-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            //企业编号 必输
            String companyNum = jsonObject.getString("companyNum");
            //卡号 必输
            String cardId = jsonObject.getString("cardId");
            //交易ID集合 必输
            JSONArray tradeIdModel = jsonObject.getJSONArray("tradeIdModel");

            if (StringUtils.isEmpty(data) || null == jsonObject || StringUtils.isEmpty(companyNum)
                    || StringUtils.isEmpty(cardId)
                    || null == tradeIdModel
            ) {
                result.setCode(200);
                result.setMsg("必传参数有为空情况，请核实后再试");
                return result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(companyNum);
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            if (null == customerRec1) {
                result.setCode(200);
                result.setMsg("该企业编号在平台中不存在，请联系管理员核实!");
                return result;
            }
            InvoiceApplVo invoiceApplVo = new InvoiceApplVo();
            invoiceApplVo.setCustomerName(appKey);
            invoiceApplVo.setCompanyName(customerRec1.getCompanyName());
            invoiceApplVo.setCardId(cardId);
            //接口
            invoiceApplVo.setInterType(0);


            List<String> tradeIds = new ArrayList<String>();
            for (int i = 0; i < tradeIdModel.size(); i++) {
                JSONObject jb = tradeIdModel.getJSONObject(i);
                //交易ID
                String tradeId = jb.getString("tradeId");
                if (null == jb || StringUtils.isEmpty(tradeId)
                ) {
                    return result;
                }
                tradeIds.add(tradeId);
            }

            invoiceApplVo.setTradeIds(tradeIds);
            RespR rs = selfCarService.applInvoice(invoiceApplVo);
            long costtimeend = System.currentTimeMillis();
            log.info("[-SelfCarInterServiceImpl.applInvoice-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if (null != rs && rs.getCode() == 0) {
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.applInvoice-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }


    @Override
    public DataResult getSelfCarInvoicesByAppl(String appKey, String api, String data, String requestid) {
        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.getSelfCarInvoicesByAppl-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            //企业编号 必输
            String companyNum = jsonObject.getString("companyNum");
            //卡号 必输
            String cardId = jsonObject.getString("cardId");
            //申请id 必输
            String applyId = jsonObject.getString("applyId");
            if (StringUtils.isEmpty(data) || null == jsonObject || StringUtils.isEmpty(companyNum)
                    || StringUtils.isEmpty(cardId)
                    || StringUtils.isEmpty(applyId)
            ) {
                result.setCode(200);
                result.setMsg("必传参数有为空情况，请核实后再试");
                return result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(companyNum);
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            if (null == customerRec1) {
                result.setCode(200);
                result.setMsg("该企业编号在平台中不存在，请联系管理员核实!");
                return result;
            }
            ApplQueryInvVo applQueryInvVo = new ApplQueryInvVo();
            applQueryInvVo.setApplId(applyId);
            applQueryInvVo.setEtcNum(cardId);
            applQueryInvVo.setCompanyNum(companyNum);
            RespR rs = selfCarService.getSelfCarInvoicesByApplyIds(applQueryInvVo);
            long costtimeend = System.currentTimeMillis();
            log.info("[-SelfCarInterServiceImpl.getSelfCarInvoicesByAppl-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if (null != rs && rs.getCode() == 0) {
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            } else {
                result.setData(3);
                if (NOT_FOUND.equals(rs.getMsg())) {
                    result.setData(2);
                }
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.getSelfCarInvoicesByAppl-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }

    /**
     * 自有车 已开发票查询接口 渠道通过此接口可以根据该公司绑定的单张卡查询此卡在某个月开具的发票
     *
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult getSelfCarInvoicesByTime(String appKey, String api, String data, String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.getSelfCarInvoicesByTime-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            //企业编号 必输
            String companyNum = jsonObject.getString("companyNum");
            //卡号 必输
            String cardId = jsonObject.getString("cardId");
            //开始时间 必输
            String startInvoiceMakeTime = jsonObject.getString("startInvoiceMakeTime");
            //结束时间 必输
            String endInvoiceMakeTime = jsonObject.getString("endInvoiceMakeTime");
            if (StringUtils.isEmpty(data) || null == jsonObject || StringUtils.isEmpty(companyNum)
                    || StringUtils.isEmpty(cardId)
                    || StringUtils.isEmpty(startInvoiceMakeTime)
                    || StringUtils.isEmpty(endInvoiceMakeTime)

            ) {
                result.setCode(200);
                result.setMsg("必传参数有为空情况，请核实后再试");
                return result;
            }

            if (!DateUtil.isValidDate(startInvoiceMakeTime)) {
                result.setMsg("日期格式有误");
                return result;
            }

            if (!DateUtil.isValidDate(endInvoiceMakeTime)) {
                result.setMsg("日期格式有误");
                return result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(companyNum);
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            if (null == customerRec1) {
                result.setCode(200);
                result.setMsg("该企业编号在平台中不存在，请联系管理员核实!");
                return result;
            }
            SelfCarDueQueryVo selfCarDueQueryVo = new SelfCarDueQueryVo();
            selfCarDueQueryVo.setCustomername(appKey);
            selfCarDueQueryVo.setCompanyName(customerRec1.getCompanyName());
            selfCarDueQueryVo.setCardId(cardId);
            selfCarDueQueryVo.setStartTime(startInvoiceMakeTime);
            selfCarDueQueryVo.setEndTime(endInvoiceMakeTime);

            RespR rs = selfCarService.getSelfCarInvoicesByTime(selfCarDueQueryVo, customerRec1);
            long costtimeend = System.currentTimeMillis();
            log.info("[-SelfCarInterServiceImpl.getSelfCarInvoicesByTime-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if (null != rs && rs.getCode() == 0) {
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            } else {
                result.setData(3);
                if (NOT_FOUND.equals(rs.getMsg())) {
                    result.setData(2);
                }
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.getSelfCarInvoicesByTime-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }


    /**
     * 自有车 发票下载 渠道通过此接口可以下载某公司某个月份开具的发票。
     *
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult getSelfCarInvoicePackage(String appKey, String api, String data, String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.getSelfCarInvoicePackage-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            //企业编号 必输
            String companyNum = jsonObject.getString("companyNum");
            //发票开具月份 必输
            String makeMonth = jsonObject.getString("makeMonth");
            if (StringUtils.isEmpty(data) || null == jsonObject || StringUtils.isEmpty(companyNum)
                    || StringUtils.isEmpty(makeMonth)

            ) {
                result.setCode(200);
                result.setMsg("必传参数有为空情况，请核实后再试");
                return result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(companyNum);
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            if (null == customerRec1) {
                result.setCode(200);
                result.setMsg("该企业编号在平台中不存在，请联系管理员核实!");
                return result;
            }
            InvoicePackageVo invoicePackageVo = new InvoicePackageVo();
            invoicePackageVo.setCustomerName(appKey);
            invoicePackageVo.setCompanyName(customerRec1.getCompanyName());
            invoicePackageVo.setMonth(makeMonth);

            RespR rs = selfCarService.getInvoicePackge(invoicePackageVo);
            long costtimeend = System.currentTimeMillis();
            log.info("[-SelfCarInterServiceImpl.getSelfCarInvoicePackage-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if (null != rs && rs.getCode() == 0) {
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                if (NOT_FOUND.equals(rs.getMsg())) {
                    result.setData(2);
                }

                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.getSelfCarInvoicePackage-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }


    /**
     * 自有车 卡解绑接口
     *
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult customerCarUnRec(String appKey, String api, String data, String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.customerCarUnRec-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            //企业编号 必输
            String companyNum = jsonObject.getString("companyNum");
            //Etc卡号 必输
            String cardId = jsonObject.getString("cardId");
            if (StringUtils.isEmpty(data) || null == jsonObject || StringUtils.isEmpty(companyNum)
                    || StringUtils.isEmpty(cardId)

            ) {
                return result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(companyNum);
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            if (null == customerRec1) {
                result.setMsg("该企业编号未注册企业!");
                return result;
            }
            List<CustomerCarRec> customerCarRecList = new ArrayList<CustomerCarRec>();

            CustomerCarRec customerCarRec = new CustomerCarRec();
            customerCarRec.setCustomerName(appKey);
            customerCarRec.setCompanyName(customerRec1.getCompanyName());
            customerCarRec.setEtcNum(cardId);
            customerCarRecList.add(customerCarRec);

            RespR rs = selfCarService.customerCarUnRec(customerCarRecList);
            long costtimeend = System.currentTimeMillis();
            log.info("[-SelfCarInterServiceImpl.customerCarUnRec-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if (null != rs && rs.getCode() == 0) {
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.customerCarUnRec-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }

    @Override
    public DataResult customerCardChangeQuery(String appKey, String api, String data, String requestid) {
        long costtimestart = System.currentTimeMillis();
        DataResult result = new DataResult();
        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.customerCarUnRec-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);

            CardChangeQueryVo cardChangeQueryVo = JSON.parseObject(data, CardChangeQueryVo.class);

            if (cardChangeQueryVo == null || StringUtils.isEmpty(cardChangeQueryVo.getCompanyNum())
                    || StringUtils.isEmpty(cardChangeQueryVo.getApplyId()) || StringUtils.isEmpty(cardChangeQueryVo.getCardId())) {
                result.setMsg("必传参数不足");
                return result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(cardChangeQueryVo.getCompanyNum());
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            if (null == customerRec1) {
                result.setMsg("该企业编号未注册企业!");
                return result;
            }
            RespR rs = selfCarService.customerCardChangeQuery(cardChangeQueryVo);
            long costtimeend = System.currentTimeMillis();
            log.info("[-SelfCarInterServiceImpl.customerCarUnRec-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if (null != rs && rs.getCode() == 0) {
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs == null ? "结果为空" : rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.customerCarUnRec-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }

    @Override
    public DataResult customerCardChange(String appKey, String api, String data, String requestid) {
        long costtimestart = System.currentTimeMillis();
        DataResult result = new DataResult();
        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.customerCarUnRec-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);

            CardChangeVo cardChangeVo = JSON.parseObject(data, CardChangeVo.class);

            if (cardChangeVo == null || StringUtils.isEmpty(cardChangeVo.getCompanyNum())
                    || StringUtils.isEmpty(cardChangeVo.getTargetCompanyNum()) || cardChangeVo.getCardIdList() == null
                    || cardChangeVo.getCardIdList().size() == 0) {
                result.setMsg("必传参数不足");
                return result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(cardChangeVo.getCompanyNum());
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            if (null == customerRec1) {
                result.setMsg("该企业编号未注册企业!");
                return result;
            }
            cardChangeVo.setCompanyName(customerRec1.getCompanyName());
            cardChangeVo.setCustomerId(customerRec1.getCustomerName());
            RespR rs = selfCarService.customerCardChange(cardChangeVo);
            long costtimeend = System.currentTimeMillis();
            log.info("[-SelfCarInterServiceImpl.customerCarUnRec-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if (null != rs && rs.getCode() == 0) {
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.customerCarUnRec-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }


    @Override
    public DataResult tradeApplyQuery(String appKey, String api, String data, String requestid) {
        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-SelfCarInterServiceImpl.tradeApplyQuery-] request appKey=" + appKey + " ,api=" + api + " ,data=" + data + " ,requestid=" + requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            //企业编号 必输 不超过32个字符
            String companyNum = jsonObject.getString("companyNum");
            //卡号 必输 20位数字字符
            String cardId = jsonObject.getString("cardId");
            //交易ID 必输 字符串
            String tradeId = jsonObject.getString("tradeId");
            if (StringUtils.isEmpty(data) || null == jsonObject || StringUtils.isEmpty(companyNum)
                    || StringUtils.isEmpty(cardId)
                    || StringUtils.isEmpty(tradeId)
            ) {
                result.setCode(200);
                result.setMsg("必传参数有为空情况，请核实后再试");
                return result;
            }


            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyNum(companyNum);
            CustomerRec customerRec1 = customerRecMapper.selectByCustomerNameAndCompanyNum(customerRec);
            if (null == customerRec1) {
                result.setCode(200);
                result.setMsg("该企业编号在平台中不存在，请联系管理员核实!");
                return result;
            }

            TradeApplyQueryInvVo tradeApplyQueryInvVo = new TradeApplyQueryInvVo();
            tradeApplyQueryInvVo.setCompanyNum(companyNum);
            tradeApplyQueryInvVo.setCardId(cardId);
            tradeApplyQueryInvVo.setTradeId(tradeId);

            RespR rs = selfCarService.getApplyQueryByTradeId(tradeApplyQueryInvVo);

            long costtimeend = System.currentTimeMillis();
            log.info("[-SelfCarInterServiceImpl.tradeApplyQuery-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if (null != rs && rs.getCode() == 0) {
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-SelfCarInterServiceImpl.tradeApplyQuery-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }
}
