
package org.linlinjava.jz.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.jz.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.jz.admin.util.SecurityHelper;
import org.linlinjava.jz.admin.vo.CatVo;
import org.linlinjava.jz.core.util.ResponseUtil;
import org.linlinjava.jz.db.domain.JzFaq;
import org.linlinjava.jz.db.domain.JzFaqCategory;
import org.linlinjava.jz.db.domain.ViewJzFaq;
import org.linlinjava.jz.db.service.JzBaseService;
import org.linlinjava.jz.db.service.JzFAQCategoryService;
import org.linlinjava.jz.db.service.JzFAQService;
import org.linlinjava.jz.db.util.BaseConstant;
import org.linlinjava.jz.db.util.FAQStatusConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/faq")
@Validated
public class AdminFAQController {
    private final Log logger = LogFactory.getLog(AdminFAQController.class);

    @Autowired
    private JzFAQService faqService;

    @Autowired
    private JzFAQCategoryService faqCategoryService;

    @Autowired
    private JzBaseService baseService;


    @GetMapping("/CateAndLabel")
    public Object cateAndLabel() {
        List<JzFaqCategory> l1CatList = faqCategoryService.queryL1();
        List<CatVo> categoryList = new ArrayList<>(l1CatList.size());

        for (JzFaqCategory l1 : l1CatList) {
            CatVo l1CatVo = new CatVo();
            l1CatVo.setValue(l1.getId());
            l1CatVo.setLabel(l1.getName());

            List<JzFaqCategory> l2CatList = faqCategoryService.queryByPid(l1.getId());
            List<CatVo> children = new ArrayList<>(l2CatList.size());
            for (JzFaqCategory l2 : l2CatList) {
                CatVo l2CatVo = new CatVo();
                l2CatVo.setValue(l2.getId());
                l2CatVo.setLabel(l2.getName());
                children.add(l2CatVo);
            }
            l1CatVo.setChildren(children);
            categoryList.add(l1CatVo);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("categoryList", categoryList);
        data.put("labelList", baseService.list(BaseConstant.STATUS_FAQ_LABEL));
        return ResponseUtil.ok(data);
    }


    @RequiresPermissions("admin:faq:list")
    @RequiresPermissionsDesc(menu = {"智库管理", "FAQ"}, button = "列表")
    @PostMapping("/list")
    public Object list(String name, Integer label) {

        List<ViewJzFaq> list = faqService.list(name, label);
        list.forEach(item -> {
            List<ViewJzFaq> jzFaqs = faqService.queryByPid(item.getId());
            if (!CollectionUtils.isEmpty(jzFaqs)) {
                item.setChildren(jzFaqs);
            }
        });

        return ResponseUtil.ok(list);
    }

    @RequiresPermissions("admin:faq:create")
    @RequiresPermissionsDesc(menu = {"智库管理", "FAQ"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody JzFaq faq) {
        logger.info(faq);
        faq.setStatus(FAQStatusConstant.STATUS_INIT);
        faq.setViews(0);
        faq.setCreateUser(SecurityHelper.getAdminId());
        faq.setUpdateUser(SecurityHelper.getAdminId());
        faq.setDeleted(false);
        faqService.add(faq);

        ViewJzFaq viewJzFaq = new ViewJzFaq();
        viewJzFaq.setId(faq.getId());
        viewJzFaq.setPid(faq.getCategoryId());
        viewJzFaq.setName(faq.getName());
        return ResponseUtil.ok(viewJzFaq);
    }

    @GetMapping("/detail")
    public Object detail(Integer id) {
        JzFaq faq = faqService.queryById(id);
        if (faq == null)
            return ResponseUtil.badArgument();
        else
            return ResponseUtil.ok(faq.getContent());
    }

    @RequiresPermissions("admin:faq:edit")
    @RequiresPermissionsDesc(menu = {"智库管理", "FAQ"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody JzFaq faq) {
        faq.setUpdateUser(SecurityHelper.getAdminId());
        faqService.update(faq);
        return ResponseUtil.ok();
    }

}
