package com.huatek.busi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.constant.ApiConstant;
import com.huatek.busi.controller.helper.BusiGeneratorRepairParamDto;
import com.huatek.busi.dto.BusiOfflineGeneratorRepairDto;
import com.huatek.busi.service.generatro.BusiGeneratorSensorService;
import com.huatek.busi.service.generatro.BusiOfflineGeneratorRepairService;
import com.huatek.busi.vo.BusiOfflineGeneratorRepairVo;
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;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = {ApiConstant.Tags.BUSI_OFFLINE_GENERATOR_REPAIR})
@Slf4j
@RestController
@RequestMapping("/api/BusiOfflineGeneratorRepair")
public class BusiOfflineGeneratorRepairController {
    @Reference(check = false)
    private BusiOfflineGeneratorRepairService busiOfflineGeneratorRepairService;
    @Reference(check = false)
    private BusiGeneratorSensorService busiGeneratorSensorService;

    /**
     * 发电机检修记录 分页列表
     *
     * @param busiOfflineGeneratorRepairDto
     *            发电机检修记录Dto
     * @return map map
     */
    @ApiOperation(value = "列表分页查询", response = BusiOfflineGeneratorRepairVo.class)
    @PostMapping(value = "/queryList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> queryList(@RequestBody BusiOfflineGeneratorRepairDto busiOfflineGeneratorRepairDto) {
        PageInfo<BusiOfflineGeneratorRepairVo> pageInfo =
                busiOfflineGeneratorRepairService.findBusiOfflineGeneratorRepairPage(busiOfflineGeneratorRepairDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

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



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



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

    /**
     * 处理其它检修记录
     *
     * @param token
     *            the token
     * @return map map
     */
    @ApiOperation(value = "处理其它检修记录")
    @PostMapping(value = "/dealGeneratorRepair", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> dealOtherRepair(@RequestHeader("token") String token,
                                               @RequestBody BusiGeneratorRepairParamDto param) {
        busiOfflineGeneratorRepairService.dealGeneratorRepair(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 = busiGeneratorSensorService.findSensorList(null);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", list);
    }
}
