package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.*;
import com.hfkj.model.ResponseData;
import com.hfkj.service.merchant.*;
import com.hfkj.sysenum.merchant.MerchantChainBrandSettleTypeEnum;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @className: BsMerchantAccountController
 * @author: HuRui
 * @date: 2024/7/2
 **/
@Controller
@RequestMapping(value = "/merchantAccount")
@Api(value = "商户管理")
public class BsMerchantAccountController {
    private static Logger log = LoggerFactory.getLogger(BsMerchantAccountController.class);
    @Resource
    private BsMerchantAccountService merchantAccountService;
    @Resource
    private BsMerchantChainBrandAccountService merchantChainBrandAccountService;
    @Resource
    private BsMerchantService merchantService;
    @Resource
    private BsMerchantAccountRecordService merchantAccountRecordService;
    @Resource
    private BsMerchantChainBrandAccountRecordService merchantChainBrandAccountRecord;

    @RequestMapping(value = "/getAccount", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询账户")
    public ResponseData getAccount(@RequestParam(value = "merNo", required = true) String merNo) {
        try {
            // 查询商户详情
            BsMerchant merchant = merchantService.getMerchant(merNo);
            if (merchant == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商户账户");
            }
            // 查询账户
            BsMerchantAccount account = merchantAccountService.getAccount(merNo);

            if (merchant.getChainBrandId() != null && merchant.getChainBrandSettleType().equals(MerchantChainBrandSettleTypeEnum.type1.getNumber())) {
                // 连锁额度
                BsMerchantChainBrandAccount brandAccount = merchantChainBrandAccountService.getAccount(merchant.getChainBrandId());
                account.setAmount(brandAccount.getAmount());
            }

            return ResponseMsgUtil.success(account);

        } catch (Exception e) {
            log.error("BsMerchantAccountController --> getAccount() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/recharge", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "充值")
    public ResponseData recharge(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("merNo"))
                    || body.getBigDecimal("amount") == null
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 查询商户
            BsMerchant merchant = merchantService.getMerchant(body.getString("merNo"));
            if (merchant == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商户");
            }
            if (merchant.getChainBrandId() != null
                    && merchant.getChainBrandSettleType().equals(MerchantChainBrandSettleTypeEnum.type1.getNumber())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "结算方式为连锁额度，不可以进行充值。");
            }
            // 账户充值
            merchantAccountService.recharge(body.getString("merNo"), body.getBigDecimal("amount"));

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

        } catch (Exception e) {
            log.error("BsMerchantAccountController --> recharge() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getMerList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户列表")
    public ResponseData getMerList(@RequestParam(value = "merNo", required = false) String merNo,
                                   @RequestParam(value = "merName", required = false) String merName,
                                   @RequestParam(value = "provinceCode", required = false) Long provinceCode,
                                   @RequestParam(value = "cityCode", required = false) Long cityCode,
                                   @RequestParam(value = "areaCode", required = false) Long areaCode,
                                   @RequestParam(value = "pageNum", required = true) Integer pageNum,
                                   @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        try {

            Map<String,Object> param = new HashMap<>();
            param.put("merNo", merNo);
            param.put("merName", merName);
            param.put("provinceCode", provinceCode);
            param.put("cityCode", cityCode);
            param.put("areaCode", areaCode);
            param.put("chainBrandIdIsNull", true);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(merchantAccountService.getMerList(param)));

        } catch (Exception e) {
            log.error("BsMerchantAccountController --> getRecordList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getRecordList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "记录列表")
    public ResponseData getRecordList(@RequestParam(value = "merNo", required = true) String merNo,
                                      @RequestParam(value = "type", required = false) Integer type,
                                      @RequestParam(value = "sourceType", required = false) Integer sourceType,
                                      @RequestParam(value = "sourceOrderNo", required = false) String sourceOrderNo,
                                      @RequestParam(value = "pageNum", required = true) Integer pageNum,
                                      @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        try {
            // 查询商户详情
            BsMerchant merchant = merchantService.getMerchant(merNo);
            if (merchant == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商户");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("merNo", merNo);
            param.put("type", type);
            param.put("sourceType", sourceType);
            param.put("sourceOrderNo", sourceOrderNo);
            PageHelper.startPage(pageNum,pageSize);
          /*  if (merchant.getChainBrandId() != null && merchant.getChainBrandSettleType().equals(MerchantChainBrandSettleTypeEnum.type1.getNumber())) {
                // 连锁额度账户
                BsMerchantChainBrandAccount brandAccount = merchantChainBrandAccountService.getAccount(merchant.getChainBrandId());
                param.put("merChainBrandAccountId", brandAccount.getId());
                return ResponseMsgUtil.success(new PageInfo<>(merchantChainBrandAccountRecord.getList(param)));

            } else {
                param.put("merNo", merNo);
                return ResponseMsgUtil.success(new PageInfo<>(merchantAccountRecordService.getList(param)));
            }*/
            return ResponseMsgUtil.success(new PageInfo<>(merchantAccountRecordService.getList(param)));

        } catch (Exception e) {
            log.error("BsMerchantAccountController --> getRecordList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
