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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.modules.serviceOrder.dao.PensionServiceDao;
import io.hmit.modules.serviceOrder.dto.PensionServiceDTO;
import io.hmit.modules.serviceOrder.entity.PensionServiceEntity;
import io.hmit.modules.serviceOrder.service.PensionServiceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 服务表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-01-26
 */
@Service
public class PensionServiceServiceImpl extends CrudServiceImpl<PensionServiceDao, PensionServiceEntity, PensionServiceDTO> implements PensionServiceService {

    @Override
    public QueryWrapper<PensionServiceEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String pid = params.get("pid") ==null?"":params.get("pid").toString();
        String isFree = (String)params.get("isFree");


        QueryWrapper<PensionServiceEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(pid), "pid", pid);
        wrapper.eq(StringUtils.isNotBlank(isFree), "is_free", isFree);

        return wrapper;
    }


    @Override
    public List<PensionServiceDTO> getFirstServiceList(Map<String, Object> params) {
        String isFree = (String)params.get("isFree");
        List<PensionServiceEntity> pensionServiceLists = baseDao.selectList(
                new QueryWrapper<PensionServiceEntity>().eq("pid",0));

        if(!"".equals(isFree) && isFree != null){
            List<PensionServiceEntity> entityList = new ArrayList<>();
            pensionServiceLists.stream().forEach(item->{
                Map<String, Object> pid = new HashMap<>();
                pid.put("pid",item.getId());
                pid.put("isFree",isFree);
                if(list(pid).size()>0){
                    entityList.add(item);
                }
            });
            return ConvertUtils.sourceToTarget(entityList,PensionServiceDTO.class) ;
        }

        return ConvertUtils.sourceToTarget(pensionServiceLists,PensionServiceDTO.class) ;
    }
}
