package com.nmgs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nmgs.config.ResultData;
import com.nmgs.service.impl.QCodeInfoServiceImpl;
import com.nmgs.util.LogUtil;
import com.nmgs.util.PubTools;
import com.nmgs.util.WebsocketUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value="qcodeInfo")
@Api(tags="核销二维码内容获取")
public class QcodeInfoController {
    public QCodeInfoServiceImpl qCodeInfoServiceImpl;
    @Autowired
    public void setqCodeInfoServiceImpl(QCodeInfoServiceImpl qCodeInfoServiceImplT) {
        this.qCodeInfoServiceImpl = qCodeInfoServiceImplT;
    }

    @RequestMapping(value = "getQcodeInfoByUserId",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="根据用户和积分类型获取二维码内容",httpMethod = "POST")
    public ResultData<String> getQcodeInfoByUserId(@RequestParam("openId") String openId,@RequestParam("integrateType") String integrateType,@RequestParam("rebateIntegrate") String rebateIntegrate){
        //String qcodeInfoByUserId = qCodeInfoServiceImpl.getQcodeInfoByUserId(openId, integrateType, rebateIntegrate);
        String qcodeInfoByUserId = qCodeInfoServiceImpl.getQcodeInfoByUserIdRedis(openId, integrateType, rebateIntegrate);
        if("-2".equals(qcodeInfoByUserId)){
            return ResultData.success(2,"用户未找到");
        }else if("-3".equals(qcodeInfoByUserId)){
            return ResultData.success(3,"用户已被冻结");
        }else if("5".equals(qcodeInfoByUserId)){
            return ResultData.success(4,"缓存池出错了");
        }else{
            return ResultData.success("生成二维码成功",qcodeInfoByUserId,1);
        }
    }

    @RequestMapping(value = "solveQcode",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="商超核销",httpMethod = "POST")
    public ResultData<String> solveQcode(@RequestParam("content") String content,
                                         @RequestParam("serverOperationId") String serverOperationId,
                                         @RequestParam("orderId") String orderId,
                                         @RequestParam(value = "payMoney",required = false) String payMoney,
                                         @RequestParam(value = "discountMoneyBefore",required = false) String discountMoneyBefore,
                                         @RequestParam(value = "discountMoney",required = false) String discountMoney) throws Exception {

        if(PubTools.isNull(payMoney)){
            payMoney="0";
        }
        if(PubTools.isNull(discountMoneyBefore)){
            discountMoneyBefore="0";
        }
        if(PubTools.isNull(discountMoney)){
            discountMoney="0";
        }
        LogUtil.WriteLog_Info("积分核减操作开始====="+PubTools.getCurrentDate(),"QcodeInfoController");
        //JSONObject retStrJSON = qCodeInfoServiceImpl.solveQcode(content, serverOperationId,orderId, Double.parseDouble(payMoney), Double.parseDouble(discountMoneyBefore), Double.parseDouble(discountMoney));
        JSONObject retStrJSON = qCodeInfoServiceImpl.solveQcodeRedis(content, serverOperationId, orderId, Double.parseDouble(payMoney), Double.parseDouble(discountMoneyBefore), Double.parseDouble(discountMoney));

        String retStr= retStrJSON.get("ret").toString();
       String openId= PubTools.isEmpty(retStrJSON.get("openId"))?"":retStrJSON.get("openId").toString();
        LogUtil.WriteLog_Info("积分核销结果==="+PubTools.getCurrentDate()+"==="+retStrJSON.toString(),"QcodeInfoController");
       JSONArray ja=new JSONArray();
       ja.add(retStrJSON);
        if("1".equals(retStr)){
            JSONObject retJo=new JSONObject();
            retJo.put("code",1);
            retJo.put("msg","核销成功");
            retJo.put("data","");
            WebsocketUtil.sendMessage(openId,JSONObject.toJSONString(retJo));
            WebsocketUtil.removeSession(openId);
           return ResultData.success("核销成功",ja.toString(),1);
       }else if("2".equals(retStr)){
            JSONObject retJo=new JSONObject();
            retJo.put("code",2);
            retJo.put("msg","用户不存在");
            retJo.put("data","");
            WebsocketUtil.sendMessage(openId,JSONObject.toJSONString(retJo));
            WebsocketUtil.removeSession(openId);
           return ResultData.success(2,"用户不存在");
       }else if("3".equals(retStr)){
            JSONObject retJo=new JSONObject();
            retJo.put("code",3);
            retJo.put("msg","用户可用积分不足");
            retJo.put("data",ja.toString());
            WebsocketUtil.sendMessage(openId,JSONObject.toJSONString(retJo));
            WebsocketUtil.removeSession(openId);
           return ResultData.fail(3,"用户可用积分不足",ja.toString());
       }else if("4".equals(retStr)){
            JSONObject retJo=new JSONObject();
            retJo.put("code",4);
            retJo.put("msg","二维码已过期");
            retJo.put("data","");
            WebsocketUtil.sendMessage(openId,JSONObject.toJSONString(retJo));
            WebsocketUtil.removeSession(openId);
           return ResultData.success(4,"二维码已过期");
       }else if("5".equals(retStr)){
            JSONObject retJo=new JSONObject();
            retJo.put("code",5);
            retJo.put("msg","积分核减失败");
            retJo.put("data","");
            WebsocketUtil.sendMessage(openId,JSONObject.toJSONString(retJo));
            WebsocketUtil.removeSession(openId);
            return ResultData.success(5,"商户未注册");
        }else if("6".equals(retStr)){
            JSONObject retJo=new JSONObject();
            retJo.put("code",6);
            retJo.put("msg","积分类型与消费方式不匹配");
            retJo.put("data","");
            WebsocketUtil.sendMessage(openId,JSONObject.toJSONString(retJo));
            WebsocketUtil.removeSession(openId);
            return ResultData.success(6,"积分类型与消费方式不匹配");
        }else if("7".equals(retStr)){
            JSONObject retJo=new JSONObject();
            retJo.put("code",7);
            retJo.put("msg","积分核减失败");
            retJo.put("data","");
            WebsocketUtil.sendMessage(openId,JSONObject.toJSONString(retJo));
            WebsocketUtil.removeSession(openId);
            return ResultData.success(7,"商户角色未设置");
        }else if("8".equals(retStr)){
            JSONObject retJo=new JSONObject();
            retJo.put("code",8);
            retJo.put("msg","用户已被冻结");
            retJo.put("data","");
            WebsocketUtil.sendMessage(openId,JSONObject.toJSONString(retJo));
            WebsocketUtil.removeSession(openId);
            return ResultData.success(8,"用户已被冻结");
        }else if("9".equals(retStr)){
            JSONObject retJo=new JSONObject();
            retJo.put("code",9);
            retJo.put("msg","折扣金额与消费积分不一致");
            retJo.put("data","");
            WebsocketUtil.sendMessage(openId,JSONObject.toJSONString(retJo));
            WebsocketUtil.removeSession(openId);
            return ResultData.success(9,"折扣金额与消费积分不一致");
        }else if("10".equals(retStr)){
            JSONObject retJo=new JSONObject();
            retJo.put("code",10);
            retJo.put("msg","请重新生成二维码");
            retJo.put("data","");
            WebsocketUtil.sendMessage(openId,JSONObject.toJSONString(retJo));
            WebsocketUtil.removeSession(openId);
            return ResultData.success(10,"请重新生成二维码");
        }else if("11".equals(retStr)){
            JSONObject retJo=new JSONObject();
            retJo.put("code",11);
            retJo.put("msg","核销的商户与订单中商户信息不匹配");
            retJo.put("data","");
            WebsocketUtil.sendMessage(openId,JSONObject.toJSONString(retJo));
            WebsocketUtil.removeSession(openId);
            return ResultData.success(11,"核销的商户与订单中商户信息不匹配");
        }else if("12".equals(retStr)){
            JSONObject retJo=new JSONObject();
            retJo.put("code",12);
            retJo.put("msg","数据已经核销");
            retJo.put("data","");
            WebsocketUtil.sendMessage(openId,JSONObject.toJSONString(retJo));
            WebsocketUtil.removeSession(openId);
            return ResultData.success(12,"数据已经核销");
        }else if("-1".equals(retStr)){
           return ResultData.fail("接口出错了");
       }
        return ResultData.success(1,"核销成功");
    }

    @RequestMapping(value = "rollbackMoney",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="商超退款(取消交易)接口",httpMethod = "POST")
    public ResultData<String> rollbackMoney(@RequestParam("content") String content,
                                            @RequestParam("serverOperationId") String serverOperationId,
                                            @RequestParam("orderId") String orderId) throws Exception {
        int retStr = qCodeInfoServiceImpl.rollBackIntegral(content, serverOperationId, orderId);
        LogUtil.WriteLog_Info("积分退款结果======"+retStr,"QcodeInfoController");
        if(retStr==1){
            return ResultData.success(1,"数据处理成功");
        }else if(retStr==2){
            return ResultData.success(2,"订单不存在");
        }else if(retStr==3){
            return ResultData.success(3,"订单已经退回了");
        }else if(retStr==4){
            return ResultData.success(4,"用户不存在");
        }else if(retStr==5){
            return ResultData.success(5,"用户已被冻结");
        }else {
            return ResultData.fail("接口出错了");
        }
    }

    @RequestMapping(value = "failBackMoney",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="商超红冲接口",httpMethod = "POST")
    public ResultData<String> failBackMoney(@RequestParam("content") String content,
                                            @RequestParam("serverOperationId") String serverOperationId,
                                            @RequestParam("orderId") String orderId) throws Exception {
        int retStr = qCodeInfoServiceImpl.failBackMoney(content, serverOperationId, orderId);
        LogUtil.WriteLog_Info("积分红冲结果======"+retStr,"QcodeInfoController");
        if(retStr==1){
            return ResultData.success(1,"数据处理成功");
        }else if(retStr==2){
            return ResultData.success(2,"订单不存在");
        }else if(retStr==3){
            return ResultData.success(3,"订单已经退回了");
        }else if(retStr==4){
            return ResultData.success(4,"用户不存在");
        }else if(retStr==5){
            return ResultData.success(5,"用户已被冻结");
        }else {
            return ResultData.fail("接口出错了");
        }
    }

    @RequestMapping(value = "getStatusByOrderId",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="查询积分核减(退款信息)结果",httpMethod = "POST")
    public ResultData<JSONArray> getStatusByOrderId(@RequestParam("orderId") String orderId,
                                            @RequestParam("checkType") String checkType){
        String retStr = qCodeInfoServiceImpl.getStatusByOrderId(orderId, checkType);
        if(!"-1".equals(retStr)){
            JSONArray ja=new JSONArray();
            JSONObject jo=new JSONObject();
            if("0".equals(retStr)){
                return ResultData.success(0,"未知错误，请联系相关人员进行核验");
            }else  if("2".equals(retStr)){
                return ResultData.success(2,"根据订单id未找到积分消费记录");
            }else{
                jo.put("STATUS","SUCCESS");
                ja.add(jo);
                return ResultData.success("查询成功",ja,ja.size());
            }
        }else {
            return ResultData.fail("接口出错了");
        }
    }

    @RequestMapping(value = "getIntegralListByServiceId",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="根据商户ID查询积分明细",httpMethod = "POST")
    public ResultData<List> getIntegralListByServiceId(@RequestParam("serverOperationId") String serverOperationId,
                                                       @RequestParam(value = "checkType",required = false) String checkType,
                                                       @RequestParam(value = "payBeginTime",required = false) String payBeginTime,
                                                       @RequestParam(value = "payEndTime",required = false) String payEndTime){
        List retStr = qCodeInfoServiceImpl.getIntegralListByServiceId(serverOperationId, checkType,payBeginTime,payEndTime);
        if(retStr !=null){
                return ResultData.success("查询成功",retStr,retStr.size());
        }else {
            return ResultData.fail("接口出错了");
        }
    }
    @RequestMapping(value = "getIntegralTotalByServiceId",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="根据商户ID查询总消费积分",httpMethod = "POST")
    public ResultData<List> getIntegralTotalByServiceId(@RequestParam("serverOperationId") String serverOperationId){
        List retStr = qCodeInfoServiceImpl.getIntegralTotalByServiceId(serverOperationId);
        if(retStr !=null){
            return ResultData.success("查询成功",retStr,retStr.size());
        }else {
            return ResultData.fail("接口出错了");
        }
    }
    @RequestMapping(value = "removeContent",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="用户没有进行核销，关闭二维码",httpMethod = "POST")
    public ResultData<List> removeContent(@RequestParam("content") String content){
        String retStr = qCodeInfoServiceImpl.removeContent(content);
        return ResultData.success(1,"取消成功");
    }
}
