package com.lovenav.controller;

import com.alibaba.fastjson2.JSONObject;
import com.lovenav.entity.UrlCateList;
import com.lovenav.entity.UrlList;
import com.lovenav.service.UrlCateListService;
import com.lovenav.service.UrlListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

@RestController
public class UrlListController {
    @Autowired
    UrlListService urlListService;
    @Autowired
    UrlCateListService urlCateListService;
    //后台修改网站状态
    @RequestMapping("/admin/weblist/alterUrlStatus")
    public String setUrlStatus(UrlList urlList){
        int result=urlListService.updateUrlStatusListById(urlList);
        if (result==1){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
    @RequestMapping("/admin/weblist/deleteUrl")
    public String deleteUrl(String urlId)
    {
        int flag = 0;
        HashMap<String, Object> result = new HashMap<>();
        UrlList urlList = urlListService.selectUrlListByUrlId(Long.valueOf(urlId));
        String parentString = urlCateListService.selectUrListCateByUrlCateId(urlList.getCateId());
        String [] parentList = parentString.split(",");
        System.out.println(parentString);
        for(String parent : parentList)
        {
            if(parent.equals("0")){
                continue;
            }
            UrlCateList urlCateList =urlCateListService.selectByPrimaryKey(Integer.valueOf(parent));
            if(urlCateListService.updateByUrlCateList(urlCateList) == 0)
            {
                flag = 0;
            }else{
                flag = 1;
            }
        }
        if(flag == 1)
        {
            result.put("code", 200);
            result.put("msg", "处理成功");
        }else{
            result.put("code", 500);
            result.put("msg", "处理失败");
        }

        return JSONObject.toJSONString(result);
    }
    @RequestMapping("/admin/weblist/insertUrlByUser")
    public String  insertUrlByUser(UrlList urlList)
    {
        int flag = 0;
        Date date = new Date();
        urlList.setCreatetime(date.getTime());
        urlList.setViews(Long.valueOf(0));
        urlList.setNeedLogin(Byte.valueOf(flag+""));
        HashMap<String, Object> result = new HashMap<>();
        flag = urlListService.insertUrlByUser(urlList);
        String parentString = urlCateListService.selectUrListCateByUrlCateId(urlList.getCateId());
        String [] parentList = parentString.split(",");
        System.out.println(parentString);
        for(String parent : parentList)
        {
            if(parent.equals("0")){
                continue;
            }
            UrlCateList urlCateList =urlCateListService.selectByPrimaryKey(Integer.valueOf(parent));
            if(urlCateListService.updateByUrlCateList(urlCateList) == 0)
            {
                flag = 0;
            }else{
                flag = 1;
            }
        }
        if(flag == 1)
        {
            result.put("code", 200);
            result.put("msg", "处理成功");

        }else{
            result.put("code", 500);
            result.put("msg", "处理失败");

        }
        return JSONObject.toJSONString(result);
    }
}
