TBankSubjectController.java 8.25 KB
Newer Older
1 2
package com.zehong.web.controller.system;

吴卿华's avatar
吴卿华 committed
3
import java.io.IOException;
4
import java.util.ArrayList;
吴卿华's avatar
吴卿华 committed
5
import java.util.Date;
6 7
import java.util.List;

吴卿华's avatar
吴卿华 committed
8 9 10
import com.alibaba.fastjson.JSONObject;
import com.zehong.common.exception.CustomException;
import com.zehong.common.utils.StringUtils;
11 12
import com.zehong.system.domain.TBankSubject;
import com.zehong.system.service.ITBankSubjectService;
吴卿华's avatar
吴卿华 committed
13 14 15
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
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.zehong.common.annotation.Log;
import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.enums.BusinessType;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.common.core.page.TableDataInfo;
吴卿华's avatar
吴卿华 committed
31 32
import org.springframework.web.multipart.MultipartFile;

33
import javax.xml.crypto.Data;
34 35

/**
吴卿华's avatar
吴卿华 committed
36
 * 题目Controller
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
 *
 * @author zehong
 * @date 2022-12-15
 */
@RestController
@RequestMapping("/system/subject")
public class TBankSubjectController extends BaseController
{
    @Autowired
    private ITBankSubjectService tBankSubjectService;

    /**
     * 查询题库题目列表
     */
    //@PreAuthorize("@ss.hasPermi('system:subject:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBankSubject tBankSubject)
    {
        startPage();
        List<TBankSubject> list = tBankSubjectService.selectTBankSubjectList(tBankSubject);
        return getDataTable(list);
    }

    /**
     * 导出题库题目列表
     */
    //@PreAuthorize("@ss.hasPermi('system:subject:export')")
    @Log(title = "题库题目", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TBankSubject tBankSubject)
    {
        List<TBankSubject> list = tBankSubjectService.selectTBankSubjectList(tBankSubject);
        ExcelUtil<TBankSubject> util = new ExcelUtil<TBankSubject>(TBankSubject.class);
        return util.exportExcel(list, "题库题目数据");
    }

    /**
     * 获取题库题目详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:subject:query')")
    @GetMapping(value = "/{subjectId}")
    public AjaxResult getInfo(@PathVariable("subjectId") Long subjectId)
    {
        return AjaxResult.success(tBankSubjectService.selectTBankSubjectById(subjectId));
    }

    /**
     * 新增题库题目
     */
    //@PreAuthorize("@ss.hasPermi('system:subject:add')")
    @Log(title = "题库题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBankSubject tBankSubject)
    {
        return toAjax(tBankSubjectService.insertTBankSubject(tBankSubject));
    }

    /**
     * 修改题库题目
     */
    //@PreAuthorize("@ss.hasPermi('system:subject:edit')")
    @Log(title = "题库题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBankSubject tBankSubject)
    {
        return toAjax(tBankSubjectService.updateTBankSubject(tBankSubject));
    }

    /**
     * 删除题库题目
     */
    //@PreAuthorize("@ss.hasPermi('system:subject:remove')")
    @Log(title = "题库题目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{subjectIds}")
    public AjaxResult remove(@PathVariable Long[] subjectIds)
    {
        return toAjax(tBankSubjectService.deleteTBankSubjectByIds(subjectIds));
    }
吴卿华's avatar
吴卿华 committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

    /**
     * 导入题目信息管理列表
     */
    @Log(title = "题库题目", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importDevice(MultipartFile file, boolean updateSupport,Long bankId) throws Exception
    {
        //装载流
        XSSFWorkbook workbook = null;
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        try {
//            workbook = new XSSFWorkbook(file.getInputStream());
            //解析xls
            workbook = new XSSFWorkbook(file.getInputStream());
        }catch (IOException e) {
            e.printStackTrace();
        }
        //最终添加数据 存储数组
        List<TBankSubject> list = new ArrayList<>();
        // 获取一个工作表,下标从0开始
        XSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        // 通过循环,逐行取出表中每行数据
        for(int i=0;i<=lastRowNum;i++){//跳过第一行和第二行
吴卿华's avatar
吴卿华 committed
143
            if(i<=1){
吴卿华's avatar
吴卿华 committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
                continue;
            }
            // 获取行
            XSSFRow row = sheet.getRow(i);
            TBankSubject excelEntity = new TBankSubject();
            List jsonArray=new ArrayList();
            try{
                //判断题目 和答案不能为空
                if (!StringUtils.isEmpty(row.getCell(0).toString())&&!StringUtils.isEmpty(row.getCell(9).toString())){
                    successNum++;
                    /**题库id*/
                    excelEntity.setBankId(bankId);
                    /**题目*/
                    excelEntity.setTopicTitle(row.getCell(0).toString());
                    /**选项*/
                    for (int s=1;s<=8;s++){
                        JSONObject jsonObject=new JSONObject();
吴卿华's avatar
吴卿华 committed
161 162 163 164
                        if (!StringUtils.isEmpty(row.getCell(s).toString())) {
                            jsonObject.put("value", row.getCell(s).toString());
                            jsonArray.add(jsonObject);
                        }
吴卿华's avatar
吴卿华 committed
165 166 167
                    }
                    /**答案*/
                    if ("A".equals(row.getCell(9).toString())){
吴卿华's avatar
吴卿华 committed
168 169
                        excelEntity.setAnswer(0);
                    }else if ("B".equals(row.getCell(9).toString())){
吴卿华's avatar
吴卿华 committed
170 171
                        excelEntity.setAnswer(1);
                    }else if ("C".equals(row.getCell(9).toString())){
吴卿华's avatar
吴卿华 committed
172
                        excelEntity.setAnswer(2);
吴卿华's avatar
吴卿华 committed
173
                    }else if ("D".equals(row.getCell(9).toString())){
吴卿华's avatar
吴卿华 committed
174
                        excelEntity.setAnswer(3);
吴卿华's avatar
吴卿华 committed
175
                    }else if ("E".equals(row.getCell(9).toString())){
吴卿华's avatar
吴卿华 committed
176
                        excelEntity.setAnswer(4);
吴卿华's avatar
吴卿华 committed
177
                    }else if ("F".equals(row.getCell(9).toString())){
吴卿华's avatar
吴卿华 committed
178
                        excelEntity.setAnswer(5);
吴卿华's avatar
吴卿华 committed
179
                    }else if ("G".equals(row.getCell(9).toString())){
吴卿华's avatar
吴卿华 committed
180
                        excelEntity.setAnswer(6);
吴卿华's avatar
吴卿华 committed
181
                    }else if ("H".equals(row.getCell(9).toString())){
吴卿华's avatar
吴卿华 committed
182
                        excelEntity.setAnswer(7);
吴卿华's avatar
吴卿华 committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
                    }
                    excelEntity.setTopicOption(String.valueOf(jsonArray));
                    excelEntity.setDatetime(new Date());
                    list.add(excelEntity);
                }
//                else {
//                    failureNum++;
//                    String msg = "<br/>" + failureNum + "、题目 " + excelEntity.getTopicOption() + " 导入失败:";
//                    failureMsg.append(msg);
//                }
            }
            catch (Exception e){
                failureNum++;
                String msg = "<br/>" + failureNum + "题目 " + excelEntity.getTopicOption()  + " 导入失败:";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "导入完成,共 " + failureNum + " 条数据格式不正确,错误如下:");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "数据已全部导入成功!共 " + successNum + " 条");
        }
吴卿华's avatar
吴卿华 committed
209 210 211 212
        if (list.size()!=0){
            /**数据添加方法*/
            tBankSubjectService.insertBank(list);
        }
吴卿华's avatar
吴卿华 committed
213 214 215
//        String message = tBankSubjectService.importDevice(file, updateSupport);
        return AjaxResult.success(successMsg.toString());
    }
216
}