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.dto.AccountsDto;
import com.ruoyi.invest.domain.vo.AccountsVo;
import com.ruoyi.invest.mapper.AccountsMapper;
import com.ruoyi.invest.service.IAccountsService;
import org.springframework.stereotype.Service;

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

/**
 * 记账账户Service业务层处理
 *
 * @author tianyongbao
 * @date 2024-03-30
 */
@Service
public class AccountsServiceImpl implements IAccountsService
{
    @Resource
    private AccountsMapper accountsMapper;

    /**
     * 查询记账账户
     *
     * @param id 记账账户主键
     * @return 记账账户
     */
    @Override
    public AccountsVo selectAccountsById(Long id)
    {
        return accountsMapper.selectAccountsById(id);
    }

    /**
     * 查询记账账户列表
     *
     * @param accountsDto 记账账户
     * @return 记账账户
     */
    @Override
    public List<AccountsVo> selectAccountsList(AccountsDto accountsDto)
    {
        List<AccountsVo> accountsList=accountsMapper.selectAccountsList(accountsDto);
        //修改名称加卡号
        for (AccountsVo accounts : accountsList) {
            String typeName="";
            switch (accounts.getType()){
                case "1" :
                    typeName="储蓄账户-";
                    break;
                case "2" :
                    typeName="信用卡账户-";
                    break;
                case "3" :
                    typeName="借贷账户-";
                    break;
                case "5" :
                    typeName="投资账户-";
                    break;
                case "6" :
                    typeName="POS机账户-";
                    break;
            }
            if(accounts.getCode()!=null){
                accounts.setNameCode(typeName+accounts.getName()+"（"+ StringUtils.getLastNumberChars(4,accounts.getCode()+"）")+"-可用额度（"+accounts.getAvailableLimit()+"）");
            }else {
                accounts.setNameCode(typeName+accounts.getName());
            }
            if(!"null".equals(String.valueOf(accounts.getBillDate()))) {
                accounts.setBillDateName(accounts.getBillDate()+"号");
            }
            if(!"null".equals(String.valueOf(accounts.getPayDate()))) {
                accounts.setPayDateName(accounts.getPayDate()+"号");
            }
        }
    return  accountsList;
    }

    /**
     * 新增记账账户
     *
     * @param accounts 记账账户
     * @return 结果
     */
    @Override
    public int insertAccounts(Accounts accounts)
    {
        accounts.setCreateBy(SecurityUtils.getUsername());
        accounts.setCreateTime(DateUtils.getNowDate());
        accounts.setId(IdWorker.getId());
        accounts.setAccountId(accounts.getId());
        return accountsMapper.insertAccounts(accounts);
    }

    /**
     * 修改记账账户
     *
     * @param accounts 记账账户
     * @return 结果
     */
    @Override
    public int updateAccounts(Accounts accounts)
    {
        accounts.setUpdateBy(SecurityUtils.getUsername());
        accounts.setUpdateTime(DateUtils.getNowDate());
        return accountsMapper.updateAccounts(accounts);
    }

    /**
     * 批量删除记账账户
     *
     * @param ids 需要删除的记账账户主键
     * @return 结果
     */
    @Override
    public int deleteAccountsByIds(Long[] ids)
    {
        return accountsMapper.removeAccountsByIds(ids);
    }

    /**
     * 删除记账账户信息
     *
     * @param id 记账账户主键
     * @return 结果
     */
    @Override
    public int deleteAccountsById(Long id)
    {
        return accountsMapper.removeAccountsById(id);
    }
}
