package com.yzt.airconditionersystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzt.airconditionersystem.bean.Gateway;
import com.yzt.airconditionersystem.bean.Operator;
import com.yzt.airconditionersystem.bean.Project;
import com.yzt.airconditionersystem.mapper.GatewayMapper;
import com.yzt.airconditionersystem.mapper.OperatorMapper;
import com.yzt.airconditionersystem.mapper.ProjectMapper;
import com.yzt.airconditionersystem.service.GatewayService;
import com.yzt.airconditionersystem.service.OperatorService;
import com.yzt.airconditionersystem.service.ProjectService;
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 TzZach
 * @since 2022-04-13 12:09
 */
@Service
public class OperatorServiceImpl extends ServiceImpl<OperatorMapper, Operator> implements OperatorService {

    @Autowired
    private OperatorMapper operatorMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private GatewayMapper gatewayMapper;

    @Override
    public Operator getOneByOperator(Operator operator) {
        return operatorMapper.getOne(operator);
    }

    @Override
    public Map<String, Object> deleteOperatorById(Integer id) {
        List<Integer> projectIds = projectMapper.selectIdByOperatorId(id);
        LambdaQueryWrapper<Gateway> gatewayLambdaQueryWrapper = new LambdaQueryWrapper<>();
        gatewayLambdaQueryWrapper.in(Gateway::getCompanyId, projectIds);
        int gateway = gatewayMapper.delete(gatewayLambdaQueryWrapper);

        LambdaQueryWrapper<Project> projectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        projectLambdaQueryWrapper.in(Project::getId, projectIds);
        int project = projectMapper.delete(projectLambdaQueryWrapper);

        int operator = operatorMapper.deleteById(id);

        Map<String, Object> map = new HashMap<>();
        map.put("operator", operator);
        map.put("project", project);
        map.put("gateway", gateway);

        return map;
    }
}
