package com.ruoyi.mall.controller;

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

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.MallSchool;
import com.ruoyi.mall.service.IMallMemberUserService;
import com.ruoyi.mall.service.IMallSchoolService;
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.mall.domain.ReportIntelligence;
import com.ruoyi.mall.service.IReportIntelligenceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 志愿报告条目Controller
 *
 * @author ruoyi
 * @date 2023-10-28
 */
@RestController
@RequestMapping("/mall/ReportIntelligence")
public class ReportIntelligenceController extends BaseController
{
    @Autowired
    private IReportIntelligenceService reportIntelligenceService;
    @Resource
    private IMallSchoolService schoolService;
    @Resource
    private IMallMemberUserService mallMemberUserService;

    /**
     * 查询志愿报告条目列表
     */
    @PreAuthorize("@ss.hasPermi('mall:ReportIntelligence:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReportIntelligence reportIntelligence)
    {
        // 获取登陆用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // MallSchool school = schoolService.selectMallSchoolBySysUserId(loginUser.getUserId());
        // if (school != null && loginUser.getUserId() != 1) {
        //     reportIntelligence.setBindingSchoolId(school.getSchoolId());
        // }

        MallMemberUser sysUserId = mallMemberUserService.selectMallMemberUserBySysUserId(loginUser.getUserId());
        if (sysUserId != null && loginUser.getUserId() != 1) {
            reportIntelligence.setIndexUserId(sysUserId.getId());
        }

        startPage();
        List<ReportIntelligence> list = reportIntelligenceService.selectReportIntelligenceList(reportIntelligence);
        return getDataTable(list);
    }

    /**
     * 导出志愿报告条目列表
     */
    @PreAuthorize("@ss.hasPermi('mall:ReportIntelligence:export')")
    @Log(title = "志愿报告条目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReportIntelligence reportIntelligence)
    {
        List<ReportIntelligence> list = reportIntelligenceService.selectReportIntelligenceList(reportIntelligence);
        ExcelUtil<ReportIntelligence> util = new ExcelUtil<ReportIntelligence>(ReportIntelligence.class);
        util.exportExcel(response, list, "志愿报告条目数据");
    }

    /**
     * 获取志愿报告条目详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:ReportIntelligence:query')")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId)
    {
        return AjaxResult.success(reportIntelligenceService.selectReportIntelligenceByItemId(itemId));
    }

    /**
     * 新增志愿报告条目
     */
    @PreAuthorize("@ss.hasPermi('mall:ReportIntelligence:add')")
    @Log(title = "志愿报告条目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportIntelligence reportIntelligence)
    {
        return toAjax(reportIntelligenceService.insertReportIntelligence(reportIntelligence));
    }

    /**
     * 修改志愿报告条目
     */
    @PreAuthorize("@ss.hasPermi('mall:ReportIntelligence:edit')")
    @Log(title = "志愿报告条目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportIntelligence reportIntelligence)
    {
        return toAjax(reportIntelligenceService.updateReportIntelligence(reportIntelligence));
    }

    /**
     * 删除志愿报告条目
     */
    @PreAuthorize("@ss.hasPermi('mall:ReportIntelligence:remove')")
    @Log(title = "志愿报告条目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds)
    {
        return toAjax(reportIntelligenceService.deleteReportIntelligenceByItemIds(itemIds));
    }
}
