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.BaseVehicleTerminal;
import com.ozs.entity.MsgAlarm;
import com.ozs.entity.TerminalIgnoreAlarm;
import com.ozs.entity.vo.TerminalIgnoreVo;
import com.ozs.service.BaseVehicleTerminalService;
import com.ozs.service.TerminalIgnoreAlarmService;
import com.ozs.system.service.ISysDictDataService;
import io.swagger.annotations.ApiOperation;
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 BaseVehicleTerminalService baseVehicleTerminalService;

    /**
     * 查询终端忽略报警信息列表
     *
     * @param terminalIgnoreVo
     * @return
     */
    @ApiOperation(value = "查询终端忽略报警信息列表")
    @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<BaseVehicleTerminal> terminal = baseVehicleTerminalService.list();
        if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(page.getRecords())) {
            List<TerminalIgnoreAlarm> dto = page.getRecords().stream().map(o -> {
            for(BaseVehicleTerminal 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
     */
    @ApiOperation(value = "终端忽略报警信息详情")
    @PostMapping("/detail")
    @Log(title = "终端忽略报警信息详情", businessType = BusinessType.SELECT)
    public AjaxResult detail(@RequestBody TerminalIgnoreVo terminalIgnoreVo) {
        MsgAlarm msgAlarm = terminalIgnoreAlarmService.selectDetailByAlarmId(terminalIgnoreVo.getAlarmId());
        if(!ObjectUtils.isEmpty(msgAlarm.getLineDir())){//行别
            msgAlarm.setLineDirName(msgAlarm.getLineDir() == 1 ? "上行" : "下行");
        }
        if(!ObjectUtils.isEmpty(msgAlarm.getAlarmType())){//报警类型
            msgAlarm.setAlarmTypeName(dictDataService.selectDictLabel("sys_alarm_type", String.valueOf(msgAlarm.getAlarmType())));
        }
        if(!ObjectUtils.isEmpty(msgAlarm.getAlarmMile())){//里程位置
            msgAlarm.setAlarmMiles(AppendUtils.stringAppend(Integer.valueOf(msgAlarm.getAlarmMile())));
        }
        if(!ObjectUtils.isEmpty(msgAlarm.getIsRelease())){//状态
            msgAlarm.setIsReleaseName(msgAlarm.getIsRelease() == 1 ? "已解除" : "未解除");
        }
        return AjaxResult.success(msgAlarm);
    }

    @ApiOperation(value = "终端忽略报警信息导出")
    @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, "终端忽略报警信息");
    }
}

