package com.ruoyi.web.controller.rc;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.rc.domain.TDeptInfo;
import com.ruoyi.system.service.ISysDeptService;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.rc.domain.TQuestionnaire;
import com.ruoyi.rc.service.ITQuestionnaireService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;

/**
 * 问卷Controller
 * 
 * @author ruoyi
 * @date 2024-07-24
 */
@RestController
@RequestMapping("/rc/questionnaire")
public class TQuestionnaireController extends BaseController
{
    @Autowired
    private ITQuestionnaireService tQuestionnaireService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询问卷列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TQuestionnaire tQuestionnaire)
    {
        startPage();
        List<TQuestionnaire> list = tQuestionnaireService.selectTQuestionnaireList(tQuestionnaire);
        for (TQuestionnaire obj : list) {
            String deptId = obj.getDeptId();
            if (StringUtils.isNotEmpty(deptId)) {
                if (deptId.indexOf(",") != -1) {
                    StringBuffer sb = new StringBuffer();
                    String[] ids = deptId.split(",");
                    for (String id : ids) {
                        SysDept sysDept = deptService.selectDeptById(Long.parseLong(id));
                        sb.append(sysDept.getDeptName()).append(" / ");
                    }
                    obj.setDeptName(sb.toString().substring(0, sb.length() - 3));
                } else {
                    obj.setDeptName(deptService.selectDeptById(Long.parseLong(deptId)).getDeptName());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出问卷列表
     */
    @Log(title = "问卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TQuestionnaire tQuestionnaire)
    {
        List<TQuestionnaire> list = tQuestionnaireService.selectTQuestionnaireList(tQuestionnaire);
        ExcelUtil<TQuestionnaire> util = new ExcelUtil<TQuestionnaire>(TQuestionnaire.class);
        util.exportExcel(response, list, "问卷数据");
    }

    /**
     * 获取问卷详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        TQuestionnaire tQuestionnaire = tQuestionnaireService.selectTQuestionnaireById(id);
        String year = tQuestionnaire.getYear();
        if (year.length() > 4) {
            tQuestionnaire.setYear(year.substring(0, year.indexOf("-")));
        }
        return success(tQuestionnaire);
    }

    /**
     * 新增问卷
     */
    @Log(title = "问卷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TQuestionnaire tQuestionnaire)
    {
        if (StringUtils.isNull(tQuestionnaire.getDeptId())) {
            tQuestionnaire.setDeptId(getLoginUser().getDeptId().toString());
        }
        return toAjax(tQuestionnaireService.insertTQuestionnaire(tQuestionnaire));
    }

    /**
     * 修改问卷
     */
    @Log(title = "问卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TQuestionnaire tQuestionnaire)
    {
        return toAjax(tQuestionnaireService.updateTQuestionnaire(tQuestionnaire));
    }

    /**
     * 删除问卷
     */
    @Log(title = "问卷", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tQuestionnaireService.deleteTQuestionnaireByIds(ids));
    }
}
