package com.dayou.service.impl;

import com.dayou.common.BaseEntity;
import com.dayou.dto.AssetsPerformanceDistributionAddDTO;
import com.dayou.dto.UserShareRate;
import com.dayou.entity.BusinessPerformanceDistribution;
import com.dayou.entity.BusinessProductionPerformance;
import com.dayou.mapper.BusinessPerformanceDistributionMapper;
import com.dayou.service.IBusinessPerformanceDistributionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 绩效分配比例信息 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2024-05-09
 */
@Service
public class BusinessPerformanceDistributionServiceImpl extends ServiceImpl<BusinessPerformanceDistributionMapper, BusinessPerformanceDistribution> implements IBusinessPerformanceDistributionService {


    @Autowired
    private BusinessPerformanceDistributionMapper businessPerformanceDistributionMapper;

    @Override
    @SuppressWarnings("unchecked")
    public Page<BusinessPerformanceDistribution> selectPage(Page page,BusinessPerformanceDistribution businessPerformanceDistribution){
        return this.page(page, new QueryWrapper<BusinessPerformanceDistribution>(businessPerformanceDistribution));
    }


    @Override
    public BusinessPerformanceDistribution detail(Long id){
        return this.getById(id);
    }

    @Override
    public Boolean add(BusinessPerformanceDistribution businessPerformanceDistribution){
        return  this.save(businessPerformanceDistribution);
    }

    @Override
    public Boolean update(BusinessPerformanceDistribution businessPerformanceDistribution){
        return  this.updateById(businessPerformanceDistribution);
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    /**
     * 新增资产绩效分配信息
     * @param dto 新增dto
     * @return Boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addAssetsPerformanceDistribution(AssetsPerformanceDistributionAddDTO dto) {

        // new批量插入集合
        List<BusinessPerformanceDistribution> performanceDistributionList = new ArrayList<>();

        // 将前端传递的数据进行处理,添加成单个的对象
        for (BusinessPerformanceDistribution distribution : dto.getDistributionList()){
            BusinessPerformanceDistribution performanceDistribution = new BusinessPerformanceDistribution();
            performanceDistribution.setBusinessId(dto.getBusinessId());
            performanceDistribution.setBusinessType(dto.getBusinessType());
            performanceDistribution.setReportNo(dto.getReportNo());
            performanceDistribution.setMemberId(distribution.getMemberId());
            performanceDistribution.setPerformanceDistribution(distribution.getPerformanceDistribution());
            performanceDistribution.setMemberType(distribution.getMemberType());
            performanceDistributionList.add(performanceDistribution);
        }

        // 批量新增
        return this.saveBatch(performanceDistributionList);
    }

    /**
     * 获取资产绩效分配信息list
     * @param dto 查询dto
     * @return List<AssetsPerformanceDistributionAddDTO>
     */
    @Override
    public List<BusinessPerformanceDistribution> getPerformanceDistributionList(AssetsPerformanceDistributionAddDTO dto) {
        return this.list(new LambdaQueryWrapper<BusinessPerformanceDistribution>()
                .eq(BusinessPerformanceDistribution::getBusinessId, dto.getBusinessId())
                .eq(BusinessPerformanceDistribution::getBusinessType, dto.getBusinessType())
                .eq(BusinessPerformanceDistribution::getReportNo, dto.getReportNo())
                .eq(BusinessPerformanceDistribution::getUserType, dto.getUserType())
                .eq(BaseEntity::getDeleted, 0));
    }

    /**
     * 修改资产绩效分配信息
     * @param dto 修改dto
     * @return Boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateAssetsPerformanceDistribution(AssetsPerformanceDistributionAddDTO dto) {
        return this.updateBatchById(dto.getDistributionList());
    }

    /**
     * 根据申报id获取绩效比列分配集合
     * @param declareId 申报id
     * @return List<UserShareRate>
     */
    @Override
    public List<UserShareRate> alloAssetsDistributionDeclareId(Long declareId) {
        return businessPerformanceDistributionMapper.alloAssetsDistributionDeclareId(declareId);
    }
}
