package com.zd.chemical.controller;

import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import com.zd.chemical.domain.vo.HxpAlarmRecordSearch;
import com.zd.chemical.domain.vo.HxpAlarmRecordVo;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.web.domain.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.chemical.domain.HxpAlarmRecord;
import com.zd.chemical.service.IHxpAlarmRecordService;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.zd.common.swagger.config.Knife4jConfiguration;

import com.zd.common.core.web.page.TableDataInfo;

/**
 * 化学品报警记录Controller
 *
 * @author liubo
 * @date 2022-06-16
 */
@RestController
@Api(tags = "【化学品报警记录】")
@RequestMapping("/hxpAlarmRecord")
public class HxpAlarmRecordController extends BaseController {
    @Autowired
    private IHxpAlarmRecordService hxpAlarmRecordService;

    /**
     * 查询化学品报警记录列表
     */
    @PreAuthorize(hasPermi = PerPrefix.HXP_ALARMRECORD+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询化学品报警记录列表")
    public TableDataInfo<HxpAlarmRecordVo> list(HxpAlarmRecordSearch hxpAlarmRecordSearch) {
        startPage();
        List<HxpAlarmRecordVo> list = hxpAlarmRecordService.selectHxpAlarmRecordList(hxpAlarmRecordSearch);
        return getDataTable(list);
    }

    /**
     * 导出化学品报警记录列表
     */
    @ApiOperation(value = "导出化学品报警记录列表")
    @PreAuthorize(hasPermi = PerPrefix.HXP_ALARMRECORD+ PerFun.EXPORT)
    @Log(title = "化学品报警记录", businessType = BusinessType.EXPORT)
    @PostMapping({"/export","/export/{ids}"})
    public void export(HttpServletResponse response, HxpAlarmRecordSearch hxpAlarmRecordSearch, @PathVariable(value = "ids",required = false) Long... ids) throws IOException {
        if(ids!=null&&ids.length>0)
        {
            hxpAlarmRecordSearch = new HxpAlarmRecordSearch();
            hxpAlarmRecordSearch.setIds(Arrays.asList(ids));
        }
        List<HxpAlarmRecordVo> list = hxpAlarmRecordService.selectHxpAlarmRecordList(hxpAlarmRecordSearch);
        ExcelUtil<HxpAlarmRecordVo> util = new ExcelUtil<HxpAlarmRecordVo>(HxpAlarmRecordVo.class);
        util.exportExcel(response, list, "化学品报警记录数据");
    }

    /**
     * 获取化学品报警记录详细信息
     */
    @ApiOperation(value = "获取化学品报警记录详细信息")
    @PreAuthorize(hasPermi = PerPrefix.HXP_ALARMRECORD+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<HxpAlarmRecord> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(hxpAlarmRecordService.selectHxpAlarmRecordById(id));
    }

    /**
     * 新增化学品报警记录
     */
    @ApiOperation(value = "新增化学品报警记录")
    @PreAuthorize(hasPermi = PerPrefix.HXP_ALARMRECORD+ PerFun.ADD)
    @Log(title = "化学品报警记录", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody HxpAlarmRecord hxpAlarmRecord) {
        return ResultData.result(hxpAlarmRecordService.insertHxpAlarmRecord(hxpAlarmRecord));
    }

    /**
     * 修改化学品报警记录
     */
    @ApiOperation(value = "修改化学品报警记录")
    @PreAuthorize(hasPermi = PerPrefix.HXP_ALARMRECORD+ PerFun.EDIT)
    @Log(title = "化学品报警记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody HxpAlarmRecord hxpAlarmRecord) {
        return ResultData.result(hxpAlarmRecordService.updateHxpAlarmRecord(hxpAlarmRecord));
    }

    /**
     * 删除化学品报警记录
     */
    @ApiOperation(value = "删除化学品报警记录")
    @PreAuthorize(hasPermi = PerPrefix.HXP_ALARMRECORD+ PerFun.REMOVE)
    @Log(title = "化学品报警记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids) {
        return ResultData.result(hxpAlarmRecordService.deleteHxpAlarmRecordByIds(ids));
    }


    /**
     * 化学品智能报警日志列表
     */
    @PostMapping("/getList")
    @ApiOperation(value = "化学品智能报警日志列表")
    public ResultData<List<LinkedHashMap <String,String>>> getList(@RequestBody HxpAlarmRecordSearch hxpAlarmRecordSearch) {
        List<HxpAlarmRecordVo> list = hxpAlarmRecordService.selectHxpAlarmRecordList(hxpAlarmRecordSearch);
        List<LinkedHashMap<String,String>> alarmList = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream()
                .map(a->{
                    LinkedHashMap<String,String> toMap = new LinkedHashMap();
                    toMap.put("id",a.getId()+"");
                    toMap.put("handlingStatus",a.getHandlingStatus()+"");
                    return toMap;
                })
                .collect(Collectors.toList());
        return ResultData.success(alarmList);
    }


    /**
     * 修改化学品报警记录
     */
    @ApiOperation(value = "修改化学品报警记录")
    @Log(title = "化学品报警记录", businessType = BusinessType.UPDATE)
    @PutMapping("/handle")
    public ResultData<Boolean> handle(@RequestBody HxpAlarmRecord hxpAlarmRecord) {
        hxpAlarmRecord.setHandlingTime(new Date());
        hxpAlarmRecord.setHandlingStatus(2);
        return ResultData.result(hxpAlarmRecordService.updateHxpAlarmRecord(hxpAlarmRecord));
    }


    /**
     * 手动处理报警记录
     */
    @ApiOperation(value = "手动处理报警记录")
    @Log(title = "手动处理报警记录", businessType = BusinessType.UPDATE)
    @PutMapping("/handleProcessing")
    public ResultData<Boolean> handleProcessing(@RequestBody HxpAlarmRecord hxpAlarmRecord) {
        hxpAlarmRecord.setHandlingTime(new Date());
        hxpAlarmRecord.setHandlingStatus(2);
        hxpAlarmRecord.setHandlingUserId(SecurityUtils.getUserId());
        return ResultData.result(hxpAlarmRecordService.handleProcessing(hxpAlarmRecord));
    }


    /**
     * 查询化学品报警记录列表
     */
    @GetMapping("/noAuthlist")
    @ApiOperation(value = "查询化学品报警记录列表")
    public TableDataInfo<HxpAlarmRecordVo> noAuthlist(HxpAlarmRecordSearch hxpAlarmRecordSearch) {
        startPage();
        hxpAlarmRecordSearch.setAlarmMode(2);
        List<HxpAlarmRecordVo> list = hxpAlarmRecordService.selectHxpAlarmRecordList(hxpAlarmRecordSearch);
        return getDataTable(list);
    }


    /**
     * 四医大短信告警方案阶梯式通知流程
     */
    @GetMapping("/sendPhoneBySyd")
    public void sendPhoneBySyd(){
        hxpAlarmRecordService.sendPhoneBySyd();
    }
}
