package com.casic.missiles.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.base.response.SuccessResponseData;
import com.casic.missiles.core.common.constant.factory.PageFactory;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.modular.system.constant.ModularDictConst;
import com.casic.missiles.modular.system.dto.DeviceDto;
import com.casic.missiles.modular.system.dto.DeviceTypeEnum;
import com.casic.missiles.modular.system.service.IDeviceService;
import com.sun.javafx.binding.StringFormatter;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/waterMeterData")
public class WaterMeterDataController extends ExportController {

    private static final Logger logger = LoggerFactory.getLogger(DeviceController.class);
    @Autowired
    private ICommonPermissionService permissionService;
    @Autowired
    private IDeviceService deviceService;

    @ApiOperation("获取水表设备列表")
    @RequestMapping(value = "/listPage")
    @ResponseBody
    public Object listPage(String keywords, String deptid, String areaId) {
        Page<DeviceDto> page = new PageFactory<DeviceDto>().defaultPage();
        //查询数据权限 使用默认DEPTID 字段
        DataScope dataScope = permissionService.getCurrUserDataScope();
        areaId += "%";
        List<DeviceDto> list = deviceService.selectDataScopePage(dataScope, page, "13", deptid, "", "", keywords, "", areaId);

        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat sdf4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(new Date());

        list.forEach(deviceDto -> {
            // 获取日月年用水量和最近的读数
            Map<String, Object> dayMeter = deviceService.selectWaterMeterDay(deviceDto.getDevcode(), date);
            deviceDto.setDayCount(dayMeter.get("FLOW_ACC_DATE").toString());

            Map<String, Object> monthMeter = deviceService.selectWaterMeterMonth(deviceDto.getDevcode(), date.substring(0, 7));
            deviceDto.setMonthCount(monthMeter.get("FLOW_ACC_MONTH").toString());

            Map<String, Object> totalMeter = deviceService.selectWaterMeterTotal(deviceDto.getDevcode(), date.substring(0, 4));
            deviceDto.setTotalCount(totalMeter.get("FLOW_ACC_TOTAL").toString());

            Map<String, Object> meterRec = deviceService.selectLatestWaterMeter(deviceDto.getDevcode());
            deviceDto.setWatchNum(meterRec.get("FLOW_ACC").toString());
            deviceDto.setUptime(sdf4.format((Date) meterRec.get("UPTIME")));

            deviceDto.setAreaFullName(deviceService.getAreaFullNameById(deviceDto.getArea()));
        });
        page.setRecords(list);
        return new SuccessResponseData(super.packForBT(page));
    }
}
