package com.ssj.sys.controller;

import com.ssj.bean.common.files.domain.FileInfo;
import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.parent.domain.SSJTermtrainQuestion;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.PropertiesUtil;
import com.ssj.framework.core.util.ResponseConstant;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.framework.files.service.FileInfoService;
import com.ssj.service.sys.parent.service.SSJTermtrainQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yanghuan
 * @title: ssjTermtrainQuestionController
 * @projectName ssj
 * @description: TODO
 * @date 2020/4/23 10:24
 */
@Controller
@RequestMapping("/sys/termtrainQuestion")
public class SsjTermtrainQuestionController extends BaseController {


    @Autowired
    private  SSJTermtrainQuestionService ssjTermtrainQuestionService;

    @Autowired
    private FileInfoService fileInfoService;

    @RequestMapping("/termtrainQuestionList")
    public String termtrainQuestionList(Model model,
                                        @RequestParam(required = false, defaultValue = "10") int pageSize,
                                        @RequestParam(required = false, defaultValue = "1") int pageNo) throws Exception {

        Map<String, Object> params = new HashMap<>(16);
        SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI().toString());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<SSJTermtrainQuestion> page = ssjTermtrainQuestionService.findByPage(params, initPage(pageNo, pageSize, Sort.Direction.DESC, "createTime"));
        sp.setRowCnt((int) page.getTotalElements());
        model.addAttribute("list", page.getContent());
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);

        return "sys/termtrainQuestion/termtrainQuestionList";
    }

    @RequestMapping("/termtrainQuestionAdd")
    public String termtrainQuestionAdd(Model model, @RequestParam(defaultValue = "0") String id) throws Exception {
        SSJTermtrainQuestion entity = ssjTermtrainQuestionService.getById(id);
        if (entity == null) {
            entity = new SSJTermtrainQuestion();
        }
        model.addAttribute("entity", entity);
        return "sys/termtrainQuestion/termtrainQuestionAdd";
    }

    @RequestMapping(value = "/termtrainQuestionSave", method = RequestMethod.POST)
    @ResponseBody
    public Response termtrainQuestionSave(SSJTermtrainQuestion entity) {
        logger.info(entity.toString());
        Response response = new Response();
        if (StringUtil.isBlank(entity.getId())) {
            entity.setCreateTime(new Date());
        }
        ssjTermtrainQuestionService.save(entity);
        response.success();
        return response;
    }

    @RequestMapping(value = "/termtrainQuestionDelete", method = RequestMethod.POST)
    @ResponseBody
    public Response messageDelete(String id) {
        Response response = new Response();
        ssjTermtrainQuestionService.delete(id);
        response.success();
        return response;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public ResponseEntity uploadFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ResponseEntity responseEntity = new ResponseEntity();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileList = multipartRequest.getFileMap();
        if (fileList != null && fileList.size() > 0) {
            String filepath = "";
            filepath= PropertiesUtil.getValue("file_save_path") + "/ssj_question/termtrainQuestion/";
            //文件信息入库
            List<FileInfo> fileInfos = fileInfoService.uploadFile(loginName(), fileList, filepath);
            if (fileList.size() == fileInfos.size()) {
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("pics", fileInfos);
                responseEntity.success(data, "上传文件成功 ");
            } else {
                responseEntity.failure(ResponseConstant.CODE_000, "上传文件失败");
            }

        } else {
            responseEntity.failure(ResponseConstant.CODE_000, "上传文件失败");
        }
        return responseEntity;
    }
}
