package org.xxpay.mgr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.xxpay.common.util.DateUtil;
import org.xxpay.common.util.MyLog;
import org.xxpay.dal.dao.model.PayChannel;
import org.xxpay.dal.dao.plugin.PageModel;
import org.xxpay.mgr.service.PayChannelService;

import java.util.List;

@Controller
@RequestMapping("/pay_channel")
public class PayChannelController {

    private final static MyLog _log = MyLog.getLog(PayChannelController.class);

    @Autowired
    private PayChannelService payChannelService;

    @RequestMapping("/list.html")
    public String listInput(ModelMap model) {
        return "pay_channel/list";
    }

    @RequestMapping("/edit.html")
    public String editInput(String id, ModelMap model) {
        PayChannel item = null;
        if(StringUtils.isNotBlank(id) && NumberUtils.isNumber(id)) {
            item = payChannelService.selectPayChannel(Integer.parseInt(id));
        }
        if(item == null) item = new PayChannel();
        model.put("item", item);
        return "pay_channel/edit";
    }

    @RequestMapping("/list")
    @ResponseBody
    public String list(@ModelAttribute PayChannel payChannel, Integer pageIndex, Integer pageSize) {
        PageModel pageModel = new PageModel();
        int count = payChannelService.count(payChannel);
        if(count <= 0) return JSON.toJSONString(pageModel);
        List<PayChannel> payChannelList = payChannelService.getPayChannelList((pageIndex-1)*pageSize, pageSize, payChannel);
        if(!CollectionUtils.isEmpty(payChannelList)) {
            JSONArray array = new JSONArray();
            for(PayChannel pc : payChannelList) {
                JSONObject object = (JSONObject) JSONObject.toJSON(pc);
                object.put("createTime", DateUtil.date2Str(pc.getCreateTime()));
                array.add(object);
            }
            pageModel.setList(array);
        }
        pageModel.setCount(count);
        pageModel.setMsg("ok");
        pageModel.setRel(true);
        return JSON.toJSONString(pageModel);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(@RequestParam String params) {
        JSONObject po = JSONObject.parseObject(params);
        PayChannel payChannel = new PayChannel();
        Integer id = po.getInteger("id");
        payChannel.setChannelId(po.getString("channelId"));
        payChannel.setMchId(po.getString("mchId"));
        payChannel.setChannelName(po.getString("channelName"));
        payChannel.setChannelMchId(po.getString("channelMchId"));
        payChannel.setState((byte) ("on".equalsIgnoreCase(po.getString("state")) ? 1 : 0));
        payChannel.setParam(po.getString("param"));
        payChannel.setRemark(po.getString("remark"));
        int result;
        if(id == null) {
            // 添加
            result = payChannelService.addPayChannel(payChannel);
        }else {
            // 修改
            payChannel.setId(id);
            result = payChannelService.updatePayChannel(payChannel);
        }
        _log.info("保存渠道记录,返回:{}", result);
        return result+"";
    }

    @RequestMapping("/view.html")
    public String viewInput(String id, ModelMap model) {
        PayChannel item = null;
        if(StringUtils.isNotBlank(id) && NumberUtils.isNumber(id)) {
            item = payChannelService.selectPayChannel(Integer.parseInt(id));
        }
        if(item == null) item = new PayChannel();
        model.put("item", item);
        return "pay_channel/view";
    }

}
@@ -488,16 +488,62 @@
 tion.*;%0A
+import org.xxpay.common.constant.PayConstant;%0A
 import o
@@ -2705,16 +2705,675 @@
 arams);%0A
+        String channelId = po.getString(%22channelId%22);%0A        String param = po.getString(%22param%22);%0A        // %E5%AF%B9%E4%BA%8E%E9%85%8D%E7%BD%AE%E6%94%AF%E4%BB%98%E5%AE%9D%E5%8F%82%E6%95%B0%E6%97%B6,%E5%89%8D%E7%AB%AF%E5%B0%86+%E5%8F%B7%E8%BD%AC%E4%B8%BA%E7%A9%BA%E6%A0%BCbug%E5%A4%84%E7%90%86%0A        if(PayConstant.PAY_CHANNEL_ALIPAY_MOBILE.equals(channelId) %7C%7C%0A                PayConstant.PAY_CHANNEL_ALIPAY_PC.equals(channelId) %7C%7C%0A                PayConstant.PAY_CHANNEL_ALIPAY_WAP.equals(channelId)) %7B%0A            JSONObject paramObj = po.getJSONObject(%22param%22);%0A            paramObj.put(%22private_key%22, paramObj.getString(%22private_key%22).replaceAll(%22 %22, %22+%22));%0A            paramObj.put(%22public_key%22, paramObj.getString(%22public_key%22).replaceAll(%22 %22, %22+%22));%0A            param = paramObj.toJSONString();%0A        %7D%0A%0A
         
@@ -3488,30 +3488,16 @@
 annelId(
-po.getString(%22
 channelI
@@ -3497,18 +3497,16 @@
 hannelId
-%22)
 );%0A     
@@ -3802,37 +3802,21 @@
 etParam(
-po.getString(%22
 param
-%22)
 );%0A     

修复保存支付宝渠道私钥+号转空格的bug