郑永安
2023-06-19 f65443d8abeaedc9d102324565e8368e7c9d90c8
src/main/java/com/gk/firework/Service/ServiceImpl/EnterpriseFeedServiceImpl.java
对比新文件
@@ -0,0 +1,183 @@
package com.gk.firework.Service.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.firework.Domain.EnterpriseFeed;
import com.gk.firework.Domain.EnterpriseResource;
import com.gk.firework.Domain.Exception.BusinessException;
import com.gk.firework.Domain.UserInfo;
import com.gk.firework.Domain.Utils.*;
import com.gk.firework.Domain.Utils.Properties;
import com.gk.firework.Domain.Vo.EnterpriseFeedVo;
import com.gk.firework.Mapper.EnterpriseFeedMapper;
import com.gk.firework.Service.EnterpriseFeedService;
import com.gk.firework.Service.EnterpriseResourceService;
import com.gk.firework.Service.ExcelExportService;
import com.gk.firework.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
@Service("enterpriseFeedService")
public class EnterpriseFeedServiceImpl extends ServiceImpl<EnterpriseFeedMapper, EnterpriseFeed> implements EnterpriseFeedService {
    @Autowired
    private EnterpriseFeedMapper enterpriseFeedMapper;
    @Autowired
    private ExcelExportService excelExportService;
    @Autowired
    private EnterpriseResourceService enterpriseResourceService;
    @Autowired
    private UserService userService;
    /**
    * @Description: 查询自检
    * @date 2021/5/13 18:44
    */
    @Override
    public IPage selectPages(Page<EnterpriseFeed> page, Long id, UserInfo user) {
        Map<String, Object> params = new HashMap<>();
        params.put("tabletype", Constants.FEED);
        params.put("id", id);
        List<EnterpriseFeed> list = enterpriseFeedMapper.selectPages(page, params);
        return page.setRecords(list);
    }
    /**
    * @Description: 新增自检
    * @date 2021/5/13 18:44
    */
    @Override
    @Transactional
    public void addFeed(EnterpriseFeedVo enterpriseFeedVo, UserInfo user) {
        if (enterpriseFeedVo.getEnterpriseid() == null) {
            throw new BusinessException("缺少企业关键数据");
        }
        enterpriseFeedVo.setValidflag(true);
        enterpriseFeedVo.setCreatetime(new Date());
        enterpriseFeedVo.setCreateby(user.getId());
        enterpriseFeedVo.setCreatebyname(user.getUsername());
        this.save(enterpriseFeedVo);
        List<EnterpriseResource> adds = null;
        MultipartFile[] files = enterpriseFeedVo.getFile();
        try {
            if (files != null && files.length > 0) {
                adds = new ArrayList<>();
                Date now = new Date();
                for (MultipartFile file : files) {
                    String name = UploadUtil.uploadFile(file, Properties.enterprisePath);
                    EnterpriseResource er = new EnterpriseResource();
                    er.setTabletype(Constants.FEED);
                    er.setFilename(file.getOriginalFilename());
                    er.setUrl(Properties.enterprise + name);
                    er.setCreatetime(now);
                    er.setCreateby(user.getId());
                    er.setCreatebyname(user.getUsername());
                    er.setBelongid(enterpriseFeedVo.getId());
                    er.setValidflag(true);
                    adds.add(er);
                }
                //执行
                enterpriseResourceService.saveBatch(adds);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("上传失败,请联系管理员");
        }
    }
    /**
    * @Description: 导入反馈
    * @date 2021/5/13 18:52
    */
    @Override
    public void importFeed(MultipartFile file, UserInfo user,Long enterpriseId) {
        if (file == null || file.getSize() == 0) {
            throw new BusinessException("上传文件或者请求出现问题");
        }
        if(!FileOptUtils.isDirExists(Properties.filePath)){
            throw new BusinessException("发生错误或不为目录");
        }
        if (enterpriseId == null) {
            throw new BusinessException("缺少企业关键数据");
        }
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS" );
        String fileSave = Properties.enterprisePath + user.getUsername() +"_feed_" + sdf.format(new Date()) +".xlsx";
        try {
            file.transferTo(new File(fileSave));
            InputStream in = new FileInputStream(fileSave);
            String name = file.getOriginalFilename();
            assert name != null;
            Boolean isExcel2007 = name.substring(name.lastIndexOf(".") + 1).endsWith("xlsx");
            excelExportService.importFeedExcel(in, user, isExcel2007,enterpriseId);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new BusinessException("找不到文件");
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("发生错误,请联系管理员");
        }
    }
    /**
    * @Description: 导出反馈
    * @date 2021/5/14 11:11
    */
    @Override
    public List<Map> exportFeed(Map filter, UserInfo user) {
        UserInfo userInfo = userService.getById(user.getId());
        Map<String, Object> params = new HashMap<>();
        //监管部门 根据 地区看所有
        params.put("province", userInfo.getProvince());
        params.put("city", userInfo.getCity());
        params.put("district", userInfo.getArea());
        params.put("street", userInfo.getTown());
        params.put("committee", userInfo.getCommunity());
        //企业用户
        params.put("enterprisenumber", userInfo.getCompanynumber());
        //过滤条件
        { //企业类型
            params.put("safetySupervision", filter.get("safetysupervision"));
            //经济类型
            params.put("economicIndustry", filter.get("economicindustry"));
            //许可证有效|过期
            params.put("valid", filter.get("valid"));
            //地区
            params.put("filterProvince", filter.get("province"));
            params.put("filterCity", filter.get("city"));
            params.put("filterDistrict", filter.get("district"));
            params.put("filterStreet", filter.get("street"));
            params.put("filterCommittee", filter.get("committee"));
            //企业名称
            params.put("enterprisename", filter.get("enterprisename"));
        }
        return enterpriseFeedMapper.selectExportFeed(params);
    }
    @Override
    public List<Map> exportFeedById(Long id) {
        if (id == null) {
            throw new BusinessException("参数传递为空");
        }
        return enterpriseFeedMapper.selectExportFeedById(id);
    }
}