package com.jeeplus.modules.API.appversion;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.appversion.entity.Appversions;
import com.jeeplus.modules.appversion.service.AppversionsService;
import com.jeeplus.modules.utils.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

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

@Controller
@RequestMapping(value="${frontPath}/app/version")
public class AppVersionController extends BaseController {

    @Autowired
    private AppversionsService appversionsService;

    /**
     * 查看用户最新版本信息
     * @param type
     * @return json数据
     */
    @RequestMapping(value = "getAppversion", method=RequestMethod.GET)
    @ResponseBody
    public AjaxJson getAppversion(String type,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Appversions app = new Appversions();
        if (type.equals("1")){
            app.setRemarks("IOS");
            app.setDelFlag("0");
        }else if((type.equals("2"))){
            app.setRemarks("Android");
            app.setDelFlag("0");
        }else {
//            app.setRemarks("1");
//            app.setDelFlag("1");
            j.setSuccess(false);
            j.setErrorCode(ErrorCode.code_2005);
            j.setMsg("未查询到版本信息");
            j.put("data",new HashMap());
            return j;
        }
        List<Appversions> list = appversionsService.findList(app);
        if(list==null || list.size()==0){
            j.setSuccess(true);
            j.setErrorCode(ErrorCode.code_1004);
            j.setMsg("获取最新app版本信息成功");
            j.put("data", new HashMap());
        }else{
            String version = list.get(0).getVersions();
            String content = list.get(0).getContent();
            String qrcode = list.get(0).getQrcode();
            /*if (qrcode!=null){
                qrcode = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+qrcode;
            }*/
            String creartDate = sdf.format(list.get(0).getCreateDate());
            Map map = new HashMap();
            map.put("id",list.get(0).getId());
            map.put("version",version==null?"":version);
            map.put("content",content==null?"":content);
            if(type.equals("1")){
                map.put("qrcode",qrcode==null?"":qrcode);
                map.put("url",list.get(0).getUrl()==null?"":list.get(0).getUrl());
            }
            if(type.equals("2")){
                map.put("qrcode",qrcode==null?"":"http://cdn.gangwaninfo.com/app-data/appVersion/qrcode_1535459137.png");
                map.put("url",list.get(0).getUrl()==null?"":"http://cdn.gangwaninfo.com/app-data/appVersion/ruihuaoa.apk");
            }
            map.put("creartDate",creartDate==null?"":creartDate);
            map.put("createBy",list.get(0).getCreateBy()==null?"":list.get(0).getCreateBy().getName());
            map.put("files",list.get(0).getFiles()==null?"":list.get(0).getFiles());
            map.put("remarks",list.get(0).getRemarks()==null?"":list.get(0).getRemarks());
            j.put("data",map);
            j.setMsg("获取最新app版本信息成功");
            j.setErrorCode(ErrorCode.code_1004);
        }
        return j;
    }

    /**
     *  根据 id 查询一条版本记录
     * @param request 版本信息的id
     * @return  json
     */
    @RequestMapping(value = "getAppversionById",method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson getAppversionById(HttpServletRequest request,HttpServletResponse response){

        AjaxJson j = new AjaxJson();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String id = request.getParameter("id");
        Appversions app = appversionsService.get(id);

        if(app != null){
            Map<String,String> map = new HashMap<String,String>();
            map.put("id",app.getId());
            map.put("content",app.getContent());
            map.put("createDate",sdf.format(app.getCreateDate()));
            map.put("createBy",app.getCreateBy().getName());
            map.put("versions",app.getVersions());
            map.put("qrcode",app.getQrcode());
            map.put("url",app.getUrl());
            map.put("files",app.getFiles());
            map.put("remarks",app.getRemarks());

            j.put("data",map);
            j.setSuccess(true);
            j.setErrorCode(ErrorCode.code_1004);
            j.setMsg("获取版本信息成功！");
        }
        return  j ;
    }


}
