package com.mvc.controller.taizhang;

import com.mvc.model.*;
import com.mvc.othermodel.GroupAndStationTree;
import com.mvc.service.copy.TAireadService;
import com.mvc.service.taizhang.DeviceAccService;
import com.mvc.service.taizhang.StationAccService;
import com.mvc.service.taizhang.TaskRemoteCfgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "遥测站点接口")
@RequestMapping("/StationAccAction")
@Controller
public class StationAccAction {
    @Resource
    private StationAccService stationAccService;

    @ApiOperation("分页查询遥测站")
    @RequestMapping(value = "/list/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllDeviceAcc(@PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            Map<String, Object> res = stationAccService.findAllStationAcc(page, rows);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("按条件查询遥测站")
    @RequestMapping(value = "/findByCondition", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findByCondition(@RequestBody StationAccModel model) {
        try {
            Map<String, Object> res = stationAccService.findByCondition(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("新增遥测站")
    @RequestMapping(value = "/saveStationAcc", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveStationAcc(@RequestBody StationAccModel model) {
        try {
            Map<String, Object> map = stationAccService.saveStationAcc(model);
            if (map.get("error") != null) {
                String errorinfo = (String) map.get("error");
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", errorinfo);
                return resMap;
            } else {
                StationAccModel resmodel = (StationAccModel) map.get("res");
                Map<String, Object> resMap = new CorrectMap();
                resMap.put("res", resmodel);
                return resMap;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("更新遥测站")
    @RequestMapping(value = "/updateStationAcc", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateDeviceAcc(@RequestBody StationAccModel model) {
        try {
            Map<String, Object> map = stationAccService.updateStationAcc(model);
            if (map.get("error") != null) {
                String errorinfo = (String) map.get("error");
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", errorinfo);
                return resMap;
            } else {
                StationAccModel resmodel = (StationAccModel) map.get("res");
                Map<String, Object> resMap = new CorrectMap();
                resMap.put("res", resmodel);
                return resMap;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("删除遥测站")
    @RequestMapping(value = "/deleteStationAcc/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteStationAcc(@PathVariable("id") int id) {
        try {
            int res = stationAccService.deleteStationAcc(id);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据user id 获取遥测站树")
    @RequestMapping(value = "/findStationTreeByUserId/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findStationTreeByUserId(@PathVariable("userId") String userId) {
        try {
            List<GroupAndStationTree> res = stationAccService.findStationTreeByUserId(userId);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据CustomUser获取遥测站树")
    @RequestMapping(value = "/findStationTreeByUserModel", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findStationTreeByUserModel(@RequestBody CustomUserModel model) {
        try {
            List<GroupAndStationTree> res = stationAccService.findStationTreeByUserModel(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据unitCode获取遥测站树")
    @RequestMapping(value = "/findStationTreeByUnitCode/{unitCode}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findStationTreeByUnitCode(@PathVariable("unitCode") String unitCode) {
        try {
            List<GroupAndStationTree> res = stationAccService.findStationTreeByUnitCode(unitCode);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("获取全部遥测站树-内部用户使用")
    @RequestMapping(value = "/findAllStationTree", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllStationTree() {
        try {
            List<GroupAndStationTree> res = stationAccService.findAllStationTree();
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("批量更新VisUnitCodes")
    @RequestMapping(value = "/updateVisUnitCodes", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateVisUnitCodes(@RequestBody StationAccUpdataModel model) {
        try {
            int res = stationAccService.updateVisUnitCodes(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("判断当前选的站点归属是否相同")
    @RequestMapping(value = "/checkSameVisUnitCodes", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> checkSameVisUnitCodes(@RequestBody StationAccUpdataModel model) {
        try {
            List<String> res = stationAccService.checkSameVisUnitCodes(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据站代码获取全部可查看的用水户")
    @RequestMapping(value = "/findTAffiliatedunitsByUnitCode/{unitCode}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findTAffiliatedunitsByUnitCode(@PathVariable("unitCode") String unitCode) {
        try {
            List<TAffiliatedunitModel> res = stationAccService.findTAffiliatedunitsByUnitCode(unitCode);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据用水户站代码获取下属的所有站点-含最新读数(小程序添加设备类型前临时过渡)")
    @RequestMapping(value = "/findStationAccsByUnitCode/{unitCode}/{deviceType}/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findStationAccsByUnitCode(@PathVariable("unitCode") String unitCode, @PathVariable("deviceType") String deviceType,
                                                         @PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            Map<String, Object> res = stationAccService.findStationAccsByUnitCode(unitCode, deviceType, false, page, rows);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据用水户站代码获取下属的所有站点-含最新读数")
    @RequestMapping(value = "/findStationAccsByUnitCode/{unitCode}/{sampleTime}/{deviceType}/{page}/{rows}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findStationAccsByUnitCode(@PathVariable("unitCode") String unitCode, @PathVariable("deviceType") String deviceType,
                                                         @PathVariable("sampleTime") String sampleTime,
                                                         @PathVariable("page") Integer page, @PathVariable("rows") Integer rows) {
        try {
            Map<String, Object> res = stationAccService.findStationAccsByUnitCode(unitCode, deviceType, true, page, rows);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据站代码查找该单位下的用水量")
    @RequestMapping(value = "/findYslByStationCodeAndTime/{unitCode}/{time}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findYslByStationCodeAndTime(@PathVariable("unitCode") String unitCode,
                                                           @PathVariable("time") String time) {
        try {
            List<YslModel> res = stationAccService.findYslByStationCodeAndTime(unitCode, time);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据StationCode获取站点对象")
    @RequestMapping(value = "/findStationAccByStationCode/{stationCode}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findStationAccByStationCode(@PathVariable("stationCode") String stationCode) {
        try {
            StationAccModel res = stationAccService.findStationAccByStationCode(stationCode);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据单位等信息分页获取可查看的所有遥测站点")
    @RequestMapping(value = "/findAllStationAccListByUnitCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllStationAccListByUnitCode(@RequestBody StationAccModel model) {
        try {
            Map<String, Object> res = stationAccService.findAllStationAccListByUnitCode(model.getUnitCode(), model.getStationName(), model.getPage(), model.getRows());
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("安装设备到站点")
    @RequestMapping(value = "/installDeviceToSite", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> installDeviceToSite(@RequestBody StationAccModel model) {
        try {
            String error = stationAccService.checkDeviceAvailability(model, false);
            if (error != null) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", error);
                return resMap;
            }

            StationAccModel res = stationAccService.installDeviceToSite(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("替换设备")
    @RequestMapping(value = "/replaceDeviceToSite", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> replaceDeviceToSite(@RequestBody StationAccModel model) {
        try {
            long timestamp0 = System.currentTimeMillis();
            String error = stationAccService.checkDeviceAvailability(model, true);

            if (error != null) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", error);
                return resMap;
            }

            StationAccModel res = stationAccService.replaceDeviceToSite(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            long timestamp1 = System.currentTimeMillis();
            //System.out.println("replaceDeviceToSite耗时：" + (timestamp1 - timestamp0));
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("在站点上停用设备")
    @RequestMapping(value = "/deactivateDeviceToSite", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deactivateDeviceToSite(@RequestBody StationAccModel model) {
        try {
            StationAccModel res = stationAccService.deactivateDeviceToSite(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("查询站点数据-APP专用")
    @RequestMapping(value = "/findStationAccForApp", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findStationAccForApp(@RequestBody StationAccModel model) {
        try {
            Map<String, Object> res = stationAccService.findStationAccForApp(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }
}
