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.controller.helper.BusiGisRepairParamDto;
import com.huatek.busi.dto.BusiOfflineGisRepairDto;
import com.huatek.busi.service.gis.BusiGisSensorService;
import com.huatek.busi.service.gis.BusiOfflineGisRepairService;
import com.huatek.busi.vo.BusiOfflineGisRepairVo;
import com.huatek.busi.vo.BusiSensorVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;

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

/**
 * GIS检修记录 控制层
 *
 * @author anker_cheng
 * @date Jul 4, 2019 2:15:27 PM
 */
@Api(tags = {Tags.BUSI_OFFLINE_GIS_REPAIR})
@Slf4j
@RestController
@RequestMapping("/api/BusiOfflineGisRepair")
public class BusiOfflineGisRepairController {
    @Reference(check = false)
    private BusiOfflineGisRepairService busiOfflineGisRepairService;
    @Reference(check = false)
    private BusiGisSensorService busiGisSensorService;

    /**
     * GIS检修记录 分页列表
     *
     * @param busiOfflineGisRepairDto
     *            GIS检修记录Dto
     * @return map map
     */
    @ApiOperation(value = "列表分页查询", response = BusiOfflineGisRepairVo.class)
    @PostMapping(value = "/queryList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> queryList(@RequestBody BusiOfflineGisRepairDto busiOfflineGisRepairDto) {
        PageInfo<BusiOfflineGisRepairVo> pageInfo =
            busiOfflineGisRepairService.findBusiOfflineGisRepairPage(busiOfflineGisRepairDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    /**
     * 通过id查找 GIS检修记录
     *
     * @param id
     *            id
     * @return map map
     */
    @ApiOperation(value = "通过id查找 GIS检修记录", response = BusiOfflineGisRepairVo.class)
    @GetMapping(value = "/findGisRepair/{id}", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> findBusiOfflineGisRepair(@PathVariable(value = "id") String id) {
        BusiOfflineGisRepairVo busiOfflineGisRepairVo = busiOfflineGisRepairService.findBusiOfflineGisRepairVo(id);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", busiOfflineGisRepairVo);
    }

    /**
     * 修改 GIS检修记录
     *
     * @param token
     *            the token
     * @param busiOfflineGisRepairDto
     *            GIS检修记录Dto
     * @return the map
     */
    @ApiOperation(value = "修改 GIS检修记录")
    @PostMapping(value = "/saveOrUpdateGisRepair", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> saveOrUpdateGisRepair(@RequestHeader("token") String token,
        @RequestBody BusiOfflineGisRepairDto busiOfflineGisRepairDto) {
        int count = busiOfflineGisRepairService.saveOrUpdateGisRepair(token, busiOfflineGisRepairDto);
        log.debug("updateBusiOfflineGisRepair: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 删除 GIS检修记录
     *
     * @param token
     *            the token
     * @param busiOfflineGisRepairDto
     *            GIS检修记录Dto
     * @return map map
     */
    @ApiOperation(value = "删除 GIS检修记录")
    @PostMapping(value = "/deleteGisRepair", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> deleteBusiOfflineGisRepair(@RequestHeader("token") String token,
        @RequestBody List<String> ids) {
        int count = busiOfflineGisRepairService.deleteBusiOfflineGisRepair(token, ids);
        log.debug("deleteBusiOfflineGisRepair: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 处理 GIS检修记录
     *
     * @param token
     *            the token
     * @param busiOfflineGisRepairDto
     *            GIS检修记录Dto
     * @return map map
     */
    @ApiOperation(value = "处理 GIS检修记录")
    @PostMapping(value = "/dealGisRepair", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> dealGisRepair(@RequestHeader("token") String token,
        @RequestBody BusiGisRepairParamDto param) {
        busiOfflineGisRepairService.dealGisRepair(token, param.getIds(), param.getDealResult());
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    @ApiOperation(value = "传感器列表", response = BusiSensorVo.class)
    @GetMapping(value = "/sensorList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> sensorList(Map<String, Object> params) {
        List<BusiSensorVo> list = busiGisSensorService.findSensorList(null);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", list);
    }
}