package com.jpsoft.shinestar.modules.business.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.config.TemperatureConfig;
import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.entity.DeviceInfo;
import com.jpsoft.shinestar.modules.base.entity.PersonDeviceLog;
import com.jpsoft.shinestar.modules.base.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;

/**
 * 健康公示墙
 */
@Controller
public class HealthPublicityController {
    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private PersonDeviceFilterLogService personDeviceFilterLogService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private PersonDeviceLogService personDeviceLogService;

    @Autowired
    private CompanyDeviceRelationService companyDeviceRelationService;

    @Autowired
    private CheckNumCompanyService checkNumCompanyService;

    @Autowired
    private TotalCheckCompanyService totalCheckCompanyService;

    @Autowired
    private PersonCompanyService personCompanyService;

    @Autowired
    private TemperatureConfig temperatureConfig;

    @Autowired
    private  DeviceInfoService deviceInfoService;

    @GetMapping("/healthPublicity")
    public String index(String companyId, Model model) {
        CompanyInfo companyInfo = companyInfoService.get(companyId);
        model.addAttribute("company", companyInfo);

        model.addAttribute("now", DateTime.now().toString("yyyy-MM-dd HH:mm"));

        List<CompanyInfo> list = companyInfoService.findByCompanyCode(companyInfo.getCode() + "%", null);

        List<String> deviceNoList = new ArrayList<>();

        for (CompanyInfo companyInfo1 : list) {
            List<DeviceInfo> deviceInfoList = companyDeviceRelationService.findDeviceByCompanyId(companyInfo1.getId());
            for (DeviceInfo deviceInfo : deviceInfoList) {
                if (!deviceNoList.contains(deviceInfo.getDeviceNo())) {
                    deviceNoList.add(deviceInfo.getDeviceNo());
                }
            }
        }

        if (deviceNoList.size()==0){
            deviceNoList.add("-1");
        }

        Page<PersonDeviceLog> personDeviceLogList1 = personDeviceLogService.findAllPersonLogByCompanyListAndDeviceNoList(DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date()),
                list, deviceNoList,1, 40);

        //已测访客数
        Integer checkedVisitorNum = 0;
        if (deviceNoList.size() > 0) {
            checkedVisitorNum = personDeviceLogService.getDayCheckedVistorNumByDeviceNoList(DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date()), deviceNoList);
        }

        List<Map> itemList = new ArrayList<>();
        int visitorNum = checkedVisitorNum;

        for (int i = 0; i < personDeviceLogList1.size(); i++) {
            PersonDeviceLog item = personDeviceLogList1.get(i);

            Map<String, Object> map = new HashMap<>();

            if (item.getPerson() == null) {
                map.put("name", "访客" + visitorNum);
                map.put("healthyCode", null);
                visitorNum--;
            } else {
                map.put("name", item.getPerson().getName());
                map.put("healthyCode", item.getPerson().getHealthyCode());
            }

            map.put("faceImage", item.getFaceImage());
            map.put("temperature", item.getTemperature());
            map.put("recordTime", item.getRecordTime());

            itemList.add(map);
        }

        model.addAttribute("itemList", itemList);

        Integer personNum = Math.toIntExact(personInfoService.countByCompanyList(list));
        Integer checkedPersonNum = personDeviceFilterLogService.getDayCheckedPersonNumByCompanyList(DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date()), list, null);

        //已测员工人次
        Integer checkPersonTotal = personDeviceLogService.getDayCheckedPersonTotalByCompanyList(DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date()), list, null);

        Integer num = checkNumCompanyService.getDayCheckNumByCompanyList(DateUtil.format(new Date(), "yyyy-MM-dd"), list);
        Integer totalNum = totalCheckCompanyService.getTotalCheckNum(list);
        Integer dayCheckNum = num == null ? 0 : num;

        //当日累计检测数
        //   Integer totalCheckNum = totalNum == null ? dayCheckNum : totalNum + dayCheckNum;
        Integer totalCheckNum = checkPersonTotal + checkedVisitorNum;

        Integer num2 = personDeviceFilterLogService.getDayAbnormalNumByCompanyList(DateUtil.beginOfDay(new Date()), new Date(), list);
        Integer totalNum2 = totalCheckCompanyService.getTotalAbnormalNumByCompanyList(list);
        Integer dayAbnormalNum = num2 == null ? 0 : num2;
        Integer totalAbnormalNum = totalNum2 == null ? 0 : totalNum2 + dayAbnormalNum;

        Integer normalDaysNum = totalCheckCompanyService.findMinNormalDayByCompanyList(list);
        Integer normalDays = normalDaysNum == null ? 0 : normalDaysNum;

        model.addAttribute("checkedPersonNum", checkedPersonNum);
        model.addAttribute("personNum", personNum);
        model.addAttribute("checkedVisitorNum", checkedVisitorNum);
        model.addAttribute("totalCheckNum", totalCheckNum);
        model.addAttribute("dayAbnormalNum", dayAbnormalNum);
        model.addAttribute("totalAbnormalNum", totalAbnormalNum);
        model.addAttribute("normalDays", normalDays);
        model.addAttribute("temperatureConfig", temperatureConfig);

        return "motemwall_pc";
    }
    @GetMapping("/healthPublicityCanteen")
    public String canteen(String id, Model model) {


        Page<PersonDeviceLog> personDeviceLogList1 = personDeviceLogService.findAllPersonLogByDeviceNo(DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date()),
                id,1, 6);



        List<Map> itemList = new ArrayList<>();


        for (int i = 0; i < personDeviceLogList1.size(); i++) {
            PersonDeviceLog item = personDeviceLogList1.get(i);

            Map<String, Object> map = new HashMap<>();

            if (item.getPerson() == null) {
                map.put("name", "访客" );
                map.put("companyName", "");


            } else {
                map.put("name", item.getPerson().getName());
                map.put("companyName", item.getPerson().getCompanyName());

            }

            map.put("faceImage", item.getFaceImage());

            map.put("recordTime", item.getRecordTime());

            itemList.add(map);
        }

        model.addAttribute("itemList", itemList);


        return "motemwall_canteen";
    }
}
