package com.xy.service.factory.sim.impl;

import com.xy.annotate.Factory;
import com.xy.dto.CtWingDto;
import com.xy.dto.CtWingVo;
import com.xy.dto.SimInfoDto;
import com.xy.service.factory.sim.SimInfoFactory;
import com.xy.utils.CtWingUtils;
import com.xy.utils.FunctionUtils;
import com.xy.utils.R;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 电信
 * </p>
 *
 * @author hechunping
 * @since 2024/8/20
 */
@Slf4j
@Factory(type = "电信")
@AllArgsConstructor
public class CtWingFactoryImpl implements SimInfoFactory {
    /**
     * 三码查询
     *
     * @param iccid
     * @return
     */
    @SneakyThrows
    private CtWingVo.TelephonePlusVo telephonePlus(String iccid) {
        CtWingDto.TelephonePlusDto telephonePlusDto = new CtWingDto.TelephonePlusDto();
        telephonePlusDto.setIccid(iccid);
        CtWingVo.TelephonePlusVo vo = CtWingUtils.getTelephonePlus(telephonePlusDto);
        return vo;
    }


    @Override
    @SneakyThrows
    public R<SimInfoDto.InfoVo> info(SimInfoDto.BaseDto dto) {
        //三码查询
        CtWingVo.TelephonePlusVo telephonePlusVo = telephonePlus(dto.getIccid());
        if (!"0".equals(telephonePlusVo.getResultCode())) {
            return R.fail("三码查询错误！");
        }
        SimInfoDto.InfoVo vo = new SimInfoDto.InfoVo();
        vo.setIccid(telephonePlusVo.getIccid())
                .setImsi(telephonePlusVo.getImsi())
                .setAccessNumber(telephonePlusVo.getAccessNumber());
        //卡主状态查询
        FunctionUtils.NoParamsNoResult queryCardMainStatus = () -> {
            CtWingDto.CardMainStatusDto cardMainStatusDto = new CtWingDto.CardMainStatusDto();
            cardMainStatusDto.setAccessNumber(telephonePlusVo.getAccessNumber());
            CtWingVo.CardMainStatusVo cardMainStatusVo = CtWingUtils.queryCardMainStatus(cardMainStatusDto);
            vo.setCreateDate(cardMainStatusVo.getServCreateDate())
                    .setActiveDate(cardMainStatusVo.getServActiveDate())
                    .setNetBlockStatus(cardMainStatusVo.getNetBlockStatusName())
                    .setSimStatus(cardMainStatusVo.getOperatorDefinitionStatusName());
        };
        //网络状态
        FunctionUtils.NoParamsNoResult queryNetStatus = () -> {
            CtWingDto.AccessNumberOrIccid accessNumberOrIccid = new CtWingDto.AccessNumberOrIccid().setIccid(telephonePlusVo.getAccessNumber());
            CtWingVo.NetStatusVo netStatusVo = CtWingUtils.queryNetStatus(accessNumberOrIccid);
        };
        queryCardMainStatus.run();
        queryNetStatus.run();
        return R.ok(vo);
    }

    @Override
    @SneakyThrows
    public R<String> refreshNet(SimInfoDto.RefreshNetDto dto) {
        //三码查询
        CtWingVo.TelephonePlusVo telephonePlusVo = telephonePlus(dto.getIccid());
        if (!"0".equals(telephonePlusVo.getResultCode())) {
            return R.fail("三码查询错误！");
        }
        String action = dto.getAction() ? "ADD":"DEL";
        CtWingDto.SingleCutNetDto singleCutNetDto = new CtWingDto.SingleCutNetDto()
                .setAccessNumber(telephonePlusVo.getAccessNumber())
                .setAction(action);
        CtWingVo.BaseVo baseVo = CtWingUtils.singleCutNet(singleCutNetDto);
        if("0".equals(baseVo.getResultCode())){
            return R.ok("处理成功！");
        }else{
            return R.fail(baseVo.getResultMsg());
        }
    }
}
