package com.ho.filecenter.controller;

/**
 * @author gyan
 * @desc: OTA升级
 * @DateTime: 2023/8/2 10:29
 */


import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.entity.UserDetailRespVO;
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.filecenter.entity.Otaupgrad;
import com.ho.filecenter.service.OtaupgradService;
import com.ho.filecenter.vo.OtaFileVO;
import com.ho.filecenter.vo.ScheduleReqVo;
import com.ho.filecenter.vo.UpdateRecords;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping(ContextConstant.MEDIA_CONTEXT + "otaupgrad")
@Api(tags = "OTA升级")
@Slf4j
public class OtaupgradController {

    @Autowired
    RedisService redisService;
    @Autowired
    OtaupgradService otaupgradService;

    @PostMapping("page")
    @ApiOperation(value = "分页查询")
    public DataResult<PageResult<Otaupgrad>> page(@RequestBody OtaFileVO vo) {
        PageResult<Otaupgrad> pageResult = otaupgradService.page(vo);
        return DataResult.success(pageResult);

    }

    @PostMapping("hisFile")
    @ApiOperation(value = "查询更新记录")
    public DataResult<List<UpdateRecords>> SelectHisFile(@RequestBody OtaFileVO vo) {
        List<UpdateRecords> list = otaupgradService.selectHisFile(vo);
        return DataResult.success(list);
    }

    @PostMapping("upgradation")
    @ApiOperation(value = "设备升级")
    public DataResult upgradation(@RequestPart("file") MultipartFile file ,
                                  @RequestParam("stationIdAndDeviceList") List<String> stationIdAndDeviceList,
                                  @RequestParam("deviceName") String deviceName,
                                  @RequestParam("version") String version,
                                  @RequestParam("updateCause") String updateCause,
                                  @RequestParam("updateTime") String updateTime,HttpServletRequest request) throws Exception {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        if(stationIdAndDeviceList.isEmpty()){
            throw new BusinessException(BaseResponseCode.NOT_CHECK_STATION_DEVICE);
        }
        UserDetailRespVO userDetailRespVO = redisService.getUserDetailByToken(token);
        String userName = userDetailRespVO.getUsername();
        String userNameKey = redisService.getUserNameKey(userDetailRespVO, userName);
        SimpleUser user = (SimpleUser) redisService.get(userNameKey);
        OtaFileVO vo =new OtaFileVO();
        vo.setDeviceName(deviceName);
        vo.setStationIdAndDeviceList(stationIdAndDeviceList);
        vo.setUpdateCause(updateCause);
        vo.setVersion(version);
        //对时间进行比对
        vo.setUpdateTime(updateTime);
        otaupgradService.tage(file,vo);
        otaupgradService.upgradation(file, vo, user);
        return DataResult.success();
    }

    @PostMapping("getUpgradeProgress")
    @ApiOperation(value = "升级进度")
    public DataResult<List<ScheduleReqVo>> getUpgradeProgress(@RequestBody OtaFileVO vo) {
        List<ScheduleReqVo> scheduleReqVo = otaupgradService.getUpgradeProgress(vo);
        return DataResult.success(scheduleReqVo);
    }
}
