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.CreditCardBill;
import com.ruoyi.invest.domain.dto.CreditCardBillDto;
import com.ruoyi.invest.domain.vo.BankCardLendVo;
import com.ruoyi.invest.domain.vo.CreditCardBillVo;
import com.ruoyi.invest.mapper.BankCardLendMapper;
import com.ruoyi.invest.mapper.CreditCardBillMapper;
import com.ruoyi.invest.service.ICreditCardBillService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 信用卡账单Service业务层处理
 *
 * @author tianyongbao
 * @date 2024-03-19
 */
@Service
public class CreditCardBillServiceImpl implements ICreditCardBillService
{
    @Resource
    private CreditCardBillMapper creditCardBillMapper;
    @Resource
    private BankCardLendMapper bankCardLendMapper;

    /**
     * 查询信用卡账单
     *
     * @param id 信用卡账单主键
     * @return 信用卡账单
     */
    @Override
    public CreditCardBillVo selectCreditCardBillById(Long id)
    {
        return creditCardBillMapper.selectCreditCardBillById(id);
    }

    /**
     * 查询信用卡账单列表
     *
     * @param creditCardBillDto 信用卡账单
     * @return 信用卡账单
     */
    @Override
    public List<CreditCardBillVo> selectCreditCardBillList(CreditCardBillDto creditCardBillDto)
    {
        List<CreditCardBillVo> creditCardBillList=creditCardBillMapper.selectCreditCardBillList(creditCardBillDto);
        //修改名称加卡号
        for (CreditCardBillVo creditCardBill : creditCardBillList) {
            if(creditCardBill.getBankName()!=null){
                creditCardBill.setBankNameCode(creditCardBill.getBankName()+"（"+ StringUtils.getLastNumberChars(4,creditCardBill.getBankCode()+"）"));
            }
        }
        return creditCardBillList;
    }

    /**
     * 新增信用卡账单
     *
     * @param creditCardBill 信用卡账单
     * @return 结果
     */
    @Override
    public int insertCreditCardBill(CreditCardBill creditCardBill)
    {
        creditCardBill.setCreateBy(SecurityUtils.getUsername());
        creditCardBill.setCreateTime(DateUtils.getNowDate());
        creditCardBill.setId(IdWorker.getId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月账单");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        BankCardLendVo bankCard=bankCardLendMapper.selectBankCardLendById(creditCardBill.getCreditCardId());
        //0表示当期，1表示下期
        String isNextBillDate  = bankCard.getIsNextBillDate();
        if(creditCardBill.getBillDate()!=null){
            creditCardBill.setName(simpleDateFormat.format(creditCardBill.getBillDate()));
            Calendar calendarStart = Calendar.getInstance();
            calendarStart.setTime(creditCardBill.getBillDate());
            calendarStart.add(Calendar.MONTH, -1);
            if(isNextBillDate.equals("0")){
                calendarStart.add(Calendar.DATE, 1);
            }
            Date lastMonth = calendarStart.getTime();
            String periodStart = formatter.format(lastMonth);

            Calendar calendarEnd = Calendar.getInstance();
            calendarEnd.setTime(creditCardBill.getBillDate());
            if(isNextBillDate.equals("1")){
                calendarEnd.add(Calendar.DATE, -1);
            }
            Date lastDay = calendarEnd.getTime();
            String periodEnd= formatter.format(lastDay);
            creditCardBill.setBillDatePeriod(periodStart+"~"+periodEnd);

        }

        return creditCardBillMapper.insertCreditCardBill(creditCardBill);
    }

    /**
     * 修改信用卡账单
     *
     * @param creditCardBill 信用卡账单
     * @return 结果
     */
    @Override
    public int updateCreditCardBill(CreditCardBill creditCardBill)
    {
        creditCardBill.setUpdateBy(SecurityUtils.getUsername());
        creditCardBill.setUpdateTime(DateUtils.getNowDate());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月账单");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        BankCardLendVo bankCard=bankCardLendMapper.selectBankCardLendById(creditCardBill.getCreditCardId());
        //0表示当期，1表示下期
        String isNextBillDate  = bankCard.getIsNextBillDate();
        if(creditCardBill.getBillDate()!=null){
            creditCardBill.setName(simpleDateFormat.format(creditCardBill.getBillDate()));
            Calendar calendarStart = Calendar.getInstance();
            calendarStart.setTime(creditCardBill.getBillDate());
            calendarStart.add(Calendar.MONTH, -1);
            if(isNextBillDate.equals("0")){
                calendarStart.add(Calendar.DATE, 1);
            }
            Date lastMonth = calendarStart.getTime();
            String periodStart = formatter.format(lastMonth);

            Calendar calendarEnd = Calendar.getInstance();
            calendarEnd.setTime(creditCardBill.getBillDate());
            if(isNextBillDate.equals("1")){
                calendarEnd.add(Calendar.DATE, -1);
            }
            Date lastDay = calendarEnd.getTime();
            String periodEnd= formatter.format(lastDay);
            creditCardBill.setBillDatePeriod(periodStart+"~"+periodEnd);

        }
        return creditCardBillMapper.updateCreditCardBill(creditCardBill);
    }

    /**
     * 批量删除信用卡账单
     *
     * @param ids 需要删除的信用卡账单主键
     * @return 结果
     */
    @Override
    public int deleteCreditCardBillByIds(Long[] ids)
    {
        return creditCardBillMapper.removeCreditCardBillByIds(ids);
    }

    /**
     * 删除信用卡账单信息
     *
     * @param id 信用卡账单主键
     * @return 结果
     */
    @Override
    public int deleteCreditCardBillById(Long id)
    {
        return creditCardBillMapper.removeCreditCardBillById(id);
    }

}
