package com.lovenav.service.serviceImpl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.lovenav.dao.UrlCateListDao;
import com.lovenav.dao.UrlListDao;
import com.lovenav.dao.UserDao;
import com.lovenav.entity.UrlCateList;
import com.lovenav.entity.UrlList;
import com.lovenav.entity.User;
import com.lovenav.service.UrlCateListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Service("urlCateListService")
public class UrlCateListServiceImpl implements UrlCateListService {



    @Autowired
    UrlCateListDao urlCateListDao;
    @Autowired
    UrlListDao urlListDao;
    @Autowired
    UserDao userDao;
    public int selectAndInsertUrlCate(String email , String cateName , String parent )
    {
        //找用户ID
        User user = userDao.selectByEmail(email);
        int userId = user.getId();

        //找父标签有没有
        UrlCateList cateParent = urlCateListDao.selectCateByNameAnduserId(parent,userId);

        UrlCateList targetCate = new UrlCateList();
        //设置属性
        targetCate.setName(cateName);
        targetCate.setUserId(userId);
        Date date = new Date();
        targetCate.setCreatetime(date.getTime());

        if(cateParent != null)
        {
            targetCate.setRootCateId(cateParent.getId());
        }else{
            targetCate.setRootCateId(0);
        }
        //插入
        int flag = urlCateListDao.insert(targetCate);
        return flag;
    }

    public List<UrlCateList> selectUrListByUserId(Integer userId){
        return urlCateListDao.selectUrListByUserId(userId);
    }

    public String selectUrListCateByUrlCateId(Integer cateId){
        return urlCateListDao.selectUrListCateByUrlCateId(cateId);
    }


    public int insertSelective(UrlCateList urlCateList)
    {
        return urlCateListDao.insertSelective(urlCateList);
    }

    public int updateByUrlCateList(UrlCateList urlCateList)
    {
        return urlCateListDao.updateByPrimaryKeySelective(urlCateList);
    }

    public List<String> selectChildrenByCateId(Integer id)
    {
        return urlCateListDao.selectChildrenByCateId(id);
    }



    public UrlCateList selectByPrimaryKey(Integer id)
    {
        return urlCateListDao.selectByPrimaryKey(id);
    }

    public String countCateContainUrlNumber(String email){
        User user = userDao.selectByEmail(email);
        int userId = user.getId();
        List<UrlCateList> urlCateLists =urlCateListDao.selectUrListByUserId(userId);
        List<UrlList> urlLists = urlListDao.selectUrList();
        HashMap<String,Integer> CateNumber = new HashMap<>();

        for(UrlCateList urlCateList :urlCateLists)
        {
            CateNumber.put(urlCateList.getName(),0);
        }
        for(UrlList urlList : urlLists){
            Long id = Long.valueOf(urlList.getCateId());

            while(id!= 0)
            {
                for(UrlCateList urlCateList :urlCateLists)
                {
                    if(id == Long.valueOf(urlCateList.getId())){
                        int cateNum = CateNumber.get(urlCateList.getName())+1;
                        CateNumber.put(urlCateList.getName(),cateNum);
                        id=Long.valueOf(urlCateList.getRootCateId());
                        break;
                    }
                }
            }



        }
        Iterator iterator = CateNumber.entrySet().iterator();
        while(iterator.hasNext()) {
            Map.Entry<String, Integer> entry=(Map.Entry<String, Integer>) iterator.next();
            for(int i = 0; i<urlCateLists.size();i++)
            {
                UrlCateList urlCateList = urlCateLists.get(i);
                if(urlCateList.getName().equals(entry.getKey()))
                {
                    urlCateList.setUrlNumber(Long.valueOf(entry.getValue()));
                    urlCateListDao.updateByPrimaryKeySelective(urlCateList);
                    break;
                }
            }
        }

        return CateNumber.toString();
    }
    public JSONArray getUrl(String userId)
    {
        List<UrlCateList> urlCateLists =urlCateListDao.selectUrListByUserId(Integer.valueOf(userId));
        List<UrlList> urlLists = urlListDao.selectUrList();
        JSONArray jsonArray  = new JSONArray();
        for(UrlCateList urlCateList : urlCateLists)
        {
            int parentId = urlCateList.getId();
            for(UrlList urlList : urlLists)
            {
                if(urlList.getCateId() == parentId)
                {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put(urlList.getName(),urlList.getUrl());
                    jsonArray.add(jsonObject);
                    break;
                }
            }
        }
        return jsonArray;
    }

    public int deleteByPrimaryKey(Integer id){
        return urlCateListDao.deleteByPrimaryKey(id);
    }
}
