package com.ho.filecenter.controller;


import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.ho.business.constant.FileTypeConstant;
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.util.PageResult;
import com.ho.common.tools.util.PageUtils;
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.FlowMonitorService;
import com.ho.filecenter.service.MobileApiService;
import com.ho.filecenter.service.TelecomApiService;
import com.ho.filecenter.vo.flowMonitor.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @author gyan
 * @desc: TODO
 * @DateTime: 2024/1/16 11:13
 */
@RestController
@RequestMapping(ContextConstant.MEDIA_CONTEXT + "flowMonitor")
@Api(tags = "多媒体-流量监控")
@Slf4j
public class FlowMonitorController {
    @Autowired
    FlowMonitorService flowMonitorService;

    @Autowired
    MobileApiService mobileApiService;

    @Autowired
    TelecomApiService telecomApiService;

    @Autowired
    CardInfoService cardInfoService;

    @Autowired
    private RedisService redisService;


    //套餐的增删改查
    //新增
    @PostMapping("add")
    @ApiOperation(value = "新增套餐")
    // @LogAnnotation(title = "业务模块-流量监控", action = "新增")
    public DataResult add(@RequestBody @Valid FlowMonitorAddVo vo) {
        flowMonitorService.insertSelective(vo);
        return DataResult.success();
    }

    @DeleteMapping("deleteByIds")
    //@LogAnnotation(title = "业务模块-流量监控", action = "删除套餐")
    @ApiOperation(value = "删除套餐接口")
    public DataResult deleteByIds(@RequestBody List<Integer> ids) {
        flowMonitorService.deletedByIds(ids);
        return DataResult.success();
    }

    @PutMapping("update")
    //  @LogAnnotation(title = "业务模块-流量监控", action = "更新套餐")
    @ApiOperation(value = "更新套餐接口")
    public DataResult update(@RequestBody @Valid FlowMonitorPutVo vo) {
        flowMonitorService.updateByPrimaryKeySelective(vo);
        return DataResult.success();
    }


    //根据运营商查询各种套餐     //根据运营商查询各种套餐
    @PostMapping("select")
    @ApiOperation(value = "查询套餐")
    public DataResult<List<FlowMonitorAllData>> select(@RequestBody @Valid FlowMonitor vo) {
        List<FlowMonitorAllData> list = flowMonitorService.selectAllPack(vo);
        return DataResult.success(list);
    }

    //根据套餐id查询卡列表
    @PostMapping("selectByCardInfo")
    @ApiOperation(value = "根据套餐id查询卡列表")
    public DataResult<PageResult<CardInfoDate>> selectByCardInfo(@RequestBody @Valid CardSelectVO vo) {
        List<CardInfoDate> pointList = flowMonitorService.selectByCardInfo(vo);
        for (CardInfoDate cardInfoDate : pointList) {
            if (null == cardInfoDate.getStatus()) {
                cardInfoDate.setStatus(CommonConstant.ZERO);
            }
        }
        //java内存分页
        PageResult pageResult = new PageResult<>();
        if (!pointList.isEmpty()) {
            List list = PageUtils.dealList(pointList, vo.getPageNum(), vo.getPageSize());
            pageResult = PageUtils.getPageResult(new PageInfo<>(list));
            pageResult.setTotalRows(pointList.size());
            pageResult.setTotalPages(pointList.size() / vo.getPageSize() + 1);
        } else {
            pageResult = PageUtils.getPageResult(new PageInfo<>(new ArrayList<>()));
        }
        return DataResult.success(pageResult);
    }

    //卡信息的录入 增删改查
    //新增
    @PostMapping("addCardInfo")
    @ApiOperation(value = "新增移动卡信息")
    // @LogAnnotation(title = "业务模块-新增移动卡信息", action = "新增")
    public DataResult addCardInfo(@RequestBody @Valid CardAddVo vo) {
        flowMonitorService.addCardInfo(vo);
        return DataResult.success();
    }

    @DeleteMapping("deleteByCardIds")
    // @LogAnnotation(title = "业务模块-删除卡信息", action = "删除卡信息")
    @ApiOperation(value = "删除卡信息接口")
    public DataResult deleteByCardIds(@RequestBody List<Integer> ids) {
        cardInfoService.deletedByIds(ids);
        return DataResult.success();
    }

    @PutMapping("updateCard")
    // @LogAnnotation(title = "业务模块-更新卡信息", action = "更新卡信息")
    @ApiOperation(value = "更新卡信息接口")
    public DataResult updateCard(@RequestBody @Valid CardInfo vo) {
        cardInfoService.updateByPrimaryKeySelective(vo);
        return DataResult.success();
    }

    //查询所以未绑定未绑定电站的卡号
    @PostMapping("selectNotStation")
    @ApiOperation(value = "查询所以未绑定未绑定电站的卡号")
    // @LogAnnotation(title = "业务模块-新增移动卡信息", action = "新增")
    public DataResult<List<CardInfo>> selectNotStation(@RequestBody @Valid CardInfo vo) {
        List<CardInfo> list = cardInfoService.selectNotStation(vo);
        return DataResult.success(list);
    }


    /**
     * 移动卡号excel表导入
     *
     * @param file
     */
    @PostMapping("/importExcel")
    @ApiOperation(value = "移动卡号excel表导入")
    public DataResult importExcel(@RequestBody MultipartFile file, @RequestParam("flowMonitorId") Integer flowMonitorId) {

        String originalFilename = file.getOriginalFilename();
        String extension = "";
        if (originalFilename != null && originalFilename.lastIndexOf(".") != -1) {
            extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            extension = extension.toLowerCase();
        }
        if (!FileTypeConstant.XLS.equals(extension)) {
            throw new BusinessException(BaseResponseCode.FILE_FORMAT_ONLY);
        }
        List<MobileImportVO> list = new ArrayList<>();
        try {
            list = EasyExcel.read(file.getInputStream()).head(MobileImportVO.class).sheet().doReadSync();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException(BaseResponseCode.SJEKK_EXCEL_DATA);
        }
        int count = cardInfoService.mobileInsertList(list, flowMonitorId);
        return DataResult.success();
    }


    /**
     * 新增电信套餐刷新接口
     */
    @PostMapping("telecomRefresh")
    @ApiOperation(value = "新增电信套餐刷新接口")
    public DataResult getTelecomRefresh() {
        String telecomRefresh = telecomApiService.getTelecomRefresh();
        return DataResult.success();
    }


    /**
     * 电信卡号excel表导入
     *
     * @param file

    @PostMapping("/importTeleExcel")
    @ApiOperation(value = "电信卡号excel表导入")  */
    public DataResult importTeleExcel(@RequestBody MultipartFile file, @RequestParam("flowMonitorId") Integer flowMonitorId) {

        String originalFilename = file.getOriginalFilename();
        String extension = "";
        if (originalFilename != null && originalFilename.lastIndexOf(".") != -1) {
            extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            extension = extension.toLowerCase();
        }
        if (!FileTypeConstant.XLS.equals(extension)) {
            throw new BusinessException(BaseResponseCode.FILE_FORMAT_ONLY);
        }
        List<TeleReqVo> list = new ArrayList<>();
        try {
            list = EasyExcel.read(file.getInputStream()).head(TeleReqVo.class).sheet().doReadSync();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException(BaseResponseCode.SJEKK_EXCEL_DATA);
        }
        int count = cardInfoService.List(list, flowMonitorId);
        return DataResult.success();
    }

}
