package com.inspect.metadata.eqpbook.controller;

import com.inspect.metadata.eqpbook.domain.BasedataEqpBook;
import com.inspect.metadata.eqpbook.domain.BasedataEqpBookChannel;
import com.inspect.metadata.eqpbook.domain.vo.PatrolDeviceStatusVo;
import com.inspect.metadata.eqpbook.service.IBasedataEqpBookChannelService;
import com.inspect.metadata.eqpbook.service.IBasedataEqpBookService;
import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.RequiresPermissions;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/eqpbook/channel"})
public class BasedataEqpBookChannelController extends BaseController {
    private final IBasedataEqpBookChannelService basedataEqpBookChannelService;
    private final IBasedataEqpBookService baseDataEqpBookService;

    public BasedataEqpBookChannelController(IBasedataEqpBookChannelService basedataEqpBookChannelService, IBasedataEqpBookService baseDataEqpBookService) {
        this.basedataEqpBookChannelService = basedataEqpBookChannelService;
        this.baseDataEqpBookService = baseDataEqpBookService;
    }

    @GetMapping({"/list"})
    public TableDataInfo list(BasedataEqpBookChannel basedataEqpbookChannel) {
        this.startPage();
        List<BasedataEqpBookChannel> list = this.basedataEqpBookChannelService.selectBasedataEqpBookChannelList(basedataEqpbookChannel);

        for (BasedataEqpBookChannel channel : list) {
            BasedataEqpBook basedataEqp = this.baseDataEqpBookService.selectBasedataEqpBookByEqpBookId(channel.getEqpBookId());
            if (basedataEqp == null) {
                return this.getDataTable(list);
            }

            channel.setEqpBookName(basedataEqp.getPatrolDeviceName());
        }

        return this.getDataTable(list);
    }

    @RequiresPermissions({"basedata:channel:export"})
    @Log(
            title = "设备通道",
            businessType = BizType.EXPORT
    )
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, BasedataEqpBookChannel basedataEqpbookChannel) {
        List<BasedataEqpBookChannel> list = this.basedataEqpBookChannelService.selectBasedataEqpBookChannelList(basedataEqpbookChannel);
        ExcelUtil<BasedataEqpBookChannel> util = new ExcelUtil<>(BasedataEqpBookChannel.class);
        util.exportExcel(response, list, "设备通道数据");
    }

    @RequiresPermissions({"basedata:channel:query"})
    @GetMapping({"/{channelId}"})
    public AjaxResult getInfo(@PathVariable("channelId") Long channelId) {
        return AjaxResult.success(this.basedataEqpBookChannelService.selectBasedataEqpBookChannelByChannelId(channelId));
    }

    @RequiresPermissions({"basedata:channel:query"})
    @GetMapping({"/channelCode/{channelCode}"})
    public AjaxResult getEqpBookChannelByChannelCode(@PathVariable("channelCode") String channelCode) {
        return AjaxResult.success(this.basedataEqpBookChannelService.selectBasedataEqpBookChannelByChannelCode(channelCode));
    }

    @RequiresPermissions({"basedata:channel:add"})
    @Log(
            title = "设备通道",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@RequestBody BasedataEqpBookChannel basedataEqpbookChannel) {
        return this.toAjax(this.basedataEqpBookChannelService.insertBasedataEqpBookChannel(basedataEqpbookChannel));
    }

    @RequiresPermissions({"basedata:channel:edit"})
    @Log(
            title = "设备通道",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody BasedataEqpBookChannel basedataEqpbookChannel) {
        return this.toAjax(this.basedataEqpBookChannelService.updateBasedataEqpBookChannel(basedataEqpbookChannel));
    }

    @RequiresPermissions({"basedata:channel:remove"})
    @Log(
            title = "设备通道",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{channelIds}"})
    public AjaxResult remove(@PathVariable Long[] channelIds) {
        return this.toAjax(this.basedataEqpBookChannelService.deleteBasedataEqpBookChannelByChannelIds(channelIds));
    }

    @RequiresPermissions({"basedata:channel:query"})
    @Log(
            title = "查询设备类型",
            businessType = BizType.OTHER
    )
    @GetMapping({"/getDevtypeList"})
    public AjaxResult getDevTypeList() {
        List<BasedataEqpBook> list = this.baseDataEqpBookService.getUavList();
        return AjaxResult.success(list);
    }

    @RequiresPermissions({"basedata:eqpbook:list"})
    @GetMapping({"/getChannelByEqpbookCode"})
    public AjaxResult getChannelByEqpBookCode(BasedataEqpBook basedataEqpbook) {
        List<BasedataEqpBookChannel> list = this.basedataEqpBookChannelService.getChannelByEqpBookCode(basedataEqpbook);
        return AjaxResult.success(list);
    }


    /**
     * 获取双光摄像头、可⻅光摄像头数量统计，包括正常和异常状 态
     * @return
     */
    @GetMapping({"/patroldeviceState"})
    public AjaxResult patroldeviceState() {
        List<BasedataEqpBookChannel> list = this.basedataEqpBookChannelService.selectBasedataEqpBookChannelList(new BasedataEqpBookChannel());
        List<PatrolDeviceStatusVo> patroDeviceStatusVoList = new ArrayList<>();
        PatrolDeviceStatusVo vl = new PatrolDeviceStatusVo("可⻅光摄像头", 0, 0);
        PatrolDeviceStatusVo ir = new PatrolDeviceStatusVo("双光摄像头", 0, 0);
        for (BasedataEqpBookChannel channel : list) {
            if(channel.getChannelType().equals("vl")){
                if(channel.getOnlineState().equals("1")){
                    vl.setNormalCount(vl.getNormalCount() + 1);
                } else {
                    vl.setAnomalyCount(vl.getAnomalyCount() + 1);
                }
            }
            if(channel.getChannelType().equals("ir")){
                if(channel.getOnlineState().equals("1")){
                    ir.setNormalCount(ir.getNormalCount() + 1);
                } else {
                    ir.setAnomalyCount(ir.getAnomalyCount() + 1);
                }
            }
        }
        patroDeviceStatusVoList.add(vl);
        patroDeviceStatusVoList.add(ir);
        return AjaxResult.success(patroDeviceStatusVoList);
    }
}
