package com.huimv.video.dhicc.controller.ScreenController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dahuatech.icc.exception.ClientException;
import com.huimv.video.dhicc.entity.SysTelecomEvent;
import com.huimv.video.dhicc.result.R;
import com.huimv.video.dhicc.service.ISysTelecomEventService;
import com.huimv.video.dhicc.util.GetResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@RequestMapping("/screen/alarmCount")

public class ScreenAlarmCountByDay {

    @Autowired
    private ISysTelecomEventService sysTelecomEventService;

    //四种事件统计
    @RequestMapping("/CountByDay")
    public R AlarmCountByDay(HttpServletRequest request, @RequestParam(name = "farmId") Integer farmId) throws ClientException {
        if (farmId != 1) {
            //返回为空数据
            return R.ok("当前牧场无设备").put("data", null);
        }

        GetResponse getResponse = new GetResponse();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        Date date = new Date();
        c.setTime(date);
        Date A = c.getTime();
        String formatA = format.format(A);
        c.add(Calendar.DATE, -7);
        Date B = c.getTime();
        String formatB = format.format(B);
        List result = new ArrayList();

        //洗消 一周
        Map map1 = new HashMap();
        map1.put("name", "车辆洗消");
        map1.put("value", getResponse.SendMassageXixiaoPerson(formatB, formatA));
        //人员通过一周---异常
        Map map2 = new HashMap();
        map2.put("name", "人员违规");
        map2.put("value", getResponse.SendMassageGetPagePersonAlarm(formatB, formatA));
        //车辆一周
        Map map3 = new HashMap();
        map3.put("name", "死猪运输");
        map3.put("value", getResponse.SendMassageSiZhuRecord(formatB, formatA));
        //烘干事件一周
        Map map4 = new HashMap();
        map4.put("name", "车辆烘干");
        map4.put("value", sysTelecomEventService.count(Wrappers.<SysTelecomEvent>lambdaQuery()
                .between(SysTelecomEvent::getEventTime, formatB, formatA)) + "");
        result.add(map1);
        result.add(map2);
        result.add(map3);
        result.add(map4);
        //翻转
        Collections.reverse(result);
        return R.ok("请求成功").put("data", result);


    }
}
