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.SSJPeiyouBook;
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.SSJPeiyouBookService;
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: PeiyouController
 * @projectName ssj
 * @description: TODO
 * @date 2020/4/20 15:50
 */
@Controller
@RequestMapping("/sys/peiyou")
public class PeiyouController extends BaseController {

    @Autowired
    private SSJPeiyouBookService ssjPeiyouBookService;

    @Autowired
    private FileInfoService fileInfoService;

    @RequestMapping("/peiyouList")
    public String peiyouList(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<SSJPeiyouBook> page = ssjPeiyouBookService.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/peiyou/peiyouList";
    }

    @RequestMapping("/peiyouAdd")
    public String peiyouAdd(Model model, @RequestParam(defaultValue = "0") String id) throws Exception {
        SSJPeiyouBook entity = ssjPeiyouBookService.getById(id);
        if (entity == null) {
            entity = new SSJPeiyouBook();
        }
        model.addAttribute("entity", entity);
        return "sys/peiyou/peiyouAdd";
    }

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

    @RequestMapping(value = "/peiyouDelete", method = RequestMethod.POST)
    @ResponseBody
    public Response messageDelete(String id) {
        Response response = new Response();
        ssjPeiyouBookService.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_book/peiyou/";
            //文件信息入库
            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;
    }
}
