package com.mx.cneeds.server.logupload.web;

import com.mx.cneeds.common.dto.DeviceLogDto;
import com.mx.cneeds.common.dto.DeviceLogFileDto;
import com.mx.cneeds.common.dto.WechatDeviceLogDto;
import com.mx.cneeds.common.dto.WechatFileDto;
import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.common.result.R;
import com.mx.cneeds.server.entity.DeviceLogEntity;
import com.mx.cneeds.server.logupload.service.DeviceLogMediaService;
import com.mx.cneeds.server.logupload.service.DeviceLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.*;

/**
 * @ClassName LogUploadController
 * @Author zzrdark
 * @Date 2020-03-27 15:26
 * @Description TODO
 **/
@RestController
@RequestMapping("/log/upload")
public class LogUploadController {
    @Autowired
    private DeviceLogService deviceLogService;

    @Autowired
    private DeviceLogMediaService deviceLogMediaService;


    @RequestMapping("/wechatUploadLog")
    public void wechatUploadLog(@RequestBody WechatDeviceLogDto dto){
        DeviceLogEntity deviceLogEntity = new DeviceLogEntity();

        deviceLogEntity.setCreateUsername(dto.getCreateUsername());
        deviceLogEntity.setUserId(dto.getUserId());
        deviceLogEntity.setLogAcceptId(dto.getLogAcceptId());
        deviceLogEntity.setDeviceId(dto.getDeviceId());
        deviceLogEntity.setLogName(dto.getBugTitle());
        deviceLogEntity.setRepetitionProbability(dto.getProbability());
        deviceLogEntity.setRepetitionSteps(dto.getSteps());
        Long logId = deviceLogService.wechatUploadLog(deviceLogEntity);
    }

    @RequestMapping("/wechatUploadAloneFile")
    public void wechatUploadAloneFile(@RequestBody WechatFileDto dto){

        DeviceLogEntity deviceLogEntity = null;
        if (dto.getLogAcceptId()!=null){
            deviceLogEntity = deviceLogService.queryOneBylogAcceptId(dto.getLogAcceptId());
        }
        if (deviceLogEntity != null){
            deviceLogMediaService.savewechatMedia(deviceLogEntity.getLogId(), dto.getFileSize(), dto.getLocationUrl());
        }
    }

    @RequestMapping("/queryLogFileByLogid")
    public DeviceLogDto queryLogFileByLogid(@RequestBody long logId){
        DeviceLogEntity deviceLogEntity = deviceLogService.getById(logId);
        DeviceLogDto dto = new DeviceLogDto();
        BeanUtils.copyProperties(deviceLogEntity, dto);
        return dto;
    }

    @RequestMapping("/queryLogFileByLogAcceptId")
    public DeviceLogDto queryLogFileByLogAcceptId(@RequestBody String logAcceptId){
        DeviceLogEntity deviceLogEntity = deviceLogService.queryOneBylogAcceptId(logAcceptId);
        if (deviceLogEntity == null){
            return null;
        }

        DeviceLogDto dto = new DeviceLogDto();
        BeanUtils.copyProperties(deviceLogEntity, dto);
        return dto;
    }


    @RequestMapping("/uploadLogFile")
    public void uploadLogFile(@RequestBody DeviceLogFileDto dto){
        DeviceLogEntity deviceLogEntity = new DeviceLogEntity();
        deviceLogEntity.setDeviceId(dto.getDeviceId());
        deviceLogEntity.setLogfileUrl(dto.getLogfileUrl());
        deviceLogEntity.setLogAcceptId(dto.getLogAcceptId());
        deviceLogEntity.setLogfileSize(dto.getLogfileSize());
        deviceLogEntity.setDeviceVersion(dto.getDeviceVersion());
        deviceLogService.deviceUploadLogFile(deviceLogEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params){
        PageUtils page = deviceLogService.queryPage(params);
        List<DeviceLogDto> list =new ArrayList<>();
        page.getList().forEach(deivceLogEntity -> {
            DeviceLogDto deviceLogDto = new DeviceLogDto();
            BeanUtils.copyProperties(deivceLogEntity, deviceLogDto);
            list.add(deviceLogDto);
        });
        page.setList(list);
        return page;
    }

    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] userIds){
        deviceLogService.deleteBatch(userIds);

        return R.ok();
    }


    @RequestMapping("/finishLog")
    public R finishLog(@RequestBody Long logId){
        deviceLogService.changeStatus(logId);
        return R.ok();
    }




}
