package com.ruoyi.web.controller.zhdd;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zhdd.domain.bo.SingleDeviceBo;
import com.ruoyi.zhdd.domain.vo.SingleDeviceVo;
import com.ruoyi.zhdd.service.IDhService;
import com.ruoyi.zhdd.service.ISingleDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
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 javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 单兵数据信息Controller
 *
 * @author xintong
 * @date 2021-12-17
 */
@Validated
@Api(value = "单兵数据信息控制器", tags = {"单兵数据信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/zhdd/singleDevice")
public class SingleDeviceController extends BaseController {

    private final ISingleDeviceService iSingleDeviceService;
    private final IDhService dhService;

    /**
     * 查询单兵数据信息列表
     */
    @ApiOperation("查询单兵数据信息列表")
    @GetMapping("/list")
    public AjaxResult<Map<String, SingleDeviceVo>> list(@Validated(QueryGroup.class) SingleDeviceBo bo) {
        bo.setDataSource(3);
        List<SingleDeviceVo> singleDeviceVos = iSingleDeviceService.queryList(bo);
        // 查询大华设备的在离线
        JSONArray ecsArr = dhService.querySingleStat();
        if (ecsArr.size() > 0) {
            Map<String, JSONObject> collect2 = ecsArr.stream().collect(Collectors.toMap(i -> {
                JSONObject a = (JSONObject) i;
                return a.getStr("deviceCode");
            }, object -> (JSONObject) object));
            for (SingleDeviceVo singleDeviceVo : singleDeviceVos) {
                JSONObject jsonObject = collect2.get(singleDeviceVo.getDeviceCode());
                if (jsonObject != null) {
                    singleDeviceVo.setStat(jsonObject.getStr("stat"));
                } else {
                    singleDeviceVo.setStat("offline");
                }
            }
        }
        Map<String, SingleDeviceVo> collect = singleDeviceVos.stream().collect(Collectors.toMap(SingleDeviceVo::getDeviceCode, a -> a));
        return AjaxResult.success(collect);
    }

    /**
     * 导出单兵数据信息列表
     */
    @ApiOperation("导出单兵数据信息列表")
    @PreAuthorize("@ss.hasPermi('zhdd:singleDevice:export')")
    @Log(title = "单兵数据信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated SingleDeviceBo bo, HttpServletResponse response) {
        List<SingleDeviceVo> list = iSingleDeviceService.queryList(bo);
        ExcelUtil.exportExcel(list, "单兵数据信息", SingleDeviceVo.class, response);
    }

    /**
     * 获取单兵数据信息详细信息
     */
    @ApiOperation("获取单兵数据信息详细信息")
    @PreAuthorize("@ss.hasPermi('zhdd:singleDevice:query')")
    @GetMapping("/{userId}")
    public AjaxResult<SingleDeviceVo> getInfo(@NotNull(message = "主键不能为空")
                                              @PathVariable("userId") String userId) {
        return AjaxResult.success(iSingleDeviceService.queryById(userId));
    }

    /**
     * 新增单兵数据信息
     */
    @ApiOperation("新增单兵数据信息")
    @PreAuthorize("@ss.hasPermi('zhdd:singleDevice:add')")
    @Log(title = "单兵数据信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody SingleDeviceBo bo) {
        return toAjax(iSingleDeviceService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改单兵数据信息
     */
    @ApiOperation("修改单兵数据信息")
    @PreAuthorize("@ss.hasPermi('zhdd:singleDevice:edit')")
    @Log(title = "单兵数据信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody SingleDeviceBo bo) {
        return toAjax(iSingleDeviceService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除单兵数据信息
     */
    @ApiOperation("删除单兵数据信息")
    @PreAuthorize("@ss.hasPermi('zhdd:singleDevice:remove')")
    @Log(title = "单兵数据信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                   @PathVariable String[] userIds) {
        return toAjax(iSingleDeviceService.deleteWithValidByIds(Arrays.asList(userIds), true) ? 1 : 0);
    }
}
