package com.inspect.patrol.controller;

import com.alibaba.fastjson.JSONObject;
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.base.redis.service.RedisService;
import com.inspect.patrol.domain.PatrolLinkagePos;
import com.inspect.patrol.domain.PatrolPoint;
import com.inspect.patrol.domain.PopupData;
import com.inspect.patrol.linkage.processor.ReceiveProcessor;
import com.inspect.patrol.model.LinkageActionConfig;
import com.inspect.patrol.service.IPatrolLinkagePosService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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;
import springfox.documentation.annotations.ApiIgnore;

import static com.inspect.patrol.linkage.constain.LinkageContain.REDIS_LINKAGE_TASK_DATA;

@RestController
@RequestMapping({"/linkage"})
@ApiIgnore
@Api(
        tags = {"联动点位管理"}
)
public class PatrolLinkagePosController extends BaseController {
    private final IPatrolLinkagePosService patrolLinkagePosService;
    private final ReceiveProcessor cimeReceiveProcessor;
    private final RedisService redisService;

    public PatrolLinkagePosController(IPatrolLinkagePosService patrolLinkagePosService, ReceiveProcessor cimeReceiveProcessor, RedisService redisService) {
        this.patrolLinkagePosService = patrolLinkagePosService;
        this.cimeReceiveProcessor = cimeReceiveProcessor;
        this.redisService = redisService;
    }

    @ApiOperation("查询联动点列表")
    @GetMapping({"/list"})
    public TableDataInfo list(PatrolLinkagePos patrolLinkagePos) {
        this.startPage();
        List<PatrolLinkagePos> list = this.patrolLinkagePosService.selectPatrolLinkagePosList(patrolLinkagePos);
        return this.getDataTable(list);
    }

    @GetMapping({"/patrolpoint"})
    public TableDataInfo queryPatrolPointByDeviceId(String deviceId) {
        this.startPage();
        List<PatrolPoint> list = this.patrolLinkagePosService.selectPatrolPointList(deviceId);
        return this.getDataTable(list);
    }

    @GetMapping({"/popup"})
    public AjaxResult popup() {
        String data = (String) this.redisService.redisTemplate.opsForValue().get(REDIS_LINKAGE_TASK_DATA);
        this.cimeReceiveProcessor.sendByWebsocket(JSONObject.parseObject(data, PopupData.class));
        return AjaxResult.success();
    }

    @GetMapping({"/{linkagePosId}"})
    public AjaxResult getInfo(@PathVariable("linkagePosId") Long linkagePosId) {
        return AjaxResult.success(this.patrolLinkagePosService.selectPatrolLinkagePosByLinkagePosId(linkagePosId));
    }

    @Log(
            title = "联动点",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@RequestBody JSONObject params) {
        return this.toAjax(this.patrolLinkagePosService.insertPatrolLinkagePos(null));
    }

    @PostMapping({"action"})
    public AjaxResult modifyConfig(@RequestBody List<LinkageActionConfig> list) {
        this.patrolLinkagePosService.updateLinkageActionConfig(list);
        return AjaxResult.success();
    }

    @Log(
            title = "联动点",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody PatrolLinkagePos patrolLinkagePos) {
        return this.toAjax(this.patrolLinkagePosService.updatePatrolLinkagePos(patrolLinkagePos));
    }

    @Log(
            title = "联动点",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{linkagePosIds}"})
    public AjaxResult remove(@PathVariable Long[] linkagePosIds) {
        return this.toAjax(this.patrolLinkagePosService.deletePatrolLinkagePosByLinkagePosIds(linkagePosIds));
    }
}
