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.appinfo.bean.AppInfo;
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.appinfo.service.AppInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
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 Mount Liu
 * @date 2018/8/22
 */
@Controller
@RequestMapping("/sys/appinfo")
public class AppInfoController extends BaseController {

    @Autowired
    private AppInfoService appInfoService;

    @Autowired
    private FileInfoService fileInfoService;

    @RequestMapping("/appinfoList")
    public String appinfoList(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<AppInfo> page = appInfoService.findByPage(params, initPage(pageNo, pageSize, 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/appinfo/appinfoList";
    }

    @RequestMapping("/appinfoAdd")
    public String appinfoAdd(Model model, @RequestParam(defaultValue = "0") String id) throws Exception {
        AppInfo entity = appInfoService.getById(id);
        if (entity == null) {
            entity = new AppInfo();
        }
        model.addAttribute("entity", entity);
        return "sys/appinfo/appinfoAdd";
    }

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

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

    @RequestMapping("/upload/{type}")
    @ResponseBody
    public ResponseEntity uploadFile(HttpServletRequest request, HttpServletResponse response, @PathVariable("type") int type) throws Exception {
        ResponseEntity responseEntity = new ResponseEntity();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileList = multipartRequest.getFileMap();
        if (fileList != null && fileList.size() > 0) {
            String filepath = "";

            //小塾端
            if (type == 1) {
                filepath = PropertiesUtil.getValue("file_save_path") + "/app_pack/scon/";
            } else if (type == 2) {
                //家长端
                filepath = PropertiesUtil.getValue("file_save_path") + "/app_pack/parent/";
            } else {
                filepath = PropertiesUtil.getValue("file_save_path") + "/app_pack/attachment/";
            }
            //文件信息入库
            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;
    }
}
