package com.stylefeng.guns.modular.system.controller.general; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.stylefeng.guns.core.base.controller.BaseController; import com.stylefeng.guns.core.base.tips.ErrorTip; import com.stylefeng.guns.core.shiro.ShiroKit; import com.stylefeng.guns.core.util.RequestSeqUtil; import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.modular.cloudPayment.example.WithdrawalExample; import com.stylefeng.guns.modular.cloudPayment.req.WithdrawalReq; import com.stylefeng.guns.modular.huifu.biz.DouGongBasicDataBiz; import com.stylefeng.guns.modular.huifu.biz.DouGongWithdrawBiz; import com.stylefeng.guns.modular.system.controller.util.Base64Util; import com.stylefeng.guns.modular.system.model.TEnterpriseWithdrawal; import com.stylefeng.guns.modular.system.model.User; import com.stylefeng.guns.modular.system.service.ITEnterpriseWithdrawalService; import com.stylefeng.guns.modular.system.service.IUserService; import com.unionpay.upyzt.resp.WithdrawalResp; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.beans.factory.annotation.Autowired; import com.stylefeng.guns.core.log.LogObjectHolder; import org.springframework.web.bind.annotation.RequestParam; import com.stylefeng.guns.modular.system.model.TCompanyWithdraw; import com.stylefeng.guns.modular.system.service.ITCompanyWithdrawService; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.Date; import java.util.List; /** * 控制器 * * @author fengshuonan * @Date 2023-03-17 14:18:09 */ @Slf4j @Controller @RequestMapping("/tCompanyWithdraw") public class TCompanyWithdrawController extends BaseController { private String PREFIX = "/system/tCompanyWithdraw/"; @Autowired private ITCompanyWithdrawService tCompanyWithdrawService; @Resource private ITEnterpriseWithdrawalService enterpriseWithdrawalService; @Autowired private DouGongBasicDataBiz douGongBasicDataBiz; @Autowired private DouGongWithdrawBiz douGongWithdrawBiz; /** * 跳转到首页 */ @RequestMapping("") public String index(Model model) { Integer id = ShiroKit.getUser().getObjectId(); List companyId = enterpriseWithdrawalService.selectList(new EntityWrapper().eq("companyId", id)); if(companyId.size()>0){ try { //BalanceAcctReq balanceAcctReq = new BalanceAcctReq(); //balanceAcctReq.setMchId(companyId.get(0).getMchId()); //BalanceAcctListResp balanceAcctListResp = BalanceAcctExample.retrieveByUserId(balanceAcctReq); //Long settledAmount = balanceAcctListResp.getBalanceAccts().get(0).getSettledAmount(); //String money = settledAmount.toString(); BigDecimal money = douGongBasicDataBiz.balanceQuery(companyId.get(0).getHuifuId()); model.addAttribute("money",money.divide(new BigDecimal(100)).toString()); } catch (Exception e) { log.error("查询企业用户汇付余额异常", e); model.addAttribute("money",0); } }else { model.addAttribute("money",0); } return PREFIX + "tCompanyWithdraw.html"; } /** * 跳转到添加 */ @RequestMapping("/tCompanyWithdraw_add") public String tCompanyWithdrawAdd() { return PREFIX + "tCompanyWithdraw_add.html"; } /** * 跳转到修改 */ @RequestMapping("/tCompanyWithdraw_update/{tCompanyWithdrawId}") public String tCompanyWithdrawUpdate(@PathVariable Integer tCompanyWithdrawId, Model model) { TCompanyWithdraw tCompanyWithdraw = tCompanyWithdrawService.selectById(tCompanyWithdrawId); model.addAttribute("item",tCompanyWithdraw); LogObjectHolder.me().set(tCompanyWithdraw); return PREFIX + "tCompanyWithdraw_edit.html"; } @Autowired private IUserService userService; /** * 获取列表 */ @RequestMapping(value = "/list") @ResponseBody public Object list(String createTime,Integer state) { Integer id = ShiroKit.getUser().getObjectId(); EntityWrapper withdrawEntityWrapper = new EntityWrapper<>(); String sTime=null; String eTime=null; if(ToolUtil.isNotEmpty(state)){ withdrawEntityWrapper.eq("status",state); } withdrawEntityWrapper.eq("companyId",id); if(ToolUtil.isNotEmpty(createTime)){ sTime=createTime.split(" - ")[0]+" 00:00:01"; eTime=createTime.split(" - ")[1]+" 23:59:59"; withdrawEntityWrapper.between("createTime",sTime,eTime); } List tCompanyWithdraws = tCompanyWithdrawService.selectList(withdrawEntityWrapper); for (TCompanyWithdraw tCompanyWithdraw : tCompanyWithdraws) { Integer createId = tCompanyWithdraw.getCreateId(); User user = userService.selectById(createId); tCompanyWithdraw.setCreateUserName(user.getName()); } return tCompanyWithdraws; } /** * 新增 */ @RequestMapping(value = "/add") @ResponseBody @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW) public Object add(Double money,String type,String remark) { List companyId = enterpriseWithdrawalService.selectList(new EntityWrapper().eq("companyId", ShiroKit.getUser().getObjectId())); if(companyId.size()>0){ try { //BalanceAcctReq balanceAcctReq = new BalanceAcctReq(); //balanceAcctReq.setMchId(companyId.get(0).getMchId()); //BalanceAcctListResp balanceAcctListResp = BalanceAcctExample.retrieveByUserId(balanceAcctReq); //Long settledAmount = balanceAcctListResp.getBalanceAccts().get(0).getSettledAmount(); BigDecimal settledAmount = douGongBasicDataBiz.balanceQuery(companyId.get(0).getHuifuId()); if(settledAmount.compareTo(BigDecimal.valueOf(money))<0){ return "5001"; } } catch (Exception e) { e.printStackTrace(); } } TCompanyWithdraw tCompanyWithdraw = new TCompanyWithdraw(); tCompanyWithdraw.setWithdrawMoney(new BigDecimal(money)); tCompanyWithdraw.setCompanyId(ShiroKit.getUser().getObjectId()); tCompanyWithdraw.setCreateId(ShiroKit.getUser().getId()); tCompanyWithdraw.setCreateTime(new Date()); if("提现".equals(type)){ tCompanyWithdraw.setType(1); } tCompanyWithdraw.setRemark(remark); tCompanyWithdraw.setStatus(0); // TODO 创建提现 需要回调进行提现状态查询 try { WithdrawalReq withdrawalReq = new WithdrawalReq(); if(companyId.size()>0){ TEnterpriseWithdrawal enterpriseWithdrawal = companyId.get(0); withdrawalReq.setOutOrderNo(ToolUtil.getRandomString(32)); int total = new BigDecimal(money).multiply(new BigDecimal(100)).intValue(); withdrawalReq.setAmount(total); withdrawalReq.setBalanceAcctId(enterpriseWithdrawal.getBalanceAcctId()); withdrawalReq.setBankAcctNo(enterpriseWithdrawal.getBankAccount()); withdrawalReq.setPassword(Base64Util.decode(enterpriseWithdrawal.getTransactionAuthorizationCode())); } String reqSeqId = RequestSeqUtil.extractPrefix("TX"); JSONObject result = douGongWithdrawBiz.encashment(reqSeqId, BigDecimal.valueOf(withdrawalReq.getAmount()), companyId.get(0).getHuifuId(), companyId.get(0).getTokenNo(), 1); if (!result.get("resp_code").equals("00000000") || !result.getBoolean("success")) { return new ErrorTip(500, "操作失败,异常原因:"+result.get("resp_desc")); } tCompanyWithdraw.setReqSeqId(reqSeqId); String hfSeqId = (String) result.get("hf_seq_id"); tCompanyWithdraw.setHfSeqId(hfSeqId); //WithdrawalResp withdrawalResp = WithdrawalExample.create(withdrawalReq); //System.out.println("公司添加提现:"+withdrawalResp); //tCompanyWithdraw.setWithdrawId(withdrawalResp.getWithdrawalId()); } catch (Exception e) { log.error("公司添加提现异常", e); return new ErrorTip(500, "公司添加提现异常"); } tCompanyWithdrawService.insert(tCompanyWithdraw); return SUCCESS_TIP; } /** * 删除 */ @RequestMapping(value = "/delete") @ResponseBody public Object delete(@RequestParam Integer tCompanyWithdrawId) { tCompanyWithdrawService.deleteById(tCompanyWithdrawId); return SUCCESS_TIP; } /** * 修改 */ @RequestMapping(value = "/update") @ResponseBody public Object update(TCompanyWithdraw tCompanyWithdraw) { tCompanyWithdrawService.updateById(tCompanyWithdraw); return SUCCESS_TIP; } /** * 详情 */ @RequestMapping(value = "/detail/{tCompanyWithdrawId}") @ResponseBody public Object detail(@PathVariable("tCompanyWithdrawId") Integer tCompanyWithdrawId) { return tCompanyWithdrawService.selectById(tCompanyWithdrawId); } }