package com.srs.web.controller.dormitory;

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

import com.srs.comprehensive.domain.Dto.StuStatusImport;
import com.srs.comprehensive.domain.SrsStudent;
import com.srs.dormitory.domain.Dto.DmsSearch;
import com.srs.dormitory.domain.Dto.WenMingImport;
import com.srs.dormitory.domain.Vo.DomInfo;
import com.srs.system.service.ISysPostService;
import com.srs.web.controller.common.RoleBool;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;

import com.srs.dormitory.domain.DmsNewWenming;
import com.srs.dormitory.service.IDmsNewWenmingService;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.enums.BusinessType;
import com.srs.common.annotation.Log;
import com.srs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文明宿舍Controller
 *
 * @author srs
 * @date 2024-03-28
 */
@RestController
@RequestMapping("/dormitory/wenming")
@Api(value = "文明宿舍管理", tags = "文明宿舍管理")
public class DmsNewWenmingController extends BaseController {
    @Autowired
    private IDmsNewWenmingService dmsNewWenmingService;
    @Autowired
    ISysPostService _postService;

    @GetMapping("/listView")
    @ApiOperation("查询文明宿舍")
    public TableDataInfo listView(DmsSearch param) {
        boolean hasRole = RoleBool.isHigh(getUserId(), _postService);
        if (hasRole) {
            startPage();
            List<DomInfo> list = dmsNewWenmingService.listView(param);
            return getDataTable(list);
        } else {
            List<String> list = new ArrayList<>();
            return getDataTable(list);
        }
    }
    /**
     * 下载导入模板
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<WenMingImport> util = new ExcelUtil<WenMingImport>(WenMingImport.class);
        util.importTemplateExcel(response, "文明宿舍信息数据");
    }

    /**
     * 导入文明宿舍
     *
     * */
    @Log(title = "文明宿舍", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<WenMingImport> util = new ExcelUtil<WenMingImport>(WenMingImport.class);
        List<WenMingImport> wenMingsList = util.importExcel(file.getInputStream());
        List<DmsNewWenming> wenmings = new ArrayList<>();
        for(WenMingImport x:wenMingsList) {
            DmsNewWenming wenming =new DmsNewWenming();
            wenming.setId(x.getId());
            wenming.setDeptName(x.getDeptName());
            wenming.setClassName(x.getClassName());
            wenming.setTermName(x.getTermName());
            wenming.setRoomNo(x.getRoomNo());
            wenming.setRanking(x.getRanking());
            wenmings.add(wenming);
        }
        String message = dmsNewWenmingService.importWenMing(wenmings, getUsername());
        return AjaxResult.success(message);
    }


    /**
     * 查询文明宿舍列表
     */
    @PreAuthorize("@ss.hasPermi('dormitory:wenming:list')")
    @GetMapping("/list")
    @ApiOperation("查询文明宿舍列表")
    public TableDataInfo list(DmsNewWenming dmsNewWenming) {
        startPage();
        List<DmsNewWenming> list = dmsNewWenmingService.selectDmsNewWenmingList(dmsNewWenming);
        return getDataTable(list);
    }

    /**
     * 导出文明宿舍列表
     */
    @PreAuthorize("@ss.hasPermi('dormitory:wenming:export')")
    @Log(title = "文明宿舍", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出文明宿舍列表")
    public void export(HttpServletResponse response, DmsNewWenming dmsNewWenming) {
        List<DmsNewWenming> list = dmsNewWenmingService.selectDmsNewWenmingList(dmsNewWenming);
        ExcelUtil<DmsNewWenming> util = new ExcelUtil<DmsNewWenming>(DmsNewWenming.class);
        util.exportExcel(response, list, "文明宿舍数据");
    }

    /**
     * 获取文明宿舍详细信息
     */
    @PreAuthorize("@ss.hasPermi('dormitory:wenming:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取文明宿舍详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dmsNewWenmingService.selectDmsNewWenmingById(id));
    }

    /**
     * 新增文明宿舍
     */
    @PreAuthorize("@ss.hasPermi('dormitory:wenming:add')")
    @Log(title = "文明宿舍", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增文明宿舍")
    public AjaxResult add(@RequestBody DmsNewWenming dmsNewWenming) {
        return toAjax(dmsNewWenmingService.insertDmsNewWenming(dmsNewWenming));
    }

    /**
     * 修改文明宿舍
     */
    @PreAuthorize("@ss.hasPermi('dormitory:wenming:edit')")
    @Log(title = "文明宿舍", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改文明宿舍")
    public AjaxResult edit(@RequestBody DmsNewWenming dmsNewWenming) {
        return toAjax(dmsNewWenmingService.updateDmsNewWenming(dmsNewWenming));
    }

    /**
     * 删除文明宿舍
     */
    @Log(title = "文明宿舍", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除文明宿舍")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dmsNewWenmingService.deleteDmsNewWenmingByIds(ids));
    }
}
