package com.ruoyi.business.controller;

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

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.domain.BusinessTemplate;
import com.ruoyi.business.domain.BusinessTemplateProp;
import com.ruoyi.business.domain.BusinessTemplatePropValue;
import com.ruoyi.business.service.IBusinessTemplatePropService;
import com.ruoyi.business.service.IBusinessTemplateService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
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.business.domain.BusinessSurvey;
import com.ruoyi.business.service.IBusinessSurveyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 调查问卷管理Controller
 * 
 * @author ruoyi
 * @date 2024-04-01
 */
@RestController
@RequestMapping("/business/survey")
public class BusinessSurveyController extends BaseController
{
    @Autowired
    private IBusinessSurveyService businessSurveyService;


    /**
     * 获取问卷属性
     */
    @GetMapping("/listBySurveyId")
    public AjaxResult listBySurveyId(String surveyId) {
        return businessSurveyService.listBySurveyId(surveyId);
    }



    /**
     * 查询调查问卷管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:survey:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusinessSurvey businessSurvey)
    {
        startPage();
        List<BusinessSurvey> list = businessSurveyService.lambdaQuery()
        .eq(StringUtils.isNotEmpty(businessSurvey.getTitle()),BusinessSurvey::getTitle,businessSurvey.getTitle())
        .eq(StringUtils.isNotBlank(businessSurvey.getOpen()),BusinessSurvey::getOpen,businessSurvey.getOpen()).list();
        return getDataTable(list);
    }

    /**
     * 导出调查问卷管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:survey:export')")
    @Log(title = "调查问卷管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessSurvey businessSurvey)
    {
        List<BusinessSurvey> list = businessSurveyService.list();
        ExcelUtil<BusinessSurvey> util = new ExcelUtil<BusinessSurvey>(BusinessSurvey.class);
        util.exportExcel(response, list, "调查问卷管理数据");
    }

    /**
     * 获取调查问卷管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:survey:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(businessSurveyService.getById(id));
    }
    @Autowired
    private IBusinessTemplateService businessTemplateService;
    /**
     * 新增调查问卷管理
     */
    @PreAuthorize("@ss.hasPermi('business:survey:add')")
    @Log(title = "调查问卷管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessSurvey businessSurvey)
    {
        BusinessTemplate one = businessTemplateService.lambdaQuery().eq(BusinessTemplate::getId, businessSurvey.getTempId()).one();
        businessSurvey.setTempName(one.getTitle());
        return toAjax(businessSurveyService.save(businessSurvey));
    }

    /**
     * 修改调查问卷管理
     */
    @PreAuthorize("@ss.hasPermi('business:survey:edit')")
    @Log(title = "调查问卷管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusinessSurvey businessSurvey)
    {
        BusinessTemplate one = businessTemplateService.lambdaQuery().eq(BusinessTemplate::getId, businessSurvey.getTempId()).one();
        businessSurvey.setTempName(one.getTitle());
        return toAjax(businessSurveyService.updateById(businessSurvey));
    }

    /**
     * 删除调查问卷管理
     */
    @PreAuthorize("@ss.hasPermi('business:survey:remove')")
    @Log(title = "调查问卷管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(businessSurveyService.removeByIds(Arrays.asList(ids)));
    }
}
