package io.hmit.modules.serviceOrder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.modules.serviceOrder.dao.PensionServiceDao;
import io.hmit.modules.serviceOrder.dao.PensionServiceTypeDao;
import io.hmit.modules.serviceOrder.dto.PensionServiceDTO;
import io.hmit.modules.serviceOrder.dto.PensionServiceTypeDTO;
import io.hmit.modules.serviceOrder.dto.RoleUserDTO;
import io.hmit.modules.serviceOrder.entity.PensionServiceEntity;
import io.hmit.modules.serviceOrder.entity.PensionServiceTypeEntity;
import io.hmit.modules.serviceOrder.service.PensionServiceService;
import io.hmit.modules.serviceOrder.service.PensionServiceTypeService;
import io.hmit.modules.serviceOrder.service.RoleServiceService;
import io.hmit.modules.serviceOrder.service.RoleUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 服务种类表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-01-26
 */
@Service
public class PensionServiceTypeServiceImpl extends CrudServiceImpl<PensionServiceTypeDao, PensionServiceTypeEntity, PensionServiceTypeDTO> implements PensionServiceTypeService {

    @Autowired
    private RoleServiceService roleServiceService;

    @Autowired
    private PensionServiceService pensionServiceService;

    @Autowired
    private RoleUserService roleUserService;

    @Autowired
    private PensionServiceDao pensionServiceDao;

//    @Autowired
    private BaseMapper baseMapper;

    @Override
    public QueryWrapper<PensionServiceTypeEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<PensionServiceTypeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<PensionServiceTypeDTO> getALLListByRole(Map<String, Object> params) {

        List<Long> servicesTypeIds = roleServiceService.list(params).stream().map(dto -> dto.getServiceTypeId()).collect(Collectors.toList());

        List<PensionServiceTypeDTO> resultList = new ArrayList<>();
        for (Long typeId:servicesTypeIds) {
            PensionServiceTypeDTO pensionServiceTypeDTO = get(typeId);
            resultList.add(pensionServiceTypeDTO);
        }
        return resultList;
    }

    @Override
    public List<PensionServiceTypeDTO> getServiceListByRole(Map<String, Object> params) {


//        List<Long> userRole = roleUserService.list(params).stream().map(dto -> dto.getRoleId()).collect(Collectors.toList());

        List<Long> allServicesTypeIds = roleServiceService.list(params).stream().map(dto -> dto.getServiceTypeId()).collect(Collectors.toList());

        String isFree = (String)params.get("isFree");
        List<PensionServiceEntity> pensionServices = pensionServiceDao.selectList(
                new QueryWrapper<PensionServiceEntity>()
                .in("service_type_id",allServicesTypeIds)
                .eq(StringUtils.isNotBlank(isFree),"is_free",isFree).select("DISTINCT service_type_id"));

//        pensionServices.stream().collect(Collectors.groupingBy(PensionServiceEntity::getServiceTypeId))
        List<PensionServiceTypeDTO> resultList = new ArrayList<>();

/*
        for (PensionServiceEntity pensionService:pensionServices) {
            PensionServiceTypeDTO pensionServiceTypeDTO = get(pensionService.getServiceParentId());
            resultList.add(pensionServiceTypeDTO);
        }
*/
        return resultList;
    }
}
