package com.yiboshi.science.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiboshi.science.base.BaseServiceImpl;
import com.yiboshi.science.dao.ComExpertSpecDAO;
import com.yiboshi.science.entity.ComExpertSpec;
import com.yiboshi.science.entity.SystemMenu;
import com.yiboshi.science.entity.SelectListItem;
import com.yiboshi.science.param.dto.ComExpertDTO;
import com.yiboshi.science.param.dto.ComExpertSpecDTO;
import com.yiboshi.science.param.query.ComExpertSpecQueryVO;
import com.yiboshi.science.service.ComExpertSpecService;
import com.yiboshi.science.utils.AgeUtil;
import com.yiboshi.science.utils.StringUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static java.util.Objects.isNull;

@Service
@AllArgsConstructor
public class ComExpertSpecServiceImpl extends BaseServiceImpl<ComExpertSpecDAO, ComExpertSpecQueryVO, ComExpertSpecDTO, ComExpertSpec> implements ComExpertSpecService {

    @Autowired
    private ComExpertSpecDAO comExpertSpecDAO;

    @Override
    protected void setCriteriaForQuery(ComExpertSpecQueryVO vo, QueryWrapper<ComExpertSpecQueryVO> criteria) {
        if (Objects.nonNull(vo.getExpertId())) {
            criteria.eq("expert_id", vo.getExpertId());
        }
        if (Objects.nonNull(vo.getSpecId())) {
            criteria.eq("spec_id", vo.getExpertId());
        }
    }

    public void insertSpecList(List<String> list, String expertId) {
        ComExpertSpec comExpertSpec = new ComExpertSpec();
        comExpertSpec.setExpertId(expertId);
        this.delete(comExpertSpec);
        if (null != list) {
            list.forEach((e) -> {
                if (!isNull(e)) {
                    ComExpertSpec expertSpec = new ComExpertSpec();
                    expertSpec.setExpertId(expertId);
                    expertSpec.setSpecId(e);
                    this.insert(expertSpec);
                }
            });
        }
    }

    public List<String> getIdListByExpertId(String expertId) {
        return comExpertSpecDAO.getIdListByExpertId(expertId);
    }

    public  List<ComExpertSpecDTO> getListByExpertId(String expertId){
        return comExpertSpecDAO.getListByExpertId(expertId);
    }

    public List<SelectListItem> getExpertListBySpecId(Map<String, Object> specId) {
        List<String> idList = null;
        if (specId.containsKey("specId")) {
            idList = (List<String>) specId.get("specId");
        }
        QueryWrapper<SystemMenu> wrapper = new QueryWrapper<SystemMenu>();
        if (Objects.nonNull(idList) && idList.size() > 0) {
            List<String> item = idList.stream().filter(f -> !f.equals("")).collect(Collectors.toList());
            if (Objects.nonNull(item) && item.size() > 0)
                wrapper.in("a.spec_id", item).or().in("d.parent_id", item);
//            wrapper.or(qw -> qw.in("a.spec_id", finalIdList).in("d.parent_id", finalIdList));
        }
        List<ComExpertDTO> list = comExpertSpecDAO.getExpertListBySpecId(wrapper);
        List<SelectListItem> allItem = new ArrayList<>();
        if (null != list && list.size() > 0) {
            list.forEach((e) -> {
                String description=e.getSex()+"/"+ AgeUtil.getResidentAge(e.getBirthday())+"岁/"+e.getWorkUnit();
                SelectListItem treeListItem = new SelectListItem(e.getPersonName(), e.getId(), description, false,false, new ArrayList<>());
                allItem.add(treeListItem);
            });
        }
        return allItem;
    }

    public List<SelectListItem> getExpertListBySpecIdProjId(List<String> SpecList, List<String> ProjList) {
        List<ComExpertDTO> list = comExpertSpecDAO.getExpertListBySpecIdProjId(SpecList, ProjList);
        List<SelectListItem> allItem = new ArrayList<>();
        if (null != list && list.size() > 0) {
            list.forEach((e) -> {
                String description=e.getSex()+"/"+ AgeUtil.getResidentAge(e.getBirthday())+"岁/"+e.getWorkUnit();
                SelectListItem treeListItem = new SelectListItem(e.getPersonName(), e.getId(), description, false,false, new ArrayList<>());
                allItem.add(treeListItem);
            });
        }
        return allItem;
    }

    public List<SelectListItem> getExpertListByExpertSpecIds(List<String> SpecList, List<String> ParentList, String personName) {
        List<ComExpertDTO> list = comExpertSpecDAO.getExpertListByExpertSpecIds(SpecList, ParentList, personName);
        List<SelectListItem> allItem = new ArrayList<>();
        if (null != list && list.size() > 0) {
            list.forEach((e) -> {
                String description=e.getSex()+"/"+ AgeUtil.getResidentAge(e.getBirthday())+"岁/"+e.getWorkUnit();
                SelectListItem treeListItem = new SelectListItem(e.getPersonName(), e.getId(), description, false,false, new ArrayList<>());
                allItem.add(treeListItem);
            });
        }
        return allItem;
    }
}
