package com.mvc.controller.sldh;

import com.mvc.dao.TempSldhStationsMapper;
import com.mvc.entity.StationAcc;
import com.mvc.entity.TempSldhStations;
import com.mvc.model.ResPageModel;
import com.mvc.model.ResultGeekerModel;
import com.mvc.model.StationAccModel;
import com.mvc.model.TAireadModel;
import com.mvc.model.sldh.SLDHReadingModel;
import com.mvc.model.sldh.SLDHTreeModel;
import com.mvc.othermodel.GroupAndStationTree;
import com.mvc.service.copy.TAireadService;
import com.mvc.service.taizhang.StationAccService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = "水利大会专用接口")
@RequestMapping("/WaterProjectController")
@Controller
public class WaterProjectController {
    @Resource
    private StationAccService stationAccService;

    @Resource
    private TAireadService aireadService;

    @Resource
    private TempSldhStationsMapper tempSldhStationsMapper;

    @ApiOperation("根据单位代码获取树结构")
    @RequestMapping(value = "/findStationTreeByUnitCode/{unitCode}", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel findStationTreeByUnitCode(@PathVariable("unitCode") String unitCode) {
        ResultGeekerModel resultModel;
        try {
            List<SLDHTreeModel> treeModels = new ArrayList<>();
            //List<GroupAndStationTree> treeList = stationAccService.findStationTreeByUnitCode(unitCode);
            List<TempSldhStations> tempSldhStations = tempSldhStationsMapper.selectByExample(null);
            List<StationAcc> stationAccs = new ArrayList<>();
            for (TempSldhStations tempSldhStation : tempSldhStations) {
                StationAccModel accModel = stationAccService.findStationAccByStationCode(tempSldhStation.getStationCode());
                if (accModel != null) {
                    StationAcc acc = new StationAcc();
                    BeanUtils.copyProperties(accModel, acc);
                    stationAccs.add(acc);
                }
            }

            List<GroupAndStationTree> treeList = stationAccService.findStationTreeByStationAccList(stationAccs);
            SLDHTreeModel tempModel = new SLDHTreeModel();
            for (GroupAndStationTree tree : treeList) {
                SLDHTreeModel model = new SLDHTreeModel(tree);
                if (model.getType().equals("0")) {
                    tempModel = model;
                    treeModels.add(model);
                } else {
                    if (tree.getParentcode().equals(tempModel.getKey())) {
                        tempModel.getChildren().add(model);
                    }
                }
            }
            resultModel = new ResultGeekerModel(treeModels);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("根据单位代码获取最新读数列表")
    @RequestMapping(value = "/findLatestReadingsByUnitCode/{unitCode}", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel findLatestReadingsByUnitCode(@PathVariable("unitCode") String unitCode) {
        ResultGeekerModel resultModel;
        try {
            List<SLDHReadingModel> readingModels = new ArrayList<>();
            Map<String, Object> map = aireadService.findLatestDataByStationCodes(1, unitCode, false, 1, 100);
            getReadingsByAireadMap(readingModels, map);
            resultModel = new ResultGeekerModel(readingModels);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("根据站点编码和分页获取读数列表")
    @RequestMapping(value = "/findReadingsByStationCodeAndPage/{stationCode}/{pageNum}/{pageSize}", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel findReadingsByStationCodeAndPage(
            @PathVariable("stationCode") String stationCode,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        ResultGeekerModel resultModel;
        try {
            List<SLDHReadingModel> readingModels = new ArrayList<>();
            TAireadModel queryModel = new TAireadModel();
            queryModel.setStationCode(stationCode);
            queryModel.setPage(pageNum);
            queryModel.setRows(pageSize);

            Map<String, Object> map = aireadService.findAllTAireadByCondition(queryModel);
            getReadingsByAireadMap(readingModels, map);
            long total = (long) map.get("total");
            ResPageModel pageModel = new ResPageModel(readingModels, pageNum, pageSize, total);
            resultModel = new ResultGeekerModel(pageModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    /**
     * 根据aireadMap获取SLDHReadingModel列表
     *
     * @param readingModels
     * @param map
     */
    private void getReadingsByAireadMap(List<SLDHReadingModel> readingModels, Map<String, Object> map) {
        if (map == null || map.isEmpty() || !map.containsKey("list")) {
            return;
        }
        List<TAireadModel> list = (List<TAireadModel>) map.get("list");
        for (TAireadModel model : list) {
            SLDHReadingModel readingModel = new SLDHReadingModel(model);
            readingModels.add(readingModel);
        }
    }
}
