package com.ruoyi.invest.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.IdWorker;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.invest.domain.FutureStocksBill;
import com.ruoyi.invest.domain.dto.FutureStocksBillDto;
import com.ruoyi.invest.domain.vo.FutureStocksBillVo;
import com.ruoyi.invest.mapper.FutureStocksBillMapper;
import com.ruoyi.invest.service.IFutureStocksBillService;
import org.springframework.stereotype.Service;

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

/**
 * 期货股票账单Service业务层处理
 *
 * @author tianyongbao
 * @date 2024-03-20
 */
@Service
public class FutureStocksBillServiceImpl implements IFutureStocksBillService
{
    @Resource
    private FutureStocksBillMapper futureStocksBillMapper;

    /**
     * 查询期货股票账单
     *
     * @param id 期货股票账单主键
     * @return 期货股票账单
     */
    @Override
    public FutureStocksBillVo selectFutureStocksBillById(Long id)
    {
        return futureStocksBillMapper.selectFutureStocksBillById(id);
    }

    /**
     * 查询期货股票账单列表
     *
     * @param futureStocksBillDto 期货股票账单
     * @return 期货股票账单
     */
    @Override
    public List<FutureStocksBillVo> selectFutureStocksBillList(FutureStocksBillDto futureStocksBillDto)
    {
        List<FutureStocksBillVo> futureStocksBillVoList=futureStocksBillMapper.selectFutureStocksBillList(futureStocksBillDto);
        //修改名称加卡号
        for (FutureStocksBillVo futureStocksBill : futureStocksBillVoList) {
            if(futureStocksBill.getFutureStocksCode()!=null){
                futureStocksBill.setFutureStocksNameCode(futureStocksBill.getFutureStocksName()+"（"+ futureStocksBill.getFutureStocksCode()+"）");
            }else {
                futureStocksBill.setFutureStocksNameCode(futureStocksBill.getFutureStocksName());
            }
        }
        return futureStocksBillVoList;
    }

    /**
     * 新增期货股票账单
     *
     * @param futureStocksBill 期货股票账单
     * @return 结果
     */
    @Override
    public int insertFutureStocksBill(FutureStocksBill futureStocksBill)
    {
        futureStocksBill.setCreateBy(SecurityUtils.getUsername());
        futureStocksBill.setCreateTime(DateUtils.getNowDate());
        futureStocksBill.setId(IdWorker.getId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月账单");
        SimpleDateFormat yearFormatter = new SimpleDateFormat("yyyy");
        SimpleDateFormat monthFormatter = new SimpleDateFormat("yyyy-MM");
        if(futureStocksBill.getBillDate()!=null){
            futureStocksBill.setName(simpleDateFormat.format(futureStocksBill.getBillDate()));
            String periodStart = DateUtils.getFirstDay(futureStocksBill.getBillDate());
            String periodEnd= DateUtils.getLastDay(futureStocksBill.getBillDate());
            futureStocksBill.setBillDatePeriod(periodStart+"~"+periodEnd);
            futureStocksBill.setBillYear(yearFormatter.format(futureStocksBill.getBillDate()));
            futureStocksBill.setBillMonth(monthFormatter.format(futureStocksBill.getBillDate()));
        }

        return futureStocksBillMapper.insertFutureStocksBill(futureStocksBill);
    }

    /**
     * 修改期货股票账单
     *
     * @param futureStocksBill 期货股票账单
     * @return 结果
     */
    @Override
    public int updateFutureStocksBill(FutureStocksBill futureStocksBill)
    {
        futureStocksBill.setUpdateBy(SecurityUtils.getUsername());
        futureStocksBill.setUpdateTime(DateUtils.getNowDate());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月账单");
        SimpleDateFormat yearFormatter = new SimpleDateFormat("yyyy");
        SimpleDateFormat monthFormatter = new SimpleDateFormat("yyyy-MM");
        if(futureStocksBill.getBillDate()!=null){
            futureStocksBill.setName(simpleDateFormat.format(futureStocksBill.getBillDate()));
            String periodStart = DateUtils.getFirstDay(futureStocksBill.getBillDate());
            String periodEnd= DateUtils.getLastDay(futureStocksBill.getBillDate());
            futureStocksBill.setBillDatePeriod(periodStart+"~"+periodEnd);
            futureStocksBill.setBillYear(yearFormatter.format(futureStocksBill.getBillDate()));
            futureStocksBill.setBillMonth(monthFormatter.format(futureStocksBill.getBillDate()));
        }
        return futureStocksBillMapper.updateFutureStocksBill(futureStocksBill);
    }

    /**
     * 批量删除期货股票账单
     *
     * @param ids 需要删除的期货股票账单主键
     * @return 结果
     */
    @Override
    public int deleteFutureStocksBillByIds(Long[] ids)
    {
        return futureStocksBillMapper.removeFutureStocksBillByIds(ids);
    }

    /**
     * 删除期货股票账单信息
     *
     * @param id 期货股票账单主键
     * @return 结果
     */
    @Override
    public int deleteFutureStocksBillById(Long id)
    {
        return futureStocksBillMapper.removeFutureStocksBillById(id);
    }
}
