 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.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.BusiEqmMalfunctionToDealDto;
import com.huatek.busi.dto.BusiEqmMalfunctionDto;
import com.huatek.busi.entity.BusiEqmMalfunction;
import com.huatek.busi.service.BusiEqmMalfunctionService;
import com.huatek.busi.vo.BusiEqmMalfunctionVo;
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;

@Api(tags = {Tags.BUSI_EQM_MALFUNCTION})
 @RestController
 @RequestMapping("/api/BusiEqmMalfunction")
 public class BusiEqmMalfunctionController {

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

    @Reference(check = false)
    private BusiEqmMalfunctionService busiEqmMalfunctionService;

    /**
     * 设备预警表 分页列表
     * @param busiEqmMalfunctionDto
     * @return
     */
    @ApiOperation(value = "故障数据分页查询", response = BusiEqmWarningVo.class)
    @PostMapping(value = "/queryList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> queryList(@RequestBody BusiEqmMalfunctionDto busiEqmMalfunctionDto) {
        PageInfo<BusiEqmMalfunctionVo> pageInfo = busiEqmMalfunctionService.selectBusiEqmMalfunctionPage(busiEqmMalfunctionDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    @ApiOperation(value = "添加故障单")
    @PostMapping(value = "/addBusiEqmMalfunction", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> addBusiEqmMalfunction(@RequestHeader("token") String token,
            @RequestBody BusiEqmMalfunctionDto busiEqmMalfunctionDto) {
        int count = busiEqmMalfunctionService.saveBusiEqmMalfunction(token, busiEqmMalfunctionDto);
        logger.debug("addBusiOfflineArresterRepair: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 故障处理
     *
     * @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 BusiEqmMalfunctionToDealDto param) {
        busiEqmMalfunctionService.doDeal(token, param.getIds(), param.getDealMsg());
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

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

    @ApiOperation("根据故障ID集合查询检修单是否处理")
    @PostMapping(value = "/isFinishedOverhaul", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> isFinishedOverhaul(@RequestHeader("token") String token,
        @RequestBody BusiEqmMalfunctionToDealDto param) {
        List<BusiEqmMalfunction> list = busiEqmMalfunctionService.isFinishedOverhaul(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 = "/findMalfunctionById/{id}", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> findMalfunctionById(@PathVariable(value = "id") String id) {
        BusiEqmMalfunctionVo busiEqmMalfunction = busiEqmMalfunctionService.findMalfunctionById(id);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", busiEqmMalfunction);
    }

}
