package com.ozs.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ozs.common.annotation.Log;
import com.ozs.common.constant.UserConstants;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.enums.BusinessType;
import com.ozs.entity.MonitorSystem;
import com.ozs.entity.VehiclePosition;
import com.ozs.entity.vo.MonitorSystemVo;
import com.ozs.service.MonitorSystemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author wyy
 * @subject
 * @creat 2023/7/25
 */
@RestController
@RequestMapping("/system/monitor")
@Slf4j
public class MonitorSystemController extends BaseController {
    @Autowired
    private MonitorSystemService monitorSystemService;

    /**
     * 首页-监测系统信息
     *
     * @param
     * @return
     */
    @Log(title = "首页-监测系统信息", businessType = BusinessType.SELECT)
    @PostMapping("/monitorSystem")
    public AjaxResult monitorSystem() {
        Map<String, Object> monitorSystem = monitorSystemService.monitorSystem();
        return AjaxResult.success(monitorSystem);
    }

    /**
     * 监测系统监控分页
     *
     * @param
     * @return
     */
    @Log(title = "监测系统监控分页", businessType = BusinessType.SELECT)
    @PostMapping("/monitorSystemPage")
    public AjaxResult monitorSystemPage(@RequestBody MonitorSystemVo monitorSystemVo) {
        Integer pageNum = monitorSystemVo.getPageNum().intValue();
        Integer pageSize = monitorSystemVo.getPageSize().intValue();
        List<MonitorSystem> list = monitorSystemService.monitorSystemPage(monitorSystemVo);
        IPage page = monitorSystemService.listToPage(list, pageNum, pageSize);
        return AjaxResult.success(page);
    }


    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @Log(title = "监测系统-分页查询", businessType = BusinessType.SELECT)
    @PostMapping("/pageList")
    public AjaxResult pageList(@RequestBody MonitorSystemVo monitorSystemVo) {
        IPage<MonitorSystemVo> page = monitorSystemService.pageList(monitorSystemVo);
        return AjaxResult.success(page);
    }

    /**
     * 监测系统名称查询
     *
     * @param
     * @return
     */
    @Log(title = "监测系统-监测系统名称查询", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public AjaxResult list() {
        LambdaQueryWrapper<MonitorSystem> wrapper = new LambdaQueryWrapper<MonitorSystem>();
//        wrapper.eq(MonitorSystem::getStatus, 1);
        List<MonitorSystem> list = monitorSystemService.list(wrapper);
        return AjaxResult.success(list);
    }

    /**
     * 详情查看
     *
     * @param
     * @return
     */
    @Log(title = "监测系统-详情查看", businessType = BusinessType.SELECT)
    @GetMapping("/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        MonitorSystem monitorSystem = monitorSystemService.getMonitorSystem(id);
        return AjaxResult.success(monitorSystem);
    }

    /**
     * 新增
     *
     * @param
     * @return
     */
    @Log(title = "监测系统-新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MonitorSystem monitorSystem) {
        if (UserConstants.NOT_UNIQUE.equals(monitorSystemService.checkClientIdUnique(monitorSystem))) {
            return error("监测系统新增'" + monitorSystem.getClientId() + "'失败，客户端编码已存在");
        }
        monitorSystem.setCreateBy(getUsername());
        return AjaxResult.success(monitorSystemService.insertMonitorSystem(monitorSystem));
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @Log(title = "监测系统-删除", businessType = BusinessType.DELETE)
    @RequestMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
         MonitorSystem monitorSystem = new MonitorSystem();
        for (Long id : ids) {
            monitorSystem.setId(id);
            if (UserConstants.EXCEPTION.equals(monitorSystemService.checkSystemOnline(monitorSystem))) {
                return error("删除失败，该系统正在使用中，无法删除");
            }   
        }
        monitorSystemService.deleteByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    @Log(title = "监测系统-修改", businessType = BusinessType.UPDATE)
    @RequestMapping("/update")
    public AjaxResult update(@RequestBody MonitorSystem monitorSystem) {
        if (UserConstants.EXCEPTION.equals(monitorSystemService.checkSystemOnline(monitorSystem))) {
            return error("监测系统修改'" + monitorSystem.getClientId() + "'失败，该系统正在使用中，无法修改");
        }
        monitorSystem.setUpdateBy(getUsername());
        monitorSystem.setUpdateTime(new Date());
        return AjaxResult.success(monitorSystemService.updateMonitorSystem(monitorSystem));
    }

    /**
     * 生成密钥
     *
     * @param
     * @return
     */
    @Log(title = "监测系统-生成密钥", businessType = BusinessType.SELECT)
    @GetMapping("/getSecret")
    public AjaxResult getSecret() {
        UUID id = UUID.randomUUID();
        String[] idd = id.toString().split("-");
        String secret = idd[0]+idd[1]+idd[2];
        return AjaxResult.success("获取密钥成功！",secret);
    }
}
