package com.huatek.busi.controller;

import java.util.Map;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.common.util.BusiResultUtil;
import com.huatek.busi.constant.ApiConstant.Tags;
import com.huatek.busi.dto.BusiTemperPointDto;
import com.huatek.busi.service.temperature.BusiTemperPointService;
import com.huatek.busi.vo.BusiTemperPointVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;

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

/**
 * @ClassName: BusiTemperatureController
 * @FullClassPath: com.huatek.busi.controller.BusiTemperatureController
 * @Description: 测温 控制层
 * @author: edward_luo
 * @date: Jule 03, 2019 14:12:13 PM
 * @version: 1.0
 */
@Api(tags = {Tags.BUSI_TEMPERPOINT})
@Slf4j
@RestController
@RequestMapping("/api/BusiTemperPoint")
public class BusiTemperPointController {
    @Reference(check = false)
    private BusiTemperPointService temperPointService;

    /**
     * 测温点 分页列表
     *
     * @param
     *
     * @return map map
     */
    @ApiOperation(value = "分页查询", response = BusiTemperPointVo.class)
    @PostMapping(value = "/queryList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> queryList(@RequestBody BusiTemperPointDto busiTemperPointDto) {
        PageInfo<BusiTemperPointVo> busiSites = temperPointService.findBusiTemperPointPage(busiTemperPointDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, busiSites.getTotal(), "", busiSites.getList());
    }

    /**
     * 添加测温点
     *
     * busiTemperPointDto 测温列表Dto
     * 
     * @return map map
     */
    @ApiOperation(value = "添加测温点")
    @PostMapping(value = "/addBusiTemperPoint", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> addBusiSite(@RequestBody BusiTemperPointDto busiTemperPointDto) {
        int count = temperPointService.saveBusiTemperPoint(busiTemperPointDto);
        log.debug("addBusiTemperPoint: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 修改测温点
     *
     * @param busiTemperPointDto
     *            测温列表Dto
     * @return the map
     */
    @ApiOperation(value = "修改测温点")
    @PostMapping(value = "/updateBusiTemperPoint", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> updateBusiTemperPoint(@RequestBody BusiTemperPointDto busiTemperPointDto) {
        if(StringUtils.isEmpty(busiTemperPointDto.getSensorCode())
            || busiTemperPointDto.getSensorCode().length() != 17) {
            return BusiResultUtil.put(ConstantUtil.REQUEST_FAIL, "Sensior编码不能为空且必须为17位!", "");
        }
        int count = temperPointService.saveBusiTemperPoint(busiTemperPointDto);
        log.debug("updateBusiTemperPoint: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 删除测温点
     *
     * @param
     *
     * @return map map
     */
    @ApiOperation(value = "删除测温点")
    @PostMapping(value = "/deleteBusiTemperPoint", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> deleteBusiTemperPoint(@RequestParam(value = "id") String[] ids) {
        int count = temperPointService.deleteBusiTemperPoint(ids);
        log.debug("deleteBusiTemperPoint: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    @ApiOperation(value = "根据传感器编码获取测温点", response = BusiTemperPointVo.class)
    @ApiImplicitParam(name = "sensorCode", value = "传感器编码")
    @GetMapping(value = "/getBySensor", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> getBySensor(@RequestParam("sensorCode") String sensorCode) {
        BusiTemperPointVo vo = temperPointService.getBusiTemperPointVoBySensor(sensorCode);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", vo);
    }
}
