package com.flossom.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.flossom.common.core.domain.entity.SysTag;
import com.flossom.common.core.domain.entity.WxScriptTemplate;
import com.flossom.common.core.utils.StringUtils;
import com.flossom.common.core.utils.poi.ExcelUtil;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import com.flossom.common.core.web.page.TableDataInfo;
import com.flossom.common.log.annotation.Log;
import com.flossom.common.log.enums.BusinessType;
import com.flossom.common.security.annotation.RequiresPermissions;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.flossom.system.service.IWxScriptTemplateService;

/**
 * 话术模板Controller
 *
 * @author flossom
 * @date 2023-12-18
 */
@RestController
@RequestMapping("/scriptTemplate")
public class WxScriptTemplateController extends BaseController
{
    @Autowired
    private IWxScriptTemplateService wxScriptTemplateService;

    /**
     * 查询话术模板列表
     */
    @RequiresPermissions("system:scriptTemplate:list")
    @GetMapping("/list")
    public AjaxResult list(WxScriptTemplate wxScriptTemplate)
    {
        List<WxScriptTemplate> list = wxScriptTemplateService.selectWxScriptTemplateList(wxScriptTemplate);
        return success(list);
    }

    /**
     * 导出话术模板列表
     */
    @RequiresPermissions("system:scriptTemplate:export")
    @Log(title = "话术模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxScriptTemplate wxScriptTemplate)
    {
        List<WxScriptTemplate> list = wxScriptTemplateService.selectWxScriptTemplateList(wxScriptTemplate);
        ExcelUtil<WxScriptTemplate> util = new ExcelUtil<WxScriptTemplate>(WxScriptTemplate.class);
        util.exportExcel(response, list, "话术模板数据");
    }

    /**
     * 获取话术模板详细信息
     */
    @RequiresPermissions("system:scriptTemplate:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wxScriptTemplateService.selectWxScriptTemplateById(id));
    }

    /**
     * 新增话术模板
     */
    @RequiresPermissions("system:scriptTemplate:add")
    @Log(title = "话术模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxScriptTemplate wxScriptTemplate)
    {
        return toAjax(wxScriptTemplateService.insertWxScriptTemplate(wxScriptTemplate));
    }

    /**
     * 修改话术模板
     */
    @RequiresPermissions("system:scriptTemplate:edit")
    @Log(title = "话术模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxScriptTemplate wxScriptTemplate)
    {
        System.out.println("标签数组==》"+wxScriptTemplate.getTagIds().toString());
        return toAjax(wxScriptTemplateService.updateWxScriptTemplate(wxScriptTemplate));
    }

    /**
     * 删除话术模板
     */
    @RequiresPermissions("system:scriptTemplate:remove")
    @Log(title = "话术模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        if (wxScriptTemplateService.hasChildByScriptId(id))
        {
            return warn("存在下级标签,不允许删除");
        }
        return toAjax(wxScriptTemplateService.deleteWxScriptTemplateById(id));
    }

//    /**
//     * 查询标签列表（排除节点）
//     */
//    @RequiresPermissions("system:scriptTemplate:list")
//    @GetMapping("/list/exclude/{id}")
//    public AjaxResult excludeChild(@PathVariable(value = "id", required = false) Long id)
//    {
//        List<WxScriptTemplate> tags = wxScriptTemplateService.selectWxScriptTemplateList(new WxScriptTemplate());
//        tags.removeIf(d -> d.getId().intValue() == id || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), id + ""));
//        return success(tags);
//    }
}
