package com.pqs9200.controller.advanced;

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.balance.BalanceInfo;
import com.njcn.pojo.balance.BalanceInfo.PointInfo;
import com.njcn.pojo.commons.AreaGeneralData;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.user.Depts;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.serviceimpl.commons.WaveServiceImpl;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.*;
import com.njcn.utils.GetBalance.Sarifi;
import com.pqs9200.mapper.weeklyreport.GetAreaPointInfoMapper;
import com.pqs9200.serviceimpl.weeklyreport.WeeklyReportServiceImpl;
import com.pqs9200.serviceimpl.weeklyreport.WeeklyReportServiceImpl.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@RequestMapping("balance")
@RestController
public class BalanceController {
    @Autowired
    private GetAreaPointInfoMapper getAreaPointInfoMapper;
    @Autowired
    private WeeklyReportServiceImpl weeklyReportServiceImpl;
    @Autowired
    private GetBalance getBalance;
    @Autowired
    private WaveServiceImpl waveServiceImpl;
    @Autowired
    private UserUtil userUtil;
    @Autowired
    private UserLogDetailService userLog;

    @Autowired
    private AppConfig appConfig;

    @PostMapping("getBalanceInfo")
    public HttpResult getBalanceInfo(String startTime, String endTime, String area, HttpServletRequest request) {
        if (area.equals("overview")) {
            area = userUtil.getDeptsIndex(TokenManager.getToken().getUserIndex());
        }

        List<AreaGeneralData> deptdLists = userUtil.getAreaDataByInfo(area);
        List<BalanceInfo> list = new ArrayList<>();

        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "getBalanceInfo");
        if (!datePojo.getValidity()) {
            return PubUtils.assignmentResult(null, 500, "解析前台传递的时间有误");
        }

        try {
            for (AreaGeneralData depts : deptdLists) {
                BalanceInfo balanceInfo = new BalanceInfo();
                balanceInfo.setAreaName(depts.getName());
                balanceInfo.setAreaIndex(depts.getDeptIndex());
                List<Depts> deptsList = userUtil.getDeptsByNode(depts.getDeptIndex());
                List<String> list1 = new ArrayList<>();
                list1.add(depts.getDeptIndex());

                for (Depts depts1 : deptsList) {
                    list1.add(depts1.getDeptsIndex());
                }

                List<Info> listLineIndex = getAreaPointInfoMapper.getLineInfo(list1, appConfig.getSysTypeZt()); // 获取监测点ID信息
                List<PointInfo> list2 = new ArrayList<>();

                for (Info info : listLineIndex) {
                    PointInfo pointInfo = balanceInfo.new PointInfo();
                    Float value = getBalance.getSarfiValue(datePojo.getStartTime(), datePojo.getEndTime(),
                            info.getId().intValue(), 0.9f); // 计算平均值
                    pointInfo.getQtIdx().r_esm = (value == null ? 0f : value.floatValue());
                    pointInfo.getQtIdx().sarfi_90 = getBalance
                            .getSarfiCount(datePojo.getStartTime(), datePojo.getEndTime(), info.getId(), 0.9f)
                            .intValue(); // 统计小于0.9的总数
                    pointInfo.getQtIdx().sarifi_50 = getBalance
                            .getSarfiCount(datePojo.getStartTime(), datePojo.getEndTime(), info.getId(), 0.5f)
                            .intValue(); // 统计小于0.5的总数

                    List<Sarifi> listSarifi = getBalance.getSarifiList(datePojo.getStartTime(), datePojo.getEndTime(),
                            info.getId(), 0.9f);

                    if (listSarifi.size() > 0) {
                        float floatYzd = 0f;
                        float floatNl = 0f;

                        for (Sarifi sarifi : listSarifi) {
                            floatYzd += Float.parseFloat(waveServiceImpl.getYzd(sarifi.getTime().floatValue(),
                                    sarifi.getSarifiValue().floatValue()));// 调用接口计算严重程度
                            floatNl += BalanceUtil.calculateNl(sarifi.getTime().floatValue() / 1000,
                                    sarifi.getSarifiValue().floatValue()); // 调用接口计算暂降能量
                        }

                        pointInfo.getQtIdx().r_assi = floatYzd / listSarifi.size();
                        pointInfo.getQtIdx().r_asei = floatNl / listSarifi.size();
                    } else {
                        pointInfo.getQtIdx().r_assi = 0f;
                        pointInfo.getQtIdx().r_asei = 0f;
                    }

                    pointInfo.setPointName(info.getName());
                    pointInfo.setPointId(info.getId());
                    list2.add(pointInfo);
                }

                balanceInfo.setList(list2);
                list.add(balanceInfo);
            }

            // 打包数据传入dll计算结果
            getBalance.translateData(list);
        } catch (Exception e) {
            userLog.getLog("区域暂降评估", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            return PubUtils.assignmentResult(list, 500, "计算失败");
        }

        userLog.getLog("区域暂降评估", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        return PubUtils.assignmentResult(list, 200, "计算成功");
    }
}
