package com.dayou.service.impl;

import com.dayou.entity.ItemBrokerageTechnicistDeduction;
import com.dayou.mapper.ItemBrokerageTechnicistDeductionMapper;
import com.dayou.service.IItemBrokerageTechnicistDeductionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.utils.LoginContext;
import com.dayou.vo.DeductionTechStatVO;
import com.dayou.vo.TechDeductionVO;
import com.google.common.cache.LoadingCache;
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.math.BigDecimal;
import java.util.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 技术人员提成抵扣记录 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-06-06
 */
@Service
public class ItemBrokerageTechnicistDeductionServiceImpl extends ServiceImpl<ItemBrokerageTechnicistDeductionMapper, ItemBrokerageTechnicistDeduction> implements IItemBrokerageTechnicistDeductionService {

    @Autowired
    private ItemBrokerageTechnicistDeductionMapper itemBrokerageTechnicistDeductionMapper;

    @Autowired
    private LoadingCache<Long,BigDecimal> baseAmountsCache;

    @Override
    @SuppressWarnings("unchecked")
    public Page<TechDeductionVO> selectPage(Page page, TechDeductionVO techDeductionVO){
        techDeductionVO.setUserId(LoginContext.getUserId());
        Page<TechDeductionVO> xPage = itemBrokerageTechnicistDeductionMapper.xPage(page,techDeductionVO);
        return xPage;
    }


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

    @Override
    public Boolean add(ItemBrokerageTechnicistDeduction itemBrokerageTechnicistDeduction){
        return  this.save(itemBrokerageTechnicistDeduction);
    }

    @Override
    public Boolean update(ItemBrokerageTechnicistDeduction itemBrokerageTechnicistDeduction){
        return  this.updateById(itemBrokerageTechnicistDeduction);
    }

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

    @Override
    public DeductionTechStatVO stat() {
        Long userId = LoginContext.getUserId();
        BigDecimal poolResidueAmount =  itemBrokerageTechnicistDeductionMapper.poolResidueAmount(userId);
        DeductionTechStatVO vo = DeductionTechStatVO
                .builder()
                .poolResidueAmount(poolResidueAmount)
                .defaultBrokerageAmount(baseAmountsCache.getIfPresent(userId))
                .build();
        return vo;
    }
}
