package com.ruoyi.invest.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.IdWorker;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.invest.domain.Accounts;
import com.ruoyi.invest.domain.BankCardLend;
import com.ruoyi.invest.domain.dto.BankCardLendDto;
import com.ruoyi.invest.domain.vo.BankCardLendVo;
import com.ruoyi.invest.mapper.AccountsMapper;
import com.ruoyi.invest.mapper.BankCardLendMapper;
import com.ruoyi.invest.service.IBankCardLendService;
import org.springframework.stereotype.Service;

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

/**
 * 银行卡信息Service业务层处理
 *
 * @author tianyongbao
 * @date 2024-03-18
 */
@Service
public class BankCardLendServiceImpl implements IBankCardLendService
{
    @Resource
    private BankCardLendMapper bankCardLendMapper;
    @Resource
    private AccountsMapper accountsMapper;

    /**
     * 查询银行卡信息
     *
     * @param id 银行卡信息主键
     * @return 银行卡信息
     */
    @Override
    public BankCardLendVo selectBankCardLendById(Long id)
    {
        return bankCardLendMapper.selectBankCardLendById(id);
    }

    /**
     * 查询银行卡信息列表
     *
     * @param bankCardLendDto 银行卡信息
     * @return 银行卡信息
     */
    @Override
    public List<BankCardLendVo> selectBankCardLendList(BankCardLendDto bankCardLendDto)
    {
        List<BankCardLendVo> bankCardVoList=bankCardLendMapper.selectBankCardLendList(bankCardLendDto);
        //修改名称加卡号
        for (BankCardLendVo bankCard : bankCardVoList) {
            if(bankCard.getCode()!=null){
              bankCard.setNameCode(bankCard.getName()+"（"+ StringUtils.getLastNumberChars(4,bankCard.getCode()+"）"));
            }else {
                bankCard.setNameCode(bankCard.getName());
            }
            if(!"null".equals(String.valueOf(bankCard.getBillDate()))) {
                bankCard.setBillDateName(bankCard.getBillDate()+"号");
            }
            if(!"null".equals(String.valueOf(bankCard.getPayDate()))) {
                bankCard.setPayDateName(bankCard.getPayDate()+"号");
            }

        }
        return bankCardVoList;
    }

    /**
     * 新增银行卡信息
     *
     * @param bankCard 银行卡信息
     * @return 结果
     */
    @Override
    public int insertBankCardLend(BankCardLend bankCard)
    {
        bankCard.setCreateBy(SecurityUtils.getUsername());
        bankCard.setCreateTime(DateUtils.getNowDate());
        bankCard.setId(IdWorker.getId());
        //记账账户信息插入
        Accounts accounts=new Accounts();
        accounts.setCreateBy(SecurityUtils.getUsername());
        accounts.setCreateTime(DateUtils.getNowDate());
        accounts.setId(IdWorker.getId());
        accounts.setAccountId(accounts.getId());
        accounts.setBalance(0.0);
        accounts.setId(bankCard.getId());
        accounts.setAccountId(bankCard.getId());
        accounts.setName(bankCard.getName());
        accounts.setType(bankCard.getType());
        accounts.setCreditLimit(bankCard.getCreditLimit());
        accounts.setCode(bankCard.getCode());
        accounts.setAvailableLimit(Double.parseDouble(String.valueOf(bankCard.getCreditLimit())));
        accounts.setState("1");
        accountsMapper.insertAccounts(accounts);

        return bankCardLendMapper.insertBankCardLend(bankCard);
    }

    /**
     * 修改银行卡信息
     *
     * @param bankCard 银行卡信息
     * @return 结果
     */
    @Override
    public int updateBankCardLend(BankCardLend bankCard)
    {
        bankCard.setUpdateBy(SecurityUtils.getUsername());
        bankCard.setUpdateTime(DateUtils.getNowDate());
        return bankCardLendMapper.updateBankCardLend(bankCard);
    }

    /**
     * 批量删除银行卡信息
     *
     * @param ids 需要删除的银行卡信息主键
     * @return 结果
     */
    @Override
    public int deleteBankCardLendByIds(Long[] ids)
    {
        return bankCardLendMapper.removeBankCardLendByIds(ids);
    }

    /**
     * 删除银行卡信息信息
     *
     * @param id 银行卡信息主键
     * @return 结果
     */
    @Override
    public int deleteBankCardLendById(Long id)
    {
        return bankCardLendMapper.removeBankCardLendById(id);
    }

}
