package com.sptg.statistics.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.permissions.api.entity.Area;
import com.sptg.permissions.api.feigns.AreaFeign;
import com.sptg.statistics.api.dto.SysDeviceLogDto;
import com.sptg.statistics.api.entity.SysDeviceLog;
import com.sptg.statistics.api.feigns.SysDeviceLogFeign;
import com.sptg.statistics.biz.service.SysDeviceLogServiceInterface;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * title: SysAdviceLogController
 * package: com.sptg.statistics.biz.controller
 * description: 机器事件日志
 *
 *                
 *                 
 */
@RestController
public class SysDeviceLogController implements SysDeviceLogFeign {

    private final SysDeviceLogServiceInterface sysDeviceLogServiceInterface;
    private final AreaFeign areaFeign;

    @Autowired
    public SysDeviceLogController(SysDeviceLogServiceInterface sysDeviceLogServiceInterface, AreaFeign areaFeign) {
        this.sysDeviceLogServiceInterface = sysDeviceLogServiceInterface;
        this.areaFeign = areaFeign;
    }

    @Override
    @PostMapping("/statistics/sysDeviceLog/showSysDeviceLogList")
    public ResultBean<Page<SysDeviceLog>> showSysDeviceLogList(@RequestBody(required = false) SysDeviceLogDto sdlDto) {
        Page<SysDeviceLog> page=sysDeviceLogServiceInterface.getSysDeviceLogList(sdlDto);
        return new ResultBean<>(ResultEnum.SUCCESS,page);
    }

    @Override
    @PostMapping("/statistics/sysDeviceLog/addSysDeviceLog")
    public ResultBean<Boolean> addSysDeviceLog(@RequestBody SysDeviceLog sysDeviceLog) {
        boolean result=sysDeviceLogServiceInterface.insert(sysDeviceLog);
        return  new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping("/statistics/sysDeviceLog/sysDeviceLogsInfo")
    public ResultBean<Boolean> info(@RequestBody List<SysDeviceLog> sysDeviceLogs) {
        boolean result=sysDeviceLogServiceInterface.insertBatch(sysDeviceLogs);
        return  new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping("/statistics/sysDeviceLog/sysDeviceLogInfo")
    public ResultBean<Boolean> info(@RequestBody SysDeviceLog sysDeviceLog) {
        boolean result=sysDeviceLogServiceInterface.insert(sysDeviceLog);
        return  new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping("/statistics/sysDeviceLog/statisticsDeviceMalfunction")
    public ResultBean<List<Map<String,Object>>> statisticsDeviceMalfunction(@RequestBody SysDeviceLogDto sysDeviceLogDto) {
        List<Map<String,Object>> maps=sysDeviceLogServiceInterface.statisticsDeviceMalfunction(sysDeviceLogDto);
        return  new ResultBean<List<Map<String,Object>>>(ResultEnum.SUCCESS,maps);
    }

    @Override
    @PostMapping("/statistics/sysDeviceLog/getDeviceFaultCountList")
    public ResultBean<List<Map<String, Object>>> getDeviceFaultCountList(@RequestBody SysDeviceLogDto sysDeviceLogDto) {
        List<Map<String,Object>> list= sysDeviceLogServiceInterface.getDeviceFaultCountList(sysDeviceLogDto);
        return  new ResultBean<List<Map<String,Object>>>(ResultEnum.SUCCESS,list);
    }

    @Override
    @PostMapping("/statistics/sysDeviceLog/deviceRankForArea")
    public ResultBean<List<Map<String, Object>>> deviceRankForArea(@RequestBody SysDeviceLogDto sysDeviceLogDto) {
        List<Map<String,Object>> list = sysDeviceLogServiceInterface.deviceRankForArea(sysDeviceLogDto);
        //转换省市编号换成省市中文
        if(list!=null){
            list.forEach(map->{
                String code=(String)map.get("regionProvince");
                if(StringUtils.isNotBlank(code)){
                    Area area = areaFeign.selectByCode(code);
                    if(null!=area){
                        //缩写的
                        String areaName=area.getShortName();
                        map.put("area",areaName);
                        map.put("deviceCount",map.get("deviceCount"));
                    }
                }
            });
        }
        return  new ResultBean<List<Map<String,Object>>>(ResultEnum.SUCCESS,list);
    }
}
