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.conch.conch.domain.SconOtaManager;
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.conch.conch.service.SconOtaManagerService;
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.*;

/**
 * @author yanghuan
 * @title: SconOtaController
 * @projectName ssj
 * @description: TODO
 * @date 2019/7/2 10:49
 */
@Controller
@RequestMapping("/sys/sconota")
public class SconOtaController extends BaseController {

    @Autowired
    private FileInfoService fileInfoService;

    @Autowired
    private SconOtaManagerService sconOtaManagerService;

    @RequestMapping("/sconotaList")
    public String sconotaList(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<SconOtaManager> page = sconOtaManagerService.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/sconota/sconotaList";
    }
    @RequestMapping("/sconotaAdd")
    public String sconotaAdd(Model model, @RequestParam(defaultValue = "0") String id) throws Exception {
        SconOtaManager entity = sconOtaManagerService.getById(id);
        List<String> list=new ArrayList<>();
        if(entity!=null){
            if((Arrays.asList(entity.getTestImei().split(","))).size()!=0){
                String [] ars=entity.getTestImei().split(",");
             List   list0=Arrays.asList(ars);
                list=new ArrayList<>(list0);
                if(5!=list.size()){
                    List list1=new ArrayList();
                    for(int i=list.size();i<5;i++){
                        list1.add("");
                    }
                 list.addAll(list1);
                }
            }else {
                list.add("");
                list.add("");
                list.add("");
                list.add("");
                list.add("");
            }
        }
        else {
            entity=new SconOtaManager();
            list.add("");
            list.add("");
            list.add("");
            list.add("");
            list.add("");
        }
        model.addAttribute("entity", entity);
        model.addAttribute("list",list);
        return "sys/sconota/sconotaAdd";
    }

    @RequestMapping(value = "/sconotaSave", method = RequestMethod.POST)
    @ResponseBody
    public Response sconotaSave(SconOtaManager entity) {
        logger.info(entity.toString());
        Response response = new Response();
        if (StringUtil.isBlank(entity.getId())) {
            entity.setCreateTime(new Date());
        }
        if(StringUtil.isNotBlank(entity.getFileKey())){
            entity.setCreateTime(new Date());
            entity.setFileName(entity.getFileKey().substring(14));
        }
        sconOtaManagerService.save(entity);
        response.success();
        return response;
    }

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

}
