package com.djhk.uav.equ.service.impl;


import com.djhk.common.constant.SecurityConstants;
import com.djhk.common.core.domain.entity.SysUser;
import com.djhk.common.utils.SecurityUtils;
import com.djhk.system.service.ISysUserService;
import com.djhk.uav.equ.mapper.StatMapper;
import com.djhk.uav.equ.service.StatService;
import com.djhk.uav.uav.service.IUavOperatorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 统计分析
 *
 * @author gzm
 * @since 1.0.0 2023-06-25
 */
@Service
@Slf4j
public class StatServiceImpl implements StatService {

    @Autowired
    private StatMapper statMapper;

//    @Autowired
//    private RemoteUserService userService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IUavOperatorService uavOperatorService;


    /*
    * 飞行统计按照机构统计飞行次数、飞行时长、飞行里程数据
    * */
/*    @Override
    public List<Map> getStatForDept(Map map) {
        System.out.println("=========================== MY DEPT SQL IS STARTING 1 ==========================");
        List<Map> statForDept = new ArrayList<>();
        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
        Long deptId = sysUser.getDeptId();
        String sqlStr = "  AND (f.dept_id = '"+deptId+"' OR f.dept_id IN ( SELECT p.dept_id FROM sys_dept p WHERE\n" +
                "              p.del_flag = 0 and find_in_set('"+deptId+"', ancestors) )) ";
        List dateRange = (ArrayList)map.get("dateRange");
        if (dateRange!=null && dateRange.size()>0){
            sqlStr = sqlStr + " and f.beg_time BETWEEN '"+dateRange.get(0)+"' and '"+dateRange.get(1)+"'";
        }
        sqlStr += "";
        log.info("============ this sql is =================:",sqlStr);
        List<Map> statForDept1 = statMapper.getStatForDept(sqlStr);
        if (statForDept1.size() <1){
            System.out.println("=========================== MY DEPT SQL IS STARTING 2.1 ==========================");
            return statForDept1;
        }else {
            for (Map map1 : statForDept1) {
                System.out.println("=========================== MY DEPT SQL IS STARTING 2 ==========================");
                // 人员数量
                List<Map> userTotal = statMapper.getUserTotal((Long) map1.get("dept_id"));
                if (userTotal.size() <1){
                    map1.put("userTotal",0);
                }else {
                    Object userTotal1 = userTotal.get(0).get("userTotal");
                    map1.put("userTotal",userTotal1);
                }
                System.out.println("=========================== MY DEPT SQL IS STARTING 3 ==========================");
                // 查询反制设备数量
                List<Map> equInfoTotal = statMapper.getEquInfo((Long) map1.get("dept_id"));
                if (userTotal.size() <1){
                    map1.put("equInfoTotal",0);
                }else {
                    Object equInfoTotal1 = equInfoTotal.get(0).get("equTotal");
                    map1.put("equInfoTotal",equInfoTotal1);
                }
                System.out.println("=========================== MY DEPT SQL IS STARTING 4 ==========================");
                // 其他设备数
                List<Map> otherTotal = statMapper.getOtherTotal((Long) map1.get("dept_id"));
                if (otherTotal.size() <1){
                    map1.put("otherTotal",0);
                }else {
                    Object otherTotal1 = otherTotal.get(0).get("otherTotal");
                    map1.put("otherTotal",otherTotal1);
                }

                System.out.println("=========================== MY DEPT SQL IS STARTING 5 ==========================");
                // 无人机数
                List<Map> flightTotal = statMapper.getFlightTotal((Long) map1.get("dept_id"));
                if (flightTotal.size() <1){
                    map1.put("flightTotal",0);
                }else {
                    Object flightTotal1 = flightTotal.get(0).get("flightTotal");
                    map1.put("flightTotal",flightTotal1);
                }
                System.out.println("=========================== MY DEPT SQL IS STARTING 6 ==========================");
                statForDept.add(map1);
            }
            log.info("返回的设备数据及飞行统计数据为:{}",statForDept);
            System.out.println("返回的设备数据及飞行统计数据为:{}"+statForDept);
            return statForDept;
        }
    }*/


    /*
     * 飞行统计按照机构统计飞行次数、飞行时长、飞行里程数据
     * */
//    @Override
//    public List<Map> getStatForDept(Map map) {
//        List<Map> statForDept = new ArrayList<>();
//        HashMap<Object, Object> map1 = new HashMap<>();
//        map1.put("flightTotal",129);
//        map1.put("otherTotal",1);
//        map1.put("equInfoTotal",34);
//        map1.put("userTotal",126);
//        map1.put("dept_name","重庆市公安局");
//        statForDept.add(map1);
//        return statForDept;
//
//    }


    @Override
    public List<Map> getStatForDept(Map map) {
        System.out.println("=========================== MY DEPT SQL IS STARTING 1 ==========================");
        List<Map> statForDept = new ArrayList<>();
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();

        List<Map> statForDept1 = statMapper.getStatForDept(deptId);
        if (statForDept1.size() <1){
            System.out.println("=========================== MY DEPT SQL IS STARTING 2.1 ==========================");
            return statForDept1;
        }else {
            for (Map map1 : statForDept1) {
                System.out.println("=========================== MY DEPT SQL IS STARTING 2 ==========================");
                // 人员数量
                List<Map> userTotal = statMapper.getUserTotal((Long) map1.get("dept_id"));
                if (userTotal.size() <1){
                    map1.put("userTotal",0);
                }else {
                    Object userTotal1 = userTotal.get(0).get("userTotal");
                    map1.put("userTotal",userTotal1);
                }
                System.out.println("=========================== MY DEPT SQL IS STARTING 3 ==========================");
                // 查询反制设备数量
                List<Map> equInfoTotal = statMapper.getEquInfo((Long) map1.get("dept_id"));
                if (userTotal.size() <1){
                    map1.put("equInfoTotal",0);
                }else {
                    Object equInfoTotal1 = equInfoTotal.get(0).get("equTotal");
                    map1.put("equInfoTotal",equInfoTotal1);
                }
                System.out.println("=========================== MY DEPT SQL IS STARTING 4 ==========================");
                // 其他设备数
                List<Map> otherTotal = statMapper.getOtherTotal((Long) map1.get("dept_id"));
                if (otherTotal.size() <1){
                    map1.put("otherTotal",0);
                }else {
                    Object otherTotal1 = otherTotal.get(0).get("otherTotal");
                    map1.put("otherTotal",otherTotal1);
                }

                System.out.println("=========================== MY DEPT SQL IS STARTING 5 ==========================");
                // 无人机数
                List<Map> flightTotal = statMapper.getFlightTotal((Long) map1.get("dept_id"));
                if (flightTotal.size() <1){
                    map1.put("flightTotal",0);
                }else {
                    Object flightTotal1 = flightTotal.get(0).get("flightTotal");
                    map1.put("flightTotal",flightTotal1);
                }
                System.out.println("=========================== MY DEPT SQL IS STARTING 6 ==========================");
                statForDept.add(map1);
            }
            log.info("返回的设备数据及飞行统计数据为:{}",statForDept);
            System.out.println("返回的设备数据及飞行统计数据为:{}"+statForDept);
            return statForDept;
        }
    }



    /*
     * 飞行统计按照类别统计飞行次数、飞行时长、飞行里程数据
     * */
    @Override
    public List<Map> getStatForType(Map map) {
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
        String sqlStr = "  AND (t.dept_id = '"+deptId+"' OR t.dept_id IN ( SELECT p.dept_id FROM sys_dept p WHERE\n" +
                "             p.del_flag = 0 and    find_in_set('"+deptId+"', ancestors) )) ";
        List dateRange = (ArrayList)map.get("dateRange");
        if (dateRange!=null && dateRange.size()>0){
            sqlStr = sqlStr + " and t.beg_time BETWEEN '"+dateRange.get(0)+"' and '"+dateRange.get(1)+"'";
        }
        return statMapper.getStatForType(sqlStr);
    }

    /*
     * 飞行统计按照型号统计飞行次数、飞行时长、飞行里程数据
     * */
    @Override
    public List<Map> getStatForModel(Map map) {
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
        String sqlStr = "  AND (t.dept_id = '"+deptId+"' OR t.dept_id IN ( SELECT p.dept_id FROM sys_dept p WHERE\n" +
                "            p.del_flag = 0 and     find_in_set('"+deptId+"', ancestors) )) ";
        List dateRange = (ArrayList)map.get("dateRange");
        if (dateRange!=null && dateRange.size()>0){
            sqlStr = sqlStr + " and t.beg_time BETWEEN '"+dateRange.get(0)+"' and '"+dateRange.get(1)+"'";
        }
        return statMapper.getStatForModel(sqlStr);
    }

    /*
     * 飞行统计按照设备统计飞行次数、飞行时长、飞行里程数据
     * */
    @Override
    public List<Map> getStatForEqu(Map map) {
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
        String sqlStr = "  AND (t.dept_id = '"+deptId+"' OR t.dept_id IN ( SELECT p.dept_id FROM sys_dept p WHERE\n" +
                "           p.del_flag = 0 and find_in_set('"+deptId+"', ancestors) )) ";
        List dateRange = (ArrayList)map.get("dateRange");
        if (dateRange!=null && dateRange.size()>0){
            sqlStr =sqlStr +  " and t.beg_time BETWEEN '"+dateRange.get(0)+"' and '"+dateRange.get(1)+"'";
        }
        return statMapper.getStatForEqu(sqlStr);
    }
}
