package com.happy.action;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.happy.Model.Config;
import com.happy.Until.GetHttpParam;
import com.happy.Until.ResUtil;
import com.happy.service.ConfigService;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class configAction extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request;
    public HttpServletResponse response;

    @Resource
    public ConfigService configService;

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public String update() {
        JSONObject resultjson = new JSONObject();
        Gson gson = new Gson();
        com.alibaba.fastjson.JSONObject json = GetHttpParam.getHttpParam(request);
        System.out.println(json);
        if (json == null) {
            resultjson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        Config config = null;
        try {
            config = gson.fromJson(json.toString(), new TypeToken<Config>() {}.getType());
            if (config == null) {
                resultjson.put("message", "数据为空");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            if (config.getFree_cancel()==null || config.getStop_book()==null ){
                resultjson.put("message", "各参数不能为空");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            int m = configService.update(config);
            if (m > 0) {
                resultjson.put("message", "修改成功");
                resultjson.put("code", 200);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            } else {
                resultjson.put("message", "修改失败");
                resultjson.put("code", 502);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultjson.put("message", "未知异常");
        resultjson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
        return null;
    }

    // 查看配置
    public String list(){
        JSONObject resultjson = new JSONObject();
        Config list = configService.queryConfig();
        if (list!=null){
            resultjson.put("message", "返回成功");
            resultjson.put("code", 200);
            resultjson.put("data",list);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        resultjson.put("message", "数据为空");
        resultjson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
        return null;
    }

}
