package com.ruoyi.benyun.service.impl;


import com.ruoyi.benyun.service.WdInfoService;
import com.ruoyi.benyun.utils.CategoryUtil;
import com.ruoyi.benyun.entity.WdOpclose;
import com.ruoyi.benyun.entity.vo.AddStatusVo;
import com.ruoyi.benyun.entity.vo.WdOpcloseVo;
import com.ruoyi.benyun.mapper.WdInfoMapper;
import com.ruoyi.benyun.mapper.WdOpcloseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class WdInfoServiceImpl implements WdInfoService {
    @Autowired
    CategoryUtil categoryUtil;
    @Autowired
    WdInfoMapper wdInfoMapper;
    @Autowired
    WdOpcloseDao wdOpcloseDao;

    @Override
    public List<AddStatusVo> searchAddStatus(List<String> addrCodes, List<String> typeCodes) {
        categoryUtil.setWdTypeMap();
        List<String> addrs = new ArrayList<>();
        List<String> times = new ArrayList<>();
        for (int i = 0; i < 12; i++){
            times.add(LocalDateTime.now(ZoneId.of("Asia/Shanghai")).plusMonths(-i).format(DateTimeFormatter.ofPattern("yyyy-MM")));
        }
        if (addrCodes != null){
            for (String s : addrCodes){
                List<String> allOtherAddrCode = categoryUtil.getAllOtherAddrCode(s);
                addrs.addAll(allOtherAddrCode);
            }
        }else{
            addrs = null;
        }
        List<AddStatusVo> addStatusVos = wdInfoMapper.searchAddStatusByMulti(times, addrs, typeCodes);
        for (AddStatusVo vo : addStatusVos){
            vo.setTypeName(categoryUtil.wdTypeMap.get(vo.getTypeCode()));
        }
        return addStatusVos;
    }

    @Override
    public List<WdOpcloseVo> searchWdOpcloses(List<String> addrCodes, List<String> wdTypeCodes) {
        categoryUtil.setAddrMap();

        List<WdOpcloseVo> vos = new ArrayList<>();
        if (addrCodes != null){
            for (String s : addrCodes){
                WdOpclose opclose = wdOpcloseDao.selectListByAddrCodeAndWdTypeCodes(categoryUtil.whatis(s), wdTypeCodes, LocalDateTime.now(ZoneId.of("Asia/Shanghai")).plusMonths(-1));
                if (opclose != null){
                    WdOpcloseVo vo = new WdOpcloseVo(s,opclose);
                    vo.setAddrName(categoryUtil.addrMap.get(s));
                    vos.add(vo);
                }
            }
            return vos;
        }
        return vos;
    }

//    @Override
//    public List<WdAddrTypeDistribution> searchAddrTypeDistribution(List<String> addrCodes, List<String> typeCodes) {
//        List<String> acodes = new ArrayList<>();
//        for (String s : addrCodes){
//            List<String> allOtherAddrCode = getAllOtherAddrCode(s);
//            acodes.addAll(allOtherAddrCode);
//        }
//        List<WdAddrTypeDistribution> list = wdInfoMapper.searchAddrDisByMulti(acodes, typeCodes);
//        setTypeMap();
//        setAddrMap();
//        for (WdAddrTypeDistribution wd : list){
//            wd.setTypeName(typeMap.get(wd.getTypeCode()));
//            wd.setAddrName(addrMap.get(wd.getAddrCode()));
//        }
//        return list;
//    }

//    @Override
//    public List<WdAddrTypeDistribution> searchWdTypeDistribution(List<String> addrCodes, List<String> typeCodes) {
//        List<WdAddrTypeDistribution> list = wdInfoMapper.searchWdTypeDisByMulti(addrCodes, typeCodes);
//        setAddrMap();
//        setWdTypeMap();
//        for (WdAddrTypeDistribution wd : list){
//            wd.setAddrName(addrMap.get(wd.getAddrCode()));
//            wd.setTypeName(wdTypeMap.get(wd.getTypeCode()));
//        }
//        return list;
//    }

//    @Override
//    public List<WdAddrTypeDistribution> searchWdTotal() {
//        List<WdAddrTypeDistribution> list = wdInfoMapper.searchWdTotal();
//        setWdTypeMap();
//        for (WdAddrTypeDistribution wd : list){
//            wd.setTypeName(wdTypeMap.get(wd.getTypeCode()));
//        }
//        return list;
//    }

}
