package com.ho.filecenter.controller;


import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.vo.req.CardInfoReqVo;
import com.ho.filecenter.entity.CardInfo;
import com.ho.filecenter.entity.FlowMonitor;
import com.ho.filecenter.service.CardInfoService;
import com.ho.filecenter.service.FileService;
import com.ho.filecenter.service.FlowMonitorService;
import com.ho.filecenter.service.MobileApiService;
import com.ho.filecenter.vo.flowMonitor.CardAddVo;
import com.ho.filecenter.vo.flowMonitor.CardInfoDate;
import com.ho.filecenter.vo.flowMonitor.PackageDetailRespVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 提供给外部调用，不校验token
 * Author yule
 * Date 2023/2/6 10:08
 */
@RestController
@RequestMapping(ContextConstant.FILE_CENTER + "outerApi")
@ApiIgnore
@Slf4j
public class OuterApiController {

    @Autowired
    FileService fileService;

    @Autowired
    CardInfoService cardInfoService;

    @Autowired
    FlowMonitorService flowMonitorService;

    @Autowired
    MobileApiService mobileApiService;

    @Autowired
    private RedisService redisService;


    @PostMapping("/getBySnIsOnline")
    @TokenIgnore
    public Boolean getBySnIsOnline(@RequestBody String sn) {
        return fileService.checkHeartbeat(sn);
    }

    //电站接口新增移动卡信息
    @PostMapping("insertValue")
    @TokenIgnore
    public DataResult insertValue(@RequestBody @Valid CardInfoReqVo vo) {
        //判断增加的卡号是否重复
        CardInfo cardValue = cardInfoService.selectByCard(vo.getCard());
        if (cardValue != null) {
            throw new BusinessException(BaseResponseCode.STATION_ICCID_EXISTS);
        }
        //判断初始状态是否存在不存在则赋值未激活状态
        if (vo.getCardStatus() == null) {
            vo.setCardStatus(CommonConstant.ZERO);
        }
        CardInfo cardInfo = new CardInfo();
        BeanUtils.copyProperties(vo, cardInfo);
        //根据套餐id查询类型
        FlowMonitor flowMonitor = flowMonitorService.selectById(vo.getFlowMonitorId());
        if (CommonConstant.ONE.equals(flowMonitor.getPackageType())) {
            cardInfo.setPackageType(flowMonitor.getPackageType());
            cardInfoService.insertValue(cardInfo);
            String singleCard = mobileApiService.getSingleCard(vo.getCard());
            if (singleCard != null) {
                PackageDetailRespVo packageDetailRespVo = new PackageDetailRespVo();
                List<CardInfoDate> cardInfoList = new ArrayList<>();
                CardInfoDate cardInfoDate = new CardInfoDate();
                cardInfoDate.setCardNo(vo.getCard());
                cardInfoDate.setStationId(vo.getStationId());
                String format = DateFormatUtils.format(new Date(), CommonConstant.DATE);
                cardInfoDate.setUpdateTime(format);
                cardInfoDate.setUserAmount(singleCard);
                cardInfoList.add(cardInfoDate);
                packageDetailRespVo.setCardInfoList(cardInfoList);
                String key = "flowMonitor:mobile:" + vo.getFlowMonitorId();
                log.info("flowMonitorKey:" + key);
                log.info("packageDetailRespVo:" + packageDetailRespVo);
                redisService.set(key, packageDetailRespVo);
            }
        } else if (CommonConstant.THREE.equals(flowMonitor.getPackageType())) {
            CardAddVo addVo = new CardAddVo();
            BeanUtils.copyProperties(vo, addVo);
            flowMonitorService.addCardInfo(addVo);
        }

        return DataResult.success();
    }

    //电站接口更新移动卡信息
    @PostMapping("updateValue")
    @TokenIgnore
    public DataResult updateValue(@RequestBody @Valid CardInfoReqVo vo) {
        CardInfo cardInfo = new CardInfo();
        BeanUtils.copyProperties(vo, cardInfo);
        cardInfoService.updateByPrimaryKeySelective(cardInfo);
        return DataResult.success();
    }

    @PostMapping("selectByCondition")
    @ApiOperation(value = "根据条件查询")
    @TokenIgnore
    public DataResult<List<CardInfoReqVo>> selectByCondition(@RequestBody @Valid CardInfoReqVo cardInfo) {
        List<CardInfoReqVo> list = cardInfoService.selectByCondition(cardInfo);
        return DataResult.success(list);
    }

    @DeleteMapping("deleteByCardIds")
    @ApiOperation(value = "删除卡信息接口")
    @TokenIgnore
    public DataResult deleteByCardIds(@RequestBody List<Integer> ids) {
        cardInfoService.deletedByIds(ids);
        return DataResult.success();
    }

}
