package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsRebateActivity;
import com.hfkj.entity.BsRebateActivityAccount;
import com.hfkj.model.ResponseData;
import com.hfkj.service.rebate.BsRebateActivityAccountService;
import com.hfkj.service.rebate.BsRebateActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @className: BsRebateActivityController
 * @author: HuRui
 * @date: 2023/10/18
 **/
@Controller
@Api(value = "活动金 - 活动账户")
@RequestMapping(value = "/rebateActivityAccount")
public class BsRebateActivityAccountController {

    private static Logger log = LoggerFactory.getLogger(BsRebateActivityAccountController.class);

    @Resource
    private BsRebateActivityService rebateActivityService;
    @Resource
    private BsRebateActivityAccountService rebateActivityAccountService;

    @RequestMapping(value="/recharge",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "充值金额")
    public ResponseData recharge(@RequestBody JSONObject body) {
        try {

            if (body == null
                    || StringUtils.isBlank(body.getString("activityNo"))
                    || body.getBigDecimal("amount") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            rebateActivityAccountService.recharge(body.getString("activityNo"), body.getBigDecimal("amount"));

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/recover",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "回收余额")
    public ResponseData recover(@RequestBody JSONObject body) {
        try {

            if (body == null || StringUtils.isBlank(body.getString("activityNo"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询活动
            BsRebateActivity activity = rebateActivityService.getActivityByNo(body.getString("activityNo"));
            if (activity == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到活动");
            }
            // 查询活动账户
            BsRebateActivityAccount activityAccount = rebateActivityAccountService.getAccountByActivityNo(body.getString("activityNo"));
            if (activityAccount == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到活动账户");
            }
            if (activityAccount.getAmount().compareTo(new BigDecimal("0")) == 0) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "活动账户余额不足");
            }
            Boolean result = rebateActivityAccountService.recover(body.getString("activityNo"));
            if (!result) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "回收余额失败");
            }

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getActivityAccount",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询活动账户")
    public ResponseData getActivityAccount(@RequestParam(value = "activityNo" , required = true) String activityNo) {
        try {

            return ResponseMsgUtil.success(rebateActivityAccountService.getAccountByActivityNo(activityNo));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
