package com.ruoyi.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.demo.entity.WdInfo;
import com.ruoyi.demo.entity.vo.WdInfoVo;
import com.ruoyi.demo.mapper.WdInfoMapper;
import com.ruoyi.demo.service.WdRecycleService;
import com.ruoyi.demo.utils.InitMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class WdRecycleServiceImpl implements WdRecycleService {



    @Autowired
    WdInfoMapper wdInfoMapper;


    @Autowired
    InitMapUtil initMapUtil;



    @Override
    public HashMap<String, Object>  list(WdInfoVo wdInfoVo) {

        PageHelper.startPage(wdInfoVo.getPageNum(), wdInfoVo.getPageSize());
        PageHelper.orderBy(wdInfoVo.getOrderBy()+" desc");
        List<WdInfo> wdInfos = wdInfoMapper.list(wdInfoVo);
        PageInfo<WdInfo> pageInfo = new PageInfo<>(wdInfos);

        List<WdInfoVo> collect = wdInfos.stream().map(wdInfo -> {
            if(wdInfo.getTypeNameBy() != null){
                String[] split = wdInfo.getTypeNameBy().split(":");
                if(split.length == 3)
                    wdInfo.setTypeNameBy(split[2]);
                else if(split.length == 2)
                    wdInfo.setTypeNameBy(split[1]);
                else
                    wdInfo.setAddrInfo(wdInfo.getTypeNameBy().replace(":",""));
            }

            WdInfoVo wdInfoVo1 = new WdInfoVo(wdInfo);
            wdInfoVo1.setAddrCodeInfo(initMapUtil.getInitAddrCodeMap(wdInfoVo1.getAddrCode()));
            return wdInfoVo1;
        }).collect(Collectors.toList());

        HashMap<String, Object> result = new HashMap<>();
        pageInfo.setList(null);
        result.put("page",pageInfo);
        result.put("data",collect);
        return result;
    }

    @Override
    public void deleteWd(String wdId) {
        wdInfoMapper.deleteById(wdId);
        return;
    }

    @Override
    public void recoverWd(String wdId) {
        wdInfoMapper.recoverWd(wdId);
    }


}
