package com.huatek.busi.controller;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import com.huatek.busi.service.WarningResultService;
import com.huatek.busi.vo.warning.BusiEqmWarningTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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.BusiEqmMalfunctionToDealDto;
import com.huatek.busi.controller.helper.BusiEqmWarningToDealDto;
import com.huatek.busi.dto.BusiEqmWarningDto;
import com.huatek.busi.entity.BusiEqmWarning;
import com.huatek.busi.service.BusiEqmMalfunctionService;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.vo.BusiEqmWarningVo;
import com.huatek.torch.frame.log.KerLog;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 设备预警表 控制层
 *
 * @author walker_wang
 * @date Jul 5, 2019 1:43:01 PM
 */
@Api(tags = {Tags.BUSI_WARNING})
@RestController
@RequestMapping("/api/BusiEqmWarning")
public class BusiEqmWarningController {

    @Reference(check = false)
    private BusiEqmWarningService busiEqmWarningService;

    @Reference(check = false)
    private BusiEqmMalfunctionService busiEqmMalfunctionService;

    @Reference(check = false)
    WarningResultService warningResultService;

    /**
     * 设备预警表 分页列表
     */
    @ApiOperation(value = "test1111")
    @GetMapping(value = "/warring", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> warring(@RequestParam String id) throws ParseException {
        BusiEqmWarningTaskVo listWarn = warningResultService.warningInfo(id);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "",listWarn);
    }

    /**
     * 设备预警表 新增测试
     */
    @ApiOperation(value = "test")
    @PostMapping(value = "/testWarringAdd", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> testWarringAdd(@RequestBody BusiEqmWarningDto busiEqmWarningDto) throws ParseException {
        boolean result = busiEqmWarningService.testWarringAdd(busiEqmWarningDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "",result);
    }


    /**
     * 设备预警表 分页列表
     *
     * @param busiEqmWarningDto 设备预警表Dto
     * @return map map
     */
    @ApiOperation(value = "告警数据分页查询", response = BusiEqmWarningVo.class)
    @PostMapping(value = "/queryList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> queryList(@RequestBody BusiEqmWarningDto busiEqmWarningDto) {
        PageInfo<BusiEqmWarningVo> pageInfo = busiEqmWarningService.findBusiEqmWarningPage(busiEqmWarningDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    /**
     * 设备预警表 分页列表
     * @param busiEqmWarningDto 设备预警表Dto
     * @return map map
     */
    @ApiOperation(value = "告警数据分页查询", response = BusiEqmWarningVo.class)
    @PostMapping(value = "/queryThresholdList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> queryThresholdList(@RequestBody BusiEqmWarningDto busiEqmWarningDto) {
        PageInfo<BusiEqmWarningVo> pageInfo = busiEqmWarningService.queryThresholdList(busiEqmWarningDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    /**
     * 告警处理
     *
     * @param token
     * @param param
     * @return
     */
    @ApiOperation("告警处理")
    @PostMapping(value = "/doDeal", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> doDeal(@RequestHeader("token") String token,
                                      @RequestBody BusiEqmWarningToDealDto param) {
        busiEqmWarningService.doDeal(token, param.getIds(), param.getWarningType(), param.getDealMsg());
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 批量告警处理
     * @param token
     * @param param
     * @return
     */
    @ApiOperation("一键清除")
    @PostMapping(value = "/dowholeDeal", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> dowholeDeal(@RequestHeader("token") String token,
                                           @RequestBody BusiEqmWarningDto param) {
        return busiEqmWarningService.dowholeDeal(token, param);
    }

    /**
     * 单个告警处理
     * @param token
     * @param param
     * @return
     */
    @ApiOperation("单个告警处理")
    @PostMapping(value = "/dosingleDeal", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> dosingleDeal(@RequestHeader("token") String token,
                                           @RequestBody BusiEqmWarningDto param) {
        return busiEqmWarningService.dosingleDeal(token, param);
    }

    @ApiOperation("告警转故障单")
    @PostMapping(value = "/toRepair", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> toRepair(@RequestHeader("token") String token,
                                        @RequestBody BusiEqmWarningToDealDto param) {
        busiEqmWarningService.toRepair(token, param.getIds(), param.getDealMsg());
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }


    @ApiOperation("根据告警ID集合查询故障单是否处理")
    @PostMapping(value = "/isFinishedEqmMalfunction", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> isFinishedEqmMalfunction(@RequestHeader("token") String token,
                                                        @RequestBody BusiEqmMalfunctionToDealDto param) {
        List<BusiEqmWarning> list = busiEqmWarningService.isFinishedEqmMalfunction(param.getIds());
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", list);
    }

    /**
     * 通过id查找告警信
     * @param id id
     * @return map map
     */
    @ApiOperation(value = "通过id查找告警信息")
    @KerLog(projectName = "PROJECTNAME", modelName = "BUSIOUTLINEGISEXPERIMENTDATA.MODELNAME", methodName = "BUSIOUTLINEGISEXPERIMENTDATA.METHODNAME.FINDBUSIOUTLINEGISEXPERIMENTDATA")
    @GetMapping(value = "/findBusiEqmWarningById/{id}", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> findBusiEqmWarningById(@PathVariable(value = "id") String id) {
        BusiEqmWarningVo vo = busiEqmWarningService.findBusiEqmWarningById(id);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", vo);
    }
}
