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.BusiTransformerRepairParamDto;
import com.huatek.busi.dto.BusiOfflineTransformerRepairDto;
import com.huatek.busi.service.transformer.BusiOfflineTransformerRepairService;
import com.huatek.busi.service.transformer.BusiTransformerSensorService;
import com.huatek.busi.vo.BusiOfflineTransformerRepairVo;
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;

/**
 * 变压器检修记录 控制层
 *
 * @author anker_cheng
 * @date Jul 4, 2019 2:15:27 PM
 */
@Api(tags = {Tags.BUSI_OFFLINE_TRANSFORMER_REPAIR})
@Slf4j
@RestController
@RequestMapping("/api/BusiOfflineTransformerRepair")
public class BusiOfflineTransformerRepairController {
    @Reference(check = false)
    private BusiOfflineTransformerRepairService busiOfflineTransformerRepairService;
    @Reference(check = false)
    private BusiTransformerSensorService busiTransformerSensorService;

    /**
     * 变压器检修记录 分页列表
     *
     * @param busiOfflineTransformerRepairDto
     *            变压器检修记录Dto
     * @return map map
     */
    @ApiOperation(value = "列表分页查询", response = BusiOfflineTransformerRepairVo.class)
    @PostMapping(value = "/queryList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> queryList(@RequestBody BusiOfflineTransformerRepairDto busiOfflineTransformerRepairDto) {
        PageInfo<BusiOfflineTransformerRepairVo> pageInfo =
            busiOfflineTransformerRepairService.findBusiOfflineTransformerRepairPage(busiOfflineTransformerRepairDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    /**
     * 添加 变压器检修记录
     *
     * @param token
     *            the token
     * @param busiOfflineTransformerRepairDto
     *            变压器检修记录Dto
     * @return map map
     */
    @ApiOperation(value = "添加 变压器检修记录")
    @PostMapping(value = "/addBusiOfflineTransformerRepair", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> addBusiOfflineTransformerRepair(@RequestHeader("token") String token,
        @RequestBody BusiOfflineTransformerRepairDto busiOfflineTransformerRepairDto) {
        int count = busiOfflineTransformerRepairService.saveOrUpdate(token, busiOfflineTransformerRepairDto);
        log.debug("addBusiOfflineTransformerRepair: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 通过id查找 变压器检修记录
     *
     * @param id
     *            id
     * @return map map
     */
    @ApiOperation(value = "通过id查找 变压器检修记录", response = BusiOfflineTransformerRepairVo.class)
    @GetMapping(value = "/findBusiOfflineTransformerRepair/{id}", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> findBusiOfflineTransformerRepair(@PathVariable(value = "id") String id) {
        BusiOfflineTransformerRepairVo busiOfflineTransformerRepairVo =
            busiOfflineTransformerRepairService.findBusiOfflineTransformerRepairVo(id);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", busiOfflineTransformerRepairVo);
    }

    /**
     * 修改 变压器检修记录
     *
     * @param token
     *            the token
     * @param busiOfflineTransformerRepairDto
     *            变压器检修记录Dto
     * @return the map
     */
    @ApiOperation(value = "修改 变压器检修记录")
    @PostMapping(value = "/updateBusiOfflineTransformerRepair", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> updateBusiOfflineTransformerRepair(@RequestHeader("token") String token,
        @RequestBody BusiOfflineTransformerRepairDto busiOfflineTransformerRepairDto) {
        int count = busiOfflineTransformerRepairService.saveOrUpdate(token, busiOfflineTransformerRepairDto);
        log.debug("updateBusiOfflineTransformerRepair: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

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

    /**
     * 处理变压器检修单
     *
     * @param token
     *            the token
     * @param busiOfflineGisRepairDto
     *            GIS检修记录Dto
     * @return map map
     */
    @ApiOperation(value = "处理变压器检修单")
    @PostMapping(value = "/dealGisRepair", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> dealGisRepair(@RequestHeader("token") String token,
        @RequestBody BusiTransformerRepairParamDto param) {
        busiOfflineTransformerRepairService.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 = busiTransformerSensorService.findSensorList(null);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", list);
    }
}