package cn.pluss.platform.controller;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.bankUnionpayCode.BankUnionpayCodeService;
import cn.pluss.platform.entity.WxCityInfo;
import cn.pluss.platform.leshua.BankNameVO;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.wxCityInfo.WxCityInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/wxCityInfo")
@Transactional
@Api(value = "WxCityInfo控制类", tags = {"WxCityInfo接口"})
public class WxCityInfoController {

    private final WxCityInfoService wxCityInfoService;
    private final BankUnionpayCodeService bankUnionpayCodeService;

    @PostMapping("/queryCodeByCityName")
    @ApiOperation(value = "根据名称查询城市编码", notes = "查询城市编码", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "WxCityInfo", value = "待添加的WxCityInfo实例", paramType = "body", dataType = "WxCityInfo", required = true)})
    public Result<Object> queryCodeByCityName(@RequestBody WxCityInfo wxCityInfo) {
        if (StringUtil.isEmpty(wxCityInfo.getCityName())) {
            return ResultGenerator.genFailResult("无城市名称");
        }

        wxCityInfo = wxCityInfoService.queryWxCityInfo(wxCityInfo);
        return ResultGenerator.genSuccessResult(wxCityInfo);
    }


    @RequestMapping(value = "/queryBranchName", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "根据省市code银行名称 查询支行 ", notes = "根据省市code 银行名称 查询支行", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cityName", value = "中国+省市区", paramType = "cityName", dataType = "cityName", required = true),
            @ApiImplicitParam(name = "bankName", value = "银行名称", paramType = "bankName", dataType = "bankName", required = true),
            @ApiImplicitParam(name = "brankName", value = "支行名称", paramType = "brankName", dataType = "brankName", required = false),
    })
    public Result queryBranchName(@RequestParam String cityName, @RequestParam String bankName, @RequestParam(required = false) String brankName) {
        //  对部分银行信息进行特殊处理
        if (bankName != null) {
            switch (bankName) {
                case "工商银行":
                    bankName = "工行";
                    break;
                case "交通银行":
                    bankName = "交行";
                    break;
                case "招商银行":
                    bankName = "招行";
                    break;
                case "农业银行":
                    bankName = "农行";
                    break;
            }
        }

        if (StringUtil.isEmpty(cityName)) {
            return ResultGenerator.genFailResult("省市区信息");
        }
        String[] cityStrings = cityName.split(",");//格式 ：xx, xx,xx,xx
        String province = cityStrings[1];//省
        String city = cityStrings[2];//市
        city = city.substring(0, 2);
        if (StringUtil.isEmpty(province)) {
            province = city;
        } else {
            province = province.replace("省", "");
        }

        String concatBankName = "%";
        if ("其他银行".equals(bankName)) {
            concatBankName = null;
        } else if (!"中国银行".equals(bankName)) {
            for (int i = 0; i < bankName.length(); i++) {
                concatBankName = concatBankName + bankName.charAt(i) + "%";
            }
        } else {
            concatBankName = bankName;
        }


        // 特殊的城市查询银行需要特殊对待
        if ("潜江".equals(city) || "天门".equals(city) || "仙桃".equals(city)) {
            city = "荆州";
        }

        if ("儋州".equals(city)) {
            city = "海口";
        }

        if (city.startsWith("省直")) {
            city = null;
        }

        if ("海南".equals(province)) {
            city = null;
        }

        List<BankNameVO> branchNameList = bankUnionpayCodeService.getBranchName(city, province, concatBankName, brankName);
        return ResultGenerator.genSuccessResult(branchNameList);
    }


}
