package com.huatek.busi.controller;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.constant.ApiConstant.Tags;
import com.huatek.busi.dto.BusiSiteDto;
import com.huatek.busi.dto.SiteUploadCycleDto;
import com.huatek.busi.entity.BusiSite;
import com.huatek.busi.service.BusiSiteService;
import com.huatek.busi.vo.BusiSiteVo;
import com.huatek.busi.vo.SiteUploadCycleVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.frame.validate.KerValid;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 站点表 控制层
 *
 * @author anker_cheng
 * @date Jun 18, 2019 6:22:48 PM
 */
@Api(tags = { Tags.BUSI_SITE })
@Slf4j
@RestController
@RequestMapping("/api/BusiSite")
public class BusiSiteController {

    @Reference(check = false)
    private BusiSiteService busiSiteService;

    /**
     * 站点表 分页列表
     *
     * @param busiSiteDto 站点表Dto
     * @return map map
     */
    @PostMapping(value = "/queryList", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> queryList(@RequestBody BusiSiteDto busiSiteDto) {
        PageInfo<BusiSiteVo> busiSites = busiSiteService.findBusiSitePage(busiSiteDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, busiSites.getTotal(), "", busiSites.getList());
    }



    /**
     * 查询站点以及经纬度
     *
     */
    @PostMapping(value = "/selectSiteInfo", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> selectSiteInfo() {
        List<BusiSiteVo> busiSites = busiSiteService.findSiteInfo();
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS,"",busiSites);
    }


    /**
     * 添加 站点表
     *
     * @param token       the token
     * @param busiSiteDto 站点表Dto
     * @return map map
     */
    @ApiOperation(value = "添加站点")
    @PostMapping(value = "/addBusiSite", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> addBusiSite(@RequestHeader("token")String token, @KerValid @RequestBody BusiSiteDto busiSiteDto) {
        int count = busiSiteService.addBusiSite(token, busiSiteDto);
        log.debug("addBusiSite: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 通过id查找 站点表
     *
     * @param id id
     * @return map map
     */
    @GetMapping(value = "/findBusiSite/{id}", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> findBusiSite(@PathVariable(value = "id") String id) {
        BusiSite busiSite = busiSiteService.findBusiSite(id);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", busiSite);
    }

    /**
     * 修改 站点表
     *
     * @param token       the token
     * @param busiSiteDto 站点表Dto
     * @return the map
     */
    @PostMapping(value = "/updateBusiSite", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> updateBusiSite(@RequestHeader("token")String token, @RequestBody BusiSiteDto busiSiteDto) {
        int count = busiSiteService.updateBusiSite(token, busiSiteDto);
        log.debug("updateBusiSite: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 删除 站点表
     *
     * @param token       the token
     * @param busiSiteDto 站点表Dto
     * @return map map
     */
    @PostMapping(value = "/deleteBusiSite", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> deleteBusiSite(@RequestHeader("token")String token, @RequestBody BusiSiteDto busiSiteDto) {
        boolean flag = busiSiteService.deleteBusiSite(token, busiSiteDto);
        log.debug("deleteBusiSite: {}", flag);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     ** 获取监测装置上传数据时间设置数据
     * 
     * @param token       the token
     * @param busiSiteDto 站点表Dto
     * @return map map
     */
    @PostMapping(value = "/getSiteUploadCycleInfo/{siteId}", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> getSiteUploadCycleInfo(@PathVariable(value = "siteId") String siteId) {
        SiteUploadCycleVo siteUploadCycleInfo = busiSiteService.getSiteUploadCycleInfo(siteId);
        log.debug("getSiteUploadCycleInfo: {}", siteUploadCycleInfo);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", siteUploadCycleInfo);
    }

    /**
     ** 维护监测装置上传数据时间设置数据
     * 
     * @param token       the token
     * @param busiSiteDto 站点表Dto
     * @return map map
     */
    @PostMapping(value = "/saveSiteUploadCycleInfo", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> saveSiteUploadCycleInfo(@RequestHeader("token")String token, @RequestBody SiteUploadCycleDto siteUploadCycleDto) {
    	int count = busiSiteService.saveSiteUploadCycleInfo(token, siteUploadCycleDto);
        log.debug("getSiteUploadCycleInfo: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }
    
    
    /**
             * 获取所有站点信息（视频流需要引用）
     * @return
     */
    @GetMapping(value = "/queryAllSite")
    public Map<String, Object> queryAllSite() {
        List<BusiSiteVo> busiSites = busiSiteService.queryAllSite();
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", busiSites);
    }
}