package com.casic.missiles.modular.system.service.impl;

import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.core.util.ToolUtil;
import com.casic.missiles.modular.system.dao.AlarmNowViewMapper;
import com.casic.missiles.modular.system.dao.StatisticMapper;
import com.casic.missiles.modular.system.dao.SysDictMapper;
import com.casic.missiles.modular.system.model.Dept;
import com.casic.missiles.modular.system.service.IStatisticsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class StatisticServiceImpl implements IStatisticsService {
    @Autowired
    private StatisticMapper statisticMapper;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private AlarmNowViewMapper alarmNowViewMapper;
    @Autowired
    private ICommonPermissionService permissionService;

    @Override
    public List<Map<String, Object>> wellStaticsByDept(DataScope dataScope) {
        List<Map<String, Object>> mapList = statisticMapper.wellStaticsByDept(dataScope);
        for (Map<String, Object> map : mapList) {
            map.put("pidName",permissionService.getDeptName(Long.valueOf(map.get("pid").toString())));
        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> wellStaticsByType(DataScope dataScope) {
        String deptIds = StringUtils.strip(dataScope.getDeptIds().toString(),"[]");
        List<Map<String, Object>> mapList = statisticMapper.wellStaticsByType(deptIds);
        for (Map<String, Object> map : mapList) {
            map.put("wellTypeName",permissionService.getDictNameByCode("sluicewellType", map.get("wellType").toString()));
        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> deviceStaticsByType(DataScope dataScope) {
        String deptIds = StringUtils.strip(dataScope.getDeptIds().toString(),"[]");
        List<Map<String, Object>> mapList = statisticMapper.deviceStaticsByType(deptIds);
//        for (Map<String, Object> map : mapList) {
//            map.replace("deviceType",sysDictMapper.getDeviceTypeNameById(map.get("deviceType").toString()));
//        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> deviceStaticsByDept(DataScope dataScope,String deviceType) {
        List<Map<String, Object>> mapList = statisticMapper.deviceStaticsByDept(dataScope,deviceType);
        for (Map<String, Object> map : mapList) {
            map.put("deptName",permissionService.getDeptName(Long.valueOf(map.get("deptid").toString())));
        }
        return mapList;
    }

    @Override
    public Map<String, Object> deviceStaticsByStatus(DataScope dataScope) {
        String deptIds = StringUtils.strip(dataScope.getDeptIds().toString(),"[]");
        Map<String, Object> map = statisticMapper.deviceStaticsByStatus(deptIds);
        for(String s:map.keySet()){ //value都转为string
            map.replace(s,map.get(s).toString());
        }
        return map;
    }

    @Override
    public List<Map<String, Object>> jobsByDept(DataScope dataScope,String beginTime, String endTime, String deviceType) throws ParseException {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<Long> deptIds = dataScope.getDeptIds();
        for (Long deptId : deptIds) {
            if(deptId==0){ // 顶级不统计
                continue;
            }
            Dept dept = permissionService.getDeptById(deptId);
            if( ToolUtil.isEmpty(dept) || "04".equals(dept.getDeptType())){ // 小组不统计
                continue;
            }
            Map<String, Object> map = new HashMap<>();
            List<Long> subDeptIds = permissionService.getSubDepts(deptId,null);
            subDeptIds.add(deptId);
            String subDeptIdstr = StringUtils.strip(subDeptIds.toString(),"[]");
            map.put("deptId", deptId);
            map.put("deptName",permissionService.getDeptName(deptId));
            Map<String, Object> countMap = statisticMapper.jobsByDeptId(subDeptIdstr,beginTime,endTime);
            if(ToolUtil.isNotEmpty(countMap)){
                map.putAll(countMap);
            }else{
                map.put("beforeGet",0);
                map.put("beforeConfirm",0);
                map.put("inHandle",0);
                map.put("over",0);
                map.put("cancel",0);
            }
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> alarmsByDay(String beginTime, String endTime, DataScope dataScope) {
        String deptIds = StringUtils.strip(dataScope.getDeptIds().toString(),"[]");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
        List<Map<String, Object>> mapList = new ArrayList<>();
        try{
            Date beginDate = sdf1.parse(beginTime);
            Date endDate = sdf1.parse(endTime);
            Calendar date = Calendar.getInstance();
            date.setTime(beginDate);
            while (date.getTime().compareTo(endDate) <= 0) {
                String tmp = sdf1.format(date.getTime());
                Map<String, Object> map = alarmNowViewMapper.countAlarmsByDay(tmp,deptIds);
                map.put("date",sdf2.format(date.getTime()));
                mapList.add(map);
                date.add(Calendar.DAY_OF_MONTH,1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return mapList;
    }
}
