package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.Base64Util;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsUserSpreadAccountRecord;
import com.hfkj.model.ResponseData;
import com.hfkj.service.spread.BsUserSpreadAccountRecordService;
import com.hfkj.service.spread.BsUserSpreadAccountService;
import com.hfkj.sysenum.spread.UserSpreadAccountRecordSourceTypeEnum;
import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: WeiXinNotifyController
 * @author: HuRui
 * @date: 2025/4/10
 **/
@Controller
@RequestMapping(value = "/wexinNotify")
@Api(value = "微信通知")
public class WeiXinNotifyController {
    @Resource
    private BsUserSpreadAccountRecordService userSpreadAccountRecordService;
    @Resource
    private BsUserSpreadAccountService userSpreadAccountService;

    @RequestMapping(value = "/withdraw", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "提现通知")
    public void withdraw(@RequestBody JSONObject body, HttpServletResponse response) {
        try {
            System.out.println("提现通知:"+body.toJSONString());

            JSONObject resource = body.getJSONObject("resource");
            // 解析后的数据
            String dataStr = new AesUtil("UXt3EGM5X6P71EXcNmszV1ONk7LX9IEj".getBytes())
                    .decryptToString(
                            resource.getString("associated_data").getBytes(),
                            resource.getString("nonce").getBytes(),
                            resource.getString("ciphertext"));

            JSONObject data = JSONObject.parseObject(dataStr);
            String outBillNo = data.getString("out_bill_no");

            // 记录
            BsUserSpreadAccountRecord record = userSpreadAccountRecordService.getRecordByOrderNo(outBillNo);
            if (record != null) {
                String state = data.getString("state");
                if (state.equals("CANCELLED") || state.equals("FAIL")) {
                    record.setStatus(0);
                    userSpreadAccountRecordService.update(record);

                    // 充值余账户余额
                    Map<String,Object> otherParam = new HashMap<>();
                    otherParam.put("sourceContent", state.equals("CANCELLED")?"撤销提现":"转账失败");
                    otherParam.put("sourceOrderNo", DateUtil.date2String(new Date(), DateUtil.YMDHMS));
                    userSpreadAccountService.increase(record.getUserId(), record.getTransactionAmount(), UserSpreadAccountRecordSourceTypeEnum.type4, otherParam);
                }
            }

            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
            JSONObject result = new JSONObject();
            result.put("code", "SUCCESS");
            result.put("message", "");
            out.write(result.toJSONString().getBytes());
            out.flush();
            out.close();

        } catch (Exception e) {
            System.out.println("提现通知出现异常:"+e.getMessage());
        }
    }

}
