package com.ruoyi.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cs.domain.CsExpandInteract;
import com.ruoyi.cs.domain.template.CsExpandInteractTemplate;
import com.ruoyi.cs.service.ICsExpandInteractService;
import org.jsoup.Jsoup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 拓展交流Controller
 *
 * @author ruoyi
 * @date 2024-05-07
 */
@RestController
@RequestMapping("/cs/csExpandInteract")
public class CsExpandInteractController extends BaseController
{
    @Resource
    private ICsExpandInteractService csExpandInteractService;

    /**
     * 查询拓展交流列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csExpandInteract:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsExpandInteract csExpandInteract)
    {
        startPage();
        csExpandInteract.setDelFlag("0");
        List<CsExpandInteract> list = csExpandInteractService.selectCsExpandInteractList(csExpandInteract);
        return getDataTable(list);
    }

    /**
     * 导出拓展交流列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csExpandInteract:export')")
    @Log(title = "拓展交流", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsExpandInteract csExpandInteract)
    {
        csExpandInteract.setDelFlag("0");
        List<CsExpandInteract> list = csExpandInteractService.selectCsExpandInteractList(csExpandInteract);
        if (CollectionUtil.isNotEmpty(list)){
            list.forEach(item -> {
                if (StringUtil.isNotEmpty(item.getExchangeContent())){
                    item.setExchangeContent(Jsoup.parse(item.getExchangeContent()).text());
                }
            });
        }
        ExcelUtil<CsExpandInteract> util = new ExcelUtil<CsExpandInteract>(CsExpandInteract.class);
        util.exportExcel(response, list, "交流数据");
    }

    /**list
     * 导出拓展交流列表模版
     */
    @Log(title = "拓展交流模版", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response)
    {
        ExcelUtil<CsExpandInteractTemplate> util = new ExcelUtil<CsExpandInteractTemplate>(CsExpandInteractTemplate.class);
        util.exportExcel(response, null, "拓展交流数据模版");
    }

    /**
     * 获取拓展交流详细信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csExpandInteract:query')")
    @GetMapping(value = "/{expandInteractId}")
    public AjaxResult getInfo(@PathVariable("expandInteractId") String expandInteractId)
    {
        return AjaxResult.success(csExpandInteractService.selectCsExpandInteractByExpandInteractId(expandInteractId));
    }

    /**
     * 新增拓展交流
     */
    @PreAuthorize("@ss.hasPermi('cs:csExpandInteract:add')")
    @Log(title = "拓展交流", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsExpandInteract csExpandInteract)
    {
        return toAjax(csExpandInteractService.insertCsExpandInteract(csExpandInteract));
    }

    /**
     * 修改拓展交流
     */
    @PreAuthorize("@ss.hasPermi('cs:csExpandInteract:edit')")
    @Log(title = "拓展交流", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsExpandInteract csExpandInteract)
    {
        return toAjax(csExpandInteractService.updateCsExpandInteract(csExpandInteract));
    }

    /**
     * 删除拓展交流
     */
    @PreAuthorize("@ss.hasPermi('cs:csExpandInteract:remove')")
    @Log(title = "拓展交流", businessType = BusinessType.DELETE)
	@DeleteMapping("/{expandInteractIds}")
    public AjaxResult remove(@PathVariable String[] expandInteractIds)
    {
        return toAjax(csExpandInteractService.deleteCsExpandInteractByExpandInteractIds(expandInteractIds));
    }


    /**
     * 导入拓展交流记录
     */
    @Log(title = "导入拓展交流记录", businessType = BusinessType.IMPORT)
    @PostMapping("/importCsExpandInteract")
    public AjaxResult importCsExpandInteract(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CsExpandInteractTemplate> util = new ExcelUtil<CsExpandInteractTemplate>(CsExpandInteractTemplate.class);
        List<CsExpandInteractTemplate> list = util.importExcel(file.getInputStream());
        String message = csExpandInteractService.importCsExpandInteract(list, updateSupport);
        return AjaxResult.success(message);
    }
}
