package com.happy.action;

import com.google.gson.Gson;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.reflect.TypeToken;
import com.happy.Model.*;
import com.happy.Model.app.Around_product;
import com.happy.Model.app.Arounds;
import com.happy.Model.app.News;
import com.happy.Model.weixin.*;
import com.happy.Until.*;
import com.happy.common.http.Get_airticle;
import com.happy.common.http.HttpsClient;
import com.happy.common.http.Message2;
import com.happy.common.http.Message3;
import com.happy.common.model.airticle.Content;
import com.happy.common.model.airticle.Item_content;
import com.happy.common.model.airticle.News_content;
import com.happy.common.util.WechatUtil;
import com.happy.common.wx.WxUtil;
import com.happy.service.*;
import com.opensymphony.xwork2.ActionSupport;
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;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class appAction extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request;
    public HttpServletResponse response;

    @Resource
    public UserService userService;
    @Resource
    public BookService bookService;
    @Resource
    public HouseService houseService;
    @Resource
    public ConfigService configService;
    @Resource
    public AppService appService;

    public Integer id;
    public Integer aid;
    public String wxcode;
    public String state;
    public String code;
    public String order_num;
    public String card_number;
    public String phone;
    public String admin_name;
    public String password;
    public String openid;
    public int page; // 当前页
    public int rows;// 每页显示的行数rows
    public String rtype;
    public String rtown;
    public String key;
    public String title;
    public Integer is_top;
    public String start_date;
    public String end_date;
    public String town;

    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 getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getWxcode() {
        return wxcode;
    }

    public void setWxcode(String wxcode) {
        this.wxcode = wxcode;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getOrder_num() {
        return order_num;
    }

    public void setOrder_num(String order_num) {
        this.order_num = order_num;
    }

    public String getCard_number() {
        return card_number;
    }

    public void setCard_number(String card_number) {
        this.card_number = card_number;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAdmin_name() {
        return admin_name;
    }

    public void setAdmin_name(String admin_name) {
        this.admin_name = admin_name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAid() {
        return aid;
    }

    public void setAid(Integer aid) {
        this.aid = aid;
    }

    public String getRtype() {
        return rtype;
    }

    public void setRtype(String rtype) {
        this.rtype = rtype;
    }

    public String getRtown() {
        return rtown;
    }

    public void setRtown(String rtown) {
        this.rtown = rtown;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getIs_top() {
        return is_top;
    }

    public void setIs_top(Integer is_top) {
        this.is_top = is_top;
    }

    public String getStart_date() {
        return start_date;
    }

    public void setStart_date(String start_date) {
        this.start_date = start_date;
    }

    public String getEnd_date() {
        return end_date;
    }

    public void setEnd_date(String end_date) {
        this.end_date = end_date;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

    public String test() throws Exception {
        JSONObject resultJson = new JSONObject();
        String msg = Message2.send(openid,"20231011163520","总统套房",
                "2023-10-11","测试","测试","");
        resultJson.put("message", "返回成功");
        resultJson.put("code", 200);
        resultJson.put("data",msg);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    // 查询是否绑定
    public String getUser(){
        JSONObject resultJson = new JSONObject();
        if (code==null){
            resultJson.put("message", "code不能为空");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        JSONObject jsonObject = appService.getGCodeInfo(code);
        Admin admin = appService.queryByOpenid(jsonObject.get("openid").toString());
        if (admin==null){
            resultJson.put("message", "未绑定信息");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        resultJson.put("message", "返回成功");
        resultJson.put("code", 200);
        resultJson.put("data", admin);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    // 开始绑定
    public String code() throws IOException {
        JSONObject resultJson = new JSONObject();
        if (code==null || admin_name==null || password==null){
            resultJson.put("message", "参数不能为空");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        Admin login = appService.login(admin_name,password);
        if (login==null){
            resultJson.put("message", "账户或密码错误");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        JSONObject jsonObject = appService.getGCodeInfo(code);
        Admin admin = appService.queryByNameAndOpenid(admin_name,jsonObject.get("openid").toString());
        if (admin!=null){
            resultJson.put("message", "该账户已绑定，请勿重复绑定");
            resultJson.put("code", 205);
            resultJson.put("data",admin);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        int m = appService.updateOpenid(jsonObject.get("openid").toString(),admin_name);
        if (m>0){
            admin = appService.queryByNameAndOpenid(admin_name,jsonObject.get("openid").toString());
            resultJson.put("message", "绑定成功");
            resultJson.put("code", 200);
            resultJson.put("data",admin);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        return null;
    }

    // 解绑
    public String uncode(){
        JSONObject resultJson = new JSONObject();
        if (code==null || admin_name==null || password==null){
            resultJson.put("message", "参数不能为空");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        Admin login = appService.login(admin_name,password);
        if (login==null){
            resultJson.put("message", "账户或密码错误");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        JSONObject jsonObject = appService.getGCodeInfo(code);
        Admin admin = appService.queryByNameAndOpenid(admin_name,jsonObject.get("openid").toString());
        if (admin==null){
            resultJson.put("message", "输入信息与当前绑定账户不匹配");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        int m = appService.updateOpenidNull(jsonObject.get("openid").toString());
        if (m>0){
            resultJson.put("message", "解绑成功");
            resultJson.put("code", 200);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        resultJson.put("message", "解绑失败");
        resultJson.put("code", 205);
        resultJson.put("data","");
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /******************************商户绑定*******************************/
    public String get_user_ma(){
        JSONObject resultJson = new JSONObject();
        if (code==null){
            resultJson.put("message", "参数不能为空");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        JSONObject jsonObject = appService.getGCodeInfo(code);
        AdminManager admin = appService.queryMaByOpenid(jsonObject.get("openid").toString());
        if (admin==null){
            resultJson.put("message", "未绑定信息");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        resultJson.put("message", "返回成功");
        resultJson.put("code", 200);
        resultJson.put("data", admin);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    public String manage_code(){
        JSONObject resultJson = new JSONObject();
        if (code==null || admin_name==null || password==null){
            resultJson.put("message", "参数不能为空");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        AdminManager login = appService.login_ma(admin_name,password);
        if (login==null){
            resultJson.put("message", "账户或密码错误");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        if (login.getLevel().equals("1")){
            resultJson.put("message", "管理员无权限绑定");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        JSONObject jsonObject = appService.getGCodeInfo(code);
        AdminManager admin = appService.queryMaByNameAndOpenid(admin_name,jsonObject.get("openid").toString());
        if (admin!=null){
            resultJson.put("message",  "该账户已绑定，请勿重复绑定");
            resultJson.put("code", 205);
            resultJson.put("data",admin);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }

        int m = appService.updateMaOpenid(jsonObject.get("openid").toString(),admin_name);
        if (m>0){
            admin = appService.queryMaByNameAndOpenid(admin_name,jsonObject.get("openid").toString());
            resultJson.put("message", "绑定成功");
            resultJson.put("code", 200);
            resultJson.put("data",admin);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        return null;
    }

    public String uncode_ma(){
        JSONObject resultJson = new JSONObject();
        if (code==null || admin_name==null || password==null){
            resultJson.put("message", "参数不能为空");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        AdminManager login = appService.login_ma(admin_name,password);
        if (login==null){
            resultJson.put("message", "账户或密码错误");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        JSONObject jsonObject = appService.getGCodeInfo(code);
        AdminManager admin = appService.queryMaByNameAndOpenid(admin_name,jsonObject.get("openid").toString());
        if (admin==null){
            resultJson.put("message", "输入信息与当前绑定账户不匹配");
            resultJson.put("code", 205);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        int m = appService.updateMaOpenidNull(jsonObject.get("openid").toString());
        if (m>0){
            resultJson.put("message", "解绑成功");
            resultJson.put("code", 200);
            resultJson.put("data","");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        resultJson.put("message", "解绑失败");
        resultJson.put("code", 205);
        resultJson.put("data","");
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**=============================新闻资讯==============================**/
    public String autoInsertNews() throws Exception {
        JSONObject resultjson = new JSONObject();
        int total = Get_airticle.getTotal();
        int rows = 10;
        int page = total % rows == 0 ? (total / rows)
                : (total / rows) + 1;
        System.out.println(page);
        int m = 0;
        for (int k = 1; k <= page; k++) {
            List<Item_content> litem = appService.getNews(k,rows);
            for (int i = 0; i < litem.size(); i++) {
                Item_content ic = litem.get(i);
                Content content = ic.getContent();
                List<News_content> news_item = content.getNews_item();
                for (int j = 0; j < news_item.size(); j++) {
                    News news = appService.queryByTit(news_item.get(j).getTitle());
                    if (news==null && !news_item.get(j).getTitle().contains("草稿") && news_item.get(j).getTitle().contains("资产")){
                        System.out.println("插入标题："+news_item.get(j).getTitle());
                        News nw = new News();
                        nw.setMedia_id(ic.getMedia_id());
                        nw.setTitle(news_item.get(j).getTitle());
                        nw.setAuthor(news_item.get(j).getAuthor());
                        nw.setContent(news_item.get(j).getContent());
                        nw.setDigest(news_item.get(j).getDigest());
                        nw.setContent_source_url(news_item.get(j).getContent_source_url());
                        nw.setUpdate_time(TimeExchange.timestampToString(Integer.parseInt(ic.getUpdate_time())));
                        File file = DownloadPicture.download(Get_airticle.getFirstImg(news_item.get(j).getContent()));
                        String url = HttpsClient.postFile("https://www.jinganrenjiams.com/hotelReservation/file/cos/upload", file);
                        nw.setFirst_img(url);
                        nw.setState(1);
                        nw.setIs_top(0);
                        appService.insertNews(nw);
                        m++;
                    }
                }
            }
        }
        if (m>0){
            resultjson.put("message", "新增完成");
            resultjson.put("code", 200);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        resultjson.put("message", "暂无最新文章");
        resultjson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
        return null;
    }

    public String delNews() {
        JSONObject resultjson = new JSONObject();
        if (id == null) {
            resultjson.put("message", "请传入id");
            resultjson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        int m = appService.delNews(id);
        if (m > 0) {
            resultjson.put("message", "删除成功");
            resultjson.put("code", 200);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        resultjson.put("message", "未知异常");
        resultjson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
        return null;
    }

    public String updateNews() throws Exception {
        JSONObject resultjson = new JSONObject();
        if (id == null) {
            resultjson.put("message", "请传入id");
            resultjson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        News news = appService.queryById(id);
        if (news.getState()==0){
            resultjson.put("message", "该条咨询已删除");
            resultjson.put("code", 205);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        String media_id = news.getMedia_id();
        Content content = Get_airticle.getByMediaId(media_id);
        List<News_content> ln = content.getNews_item();
        News_content nc = ln.get(0);
        news.setTitle(nc.getTitle());
        news.setAuthor(nc.getAuthor());
        news.setDigest(nc.getDigest());
        news.setContent(nc.getContent());
        int m = appService.updateNews(news);
        if (m>0){
            resultjson.put("message", "修改成功");
            resultjson.put("code", 200);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        resultjson.put("message", "远程咨询已删除，不可更新");
        resultjson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
        return null;
    }

    public String updateTop(){
        JSONObject resultjson = new JSONObject();
        if (id == null || is_top == null) {
            resultjson.put("message", "请传入参数");
            resultjson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        News news = appService.queryById(id);
        if (news.getState()==0){
            resultjson.put("message", "该条咨询已删除");
            resultjson.put("code", 205);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        if (news.getIs_top()==1 && is_top ==1){
            resultjson.put("message", "该条咨询已置顶");
            resultjson.put("code", 205);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        if (news.getIs_top()==0 && is_top ==0){
            resultjson.put("message", "该条咨询已取消置顶");
            resultjson.put("code", 205);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        news.setIs_top(is_top);
        if (news.getIs_top()==1){
            news.setTop_time(TimeExchange.getTime());
        } else {
            news.setTop_time(null);
        }
        int m = appService.updateTop(news);
        if (m>0){
            resultjson.put("message", "操作成功");
            resultjson.put("code", 200);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        resultjson.put("message", "操作失败");
        resultjson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
        return null;
    }

    public String newlist() {
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        if (start_date!=null){
            s1.append(" and update_time > '").append(this.start_date).append("' ");
        }
        if (end_date!=null){
            s1.append(" and update_time < '").append(this.end_date).append(" 24:60:60' ");
        }
        if (key!=null){
            s1.append(" and ( title like '%").append(this.key)
                    .append("%' or content like '%").append(this.key).append("%' ) ");
        }
        if (is_top!=null && is_top==1){
            s1.append(" order by is_top desc,top_time desc,update_time desc ");
        } else {
            s1.append(" order by update_time desc ");
        }
        int total = appService.queryNewTotal(s1.toString());// 查询表中的总记录数
        List<News> listPage = appService.queryNewPage(page, rows,s1.toString());// 查询分页

        if (listPage == null) {
            resultJson.put("code", 200);
            resultJson.put("rows", "");
            resultJson.put("total", 0);
        } else {
            resultJson.put("code", 200);
            resultJson.put("rows", listPage);
            // int total = listAll.size();
            resultJson.put("total", total);// 总记录数
            int totalPage = total % rows == 0 ? (total / rows)
                    : (total / rows) + 1;// 总页数
            resultJson.put("totalPage", totalPage);
            resultJson.put("currentPage", page);// 当前页
            resultJson.put("numPerPage", rows);// 每页数
            resultJson.put("nextPage", totalPage - page == 0 ? page : page + 1);// 下一页
            resultJson.put("previousPage", page - 0 == 1 ? page : page - 1);// 上一页
            resultJson.put("hasPreviousPage", true);// 有上一页
            resultJson.put("hasNextPage", true);// 有下一页
            resultJson.put("firstPage", true);// 首页
            resultJson.put("lastPage", true);// 尾页
        }
        ResponseUtil.writeJson(ServletActionContext.getResponse(),
                resultJson.toString());
        return null;
    }

    /** ==================================周边================================== **/
    public String insertArounds() {
        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;
        }
        Arounds arounds = null;
        try {
            arounds = gson.fromJson(json.toString(), new TypeToken<Arounds>() {}.getType());
            if (arounds == null) {
                resultjson.put("message", "数据为空");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            if (arounds.getRtype()==null || arounds.getRname()==null || arounds.getRtown()==null || arounds.getRadress()==null || arounds.getRphone()==null || arounds.getDetail()==null || arounds.getFirst_img()==null || arounds.getDetail_img()==null || arounds.getJingwei()==null){
                resultjson.put("message", "各参数不能为空");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            Arounds listc = appService.queryAroundByName(arounds.getRname());
            if (listc != null) {
                resultjson.put("message", "该周边已存在");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            arounds.setCnum(0);
            int m = appService.insertRound(arounds);
            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 updateArounds() {
        JSONObject resultjson = new JSONObject();
        Gson gson = new Gson();
        JSONObject json = GetHttpParam.getHttpParam(request);
        System.out.println(json);
        if (json == null) {
            resultjson.put("code", 205);
            resultjson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        Arounds arounds = null;
        try {
            arounds = gson.fromJson(json.toString(), new TypeToken<Arounds>() {}.getType());
            if (arounds == null) {
                resultjson.put("message", "数据为空");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            if (arounds.getId()==null || arounds.getRtype()==null || arounds.getRname()==null || arounds.getRtown()==null || arounds.getRadress()==null || arounds.getRphone()==null || arounds.getDetail()==null || arounds.getFirst_img()==null || arounds.getDetail_img()==null || arounds.getJingwei()==null){
                resultjson.put("message", "各参数不能为空");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            Arounds listy = appService.queryAroundById(arounds.getId());
            if (listy==null){
                resultjson.put("message", "该条数据已删除");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            Arounds listc = appService.queryAroundByName(arounds.getRname());
            if (listc!=null && !Objects.equals(listc.getId(), arounds.getId())){
                resultjson.put("message", "周边已存在");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            int m = appService.updateRoundById(arounds);
            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 delAround() {
        JSONObject resultjson = new JSONObject();
        if (id == null) {
            resultjson.put("message", "请传入id");
            resultjson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        Arounds arounds = appService.queryAroundById(id);
        if (arounds==null){
            resultjson.put("message", "该周边已删除");
            resultjson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        List<Around_product> aps = appService.queryAP(id, "");
        if (aps!=null){
            resultjson.put("message", "存在已关联产品不可删除");
            resultjson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        int m = appService.delAround(id);
        if (m > 0) {
            resultjson.put("message", "删除成功");
            resultjson.put("code", 200);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        resultjson.put("message", "未知异常");
        resultjson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
        return null;
    }

    public String listAround() {
        System.out.println("周边城镇："+rtown);
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        if (rtype!=null){
            s1.append(" and rtype = '").append(this.rtype).append("' ");
        }
        if (rtown!=null){
            s1.append(" and rtown = '").append(this.rtown).append("' ");
        }
        if (key!=null){
            s1.append(" and ( rname like '%").append(this.key)
                    .append("%' or rphone like '%").append(this.key)
                    .append("%' or radress like '%").append(this.key).append("%' ) ");
        }
        int total = appService.queryAroundTotal(s1.toString()); // 查询表中的总记录数
        List<Arounds> listPage = appService.queryAroundPage(s1.toString(),page,rows); // 查询分页

        if (listPage == null) {
            resultJson.put("code", 200);
            resultJson.put("rows", "");
            resultJson.put("total", 0);
        } else {
            resultJson.put("code", 200);
            resultJson.put("rows", listPage);
            // int total = listAll.size();
            resultJson.put("total", total);// 总记录数
            int totalPage = total % rows == 0 ? (total / rows)
                    : (total / rows) + 1;// 总页数
            resultJson.put("totalPage", totalPage);
            resultJson.put("currentPage", page);// 当前页
            resultJson.put("numPerPage", rows);// 每页数
            resultJson.put("nextPage", totalPage - page == 0 ? page : page + 1);// 下一页
            resultJson.put("previousPage", page - 0 == 1 ? page : page - 1);// 上一页
            resultJson.put("hasPreviousPage", true);// 有上一页
            resultJson.put("hasNextPage", true);// 有下一页
            resultJson.put("firstPage", true);// 首页
            resultJson.put("lastPage", true);// 尾页
        }
        ResponseUtil.writeJson(ServletActionContext.getResponse(),
                resultJson.toString());
        return null;
    }

    /** =========================产品============================== **/
    public String insertAroundPd() {
        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("code", 500);
            resultjson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        Around_product around_product = null;
        try {
            around_product = gson.fromJson(json.toString(), new TypeToken<Around_product>() {}.getType());
            if (around_product == null) {
                resultjson.put("message", "数据为空");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            if (around_product.getAid()==null || around_product.getProduct_name()==null || around_product.getProduct_desc()==null || around_product.getPrice()==null){
                resultjson.put("message", "各参数不能为空");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            Around_product listc = appService.queryAPByName(around_product.getAid(),around_product.getProduct_name());
            if (listc != null) {
                resultjson.put("message", "该产品已存在");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            Arounds arounds = appService.queryAroundById(around_product.getAid());
            if (arounds==null){
                resultjson.put("message", "关联周边已删除");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            int m = appService.insertProduct(around_product);
            if (m > 0) {
                arounds.setCnum(arounds.getCnum()+1);
                appService.updateRoundCountById(arounds);
                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 updateAround_pd() {
        JSONObject resultjson = new JSONObject();
        Gson gson = new Gson();
        JSONObject json = GetHttpParam.getHttpParam(request);
        System.out.println(json);
        if (json == null) {
            resultjson.put("code", 500);
            resultjson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        Around_product around_product = null;
        try {
            around_product = gson.fromJson(json.toString(), new TypeToken<Around_product>() {}.getType());
            if (around_product == null) {
                resultjson.put("message", "数据为空");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            if (around_product.getProduct_name()==null || around_product.getProduct_desc()==null || around_product.getPrice()==null){
                resultjson.put("message", "各参数不能为空");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            Around_product listy = appService.queryAPById(around_product.getId());
            if (listy==null){
                resultjson.put("message", "该条数据已删除");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            Around_product listc = appService.queryAPByName(listy.getAid(), around_product.getProduct_name());
            if (listc!=null && !Objects.equals(listc.getId(), around_product.getId())){
                resultjson.put("message", "产品已存在");
                resultjson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
                return null;
            }
            int m = appService.updateProductById(around_product);
            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 delAround_pd() {
        JSONObject resultjson = new JSONObject();
        if (id == null) {
            resultjson.put("message", "请传入id");
            resultjson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        Around_product around_product = appService.queryAPById(id);
        if (around_product==null){
            resultjson.put("message", "该产品已删除");
            resultjson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        Arounds arounds = appService.queryAroundById(around_product.getAid());
        if (arounds==null){
            resultjson.put("message", "关联周边已删除");
            resultjson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        int m = appService.delAroundProduct(id);
        if (m > 0 ) {
            arounds.setCnum(arounds.getCnum() - 1);
            appService.updateRoundCountById(arounds);
            resultjson.put("message", "删除成功");
            resultjson.put("code", 200);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        resultjson.put("message", "未知异常");
        resultjson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
        return null;
    }

    public String listAround_pd() {
        JSONObject resultJson = new JSONObject();
        int total = appService.queryAPTotal(aid,""); // 查询表中的总记录数
        List<Around_product> listPage = appService.queryAPPage(aid,"",page,rows); // 查询分页

        if (listPage == null) {
            resultJson.put("code", 200);
            resultJson.put("rows", "");
            resultJson.put("total", 0);
        } else {
            resultJson.put("code", 200);
            resultJson.put("rows", listPage);
            // int total = listAll.size();
            resultJson.put("total", total);// 总记录数
            int totalPage = total % rows == 0 ? (total / rows)
                    : (total / rows) + 1;// 总页数
            resultJson.put("totalPage", totalPage);
            resultJson.put("currentPage", page);// 当前页
            resultJson.put("numPerPage", rows);// 每页数
            resultJson.put("nextPage", totalPage - page == 0 ? page : page + 1);// 下一页
            resultJson.put("previousPage", page - 0 == 1 ? page : page - 1);// 上一页
            resultJson.put("hasPreviousPage", true);// 有上一页
            resultJson.put("hasNextPage", true);// 有下一页
            resultJson.put("firstPage", true);// 首页
            resultJson.put("lastPage", true);// 尾页
        }
        ResponseUtil.writeJson(ServletActionContext.getResponse(),
                resultJson.toString());
        return null;
    }

    public String queryMsByTown() {
        JSONObject resultJson = new JSONObject();
        if (town==null){
            resultJson.put("message", "请传入乡镇");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        List<Hotel> list = appService.queryMs(town); // 查询分页
        if (list==null){
            resultJson.put("message", "数据为空");
            resultJson.put("code", 205);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        resultJson.put("message", "返回成功");
        resultJson.put("code", 200);
        resultJson.put("data", list);
        ResponseUtil.writeJson(ServletActionContext.getResponse(),
                resultJson.toString());
        return null;
    }

}
