package com.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.om.entity.dto.DeviceQueryPageDTO;
import com.om.entity.po.Device;
import com.om.entity.po.DiagnosticMenu;
import com.om.entity.po.Repair;
import com.om.entity.po.User;
import com.om.entity.vo.DeviceQueryPageVO;
import com.om.entity.vo.RepairQueryPageVO;
import com.om.exception.BadReqException;
import com.om.mapper.DeviceMapper;
import com.om.mapper.UserMapper;
import com.om.service.IDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.om.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author bmmx
 * @since 2024-04-08
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    private final UserMapper userMapper;

    public DeviceServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public Result<DeviceQueryPageVO> getPageList(DeviceQueryPageDTO dto) {
        Integer pageIndex = dto.getPageIndex();
        Integer pageSize = dto.getPageSize();
        String searchDeviceSn = dto.getSearchDeviceSn();
        String searchUsername = dto.getSearchUsername();
        String searchClientNum = dto.getSearchClientNum();

        Page<Device> page = this.lambdaQuery()
                .like(StringUtils.isNotBlank(searchDeviceSn), Device::getDeviceSn, searchDeviceSn)
                .like(StringUtils.isNotBlank(searchUsername), Device::getUsername, searchUsername)
                .like(StringUtils.isNotBlank(searchClientNum), Device::getClientNum, searchClientNum)
                .orderByDesc(Device::getCreateTime)
                .page(new Page<>(pageIndex, pageSize));

        DeviceQueryPageVO vo = new DeviceQueryPageVO();
        vo.setCurrent((int) page.getCurrent());
        vo.setSize((int) page.getSize());
        vo.setPages((int) page.getPages());
        vo.setTotal((int) page.getTotal());

        if (StringUtils.isNotBlank(searchDeviceSn) || StringUtils.isNotBlank(searchUsername) || StringUtils.isNotBlank(searchClientNum)) {
            vo.setSearchCount(true);
        }

        List<Device> records = page.getRecords();
        if (records.isEmpty()) {
            return Result.ok(vo);
        }
        vo.setRecords(records);
        return Result.ok(vo);
    }

    @Override
    public List<String> deviceList() {
        List<Device> deviceList = this.list();
        List<String> collect = deviceList.stream().map(c -> c.getDeviceSn())
                .filter(deviceSn -> StringUtils.isBlank(lambdaQuery()
                        .eq(Device::getDeviceSn, deviceSn).one().getUsername()))
                .distinct().collect(Collectors.toList());
        return collect;
    }

    @Override
    public Result unbind(Integer id) {
        Device device = this.getById(id);
        if (BeanUtil.isEmpty(device)){
            throw new BadReqException("该平板设备不存在");
        }
        String username = device.getUsername();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        User user = userMapper.selectOne(wrapper);
        if (BeanUtil.isEmpty(user)){
            throw new BadReqException("该平板设备绑定的用户不存在");
        }
        user.setDeviceSn("");
        device.setUsername("");

        userMapper.updateById(user);
        this.updateById(device);

        return Result.ok();
    }
}
