package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.his.domain.FsComplaintTemplate;
import com.fs.his.domain.FsHisComplaint;
import com.fs.his.service.IFsComplaintTemplateService;
import com.fs.his.service.IFsHisComplaintService;
import com.fs.his.utils.ComplaintTreeUtil;
import com.fs.his.vo.FsComplaintTemplateVO;
import com.fs.his.vo.FsUserComplaintVo;
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("/app/complaint")
public class FsUserComplaintController extends AppBaseController
{
    @Autowired
    private IFsHisComplaintService fsUserComplaintService;
    @Autowired
    private IFsComplaintTemplateService fsComplaintTemplateService;

    /**
     * 查询投诉模板列表
     */
    @GetMapping("/templateList")
    public TableDataInfo list(FsComplaintTemplate fsComplaintTemplate)
    {
        startPage();
        fsComplaintTemplate.setIsDel(0L);
        List<FsComplaintTemplate> list = fsComplaintTemplateService.selectFsComplaintTemplateList(fsComplaintTemplate);
        return getDataTable(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:complaint:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsHisComplaint fsHisComplaint)
    {
        startPage();
        List<FsUserComplaintVo> list = fsUserComplaintService.selectFsUserComplaintList(fsHisComplaint);
        return getDataTable(list);
    }



    /**
     * 获取用户投诉详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsUserComplaintService.selectFsUserComplaintById(id));
    }

    /**
     * 新增用户投诉
     */
    @Login
    @PostMapping
    public AjaxResult add(@RequestBody FsHisComplaint fsHisComplaint)
    {
        String userId = getUserId();
        fsHisComplaint.setUserId(Long.valueOf(userId));
        return toAjax(fsUserComplaintService.insertFsUserComplaint(fsHisComplaint));
    }

    /**
     * 修改用户投诉
     */
    @Login
    @PutMapping
    public AjaxResult edit(@RequestBody FsHisComplaint fsHisComplaint)
    {
        return toAjax(fsUserComplaintService.updateFsUserComplaint(fsHisComplaint));
    }

    /**
     * 删除用户投诉
     */
    @Login
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsUserComplaintService.deleteFsUserComplaintByIds(ids));
    }
}
