package com.xunmei.system.service.impl;

import java.util.List;

import com.xunmei.common.core.utils.DateUtils;
import com.xunmei.common.security.utils.SecurityUtils;
import com.xunmei.system.api.domain.SysDevice;
import com.xunmei.system.api.domain.SysOrg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xunmei.system.service.ISysOrgService;
import java.util.Arrays;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunmei.common.core.web.page.TableDataInfo;
import com.xunmei.system.mapper.SysDeviceMapper;
import com.xunmei.system.service.ISysDeviceService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-22
 */
@Service
public class SysDeviceServiceImpl extends ServiceImpl<SysDeviceMapper, SysDevice> implements ISysDeviceService {
    @Autowired
    private SysDeviceMapper sysDeviceMapper;
    @Autowired
    private ISysOrgService orgService;

    @Override
    public TableDataInfo<SysDevice> selectPage(SysDevice sysDevice) {
        //未删除
        sysDevice.setDelFlag("0");
        Page<SysDevice> page;
        //分页
        if (sysDevice.getPageNum() != null && sysDevice.getPageSize() != null) {
            page = new Page<>(sysDevice.getPageNum(), sysDevice.getPageSize());
        } else {
            page = new Page<>();
        }
        //查询条件
        QueryWrapper<SysDevice> query = new QueryWrapper<>(sysDevice);
        //下穿
        if (sysDevice.getCheckSub()) {
            List<Long> ids = orgService.selectCheckSubOrgIdList(sysDevice.getOrgId());
            //清空前端传递的org_id
            sysDevice.setOrgId(null);
            //添加in条件
            query.in("org_id", ids);
        }
        //时间范围查询
        if (sysDevice.getParams().get("beginTime") != null && sysDevice.getParams().get("endTime") != null) {
            query.between("create_time", sysDevice.getParams().get("beginTime"), sysDevice.getParams().get("endTime"));
        }
        //获取数据
        page = sysDeviceMapper.selectPage(page, query);
        //抓换为TableDataInfo适配前端
        return TableDataInfo.build(page);


    }


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysDevice selectSysDeviceById(Long id) {
        return sysDeviceMapper.selectById(id);
    }

    @Override
    public List<SysDevice> getHostByOrgId(Long orgId) {
        return sysDeviceMapper.getHostByOrgId(orgId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysDevice 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysDevice> selectSysDeviceList(SysDevice sysDevice) {
        return sysDeviceMapper.selectList(new QueryWrapper<>(sysDevice));
    }

    @Override
    public List<SysDevice> getSysDeviceByHostId(Long hostId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("del_flag","0");
        queryWrapper.eq("host_id",hostId);
        List<SysDevice> list = baseMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysDevice 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysDevice(SysDevice sysDevice) {
        sysDevice.setCreateTime(DateUtils.getNowDate());
        sysDevice.setCreateBy(SecurityUtils.getUsername());
        SysOrg sysOrg = orgService.selectSysOrgById(sysDevice.getOrgId());
        sysDevice.setOrgName(sysOrg.getName());
        return sysDeviceMapper.insert(sysDevice);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysDevice 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysDevice(SysDevice sysDevice) {
        sysDevice.setUpdateTime(DateUtils.getNowDate());
        sysDevice.setUpdateBy(SecurityUtils.getUsername());
        SysOrg sysOrg = orgService.selectSysOrgById(sysDevice.getOrgId());
        sysDevice.setOrgName(sysOrg.getName());
        return sysDeviceMapper.updateById(sysDevice);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysDeviceByIds(Long[] ids) {
        return sysDeviceMapper.deleteBatchIds(Arrays.asList((ids)));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysDeviceById(Long id) {
        return sysDeviceMapper.deleteById(id);
    }
}
