package com.willalp.device.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.core.domain.entity.SysUser;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.StringUtils;
import com.willalp.device.domain.Device;
import com.willalp.device.domain.vo.DeviceInitVo;
import com.willalp.device.mapper.DeviceMapper;
import com.willalp.device.service.IDeviceService;
import com.willalp.system.mapper.SysUserMapper;
import com.willalp.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * deviceService业务层处理
 *
 * @author willalp
 * @date 2021-11-23
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {
    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private ISysUserService userService;

    @Resource
    private SysUserMapper userMapper;

    /**
     * 查询device列表
     *
     * @param device device
     * @return device
     */
    @Override
    public List<Device> selectDeviceList(Device device) {
        return deviceMapper.selectDeviceList(device);
    }

    @Override
    public String deviceInit(DeviceInitVo deviceInitVo) {
        String initCode = deviceInitVo.getInitCode();
        String orgCode = deviceInitVo.getOrgCode();
        String jcdCode = deviceInitVo.getJcdCode();
        if (StringUtils.isBlank(initCode)) {
            throw new BaseException("初始化码不能为空");
        }
        if (StringUtils.isBlank(orgCode)) {
            throw new BaseException("机构不能为空");
        }
        if (StringUtils.isBlank(jcdCode)) {
            throw new BaseException("就餐地不能为空");
        }
        QueryWrapper<Device> qw = new QueryWrapper<>();
        qw.eq("device_init_code", initCode);
        Device device = deviceMapper.selectOne(qw);
        if (ObjectUtil.isNull(device)) {
            throw new BaseException("非法初始化码");
        }
        if (!orgCode.equals(device.getDeviceOrgId())) {
            throw new BaseException("机构不匹配");
        }
        if (!jcdCode.equals(device.getPlaceNumber())) {
            throw new BaseException("就餐地不匹配");
        }
        return device.getDeviceNo();
    }

    @Override
    public List<SysUser> getDeviceBindingUsers(String deviceId) {
        if (StringUtils.isBlank(deviceId)) {
            throw new BaseException("设备ID不能为空");
        }
        String deviceBindingUsers = this.baseMapper.getDeviceBindingUsers(deviceId);
        if (StringUtils.isBlank(deviceBindingUsers)) {
            throw new BaseException("暂无绑定用户");
        }
        String[] split = deviceBindingUsers.split(",");
        return userService.getUserBindingList(split);
    }


    @Override
    public List<SysUser> selectAllUserList(String userType) {
        return userMapper.getAllUserListByUserType(userType);
    }

    @Override
    public int bindingUser(String deviceId, String userId) {
        if (StringUtils.isBlank(deviceId)) {
            throw new BaseException("设备ID不能为空");
        }
        Device device = this.baseMapper.selectById(deviceId);
        String deviceBindingUsers = device.getDeviceUseId();
        if (StringUtils.isBlank(deviceBindingUsers)) {
            device.setDeviceUseId(userId);
            return this.baseMapper.updateById(device);
        }
        String[] split = deviceBindingUsers.split(",");
        for (String s : split) {
            if (StringUtils.equals(s, userId)) {
                throw new BaseException("该用户已绑定，请勿重复绑定");
            }
        }
        device.setDeviceUseId(
                deviceBindingUsers + "," + userId);
        return this.baseMapper.updateById(device);
    }

    @Override
    public int untieUser(String deviceId, String userId) {
        if (StringUtils.isBlank(deviceId)) {
            throw new BaseException("设备ID不能为空");
        }
        Device device = this.baseMapper.selectById(deviceId);
        String deviceBindingUsers = device.getDeviceUseId();
        if (StringUtils.isBlank(deviceBindingUsers)) {
            throw new BaseException("暂无绑定用户");
        }
        String[] split = deviceBindingUsers.split(",");
        StringBuilder newUserIds = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            if (StringUtils.equals(split[i], userId)) {
                if (i == split.length - 1) {
                    newUserIds.deleteCharAt(newUserIds.length() - 1);
                    break;
                }
                continue;
            }
            if (i == split.length - 1) {
                newUserIds.append(split[i]);
                break;
            }
            newUserIds.append(split[i]).append(",");
        }
        device.setDeviceUseId(newUserIds.toString());
        return this.baseMapper.updateById(device);
    }
}
