package com.ghy.worker.service.impl;

import com.ghy.common.core.text.Convert;
import com.ghy.common.enums.WorkerStatus;
import com.ghy.common.utils.StringUtils;
import com.ghy.worker.domain.Worker;
import com.ghy.worker.domain.WorkerTeam;
import com.ghy.worker.mapper.WorkerMapper;
import com.ghy.worker.mapper.WorkerTeamMapper;
import com.ghy.worker.service.WorkerService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service
public class WorkerServiceImpl implements WorkerService {

    @Resource
    private WorkerMapper workerMapper;
    @Resource
    private WorkerTeamMapper workerTeamMapper;

    @Override
    public List<Worker> getWorkList(Worker worker) {
        return workerMapper.getWorkerList(worker);
    }

    @Override
    public Worker selectByOpenId(Worker worker) {
        List<Worker> list = workerMapper.getWorkerList(worker);
        if (StringUtils.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public int insertWorker(Worker worker) {
        return workerMapper.insertWorker(worker);
    }

    @Override
    public int updateWorker(Worker worker) {
        return workerMapper.updateWorker(worker);
    }

    @Override
    public List<Worker> getWorkByPhoneAndPwd(Worker worker) {
        return workerMapper.getWorkByPhoneAndPwd(worker);
    }

    @Override
    public Worker selectById(Long workerId) {
        return workerMapper.selectById(workerId);
    }

    @Override
    public boolean checkInTeam(Long leaderId, Long workerId) {
        WorkerTeam query = new WorkerTeam();
        query.setLeaderId(leaderId);
        query.setWorkerId(workerId);
        List<WorkerTeam> teams = workerTeamMapper.getWorkerTeamList(query);
        return !CollectionUtils.isEmpty(teams);
    }

    @Override
    public Map<Long, Worker> byWorkUserIdInMap(List<Long> workUserIdList) {
        Map<Long, Worker> longWorkerMap = new HashMap<>();
        if(workUserIdList != null && workUserIdList.size() > 0){
            List<Worker> workerList = workerMapper.selectByIds(workUserIdList);
            if(workerList != null && workerList.size() > 0){
                for(Worker worker : workerList){
                    longWorkerMap.put(worker.getWorkerId(), worker);
                }
            }
        }
        return longWorkerMap;
    }

    @Override
    public List<Worker> selectByIds(Set<Long> workerIds) {
        if (CollectionUtils.isEmpty(workerIds)){
            return new ArrayList<>();
        }
        return workerMapper.selectByIds(workerIds);
    }

    @Override
    public int deleteWorkerByIds(String ids) {
        Long[] workerIds = Convert.toLongArray(ids);
        int result = 0;
        for (Long workerId : workerIds) {
            Worker worker = new Worker();
            worker.setWorkerId(workerId);
            worker.setStatus(WorkerStatus.DELETED.getCode());
            result += workerMapper.updateWorker(worker);
        }
        return result;
    }
}
