package org.dromara.mp.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.service.BaseService;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.satoken.utils.MpLoginHelper;
import org.dromara.mp.domain.UserWorkerAccount;
import org.dromara.mp.domain.bo.UserWokerOperBo;
import org.dromara.mp.domain.vo.SettlementChannelVo;
import org.dromara.mp.domain.vo.UserWorkerAccountVo;
import org.dromara.mp.domain.vo.UserWorkerVo;
import org.dromara.mp.exception.MpException;
import org.dromara.mp.mapper.UserWorkerAccountMapper;
import org.dromara.mp.mapper.UserWorkerMapper;
import org.dromara.mp.service.ISettlementAccountService;
import org.dromara.settlement.api.RemoteAccountService;
import org.dromara.settlement.api.domain.Account;
import org.dromara.settlement.api.domain.Result;
import org.dromara.system.api.RemoteDictService;
import org.dromara.system.api.domain.vo.RemoteDictDataVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author sunzexing
 * @version 1.0
 * @title SettlementAccountServiceImpl
 * @description
 * @create 2024-05-08 14:11
 */

@Service
public class SettlementAccountServiceImpl extends BaseService implements ISettlementAccountService {

    @DubboReference
    private RemoteDictService remoteDictService;

    @Resource
    private UserWorkerAccountMapper userWorkerAccountMapper;


    @Resource
    private UserWorkerMapper userWorkerMapper;


    @DubboReference
    private RemoteAccountService accountService;




    @Override
    public List<SettlementChannelVo> getSettleChannel() {
        List<RemoteDictDataVo>  list = remoteDictService.selectDictDataByType("pay_channel");
        QueryWrapper wapper = new QueryWrapper();
        wapper.eq("work_id",MpLoginHelper.getUserId());
        wapper.eq("is_default",1);
        wapper.eq("status",1);
        List<UserWorkerAccountVo> uwaList =  this.userWorkerAccountMapper.selectVoList(wapper);

        List<SettlementChannelVo>  settlementChannelVoList = new ArrayList<>();
        for(RemoteDictDataVo vo : list){
            SettlementChannelVo stcv = new SettlementChannelVo();
            stcv.setChannelName(vo.getDictLabel());
            uwaList.stream().filter(item->item.getPassage().equals(vo.getDictLabel())).forEach(item->{
                stcv.setBankCard(StringUtils.bankHide(item.getBankCard()));
                stcv.setId(item.getId());
                stcv.setBankName(item.getBankType());
                stcv.setCardType(item.getCardType());
            });
            settlementChannelVoList.add(stcv);
        }

        return settlementChannelVoList;
    }

    @Override
    public List getBankList(String bankName) {
        List<RemoteDictDataVo>  list = remoteDictService.selectDictDataByType("bank_list");

        if(StringUtils.isNotEmpty(bankName)){
            List<RemoteDictDataVo>  list1 = new ArrayList<>();
            list.stream().filter(item -> item.getDictLabel().contains(bankName)).forEach(item -> {
                list1.add(item);
            });
            return list1;
        }
        return list;
    }


    /***
     * 添加结算账户
     * @param bankType 银行类型
     * @param name  姓名
     * @param bankCard 银行卡号
     * @param channelName 通道名称
     * @param phone 手机号
     * @param bankPic 银行卡图片
     * @return
     */
    @Transactional
    public int addAccount(String bankType, String name, String bankCard,String channelName,String phone,
                          String bankPic,String cardType){

        if(StringUtils.isEmpty(channelName)){
            List<RemoteDictDataVo> channelList = remoteDictService.selectDictDataByType("pay_channel");
            if(channelList != null && !channelList.isEmpty()){
                channelName = channelList.get(0).getDictLabel();
            }else{
                throw new MpException("系统无可用结算通道");
            }
        }

        UserWokerOperBo  bo = new UserWokerOperBo();
        bo.setChannelName(channelName);
        bo.setWorkId(MpLoginHelper.getUserId());

        List<UserWorkerAccountVo> accountList= this.userWorkerAccountMapper.selctBankInfoByUidChannelBankNo(bo);
        long  isDefault = 1;
        UserWorkerAccountVo  hav = null;
        if(accountList != null && !accountList.isEmpty()){
            for(UserWorkerAccountVo vo :accountList){
                if(bankCard.equals(vo.getBankCard())){
                    hav = vo;
                }
                if(vo.getIsDefault().intValue() == 1 ){
                    isDefault = 0;
                }
            }
        }

        UserWorkerAccount uwa1  = new UserWorkerAccount();
        uwa1.setWorkId(MpLoginHelper.getUserId());
        uwa1.setIsDefault(0L);
        uwa1.setUpdateTime(new Date());
        this.userWorkerAccountMapper.updateById(uwa1);

        if(hav != null){
            UserWorkerAccount userwo = new UserWorkerAccount();
            userwo.setStatus(1);
            userwo.setId(hav.getId());
            userwo.setIsDefault(1L);
            userwo.setPassage(channelName);
            userwo.setBankCardPic(bankPic);
            userwo.setBankType(bankType);
            userwo.setCardType(cardType);
            this.userWorkerAccountMapper.updateById(userwo);
            return 1;
        }

        Account  bankAcc = new Account();
        bankAcc.setType((short)2);//2:真实个人账户
        bankAcc.setBankCard(bankCard);
        bankAcc.setBankType(bankType);
        bankAcc.setUnitId(0L);
        bankAcc.setName(name);
        Result<Account> result = this.accountService.addAccount(bankAcc);
        Integer accountId = result.getRtnData().getId();
        if(accountId != null){
            UserWorkerAccount userWorkerAccount = new UserWorkerAccount();
            userWorkerAccount.setCreateBy(MpLoginHelper.getUserId());
            userWorkerAccount.setWorkId(MpLoginHelper.getUserId());
            userWorkerAccount.setCreateTime(new Date());
            userWorkerAccount.setPassage(channelName);
            userWorkerAccount.setAccId(Long.valueOf(accountId));
            userWorkerAccount.setBankCardPic(bankPic);
            userWorkerAccount.setBankCard(bankCard);
            userWorkerAccount.setBankType(bankType);
            userWorkerAccount.setCardType(cardType);
            userWorkerAccount.setIsDefault(1L);
            userWorkerAccount.setPhone(phone);
           return this.userWorkerAccountMapper.insert(userWorkerAccount);
        }else{
            return -1;
        }
    }

    @Override
    public UserWorkerAccountVo getBankDetail(Long id) {
        if(id == null){
            this.invalidationParamsException("ID不能为空");
        }
        UserWorkerAccountVo  vo =  this.userWorkerAccountMapper.selectVoById(id);


        if(vo != null){
            vo.setAccId(null);
            UserWorkerVo user = this.userWorkerMapper.selectVoById(vo.getWorkId());
            vo.setName(user.getName());
        }
        return vo;
    }
}
