package com.simuwang.manage.service.impl;

import com.simuwang.base.common.enums.DeletionType;
import com.simuwang.base.common.support.MybatisPage;
import com.simuwang.base.common.util.DateUtils;
import com.simuwang.base.common.util.StringUtil;
import com.simuwang.base.mapper.DeletionInfoMapper;
import com.simuwang.base.mapper.FundInfoMapper;
import com.simuwang.base.pojo.dos.DeletionInfoDO;
import com.simuwang.base.pojo.dos.FundDeletionInfoDO;
import com.simuwang.base.pojo.dto.ExcelDeletionInfoDTO;
import com.simuwang.base.pojo.dto.query.DeletionPageQuery;
import com.simuwang.base.pojo.dto.query.FundDeletionPageQuery;
import com.simuwang.base.pojo.vo.*;
import com.simuwang.manage.service.DeletionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * FileName: DeletionServiceImpl
 * Author:   chenjianhua
 * Date:     2024/9/17 18:54
 * Description: ${DESCRIPTION}
 */
@Service
public class DeletionServiceImpl implements DeletionService {

    @Autowired
    private DeletionInfoMapper deletionInfoMapper;

    @Autowired
    private FundInfoMapper fundInfoMapper;
    @Override
    public MybatisPage<DeletionInfoVO> searchDeletionList(DeletionPageQuery deletionPageQuery) {
        List<DeletionInfoDO> deletionInfoDOList = deletionInfoMapper.searchDeletionList(deletionPageQuery);
        long total = deletionInfoMapper.countDeletion(deletionPageQuery);
        List<DeletionInfoVO> dataList = deletionInfoDOList.stream().map(DeletionInfoDO::toVO).collect(Collectors.toList());
        for(DeletionInfoVO vo : dataList){
            String fundId = vo.getFundId();
            String lastDeletionDate = deletionInfoMapper.getLastDeletionDateByFundId(fundId,vo.getDeletionType());
            vo.setLastDeletionDate(lastDeletionDate);
        }
        return MybatisPage.of(total,dataList);
    }

    @Override
    public MybatisPage<FundDeletionInfoVO> searchFundDeletionList(FundDeletionPageQuery fundDeletionPageQuery) {
        List<FundDeletionInfoDO> deletionInfoDOList = deletionInfoMapper.searchFundDeletionList(fundDeletionPageQuery);
        long total = deletionInfoMapper.countFundDeletionList(fundDeletionPageQuery);
        List<FundDeletionInfoVO> dataList = deletionInfoDOList.stream().map(FundDeletionInfoDO::toVO).collect(Collectors.toList());
        for(FundDeletionInfoVO infoVO : dataList){
            infoVO.setFundName(fundInfoMapper.getFundNameByFundId(infoVO.getFundId()));
        }
        return MybatisPage.of(total,dataList);
    }

    @Override
    public void saveFundDeletionList(List<FundDeletionInfoVO> fundDeletionInfoVOList) {
        deletionInfoMapper.batchUpdate(fundDeletionInfoVOList);
    }

    @Override
    public void saveBatchDeletionRemark(FundDeletionRemarkVO fundDeletionRemarkVO) {
        String remark = fundDeletionRemarkVO.getRemark();
        for(FundDeletionTypeVO remarkVO : fundDeletionRemarkVO.getFundDeletionTypeList()){
            deletionInfoMapper.updateRemark(remarkVO.getFundId(),remarkVO.getDeletionType(),remarkVO.getDeletionDate(),remark);
        }
    }

    @Override
    public List<ExcelDeletionInfoDTO> selectFundDeletionInfoVOList(FundIdListVO fundIdListVO) {
        List<ExcelDeletionInfoDTO> result = new ArrayList<>();
        List<String> fundIdList = fundIdListVO.getFundIdList();
        List<FundDeletionInfoDO> deletionInfoDOList = deletionInfoMapper.selectFundDeletionInfoVOList(fundIdList);
        for(FundDeletionInfoDO infoDO : deletionInfoDOList){
            ExcelDeletionInfoDTO dto = new ExcelDeletionInfoDTO();
            dto.setFundName(fundInfoMapper.getFundNameByFundId(infoDO.getFundId()));
            dto.setCompanyName(fundInfoMapper.getCompanyNameByFundId(infoDO.getFundId()));
            dto.setDeletionType(DeletionType.getDeletionTypeByCode(infoDO.getDeletionType()).getInfo());
            dto.setDeletionDate(infoDO.getDeletionDate());
            dto.setRemark(infoDO.getRemark());
            dto.setFundId(infoDO.getFundId());
            result.add(dto);
        }
        return result;
    }
}
