package com.zhy.service.impl;

import com.zhy.mapper. ArchiveMapper;
import com.zhy.service. ArchiveService;
import com.zhy.service. ArticleService;
import com.zhy.utils. DataMap;
import com.zhy.utils. TimeUtil;
import net.sf.json. JSONArray;
import net.sf.json. JSONObject;
import org.springframework.beans.factory.annotation. Autowired;
import org.springframework.stereotype. Service;

import java.util. List;

/**
 * @author: zhangocean
 * @Date: 2018/7/18 12:08
 * Describe:
 */
@Service
public class ArchiveServiceImpl implements ArchiveService {

    @Autowired
    ArchiveMapper archiveMapper;
    @Autowired
    ArticleService articleService;

    @Override
    public DataMap findArchiveNameAndArticleNum() {
        List<String> archives = archiveMapper.findArchives();
        JSONArray archivesJsonArray = new JSONArray();
        JSONObject archiveJson;
        TimeUtil timeUtil = new TimeUtil();
        for(String archiveName : archives){
            archiveJson = new JSONObject();
            archiveJson.put("archiveName",archiveName);
            archiveName = timeUtil.timeYearToWhippletree(archiveName);
            archiveJson.put("archiveArticleNum",articleService.countArticleArchiveByArchive(archiveName));
            archivesJsonArray.add(archiveJson);
        }
        JSONObject returnJson = new JSONObject();
        returnJson.put("result", archivesJsonArray);
        return DataMap.success().setData(returnJson);
    }

    @Override
    public void addArchiveName(String archiveName) {
        int archiveNameIsExist = archiveMapper.findArchiveNameByArchiveName(archiveName);
        if(archiveNameIsExist == 0){
            archiveMapper.save(archiveName);
        }
    }

}
