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

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

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

35
import javax.xml.crypto.Data;
36 37

/**
吴卿华's avatar
吴卿华 committed
38
 * 题目Controller
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 115 116
 *
 * @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
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 143 144

    /**
     * 导入题目信息管理列表
     */
    @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
145
            if(i<=1){
吴卿华's avatar
吴卿华 committed
146 147 148 149 150 151 152 153
                continue;
            }
            // 获取行
            XSSFRow row = sheet.getRow(i);
            TBankSubject excelEntity = new TBankSubject();
            List jsonArray=new ArrayList();
            try{
                //判断题目 和答案不能为空
吴卿华's avatar
吴卿华 committed
154 155
                if (!StringUtils.isEmpty(row.getCell(0).toString())&&!StringUtils.isEmpty(row.getCell(9).toString())
                        &&!StringUtils.isEmpty(row.getCell(10).toString())){
吴卿华's avatar
吴卿华 committed
156 157 158 159 160
                    successNum++;
                    /**题库id*/
                    excelEntity.setBankId(bankId);
                    /**题目*/
                    excelEntity.setTopicTitle(row.getCell(0).toString());
161 162 163 164

                    String  s1= row.getCell(10).toString();
                    String s2 = StringUtils.substringBefore(s1, ".");

吴卿华's avatar
吴卿华 committed
165
                    /**题目类型*/
166
                    excelEntity.setTopicType(Integer.valueOf(s2));
吴卿华's avatar
吴卿华 committed
167
                    /**题目*/
吴卿华's avatar
吴卿华 committed
168 169
                    for (int s=1;s<=8;s++){
                        JSONObject jsonObject=new JSONObject();
吴卿华's avatar
吴卿华 committed
170 171 172 173
                        if (!StringUtils.isEmpty(row.getCell(s).toString())) {
                            jsonObject.put("value", row.getCell(s).toString());
                            jsonArray.add(jsonObject);
                        }
吴卿华's avatar
吴卿华 committed
174
                    }
175
                    System.out.println(row.getCell(9).toString());
吴卿华's avatar
吴卿华 committed
176
                    /**答案*/
177
                    excelEntity.setAnswer((row.getCell(9).toString()).replace(",",",") );
吴卿华's avatar
吴卿华 committed
178

吴卿华's avatar
吴卿华 committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
//                    if ("A".equals(row.getCell(9).toString())){
//                        excelEntity.setAnswer(0);
//                    }else if ("B".equals(row.getCell(9).toString())){
//                        excelEntity.setAnswer(1);
//                    }else if ("C".equals(row.getCell(9).toString())){
//                        excelEntity.setAnswer(2);
//                    }else if ("D".equals(row.getCell(9).toString())){
//                        excelEntity.setAnswer(3);
//                    }else if ("E".equals(row.getCell(9).toString())){
//                        excelEntity.setAnswer(4);
//                    }else if ("F".equals(row.getCell(9).toString())){
//                        excelEntity.setAnswer(5);
//                    }else if ("G".equals(row.getCell(9).toString())){
//                        excelEntity.setAnswer(6);
//                    }else if ("H".equals(row.getCell(9).toString())){
//                        excelEntity.setAnswer(7);
//                    }
吴卿华's avatar
吴卿华 committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                    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
221 222 223 224
        if (list.size()!=0){
            /**数据添加方法*/
            tBankSubjectService.insertBank(list);
        }
吴卿华's avatar
吴卿华 committed
225 226 227
//        String message = tBankSubjectService.importDevice(file, updateSupport);
        return AjaxResult.success(successMsg.toString());
    }
228
}