package com.dayou.service.impl;

import com.dayou.dto.CustomerDTO;
import com.dayou.entity.BusinessOpportunity;
import com.dayou.entity.ResourceTransfer;
import com.dayou.enums.BusinessResourceEnum;
import com.dayou.mapper.ResourceTransferMapper;
import com.dayou.service.IBusinessOpportunityService;
import com.dayou.service.ICustomerService;
import com.dayou.service.IItemService;
import com.dayou.service.IResourceTransferService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.vo.ItemVO;
import com.dayou.vo.ResourceTransferVO;
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 2023-05-31
 */
@Service
public class ResourceTransferServiceImpl extends ServiceImpl<ResourceTransferMapper, ResourceTransfer> implements IResourceTransferService {

    @Autowired
    private ResourceTransferMapper resourceTransferMapper;

    @Autowired
    private IItemService iItemService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IBusinessOpportunityService businessOpportunityService;

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


    @Override
    public ResourceTransferVO detail(Long id){
        ResourceTransferVO resourceTransfer = resourceTransferMapper.getDetailById(id);
        Long resourceId = resourceTransfer.getResourceId();
        BusinessResourceEnum resourceType = resourceTransfer.getResourceType();

        switch (resourceType){
            case ITEM:
                ItemVO itemVO = iItemService.detail(resourceId);
                resourceTransfer.setResource(itemVO);
                return resourceTransfer;
            case CUSTOMER:
                CustomerDTO customerDTO = customerService.detail(resourceId);
                resourceTransfer.setResource(customerDTO);
                return resourceTransfer;
            case BUSINESS_OPPORTUNITY:
                BusinessOpportunity opportunity = businessOpportunityService.detail(resourceId);
                resourceTransfer.setResource(opportunity);
                return resourceTransfer;
            default:
                return resourceTransfer;
        }
    }

    @Override
    public Boolean add(ResourceTransfer resourceTransfer){
        return  this.save(resourceTransfer);
    }

    @Override
    public Boolean update(ResourceTransfer resourceTransfer){
        return  this.updateById(resourceTransfer);
    }

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

    @Override
    public List<ResourceTransferVO> transferHistory(String resourceType, Long resourceId) {
        List<ResourceTransferVO> list = resourceTransferMapper.transferHistory(resourceType,resourceId);
        return list;
    }
}
