package com.zcxk.rmcp.web.excel.download.service;

import com.zcxk.core.oauth2.util.UserUtil;
import com.zcxk.rmcp.api.dto.product.ProductVo;
import com.zcxk.rmcp.api.vo.CommunityVo;
import com.zcxk.rmcp.core.dao.CommunityMapper;
import com.zcxk.rmcp.core.dao.OrgMapper;
import com.zcxk.rmcp.core.dao.ProductMapper;
import com.zcxk.rmcp.core.entity.Community;
import com.zcxk.rmcp.core.entity.Org;
import com.zcxk.rmcp.web.excel.download.DownloadExcelService;
import com.zcxk.rmcp.web.excel.model.CommImportData;
import com.zcxk.rmcp.web.excel.model.DownloadExcelData;
import com.zcxk.rmcp.web.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author linqingwei
 * @date 2021-07-30 10:54
 */
@Service
public class InstallPlanExcelServiceImpl implements DownloadExcelService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private OrgMapper orgMapper;
    @Resource
    private CommunityMapper communityMapper;
    @Autowired
    private CommonService commonService;

    @Override
    public void setParam(DownloadExcelData data) {
        data.setFilePath(commonService.getExcelFilePath("template"));
        data.setDownloadName("安装计划批量导入模板");


        data.setProductDataList(convertProductData(productMapper.findList(data.getCategoryId())));
        data.setCommunityDataList(convertCommunityData(communityMapper.listCommunity(new Community(), UserUtil.getCurrentUser().getUserCondition())));
        data.setOrgDataList(convertOrgData(orgMapper.findList(UserUtil.getCurrentUser().getUserCondition())));
    }
    private List<CommImportData> convertCommunityData(List<CommunityVo> list){
        return list.stream().map(vo ->new CommImportData(vo.getName(),Math.toIntExact(vo.getId()))).collect(Collectors.toList());
    }

    private List<CommImportData> convertOrgData(List<Org> list){
        return list.stream().map(vo ->new CommImportData(vo.getOrgName(),Math.toIntExact(vo.getId()))).collect(Collectors.toList());
    }
    private List<CommImportData> convertProductData(List<ProductVo> list){
        return list.stream().map(vo ->new CommImportData(vo.getManufacturerName()+"/"+vo.getProductName()+"/"+vo.getProductModel(),Math.toIntExact(vo.getId()))).collect(Collectors.toList());
    }
}
