
package com.zhongdao.jlr.business.action;


import com.util.RequestUtil;
import com.util.UtilTools;
import com.util.Vo;
import com.zhongdao.jlr.business.service.CouponService;
import com.zhongdao.jlr.business.vo.LoginUserVo;
import org.apache.http.util.TextUtils;
import org.json.JSONObject;
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.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * 代金劵控制器
 */
@Controller
@RequestMapping("/coupon")
public class CouponAction {

    @Resource(name = "couponServiceImpl")
    CouponService baseService;

    /**
     * 账户查询页面
     */
    @RequestMapping(value = "/coupon_history.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET, RequestMethod.POST})
    public String couponHistory(HttpServletRequest request, HttpServletResponse response) {
        return "/page/coupon_consume_history";
    }

    /**
     * 代金劵搜索
     */
    @RequestMapping(value = "/search_consume.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET, RequestMethod.POST})
    public void searchConsume(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String vinCode = request.getParameter("vinCode");
        String userPhone = request.getParameter("userPhone");
        String electronicAccounts = request.getParameter("electronicAccounts");
        //查询代金券数据
        Vo list = baseService.searchConsume(vinCode, electronicAccounts,userPhone, UtilTools.getPage(request), UtilTools.getRows(request));
        //将数据响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(list));
    }

    /**
     * 获取个人代金券消费记录
     */

    @RequestMapping(value = "/get_coupon_consume.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET, RequestMethod.POST})
    public void getCouponConsume(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String vin = request.getParameter("vinCode");
        if (!TextUtils.isEmpty(vin)) {
            //获取个人代金券消费信息
            Vo vo = baseService.getCouponConsume(vin, UtilTools.getPage(request), UtilTools.getRows(request));
            //将数据响应到客户端
            RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(vo));

        } else {
            RequestUtil.responseResult4JsonError(response, "参数为空");
            ;
        }

    }

    /**
     * 获取个人充值记录
     */
    @RequestMapping(value = "/get_coupon_recharge.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET, RequestMethod.POST})
    public void getCouponRecharge(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String vin = request.getParameter("vinCode");
        if (!TextUtils.isEmpty(vin)) {
            //获取用户充值记录
            Vo vo = baseService.getCouponRecharge(vin, UtilTools.getPage(request), UtilTools.getRows(request));
            //将用户充值记录信息发送到客户端
            RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(vo));
        } else {
            RequestUtil.responseResult4JsonError(response, "参数为空");
            ;
        }

    }

    /**
     * 消费记录查询、处理页面
     */
    @RequestMapping(value = "/consume_query.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET})
    public String couponConsumeQuery(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String state = request.getParameter("state");
        //共享数据
        request.setAttribute("state",state);
        return "/page/coupon_consume_query";
    }

    /**
     * 消费记录查询、处理页面
     */
    @RequestMapping(value = "/consume_querys.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.POST})
    public void getConsumeQuery(@RequestParam(value = "vininput", required = false, defaultValue = "") String vin,
                                @RequestParam(value = "electronicAccounts", required = false, defaultValue = "") String account,
                                @RequestParam(value = "license", required = false, defaultValue = "") String license,
                                @RequestParam(value = "phone", required = false, defaultValue = "") String phone,
                                @RequestParam(value = "rescueCompanyName", required = false, defaultValue = "") String rescueCompanyName,
                                @RequestParam(value = "queryDate1", required = false, defaultValue = "") String queryDate1,
                                @RequestParam(value = "queryDate2", required = false, defaultValue = "") String queryDate2,
                                @RequestParam(value = "checkState", required = false, defaultValue = "") String checkState,
                                HttpServletRequest request, HttpServletResponse response) {
        //获取session信息
        LoginUserVo logininfo = (LoginUserVo) request.getSession().getAttribute("logininfo");
        String rescueCompanyCode = "";
        if (logininfo.getEnterpriseJlrId() != null) rescueCompanyCode = logininfo.getEnterpriseJlrId().toString();
        //查询用户消费记录信息并发送到客户端
        Vo vo = baseService.queryCouponConsume(vin, account, rescueCompanyName, rescueCompanyCode, license, phone, checkState, queryDate1, queryDate2, UtilTools.getPage(request), UtilTools.getRows(request));
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(vo));
    }

    /**
     * 导出消费记录报表
     */
    @RequestMapping(value = "/exportData.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET, RequestMethod.POST}
    )
    public void exportData(@RequestParam(value = "vininput", required = false, defaultValue = "") String vin,
                           @RequestParam(value = "electronicAccounts", required = false, defaultValue = "") String account,
                           @RequestParam(value = "license", required = false, defaultValue = "") String license,
                           @RequestParam(value = "phone", required = false, defaultValue = "") String phone,
                           @RequestParam(value = "rescueCompanyName", required = false, defaultValue = "") String rescueCompanyName,
                           @RequestParam(value = "queryDate1", required = false, defaultValue = "") String queryDate1,
                           @RequestParam(value = "queryDate2", required = false, defaultValue = "") String queryDate2,
                           @RequestParam(value = "checkState", required = false, defaultValue = "") String checkState, HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsonObject = null;
        //获取session数据
        LoginUserVo logininfo = (LoginUserVo) request.getSession().getAttribute("logininfo");
        try {
            String rescueCompanyCode = "";
            //获取导出的报表信息
            if (logininfo.getEnterpriseJlrId() != null) rescueCompanyCode = logininfo.getEnterpriseJlrId().toString();
            jsonObject = baseService.exportData(request, vin, account, rescueCompanyName, rescueCompanyCode, license, phone, checkState, queryDate1, queryDate2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将报表信息响应到客户端
        RequestUtil.responseResult4Json(response, jsonObject.toString());
    }

    /**
     * 消费记录查询、处理页面
     */
    @RequestMapping(value = "/consume_update.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.POST})
    public void updateConsumeState(@RequestParam(value = "ids", required = false, defaultValue = "") String ids,
                                   @RequestParam(value = "state", required = false, defaultValue = "") String state,
                                   HttpServletRequest request, HttpServletResponse response) {
        //修改消费记录的状态
        Boolean b = baseService.updateConsumeState(ids, state);
        //将修改记录的状态响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(b));
    }

    /**
     * 代金卷账户密码短信发送
     */
    @RequestMapping(value = "/sendMsg.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void sendMsg(HttpServletRequest request, HttpServletResponse response) {
        String phoneNum = request.getParameter("phoneNum");
        String msgContent = request.getParameter("msgContent");
        phoneNum = phoneNum == null ? "" : phoneNum;
        msgContent = msgContent == null ? "" : msgContent;
        boolean sign = baseService.sendMsg(phoneNum, msgContent);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sign", sign);
        RequestUtil.responseResult4Json(response, jsonObject.toString());
    }

    /**
     * 充值记录查询页面
     */
    @RequestMapping(value = "/recharge_query.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET})
    public String couponRechargeQuery(HttpServletRequest request, HttpServletResponse response) {
        return "/page/coupon_recharge_query";
    }

    /**
     * 充值记录查询、处理页面
     */
    @RequestMapping(value = "/recharges_query.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.POST})
    public void getRechargeQuery(@RequestParam(value = "vininput", required = false, defaultValue = "") String vin,
                                 @RequestParam(value = "electronicAccounts", required = false, defaultValue = "") String account,
                                 @RequestParam(value = "license", required = false, defaultValue = "") String license,
                                 @RequestParam(value = "phone", required = false, defaultValue = "") String phone,
                                 @RequestParam(value = "orderCode", required = false, defaultValue = "") String orderCode,
                                 @RequestParam(value = "queryDate1", required = false, defaultValue = "") String queryDate1,
                                 @RequestParam(value = "queryDate2", required = false, defaultValue = "") String queryDate2,
                                 HttpServletRequest request, HttpServletResponse response) {
        Vo vo = baseService.queryCouponRecharge(vin, account, orderCode, license, phone, queryDate1, queryDate2, UtilTools.getPage(request), UtilTools.getRows(request));
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(vo));
    }

    /**
     * 导出充值记录报表
     */
    @RequestMapping(value = "/rechangeExportData.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void rechangeExportData(@RequestParam(value = "vininput", required = false, defaultValue = "") String vin,
                                   @RequestParam(value = "electronicAccounts", required = false, defaultValue = "") String account,
                                   @RequestParam(value = "phone", required = false, defaultValue = "") String phone,
                                   @RequestParam(value = "orderCode", required = false, defaultValue = "") String orderCode,
                                   @RequestParam(value = "queryDate1", required = false, defaultValue = "") String queryDate1,
                                   @RequestParam(value = "queryDate2", required = false, defaultValue = "") String queryDate2,
                                   HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsonObject = null;
        try {
            //获取充值记录的报表信息
            jsonObject = baseService.rechangeExportData(request, vin, account, orderCode,  phone, queryDate1, queryDate2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将报表信息响应到客户端
        RequestUtil.responseResult4Json(response, jsonObject.toString());
    }

    /**
     * 代金劵消费页面
     */
    @RequestMapping(value = "/coupon_consume.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET, RequestMethod.POST})
    public String couponConsume(HttpServletRequest request, HttpServletResponse response) {
        return "/page/coupon_consume";
    }

    /**
     * 获取个人账户信息
     */
    @RequestMapping(value = "/get_coupon.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET, RequestMethod.POST})
    public void getCoupon(HttpServletRequest request, HttpServletResponse response) {
        String vin = request.getParameter("vin");
        String electronicAccounts = request.getParameter("electronicAccounts");

        if (!TextUtils.isEmpty(vin) || !TextUtils.isEmpty(electronicAccounts)) {
            Vo list = baseService.getCoupon(vin, electronicAccounts, UtilTools.getPage(request), UtilTools.getRows(request));

            RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(list));
        } else {
            RequestUtil.responseResult4JsonError(response, "参数为空");
        }
    }

    /**
     * 代金劵消费
     */
    @RequestMapping(value = "/consume.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET, RequestMethod.POST})
    public void consume(HttpServletRequest request, HttpServletResponse response) {
        //声明一个map集合，用来存储返回的结果集
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "消费失败");
        //获取参数
        String vinCode = request.getParameter("vinCode");
        String consumeCount = request.getParameter("consumeCount");
        String remark = request.getParameter("remark");
        String epassword = request.getParameter("epassword");
        String consumeTime = request.getParameter("consumeTime");
        String reparieInfo = request.getParameter("reparieInfo");
        String hitchInfo = request.getParameter("hitchInfo");
        //获取用户信息
        LoginUserVo loginuser = (LoginUserVo) request.getSession().getAttribute("logininfo");
        //若用户为空则返回错误信息
        if (loginuser == null) {
            result.put("msg", "没有登录");
            RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));
            return;
        }
        if (!TextUtils.isEmpty(consumeTime) && !TextUtils.isEmpty(vinCode) && !TextUtils.isEmpty(consumeCount) && !TextUtils.isEmpty(epassword)) {
            Map back = baseService.consume(consumeTime, vinCode, Double.valueOf(consumeCount), Integer.valueOf(loginuser.getEnterpriseJlrId()), reparieInfo, hitchInfo, remark, epassword);
            if ((boolean) back.get("status")) {
                result.put("flag", "1");
                result.put("msg", "消费成功");
            } else {
                result.put("msg", back.get("msg").toString());
            }
        } else {

            result.put("msg", "参数为空");
        }
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));
    }
}
