package com.ruoyi.demo.service.impl;

import com.ruoyi.demo.entity.vo.AddStatusVo;
import com.ruoyi.demo.entity.vo.OpcloseTimeVo;
import com.ruoyi.demo.mapper.StoreWdOpcloseDao;
import com.ruoyi.demo.mapper.WdInfoMapper;
import com.ruoyi.demo.mapper.WdOpcloseDao;
import com.ruoyi.demo.service.WdInfoService;
import com.ruoyi.demo.utils.CategoryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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

    @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 || !addrCodes.isEmpty()) {
            for (String s : addrCodes)
                addrs.addAll(categoryUtil.getAllOtherAddrCode(s));
        } else
            addrs = null;

        List<AddStatusVo> addStatusVos = wdInfoMapper.searchAddStatusByMulti(times, addrs, typeCodes);
        addStatusVos.forEach(vo -> vo.setTypeName(categoryUtil.wdTypeMap.get(vo.getTypeCode())));
        return addStatusVos;
    }

    public Integer timeUtil(String time) {
        String[] split = time.split(":");
        int hour = Integer.parseInt(split[0]);
        int min = Integer.parseInt(split[1]);
        return hour * 100 + min;
    }

    public int compareTime(String time1, String time2) {
        return timeUtil(time1).compareTo(timeUtil(time2));
    }

    @Override
    public List<OpcloseTimeVo> searchOpenTimes(List<String> addrCodes, List<String> typeCodeBys) {
        // 补齐地址码和类型码
        List<String> addrs = new ArrayList<>();
        if (addrCodes != null || !addrCodes.isEmpty()) {
            for (String addr : addrCodes)
                addrs.addAll(categoryUtil.getAllOtherAddrCode(addr));
        } else
            addrs = null;
        List<String> types = new ArrayList<>();
        if (typeCodeBys != null || !typeCodeBys.isEmpty()) {
            for (String type : typeCodeBys)
                types.addAll(categoryUtil.getAllOtherTypeBy(type));
        } else
            types = null;

        List<OpcloseTimeVo> vos = storeWdOpcloseDao.selectWdOpenTimes(addrs, types);
        // 初始化
        List<OpcloseTimeVo> rvos = new ArrayList<>();
        for (int i = 0; i < 24; i += 2) {
            OpcloseTimeVo vo = new OpcloseTimeVo();
            vo.setTime(i + ":00~" + (i + 2) + ":00");
            vo.setCount(0);
            rvos.add(vo);
        }
        // 赋值
        vos.forEach(vo -> {
            for (OpcloseTimeVo rvo : rvos) {
                String[] times = rvo.getTime().split("~");
                if (compareTime(vo.getTime(), times[0]) < 0 || compareTime(times[1], vo.getTime()) < 0)
                    continue;
                rvo.setCount(rvo.getCount() + vo.getCount());
            }
        });
        return rvos;
    }

    @Override
    public List<OpcloseTimeVo> searchCloseTimes(List<String> addrCodes, List<String> typeCodeBys) {
        // 补齐地址码和类型码
        List<String> addrs = new ArrayList<>();
        if (addrCodes != null || !addrCodes.isEmpty()) {
            for (String addr : addrCodes)
                addrs.addAll(categoryUtil.getAllOtherAddrCode(addr));
        } else
            addrs = null;
        List<String> types = new ArrayList<>();
        if (typeCodeBys != null || !typeCodeBys.isEmpty()) {
            for (String type : typeCodeBys)
                types.addAll(categoryUtil.getAllOtherTypeBy(type));
        } else
            types = null;

        List<OpcloseTimeVo> vos = storeWdOpcloseDao.selectWdCloseTimes(addrs, types);
        // 初始化
        List<OpcloseTimeVo> rvos = new ArrayList<>();
        for (int i = 0; i < 24; i += 2) {
            OpcloseTimeVo vo = new OpcloseTimeVo();
            vo.setTime(i + ":00~" + (i + 2) + ":00");
            vo.setCount(0);
            rvos.add(vo);
        }
        // 赋值
        vos.forEach(vo -> {
            for (OpcloseTimeVo rvo : rvos) {
                String[] times = rvo.getTime().split("~");
                if (compareTime(vo.getTime(), times[0]) < 0 || compareTime(times[1], vo.getTime()) < 0)
                    continue;
                rvo.setCount(rvo.getCount() + vo.getCount());
            }
        });
        return rvos;
    }
}
