package com.chaozhanggui.admin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chaozhanggui.admin.system.service.AccountService;
import com.chaozhanggui.admin.system.service.MerchantInfoService;
import com.chaozhanggui.admin.system.service.SxfMccInfoService;
import com.chaozhanggui.common.system.config.RespBody;
import com.chaozhanggui.dao.system.dao.TbPlussBankCodeSxfMapper;
import com.chaozhanggui.dao.system.entity.*;
import com.chaozhanggui.dao.system.entity.DTO.AccountDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lyf
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/merchantInfo")
@Slf4j
public class MerchantInfoController {

    @Resource
    private MerchantInfoService merchantInfoService;
    @Resource
    private TbPlussBankCodeSxfMapper bankCodeSxfMapper;
    @Resource
    private SxfMccInfoService sxfMccInfoService;
    @Resource
    private AccountService accountService;


    /**
     * 实名认证信息
     * @return
     */
    @GetMapping("/detail/audit/{userId}")
    public RespBody auditInfo(@PathVariable("userId") Integer userId) {
       return merchantInfoService.merchantReal(userId);
    }

    /**
     * 更改实名认证信息
     * @param userApp
     * @return
     */
    @PostMapping("/updatePromoterInformation")
    public RespBody updatePromoterInformation(@RequestBody TbPlussUserApp userApp){
        if (userApp == null){
            log.error("参数错误");
            return new RespBody("000019");
        }
        return merchantInfoService.updatePromoterInformation(userApp);
    }

    /**
     * 修改商户相关信息
     * @param merchantBaseInfo
     * @return
     */
    @PostMapping("/updateMerchantInformation")
    public RespBody updateMerchantInformation(@RequestBody TbPlussMerchantBaseInfo merchantBaseInfo){
        if (merchantBaseInfo == null ){
            log.error("参数错误");
            return new RespBody("000019");
        }
        return merchantInfoService.updateMerchantInformation(merchantBaseInfo);
    }
    @PostMapping("/updateIdCard")
    public RespBody updateIdCard(@RequestBody TbPlussIdCard idCard){
        if (idCard == null){
            log.error("参数错误");
            return new RespBody("000019");
        }
        return merchantInfoService.updateIdCard(idCard);
    }

    @PostMapping("/updateBankCard")
    public RespBody updateBankCard(@RequestBody TbPlussBankCard bankCard){
        if (bankCard == null){
            log.error("参数错误");
            return new RespBody("000019");
        }
        return merchantInfoService.updateBankCard(bankCard);
    }
    /**
     * 商户基本信息
     * @param userId
     * @return
     */
    @GetMapping("/detail/merchBaseInfo/{userId}")
    public RespBody merchBaseInfo(@PathVariable("userId") Integer userId){
        return merchantInfoService.merchantBaseInfo(userId);
    }
    /**
     * 结算信息
     * @param userId
     * @return
     */
    @GetMapping("/detail/merchBaseAccount/{userId}")
    public RespBody merchBaseAccount(@PathVariable("userId") Integer userId){
        return merchantInfoService.merchantAccount(userId);
    }


    /**
     * 获取对应的银行
     */
    @GetMapping(value = "/getBranchList")
    public Map<String, Object> getBranchList(@RequestParam("bankName")String bankName, @RequestParam("cityName")String cityName) {
        if ("浦发银行".equals(bankName)) {
            bankName = "上海浦东发展银行";
        }
        QueryWrapper<TbPlussBankCodeSxf> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_name", cityName);
        if (!"其他银行".equals(bankName)) {
            queryWrapper.likeLeft("bank_name", bankName);
        }

        List<TbPlussBankCodeSxf> data = bankCodeSxfMapper.selectList(queryWrapper);

        Map<String, Object> result = new HashMap<>(8);
        result.put("branchList", data);
        result.put("code", 200);
        return result;
    }

    /**
     * mcc分页数据
     * @param current
     * @param size
     * @param keyWord
     * @return
     */
    @GetMapping("/mccPageData")
    public RespBody pageData(@RequestParam("current")Integer current, @RequestParam("size")Integer size,@RequestParam("keyWord")String keyWord) {
        return sxfMccInfoService.pageData(current,size,keyWord);
    }



//    @PostMapping("/updateMerchantSettlement2")
//    public RespBody updateMerchantSettlement2(@RequestBody AccountDTO accountDTO) {
//        if (accountDTO != null && accountDTO.getUserid() != null) {
//            accountService.saveV4(accountDTO, false);
//        }

//         System.out.println(JSON.toJSONString(accountDTO));
//        BankCard editBankCard = accountDTO.getBankCard();
//        if (editBankCard != null) {
//            MsgException.checkNull(editBankCard.getId(), "无结算银行卡信息，请先从通过app填写结算信息!");
//
//            LambdaQueryWrapper<BankCodeSxf> qWrapper = new LambdaQueryWrapper<BankCodeSxf>()
//                    .eq(BankCodeSxf::getCnapsCode, editBankCard.getContactLine());
//            BankCodeSxf bankCodeSxf = bankCodeSxfMapper.selectOne(qWrapper);
//            editBankCard.setBranchName(bankCodeSxf.getCnapsName());
//
//            bankCardService.updateById(editBankCard);
//        }
//
//        IdCard editIdCard = accountDTO.getIdcard();
//        if (editIdCard != null) {
//            MsgException.checkNull(editBankCard.getId(), "无结算身份证信息，请先从通过app填写结算信息!");
//
//            idCardService.updateById(editIdCard);
//        }
//
//        if (merchantImage.getMerchantCode() != null) {
//            //修改结算人与收银台合照
//            if (!StringUtils.isEmpty(merchantImage.getPicUrl999())) {
//                merchantImage.setPicUrl(merchantImage.getPicUrl999());
//                merchantImage.setPhotoType(MerchantImage.ImageType.CASH_DESK_WITH_PERSON);
//
//                UpdateWrapper<MerchantImage> merchantImageUpdateWrapper = new UpdateWrapper<MerchantImage>()
//                        .eq("merchantCode", merchantImage.getMerchantCode()).eq("photoType", merchantImage.getPhotoType());
//                boolean update = merchantImageService.update(merchantImage, merchantImageUpdateWrapper);
//                if (!update) {
//                    merchantImage.setMerchantCode(merchantImage.getMerchantCode());
//                    merchantImageService.save(merchantImage);
//                }
//            }
//            //修改结算人手持身份证照
//            if (!StringUtils.isEmpty(merchantImage.getPicUrl11())) {
//                merchantImage.setPicUrl(merchantImage.getPicUrl11());
//                merchantImage.setPhotoType(MerchantImage.ImageType.HAND_IDCARD_PHOTO);
//
//                UpdateWrapper<MerchantImage> merchantImageUpdateWrapper = new UpdateWrapper<MerchantImage>()
//                        .eq("merchantCode", merchantImage.getMerchantCode()).eq("photoType", merchantImage.getPhotoType());
//                boolean update = merchantImageService.update(merchantImage, merchantImageUpdateWrapper);
//                if (!update) {
//                    merchantImage.setMerchantCode(merchantImage.getMerchantCode());
//                    merchantImageService.save(merchantImage);
//                }
//            }
//        }
//
//        // throw new MsgException("终止操作");
//        return ResultGenerator.genSuccessResult("保存成功！", null);
//    }
//    /**
//     * 进件相关信息
//     * @param userId
//     * @return
//     */
//    @GetMapping("/detail/channelAuditInfo/{userId}")
//    public RespBody channelAuditInfo(@PathVariable("userId") Long userId) {
//
//    }

    /**
     * 获取所有省
     */
//    private void getProvince() {
//        QueryWrapper<TbPlussAreaCity> queryWrapper = new QueryWrapper<TbPlussAreaCity>().eq("type", "1");
//        List<TbPlussAreaCity> areaCitiesList = TbPlussAreaCityMapper.selectList(queryWrapper);
//        for (AreaCity areaCity : areaCitiesList) {
//            switch (areaCity.getAreaName()) {
//                case "北京":
//                    areaCity.setAreaName("北京市");
//                    break;
//                case "天津":
//                    areaCity.setAreaName("天津市");
//                    break;
//                case "重庆":
//                    areaCity.setAreaName("重庆市");
//                    break;
//                case "上海":
//                    areaCity.setAreaName("上海市");
//                    break;
//                default:
//                    break;
//            }
//        }
//
//        request.setAttribute("areaCitiesList", areaCitiesList);
//    }
}
