/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.maintain.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.api.DepartmentClient;
import com.tofly.api.UserClent;
import com.tofly.gpsboot.mapper.maintain.MobileDeviceMapper;
import com.tofly.gpsboot.service.maintain.MobileDeviceService;
import com.tofly.pojo.Department;
import com.tofly.pojo.User;
import com.tofly.pojo.maintain.MobileDevice;
import com.tofly.pojo.maintain.vo.MobileDeviceVo;
import com.tofly.pojo.maintain.vo.UserViewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 移动设备信息表
 *
 * @author wangzhun
 * @date Mon May 16 00:00:00 CST 2022
 */
@Service("mobileDeviceService")
public class MobileDeviceServiceImpl extends ServiceImpl<MobileDeviceMapper, MobileDevice> implements MobileDeviceService {

    @Autowired
    private DepartmentClient departmentClient;
    @Autowired
    private UserClent userClent;
    @Override
    public MobileDeviceVo entityToVo(MobileDevice mobileDevice) {
        MobileDeviceVo mobileDeviceVo = JSONObject.parseObject(JSON.toJSONString(mobileDevice),MobileDeviceVo.class);
        Long useDeptId = mobileDevice.getUseDeptId();
        Department result = departmentClient.getById(useDeptId).getResult();
        if(result!=null){
            Map map = new HashMap<>();
            map.put("departmentName",result.getName());
            map.put("departmentId",useDeptId);
            mobileDeviceVo.setDepartment(map);
        }
        Long userUserId = mobileDevice.getUserUserId();
        User result1 = userClent.getById(userUserId).getResult();
        if(result1!=null){
            UserViewVo userViewVo = new UserViewVo();
            userViewVo.setUserName(result1.getRealName());
            userViewVo.setId(result1.getId());
            userViewVo.setPhone(result1.getPhone());
            userViewVo.setWorknumber(result1.getWorknumber());
            mobileDeviceVo.setUseUser(userViewVo);
        }


        return mobileDeviceVo;
    }

    @Override
    public List<User> findMobileUser(Long deptId) {
        return baseMapper.findMobileUser(deptId);
    }

    /**
     * 判定设备是否已经注册过
     * @param mobileDevice
     */
    @Override
    public void checkHasRegister(MobileDevice mobileDevice) {
        int count =
                this.count(Wrappers.<MobileDevice>lambdaQuery()
                        .eq(MobileDevice::getMeid, mobileDevice.getMeid())
                        .eq(MobileDevice::getStatus,"1")
                        .ne(mobileDevice.getId() !=null,MobileDevice::getId,mobileDevice.getId())
                ) ;
        Assert.isTrue(count==0,"该设备已注册，请不要重复注册");

        int count1 = this.count(Wrappers.<MobileDevice>lambdaQuery()
                .eq(MobileDevice::getUserUserId, mobileDevice.getUserUserId())
                .eq(MobileDevice::getStatus, "1")
                .ne(mobileDevice.getId() !=null,MobileDevice::getId,mobileDevice.getId())
        );
        Assert.isTrue(count1==0,"该用户已绑定其它设备，请先解绑");

    }
}
