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

import cn.hutool.core.date.DateUtil;
import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.sys.entity.User;
import com.jpsoft.shinestar.modules.sys.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author 墨鱼_mo
 * @date 2020-3-27 13:40
 */
@RestController
@RequestMapping("/home")
@Slf4j
public class HomePageController {

    @Autowired
    private UserService userService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private PersonDeviceFilterLogService personDeviceFilterLogService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private CheckNumCompanyService checkNumCompanyService;

    @Autowired
    private TotalCheckCompanyService totalCheckCompanyService;


    @ApiOperation(value = "获取首页第一行数据")
    @GetMapping("getFirstData")
    @ResponseBody
    public MessageResult<Map> getFirstData(HttpServletRequest request) {
        MessageResult<Map> msgResult = new MessageResult<>();
        HashMap<String, Object> map = new HashMap<>();
        try {


            String subject = (String) request.getAttribute("subject");
            User user = userService.get(subject);
            List<CompanyInfo> list = new ArrayList<>();

            Integer companyNum = 0;
            Integer personNum = 0;
            Integer normalDays = 0;
            Integer dayCheckNum = 0;
            Integer totalCheckNum = 0;
            Integer dayAbnormalNum = 0;
            Integer totalAbnormalNum = 0;
            if (userService.hasRole(subject, "SYSADMIN")) {
                list = companyInfoService.list();
                companyNum = list.size();
                personNum = Math.toIntExact(personInfoService.notGuestList());


                /*Integer normalDaysNum =  totalCheckCompanyService.findMinNormalDay();
                normalDays = normalDaysNum==null?0:normalDaysNum;*/
                Date abnormalTime = totalCheckCompanyService.findMaxAbnormalTimeByCompanyList(list);
                if (abnormalTime == null){
                    normalDays = totalCheckCompanyService.findMaxNormalDayByCompanyList(list);
                }
                else {
                    Long today = DateUtil.beginOfDay(new Date()).getTime();
                    Long abnormalDay = abnormalTime.getTime();
                    normalDays = Math.toIntExact((today - abnormalDay) / (1000 * 60 * 60 * 24)) -1;

                }



                Integer num = checkNumCompanyService.getDayCheckNum(DateUtil.format(new Date(), "yyyy-MM-dd"));
                Integer totalNum = totalCheckCompanyService.getTotalCheckNum(null);
                dayCheckNum = num == null ? 0 : num;
                totalCheckNum = totalNum == null ?dayCheckNum : totalNum+dayCheckNum;
                dayAbnormalNum = personDeviceFilterLogService.getDayAbnormalNum(DateUtil.beginOfDay(new Date()), new Date());
                totalAbnormalNum = totalCheckCompanyService.getTotalAbnormalNum() + dayAbnormalNum;
            } else  {
                CompanyInfo companyInfo = companyInfoService.get(user.getCompanyId());
                list = companyInfoService.findByCompanyCode(companyInfo.getCode() + "%", null);
                companyNum = list.size();
                personNum = Math.toIntExact(personInfoService.countByCompanyList(list));

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

           //     Integer normalDays = 0;
                Date totalCheckCompany = totalCheckCompanyService.findMaxAbnormalTimeByCompanyList(list);
                if (totalCheckCompany == null){
                    normalDays = totalCheckCompanyService.findMaxNormalDayByCompanyList(list);
                }
                else {
                    Long today = DateUtil.beginOfDay(new Date()).getTime();
                    Long abnormalDay = totalCheckCompany.getTime();
                    normalDays = Math.toIntExact((today - abnormalDay) / (1000 * 60 * 60 * 24)) -1;

                }



                Integer num = checkNumCompanyService.getDayCheckNumByCompanyList(DateUtil.format(new Date(), "yyyy-MM-dd"), list);
                Integer totalNum = totalCheckCompanyService.getTotalCheckNum(list);
                dayCheckNum = num == null ? 0 : num;
                totalCheckNum = totalNum == null ?dayCheckNum : totalNum+dayCheckNum;
                Integer num2 = personDeviceFilterLogService.getDayAbnormalNumByCompanyList(DateUtil.beginOfDay(new Date()), new Date(), list);
                Integer totalNum2 = totalCheckCompanyService.getTotalAbnormalNumByCompanyList(list);
                dayAbnormalNum = num2 == null ? 0 : num2;
                totalAbnormalNum = totalNum2 == null ? 0 : totalNum2 + dayAbnormalNum;


            }


            map.put("companyNum", companyNum);
            map.put("personNum", personNum);
            map.put("normalDays", normalDays<0?0:normalDays);
            map.put("dayCheckNum", dayCheckNum);
            map.put("totalCheckNum", totalCheckNum);
            map.put("dayAbnormalNum", dayAbnormalNum);
            map.put("totalAbnormalNum", totalAbnormalNum);


            msgResult.setData(map);
            msgResult.setResult(true);
            msgResult.setMessage("获取成功");


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            msgResult.setData(map);
            msgResult.setResult(false);
            msgResult.setMessage(e.getMessage());

        }
        return msgResult;
    }

    @ApiOperation(value = "获取首页第一个表格数据")
    @GetMapping("getFirstExcelData")
    @ResponseBody
    public MessageResult<Map> getFirstExcelData(HttpServletRequest request) {
        MessageResult<Map> msgResult = new MessageResult<>();
        HashMap<String, Object> map = new HashMap<>();
        try {


            String subject = (String) request.getAttribute("subject");
            User user = userService.get(subject);
            List<CompanyInfo> list = new ArrayList<>();
            List<String> dayList = new ArrayList<>();
            List<Integer> normalNumList = new ArrayList<>();
            List<Integer> abnormalNumList = new ArrayList<>();



            if (userService.hasRole(subject, "SYSADMIN")) {
                for (int i=30;i>0;i--){
                    Calendar   calendar   =   new GregorianCalendar();
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE,-i);
                    Date date  =calendar.getTime();
                    String day = DateUtil.format(date, "yyyy-MM-dd");
                    dayList.add(day);
                    Integer normalNum = checkNumCompanyService.getDayNormalNumByCompanyList(day,null);
                    Integer abnormalNum = checkNumCompanyService.getDayAbnormalNumByCompanyList(day,null);
                    normalNumList.add(normalNum==null?0:normalNum);
                    abnormalNumList.add(abnormalNum==null?0:abnormalNum);


                }


            } else  {
                CompanyInfo companyInfo = companyInfoService.get(user.getCompanyId());
                list = companyInfoService.findByCompanyCode(companyInfo.getCode() + "%", null);
                for (int i=30;i>0;i--) {
                    Calendar calendar = new GregorianCalendar();
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -i);
                    Date date = calendar.getTime();
                    String day = DateUtil.format(date, "yyyy-MM-dd");
                    dayList.add(day);
                    Integer normalNum = checkNumCompanyService.getDayNormalNumByCompanyList(day, list);
                    Integer abnormalNum = checkNumCompanyService.getDayAbnormalNumByCompanyList(day, list);
                    normalNumList.add(normalNum == null ? 0 : normalNum);
                    abnormalNumList.add(abnormalNum == null ? 0 : abnormalNum);
                }

            }


            map.put("day", dayList);
            map.put("normalNum", normalNumList);
            map.put("abnormalNum", abnormalNumList);



            msgResult.setData(map);
            msgResult.setResult(true);
            msgResult.setMessage("获取成功");


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            msgResult.setData(map);
            msgResult.setResult(false);
            msgResult.setMessage(e.getMessage());

        }
        return msgResult;
    }

}
