package com.ruoyi.zhushi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhushi.entity.*;
import com.ruoyi.zhushi.mapper.DkAttendanceGroupMapper;
import com.ruoyi.zhushi.mapper.DkConfigMapper;
import com.ruoyi.zhushi.service.DkConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DkConfigServiceImpl implements DkConfigService {


    @Autowired
    private DkConfigMapper dkConfigMapper;

    @Autowired
    private DkAttendanceGroupMapper dkAttendanceGroupMapper;


    @Override
    public List<DkCheckInConfigDTO> queryList(DkCheckInConfigDTO dto) {
        LambdaQueryWrapper<DkCheckInConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq((dto.getEnabled() != null && dto.getEnabled()), DkCheckInConfig::getEnabled, dto.getEnabled());
        List<DkCheckInConfig> dkCheckInConfigs = dkConfigMapper.selectList(queryWrapper);
        List<DkCheckInConfigDTO> dkCheckInConfigDTOS = dkCheckInConfigs.stream()
            .map(dkCheckInConfig -> {
                DkCheckInConfigDTO dkCheckInConfigDTO = new DkCheckInConfigDTO();
                dkCheckInConfigDTO.setId(dkCheckInConfig.getId());
                dkCheckInConfigDTO.setName(dkCheckInConfig.getName());
                dkCheckInConfigDTO.setRadius(dkCheckInConfig.getRadius());
                dkCheckInConfigDTO.setEnabled(dkCheckInConfig.getEnabled());
                dkCheckInConfigDTO.setLng(dkCheckInConfig.getLng());
                dkCheckInConfigDTO.setLat(dkCheckInConfig.getLat());
                // 这里需要返回DTO对象
                return dkCheckInConfigDTO;
            })
            .collect(Collectors.toList());

        // 返回转换后的DTO列表，而不是null
        return dkCheckInConfigDTOS;
    }

    @Override
    public boolean addConfig(DkCheckInConfigDTO dkCheckInConfigDTO) {
        DkCheckInConfig dkCheckInConfig = BeanUtil.toBean(dkCheckInConfigDTO, DkCheckInConfig.class);
//        DkCheckInConfig dkCheckInConfig = new DkCheckInConfig();
//        dkCheckInConfig.setAreaName(dkCheckInConfigDTO.getAreaName());
//        dkCheckInConfig.setCheckInRadious(dkCheckInConfigDTO.getCheckInRadious());
//        dkCheckInConfig.setEnableStatus(dkCheckInConfigDTO.getEnableStatus());
//        dkCheckInConfig.setLatitude(dkCheckInConfigDTO.getLatitude());
//        dkCheckInConfig.setLongitude(dkCheckInConfigDTO.getLongitude());
//        dkCheckInConfig.setCreateTime(new Date());
//        dkCheckInConfig.setUpdateTime(new Date());
//        dkCheckInConfig.setCreateBy(dkCheckInConfigDTO.getCreateBy());
        return dkConfigMapper.insertOrUpdate(dkCheckInConfig);
    }

    @Override
    public int deleteConfig(long id) {
        // 根据id查询配置是否被使用，如果被使用则不允许删除
        List<DkAttendanceGroup> dkAttendanceGroups = dkAttendanceGroupMapper.selectList(new LambdaQueryWrapper<DkAttendanceGroup>().eq(DkAttendanceGroup::getAreaId, id));
        if(dkAttendanceGroups.size() > 0){
            return -1;
        }
        return dkConfigMapper.deleteById(id);
    }

    @Override
    public int updateConfig(DkCheckInConfigDTO dkCheckInConfigDTO) {
        DkCheckInConfig dkCheckInConfig = new  DkCheckInConfig();
        dkCheckInConfig.setId(dkCheckInConfigDTO.getId());
        dkCheckInConfig.setEnabled(dkCheckInConfigDTO.getEnabled());
        return dkConfigMapper.updateById(dkCheckInConfig);
    }
}
