package com.ozs.web.controller.accountmanagment;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ozs.common.annotation.Log;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.utils.AppendUtils;
import com.ozs.common.utils.poi.ExcelUtil;
import com.ozs.entity.BaseTerminal;
import com.ozs.entity.TerminalIgnoreAlarm;
import com.ozs.entity.vo.MsgAlarmDTO;
import com.ozs.entity.vo.TerminalIgnoreVo;
import com.ozs.service.BaseTerminalService;
import com.ozs.service.TerminalIgnoreAlarmService;
import com.ozs.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ozs
 * @since 2023-04-11
 */
@RestController
@RequestMapping("/terminal/ignore")
public class TerminalIgnoreAlarmController extends BaseController {
    @Autowired
    private TerminalIgnoreAlarmService terminalIgnoreAlarmService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private BaseTerminalService baseTerminalService;

    /**
     * 查询终端忽略报警信息列表
     *
     * @param terminalIgnoreVo
     * @return
     */
    @PostMapping("/list")
    @Log(title = "终端忽略报警信息列表", businessType = BusinessType.SELECT)
    public AjaxResult list(@RequestBody TerminalIgnoreVo terminalIgnoreVo) {
        QueryWrapper<TerminalIgnoreAlarm> wrapper = new QueryWrapper<TerminalIgnoreAlarm>();
        if (!ObjectUtils.isEmpty(terminalIgnoreVo.getTerminalCode())) {
            wrapper.lambda().like(TerminalIgnoreAlarm::getTerminalCode, terminalIgnoreVo.getTerminalCode());
        }
        wrapper.orderByDesc("ignore_time");
        IPage<TerminalIgnoreAlarm> page = terminalIgnoreAlarmService.page(new Page<>(terminalIgnoreVo.getPageNum(), terminalIgnoreVo.getPageSize()), wrapper);
        List<BaseTerminal> terminal = baseTerminalService.list();
        if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(page.getRecords())) {
            List<TerminalIgnoreAlarm> dto = page.getRecords().stream().map(o -> {
            for(BaseTerminal ter:terminal){
                if(ter.getTerminalCode().equals(o.getTerminalCode())){
                    o.setTerminalName(ter.getTerminalName());
                }
            }
            return o;
        }).collect(Collectors.toList());
            page.setRecords(dto);
        }
        return AjaxResult.success(page);
    }

    /**
     * 查询终端忽略报警信息详情
     *
     * @param
     * @return
     */
    @PostMapping("/detail")
    @Log(title = "终端忽略报警信息详情", businessType = BusinessType.SELECT)
    public AjaxResult detail(@RequestBody TerminalIgnoreVo terminalIgnoreVo) {
        MsgAlarmDTO msgAlarmDTO = terminalIgnoreAlarmService.selectDetailByAlarmId(terminalIgnoreVo.getId());
        if(!ObjectUtils.isEmpty(msgAlarmDTO.getLineDir())){//行别
            msgAlarmDTO.setLineDirName(msgAlarmDTO.getLineDir() == 1 ? "上行" : "下行");
        }
        if(!ObjectUtils.isEmpty(msgAlarmDTO.getAlarmType())){//报警类型
            msgAlarmDTO.setAlarmTypeName(dictDataService.selectDictLabel("sys_alarm_type", String.valueOf(msgAlarmDTO.getAlarmType())));
        }
        if(!ObjectUtils.isEmpty(msgAlarmDTO.getAlarmMile())){//里程位置
            msgAlarmDTO.setAlarmMiles(AppendUtils.stringAppend(Integer.valueOf(msgAlarmDTO.getAlarmMile())));
        }
        if(!ObjectUtils.isEmpty(msgAlarmDTO.getIsRelease())){//状态
            msgAlarmDTO.setIsReleaseName(msgAlarmDTO.getIsRelease() == 1 ? "已解除" : "未解除");
        }
        return AjaxResult.success(msgAlarmDTO);
    }

    @Log(title = "终端忽略报警信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody TerminalIgnoreVo terminalIgnoreVo) {
        List<TerminalIgnoreAlarm> list = terminalIgnoreAlarmService.list(terminalIgnoreVo.getTerminalCode());
        ExcelUtil<TerminalIgnoreAlarm> util = new ExcelUtil<TerminalIgnoreAlarm>(TerminalIgnoreAlarm.class);
        util.exportExcel(response, list, "终端忽略报警信息");
    }
}

