package com.fs.his.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.his.domain.FsComplaintTemplate;
import com.fs.his.service.IFsComplaintTemplateService;
import com.fs.his.utils.ComplaintTreeUtil;
import com.fs.his.vo.FsComplaintTemplateVO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 投诉模板Controller
 *
 * @author fs
 * @date 2025-06-09
 */
@RestController
@RequestMapping("/his/template")
public class FsComplaintTemplateController extends BaseController
{
    @Autowired
    private IFsComplaintTemplateService fsComplaintTemplateService;

    /**
     * 查询投诉模板列表
     */
    @PreAuthorize("@ss.hasPermi('his:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsComplaintTemplate fsComplaintTemplate)
    {
        startPage();
        fsComplaintTemplate.setIsDel(0L);
        List<FsComplaintTemplate> list = fsComplaintTemplateService.selectFsComplaintTemplateList(fsComplaintTemplate);
        return getDataTable(list);
    }

    /**
     * 查询投诉模板树形结果
     */
    @PreAuthorize("@ss.hasPermi('his:template:list')")
    @GetMapping("/treeList")
    public R treeList(FsComplaintTemplate fsComplaintTemplate)
    {
        List<FsComplaintTemplate> list = fsComplaintTemplateService.selectFsComplaintTemplateList(fsComplaintTemplate);
        List<FsComplaintTemplateVO> templateVOS = Lists.newArrayList();
        for (FsComplaintTemplate template : list){
            FsComplaintTemplateVO templateVO = new FsComplaintTemplateVO();
            templateVO.setId(template.getId());
            templateVO.setName(template.getName());
            templateVO.setParentId(template.getParentId());
            templateVO.setDescription(template.getDescription());
            templateVO.setSort(template.getSort());
            templateVOS.add(templateVO);
        }
        return R.ok().put("data", ComplaintTreeUtil.list2TreeConverter(templateVOS, 0));
    }


    /**
     * 导出投诉模板列表
     */
    @PreAuthorize("@ss.hasPermi('his:template:export')")
    @Log(title = "投诉模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsComplaintTemplate fsComplaintTemplate)
    {
        List<FsComplaintTemplate> list = fsComplaintTemplateService.selectFsComplaintTemplateList(fsComplaintTemplate);
        ExcelUtil<FsComplaintTemplate> util = new ExcelUtil<FsComplaintTemplate>(FsComplaintTemplate.class);
        return util.exportExcel(list, "投诉模板数据");
    }

    /**
     * 获取投诉模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsComplaintTemplateService.selectFsComplaintTemplateById(id));
    }

    /**
     * 新增投诉模板
     */
    @PreAuthorize("@ss.hasPermi('his:template:add')")
    @Log(title = "投诉模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsComplaintTemplate fsComplaintTemplate)
    {
        return toAjax(fsComplaintTemplateService.insertFsComplaintTemplate(fsComplaintTemplate));
    }

    /**
     * 修改投诉模板
     */
    @PreAuthorize("@ss.hasPermi('his:template:edit')")
    @Log(title = "投诉模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsComplaintTemplate fsComplaintTemplate)
    {
        return toAjax(fsComplaintTemplateService.updateFsComplaintTemplate(fsComplaintTemplate));
    }

    /**
     * 删除投诉模板
     */
    @PreAuthorize("@ss.hasPermi('his:template:remove')")
    @Log(title = "投诉模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsComplaintTemplateService.deleteFsComplaintTemplateByIds(ids));
    }
}
