package com.ho.filecenter.service.impl;


import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.vo.req.CardInfoReqVo;
import com.ho.filecenter.entity.CardInfo;
import com.ho.filecenter.entity.FlowMonitor;
import com.ho.filecenter.feignclient.BusinessFeignClient;
import com.ho.filecenter.mapper.CardInfoMapper;
import com.ho.filecenter.service.CardInfoService;
import com.ho.filecenter.service.FlowMonitorService;
import com.ho.filecenter.service.MobileApiService;
import com.ho.filecenter.vo.flowMonitor.MobileImportVO;
import com.ho.filecenter.vo.flowMonitor.TeleReqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author yy
 * @description 针对表【card_info】的数据库操作Service实现
 * @createDate 2024-01-16 11:23:54
 */
@Service
@Slf4j
public class CardInfoServiceImpl implements CardInfoService {
    @Autowired
    CardInfoMapper cardInfoMapper;

    @Autowired
    FlowMonitorService flowMonitorService;

    @Autowired
    MobileApiService mobileApiService;


    @Autowired
    CardInfoService cardInfoService;

    @Autowired
    private BusinessFeignClient businessFeignClient;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return cardInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(CardInfo record) {
        return cardInfoMapper.insert(record);
    }

    @Override
    public int insertSelective(CardInfo record) {
        return cardInfoMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(CardInfo record) {
        return cardInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CardInfo record) {
        return cardInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public void deletedByIds(List<Integer> ids) {
        //查询卡的信息关联电站不给删除
        for (Integer id : ids) {
            CardInfo cardInfo = cardInfoMapper.selectById(id);
            if (cardInfo.getStationId() == null) {
                cardInfoMapper.deleteByPrimaryKey(id);
            } else {
                throw new BusinessException(BaseResponseCode.SELECT_CORRSTATION_NO_DELETED);
            }
        }
    }

    @Override
    public void insertValue(CardInfo cardInfo) {
        int i = cardInfoMapper.insertSelective(cardInfo);
        return;
    }

    //根据套餐id查询卡号总条数
    @Override
    public Integer selectByCardTotal(Integer flowMonitorId) {
        return cardInfoMapper.selectByCardTotal(flowMonitorId);
    }


    //根据套餐id查询卡号
    @Override
    public List<CardInfo> selectByFlowCardList(int offset, int pageSize, Integer flowMonitorId) {
        return cardInfoMapper.selectByFlowCardList(offset, pageSize, flowMonitorId);
    }

    @Override
    public List<CardInfoReqVo> selectByCondition(CardInfoReqVo vo) {
        List<CardInfoReqVo> list = new ArrayList<>();
        CardInfo cardInfo = new CardInfo();
        cardInfo.setCard(vo.getCard());
        cardInfo.setCardStatus(vo.getCardStatus());
        cardInfo.setPackageType(vo.getPackageType());
        cardInfo.setStationId(vo.getStationId());
        cardInfo.setFlowMonitorId(vo.getFlowMonitorId());
        List<CardInfo> cardInfos = cardInfoMapper.selectByCondition(cardInfo);
        for (CardInfo info : cardInfos) {
            CardInfoReqVo cardInfoReqVo = new CardInfoReqVo();
            BeanUtils.copyProperties(info, cardInfoReqVo);
            list.add(cardInfoReqVo);
        }
        return list;
    }

    @Override
    public List<CardInfo> selectNotStation(CardInfo vo) {
        List<CardInfo> dataList = new ArrayList<>();
        List<CardInfo> cardInfoList = cardInfoMapper.selectAll(vo);
        cardInfoList.stream().filter(i -> null == i.getStationId()).forEach(s -> dataList.add(s));
        return dataList;
    }

    @Override
    public int mobileInsertList(List<MobileImportVO> list, Integer flowMonitorId) {
        //根据套餐id查询运营商
        FlowMonitor flowMonitor = flowMonitorService.selectById(flowMonitorId);
        if (flowMonitor != null && !list.isEmpty()) {
            for (MobileImportVO mobileImportVO : list) {
                if (mobileImportVO.getICCID() == null) {
                    throw new BusinessException(BaseResponseCode.PLEASE_CONFIRM_CORRECT);
                }
                CardInfo cardInfo = new CardInfo();
                cardInfo.setCard(mobileImportVO.getICCID());
                if (flowMonitor.getPackageType() != null) {
                    cardInfo.setPackageType(flowMonitor.getPackageType());
                }
                cardInfo.setFlowMonitorId(flowMonitorId);
                //已激活
                if (CommonConstant.MobileCardStatus.Active.equals(mobileImportVO.getCardStatus())) {
                    cardInfo.setCardStatus(CommonConstant.ZERO);
                }
                //待激活
                if (CommonConstant.MobileCardStatus.NOTACTIVE.equals(mobileImportVO.getCardStatus())) {
                    cardInfo.setCardStatus(CommonConstant.ONE);
                }
                cardInfo.setOpenDate(mobileImportVO.getOpenDate());
                cardInfo.setActivationDate(mobileImportVO.getActivationDate());
                //入库前查询卡号是否重复
                CardInfo cardInfo1 = selectByCard(cardInfo.getCard());
                if (cardInfo1 != null) {
                    throw new BusinessException(BaseResponseCode.EXCEL_ICCID_EXISTS);
                }
                insertValue(cardInfo);
            }
            List<FlowMonitor> flowMonitorList = new ArrayList<>();
            flowMonitorList.add(flowMonitor);
            flowMonitor.setId(flowMonitorId);
            mobileApiService.getMobileAllData(flowMonitorList);
        } else {
            throw new BusinessException(BaseResponseCode.SJEKK_EXCEL_DATA);
        }
        return 0;
    }

    @Override
    public CardInfo selectByCard(String card) {
        CardInfo cardInfo = cardInfoMapper.selectByCard(card);
        return cardInfo;
    }


    @Override
    public int List(List<TeleReqVo> list, Integer flowMonitorId) {
        //根据套餐id查询运营商
        FlowMonitor flowMonitor = flowMonitorService.selectById(flowMonitorId);
        if (flowMonitor != null && !list.isEmpty()) {
            for (TeleReqVo mobileImportVO : list) {
                if (mobileImportVO.getICCID() == null) {
                    throw new BusinessException(BaseResponseCode.PLEASE_CONFIRM_CORRECT);
                }
                CardInfo cardInfo = new CardInfo();
                cardInfo.setCard(mobileImportVO.getICCID());
                if (flowMonitor.getPackageType() != null) {
                    cardInfo.setPackageType(flowMonitor.getPackageType());
                }
                cardInfo.setFlowMonitorId(flowMonitorId);
                //已激活
                if (CommonConstant.MobileCardStatus.Active.equals(mobileImportVO.getCardStatus())) {
                    cardInfo.setCardStatus(CommonConstant.ZERO);
                }
                //待激活
                if (CommonConstant.MobileCardStatus.NOTACTIVE.equals(mobileImportVO.getCardStatus())) {
                    cardInfo.setCardStatus(CommonConstant.ONE);
                }

                //待激活
                if (CommonConstant.MobileCardStatus.HALT.equals(mobileImportVO.getCardStatus())) {
                    cardInfo.setCardStatus(CommonConstant.TWO);
                }
                cardInfo.setOpenDate(mobileImportVO.getOpenDate());
                //入库前查询卡号是否重复
                CardInfo cardInfo1 = selectByCard(cardInfo.getCard());
                if (cardInfo1 != null) {
                    throw new BusinessException(BaseResponseCode.EXCEL_ICCID_EXISTS);
                }
                insertValue(cardInfo);
            }
            List<FlowMonitor> flowMonitorList = new ArrayList<>();
            flowMonitorList.add(flowMonitor);
            flowMonitor.setId(flowMonitorId);
            mobileApiService.getMobileAllData(flowMonitorList);
        } else {
            throw new BusinessException(BaseResponseCode.SJEKK_EXCEL_DATA);
        }
        return 0;
    }
}
