package com.mx.cneeds.server.datashow.web.device;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mx.cneeds.common.converter.RequestParamterConverter;
import com.mx.cneeds.common.dto.*;
import com.mx.cneeds.common.result.R;
import com.mx.cneeds.common.vo.DeviceLogFileVo;
import com.mx.cneeds.common.vo.WechatDeviceLogVo;
import com.mx.cneeds.server.datashow.client.DeviceClient;
import com.mx.cneeds.server.datashow.client.LogFileClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName LogFileController
 * @Author zzrdark
 * @Date 2020-04-01 15:14
 * @Description TODO
 **/
@RestController
@Slf4j
@RequestMapping("/remotelog")
public class LogController {

    @Autowired
    private LogFileClient logFileClient;

    @Autowired
    private DeviceClient deviceClient;

    @PreAuthorize("hasRole('remotelog:logfile:list') or hasRole('admin')")
    @PostMapping("/logfile/list")
    public R logFileList(@RequestParam(required = false) Integer page,
                        @RequestParam(required = false) Integer pageSize,
                        @RequestParam(required = false) String sort){

        String orderField = null;
        String order = null;

        if (page==null || page==0){
            page = new Integer(1);
        }
        if (pageSize==null || pageSize==0){
            pageSize = new Integer(10);
        }

        if (StringUtils.isNotEmpty(sort) && StringUtils.isNotBlank(sort.trim())){
            if (sort.contains("+")){
                order = new String("asc");
            }
            if (sort.contains("-")){
                order = new String("desc");
            }
            orderField = sort.substring(1);
            orderField = RequestParamterConverter.toLine(orderField);
        }

        PageDto pageDto = logFileClient.logFileList(page,pageSize,orderField,order);

        Gson gson = RequestParamterConverter.getGson();
        List<DeviceLogDto> deviceLogDtos = new ArrayList<>();

        //  LinkedHashMap 转换成List
        for (Object deviceLogDto: pageDto.getList() ) {
            String deviceLogDtoStr = gson.toJson(deviceLogDto);
            DeviceLogDto dto = gson.fromJson(deviceLogDtoStr,DeviceLogDto.class);
            deviceLogDtos.add(dto);
        }

        Set<Long> deviceSet = new HashSet<>();
        deviceLogDtos.forEach(deviceLogDto -> {
            deviceSet.add(deviceLogDto.getDeviceId());
        });

        if (deviceLogDtos != null && deviceLogDtos.size() != 0){
            List<DeviceInfoDto> deviceInfoDtos = deviceClient.queryDeviceByIds(deviceSet);

            deviceLogDtos.forEach(deviceLogDto -> {
                deviceInfoDtos.forEach(deviceInfoDto -> {
                    if (deviceLogDto.getDeviceId().equals(deviceInfoDto.getDeviceId()) ){
                        deviceLogDto.setSeriesNum(deviceInfoDto.getSeriesNum());
                        deviceLogDto.setImei(deviceInfoDto.getImei());
                    }
                });
            });
        }

        pageDto.setList(deviceLogDtos);

        return new R().put("data",pageDto);
    }

    @PreAuthorize("hasRole('remotelog:logfile:delete') or hasRole('admin')")
    @PostMapping("/logfile/delete")
    public R deleteLogFile(@RequestParam(value = "ids",required = false) List<Long> ids){
        logFileClient.deleteLogFile(ids);
        return R.ok();
    }

    /**
     * 处理完成日志
     * @return
     */
    @PreAuthorize("hasRole('remotelog:logfile:edit') or hasRole('admin')")
    @PostMapping("/logfile/finish")
    public R finishLog(@RequestParam(value = "logId") Long logId){
        logFileClient.finishLog(logId);
        return R.ok();
    }

}
