package cn.cecep.talroad.controller.analysis;


import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.domain.PcOutlet;
import cn.cecep.talroad.domain.analysis.SRaOverWaterPollutionTraceability;
import cn.cecep.talroad.domain.dto.MapOutlet24HourDataDTO;
import cn.cecep.talroad.service.IPcOutletService;
import cn.cecep.talroad.service.ISEnvWaterMonRealService;
import cn.cecep.talroad.service.analysis.SRaOverWaterPollutionTraceabilityService;
import cn.cecep.talroad.vo.EChartsVO;
import cn.cecep.talroad.vo.SEnvWaterMonRealVo;
import cn.cecep.talroad.vo.analysis.WaterPollutionTraceabilityVo;
import cn.cecep.talroad.vo.query.SEnvWaterMonRealQuery;
import cn.cecep.talroad.vo.query.analysis.SRaOverWaterPollutionTraceabilityQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 当前使用原始表数据   outletService  这个
 */
@RestController
@RequestMapping("/waterPollutionTraceability")
@Api("水污染溯源信息")
public class SRaOverWaterPollutionTraceabilityController {

    @Autowired
    private SRaOverWaterPollutionTraceabilityService service;
    @Autowired
    private IPcOutletService outletService;
    @Autowired
    private ISEnvWaterMonRealService waterMonRealService;


    @GetMapping ("/getList")
    @ApiOperation("获取列表")
    public AjaxResults<IPage<SRaOverWaterPollutionTraceability>> getList(SRaOverWaterPollutionTraceabilityQuery query){
        Page<SRaOverWaterPollutionTraceability> page = new Page<>(query.getPageNum(), query.getPageSize());
        IPage<SRaOverWaterPollutionTraceability> list =
                service.page(page,
                        SRaOverWaterPollutionTraceability.createLambdaQueryWrapper(query));
        return AjaxResults.success( list);
    }
    @GetMapping ("/getListRaw")
    @ApiOperation("获取列表---原始表数据")
    public AjaxResults<IPage<SRaOverWaterPollutionTraceability>> getList1(SRaOverWaterPollutionTraceabilityQuery query){
        Page<PcOutlet> page = new Page<>(query.getPageNum(), query.getPageSize());
        IPage<SRaOverWaterPollutionTraceability> list =
                outletService.pageAndFactory(page,
                       query);
        return AjaxResults.success( list);
    }
    /**
     * 导出区域传输率分析列表
     */
    @ApiOperation(value = "导出水污染溯源列表")
    @Log(title = "导出水污染溯源列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SRaOverWaterPollutionTraceabilityQuery query) {
        List<SRaOverWaterPollutionTraceability> list = service.list(SRaOverWaterPollutionTraceability.createLambdaQueryWrapper(query));
        ExcelUtil<SRaOverWaterPollutionTraceability> util = new ExcelUtil<>(SRaOverWaterPollutionTraceability.class);
        util.exportExcel(response, list, "水污染溯源列表信息");
    }
    @ApiOperation(value = "导出水污染溯源列表--原始表数据")
    @Log(title = "导出水污染溯源列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportRaw")
    public void export1(HttpServletResponse response, @RequestBody SRaOverWaterPollutionTraceabilityQuery query) {
        IPage<SRaOverWaterPollutionTraceability> page= outletService.pageAndFactory(new Page<>(0,999999),
                query);
        List<SRaOverWaterPollutionTraceability> list = page.getRecords();
        ExcelUtil<SRaOverWaterPollutionTraceability> util = new ExcelUtil<>(SRaOverWaterPollutionTraceability.class);
        util.exportExcel(response, list, "水污染溯源列表信息");
    }
    @GetMapping ("/getMapList")
    @ApiOperation("获取地图列表")
    public AjaxResults<Map<String,Object>> getMapList(SRaOverWaterPollutionTraceabilityQuery query){
        List<SRaOverWaterPollutionTraceability> list = service.list(SRaOverWaterPollutionTraceability.createLambdaQueryWrapper(query));
        return AjaxResults.success(service.workWithMapData(list));
    }
    @GetMapping ("/getMapListRaw")
    @ApiOperation("获取地图列表--原始表数据")
    public AjaxResults<Map<String,Object>> getMapListRaw(SRaOverWaterPollutionTraceabilityQuery query){
        IPage<SRaOverWaterPollutionTraceability> page= outletService.pageAndFactory(new Page<>(query.getPageNum(), query.getPageSize()),
                query);
        List<SRaOverWaterPollutionTraceability> list = page.getRecords();
        return AjaxResults.success(service.workWithMapData(list));
    }
    @GetMapping ("/getMapInfoRaw")
    @ApiOperation("获取地图点位信息--原始表数据")
    public AjaxResults<SRaOverWaterPollutionTraceability> getMapInfoRaw(WaterPollutionTraceabilityVo query){
        Page<PcOutlet> page = new Page<>(1, 1);
        //排口数据
        if(query.getType().equals("1")||"2".equals(query.getType())){
            SRaOverWaterPollutionTraceabilityQuery st=new SRaOverWaterPollutionTraceabilityQuery();
            st.setId(query.getId());
            IPage<SRaOverWaterPollutionTraceability> result = outletService.pageAndFactory(page,st);
            return AjaxResults.success(result.getRecords().get(0));
            //污水处理厂数据
        } else if ("3".equals(query.getType())) {
            SRaOverWaterPollutionTraceabilityQuery st=new SRaOverWaterPollutionTraceabilityQuery();
            st.setId(query.getId());
//            st.setEstuaryCode(query.getCode());
            st.setReceivingSewagePlantName(query.getName());
            IPage<SRaOverWaterPollutionTraceability> result = outletService.pageAndFactory(page,st);
            return AjaxResults.success(result.getRecords().get(0));
            //入河口数据
        }else if ("4".equals(query.getType())) {
            SRaOverWaterPollutionTraceabilityQuery st=new SRaOverWaterPollutionTraceabilityQuery();
            st.setId(query.getId());
            st.setReceivingSewagePlantName(query.getName());
            if(StringUtils.isEmpty(query.getName())){
                return AjaxResults.error("点位没有入河口名称");
            }
            IPage<SRaOverWaterPollutionTraceability> result =  outletService.pageAndFactory(page,st);
            return AjaxResults.success(result.getRecords().get(0));
        }
        return AjaxResults.success();
    }
    @GetMapping ("/getMapEchartsRaw")
    @ApiOperation("获取地图点位信息echarts--原始表数据")
    public  AjaxResults<EChartsVO> getMapInfoRaw(String  monitoringPointName,String pollName){
        SEnvWaterMonRealQuery sEnvWaterMonReal=new SEnvWaterMonRealQuery();
        sEnvWaterMonReal.setMonitoringPointName(monitoringPointName);
        sEnvWaterMonReal.setPageNum(1);
        sEnvWaterMonReal.setPageSize(99);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH");
        String end = format.format(new Date(System.currentTimeMillis()- 60L *60*1000*24*40));
        String start = format.format(new Date(System.currentTimeMillis()- 60L *60*1000*24*45));
        sEnvWaterMonReal.setDataType("H");
        sEnvWaterMonReal.setMonitoringEndTime(end);
        sEnvWaterMonReal.setMonitoringStartTime(start);
        IPage<SEnvWaterMonRealVo> sEnvWaterMonRealVoIPage = waterMonRealService.selectSEnvWaterMonRealPage(sEnvWaterMonReal);
        EChartsVO eChartsVO = service.chartData(sEnvWaterMonRealVoIPage.getRecords(),pollName,new Date(System.currentTimeMillis()- 60L *60*1000*24*45),new Date());
        return AjaxResults.success(eChartsVO);
    }


    /**
     * 获取24小时浓度趋势
     * @param mapOutlet24HourDataDTO
     * @return
     */
    @PostMapping("/getOutlet24HourData")
    @ApiOperation(value = "获取地图排口的24小时浓度趋势")
    public AjaxResults<List<EChartsVO>> getOutlet24HourData(@RequestBody MapOutlet24HourDataDTO mapOutlet24HourDataDTO){
        SEnvWaterMonRealQuery sEnvWaterMonRealQuery = new SEnvWaterMonRealQuery();
        BeanUtils.copyProperties(mapOutlet24HourDataDTO,sEnvWaterMonRealQuery);
        if (mapOutlet24HourDataDTO.getPollutantType() == 1) {
            sEnvWaterMonRealQuery.setCod("cod");
        }else{
            sEnvWaterMonRealQuery.setNitrogenConcentration("nitrogenConcentration");
        }
        List<EChartsVO> charDataList = waterMonRealService.getCharDataList(sEnvWaterMonRealQuery);
        if(charDataList.size()>0){
            return AjaxResults.success(charDataList);
        }
        return AjaxResults.success("查询成功",null);
    }
}
