package com.huatek.busi.controller;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestParam;
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.BusiOfflineArresterRepairDto;
import com.huatek.busi.service.arrester.BusiArresterSensorService;
import com.huatek.busi.service.arrester.BusiOfflineArresterRepairService;
import com.huatek.busi.vo.BusiOfflineArresterRepairVo;
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.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 出场线（避雷器）——检修单 控制层
 *
 * @author anker_cheng
 * @date Jul 8, 2019 4:38:14 PM
 */
@Api(tags = { Tags.BUSI_OFFLINE_ARRESTER_REPAIR })
@RestController
@RequestMapping("/api/BusiOfflineArresterRepair")
public class BusiOfflineArresterRepairController {

    protected final Logger logger = LoggerFactory.getLogger(BusiOfflineArresterRepairController.class);

    @Reference(check = false)
    private BusiOfflineArresterRepairService busiOfflineArresterRepairService;
    @Reference(check = false)
    private BusiArresterSensorService busiArresterSensorService;

    @ApiOperation(value = "列表分页查询", response = BusiOfflineArresterRepairVo.class)
    @PostMapping(value = "/queryList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> queryList(@RequestBody BusiOfflineArresterRepairDto busiOfflineArresterRepairDto) {
        PageInfo<BusiOfflineArresterRepairVo> pageInfo =
            busiOfflineArresterRepairService.findBusiOfflineArresterRepairPage(busiOfflineArresterRepairDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    @ApiOperation(value = "添加检修单")
    @PostMapping(value = "/addBusiOfflineArresterRepair", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> addBusiOfflineArresterRepair(@RequestHeader("token") String token,
            @RequestBody BusiOfflineArresterRepairDto busiOfflineArresterRepairDto) {
        int count = busiOfflineArresterRepairService.saveOrUpdateArresterRepair(token, busiOfflineArresterRepairDto);
        logger.debug("addBusiOfflineArresterRepair: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    @ApiOperation(value = "通过id查找检修单", response = BusiOfflineArresterRepairVo.class)
    @GetMapping(value = "/findBusiOfflineArresterRepair/{id}", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> findBusiOfflineArresterRepair(@PathVariable(value = "id") String id) {
        BusiOfflineArresterRepairVo busiOfflineArresterRepairVo = busiOfflineArresterRepairService
                .findBusiOfflineArresterRepairVo(id);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", busiOfflineArresterRepairVo);
    }

    @ApiOperation(value = "修改检修单")
    @PostMapping(value = "/updateBusiOfflineArresterRepair", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> updateBusiOfflineArresterRepair(@RequestHeader("token")String token,
            @RequestBody BusiOfflineArresterRepairDto busiOfflineArresterRepairDto) {
        int count = busiOfflineArresterRepairService.saveOrUpdateArresterRepair(token,
                busiOfflineArresterRepairDto);
        logger.debug("updateBusiOfflineArresterRepair: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    @ApiOperation(value = "删除检修单")
    @ApiImplicitParam(name = "id", value = "避雷器检修单ID或ID数组", allowMultiple = true)
    @PostMapping(value = "/deleteBusiOfflineArresterRepair", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> deleteBusiOfflineArresterRepair(@RequestHeader("token")String token, @RequestParam(value = "id") String[] ids) {
        int count = busiOfflineArresterRepairService.deleteBusiOfflineArresterRepair(token, ids);
        logger.debug("deleteBusiOfflineArresterRepair: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    @ApiOperation(value = "处理检修单")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "避雷器检修单ID或ID数组", allowMultiple = true),
        @ApiImplicitParam(name = "dealResult", value = "处理结果")
    })
    @PostMapping(value = "/dealBusiOfflineArresterRepair", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> dealBusiOfflineArresterRepair(@RequestHeader("token")String token, @RequestParam(value = "id") String[] ids,
        @RequestParam("dealResult") String dealResult) {
        int count = busiOfflineArresterRepairService.dealBusiOfflineArresterRepair(token, ids, dealResult);
        logger.debug("deleteBusiOfflineArresterRepair: {}", count);
        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 = busiArresterSensorService.findSensorList(null);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", list);
    }
}