package com.om.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.om.entity.dto.DeviceQueryPageDTO;
import com.om.entity.po.Device;
import com.om.entity.po.User;
import com.om.entity.vo.DeviceQueryPageVO;
import com.om.exception.BadReqException;
import com.om.mapper.UserMapper;
import com.om.service.IDeviceService;
import com.om.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bmmx
 * @since 2024-04-08
 */
@RestController
@Api(tags = "平板设备管理")
@RequestMapping("/web/device")
public class DeviceController {

    @Resource
    private IDeviceService deviceService;
    @Autowired
    private UserMapper userMapper;

    @ApiOperation("分页查询接口")
    @PostMapping("getPageList")
    public Result<DeviceQueryPageVO> getPageList(@RequestBody DeviceQueryPageDTO dto) {
        return deviceService.getPageList(dto);
    }

    @ApiOperation("查询集合接口")
    @GetMapping("list")
    public Result<List<String>> list() {
        return Result.ok(deviceService.deviceList());
    }

    @ApiOperation("根据id查询平板设备")
    @GetMapping("getById")
    public Result<Device> getById(@RequestParam("id") Integer id) {
        return Result.ok(deviceService.getById(id));
    }

    @ApiOperation("新增平板设备")
    @PostMapping("add")
    @Transactional
    public Result add(@RequestBody Device device) {
        device.setCreateTime(LocalDateTime.now());
        String deviceSn = device.getDeviceSn();
        Device one = deviceService.lambdaQuery()
                .eq(Device::getDeviceSn, deviceSn)
                .one();
        if (BeanUtil.isNotEmpty(one)){
            throw new BadReqException("该设备sn号已经存在");
        }

        if (StringUtils.isNotBlank(device.getUsername())) {
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getUsername, device.getUsername());
            User user = userMapper.selectOne(queryWrapper);
            user.setDeviceSn(device.getDeviceSn());
            user.setUpdateTime(LocalDateTime.now());
            userMapper.updateById(user);
        }
        deviceService.save(device);
        return Result.ok();
    }

    @ApiOperation("根据id修改平板设备")
    @PostMapping("update")
    @Transactional
    public Result update(@RequestBody Device device) {
        if (StringUtils.isNotBlank(device.getUsername())) {
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getUsername, device.getUsername());
            User user = userMapper.selectOne(queryWrapper);
            user.setDeviceSn(device.getDeviceSn());
            user.setUpdateTime(LocalDateTime.now());
            userMapper.updateById(user);
        }
        deviceService.updateById(device);
        return Result.ok();
    }

    @ApiOperation("根据id删除平板设备")
    @GetMapping("delete")
    @Transactional
    public Result delete(@RequestParam("id") Integer id) {
        Device device = deviceService.getById(id);

        String username = device.getUsername();
        if (StringUtils.isNotBlank(username)) {
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getUsername, username);
            User user = userMapper.selectOne(queryWrapper);
            user.setDeviceSn("");
            userMapper.updateById(user);
        }
        deviceService.removeById(id);
        return Result.ok();
    }

    @GetMapping("unbind")
    @ApiOperation("平板设备解除客户端用户绑定")
    public Result unbind(@RequestParam("id") Integer id) {
        return deviceService.unbind(id);
    }
}
