tangxiaobao
2021-07-20 a0fc229c0df1d0bd87b7638805b810e8031c8942
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActWorkGuideServiceImpl.java
@@ -1,23 +1,35 @@
package com.panzhihua.service_community.service.impl;
import com.alibaba.fastjson.JSONObject;
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.panzhihua.common.model.dtos.community.ComActWorkGuideDTO;
import com.panzhihua.common.model.dtos.community.PageActWorkGuideDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.ComActWorkGuideMaterialVO;
import com.panzhihua.common.model.vos.community.ComActWorkGuideVO;
import com.panzhihua.common.utlis.DifferentLongListUtil;
import com.panzhihua.service_community.dao.ComActDAO;
import com.panzhihua.service_community.dao.ComActWorkGuideDAO;
import com.panzhihua.service_community.dao.ComActWorkGuideMaterialDAO;
import com.panzhihua.service_community.model.dos.ComActDO;
import com.panzhihua.service_community.model.dos.ComActWorkGuideDO;
import com.panzhihua.service_community.model.dos.ComActWorkGuideMaterialDO;
import com.panzhihua.service_community.service.ComActWorkGuideService;
import net.sf.json.xml.XMLSerializer;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@@ -27,17 +39,20 @@
 */
@Service
public class ComActWorkGuideServiceImpl extends ServiceImpl<ComActWorkGuideDAO, ComActWorkGuideDO> implements ComActWorkGuideService {
    static String AK = "W3x8DhCWKc2UFIwYgclpRBdL6BeGLLQt"; // 百度地图密钥
    @Resource
    ComActWorkGuideMaterialDAO workGuideMaterialDAO;
    @Resource
    ComActDAO comActDAO;
    @Override
    public R addWorkGuide(ComActWorkGuideVO workGuideVO, Long userId) {
    public R addWorkGuide(ComActWorkGuideDTO workGuideDTO, Long userId) {
        ComActWorkGuideDO comActWorkGuideDO = new ComActWorkGuideDO();
        BeanUtils.copyProperties(workGuideVO, comActWorkGuideDO);
        BeanUtils.copyProperties(workGuideDTO, comActWorkGuideDO);
        comActWorkGuideDO.setCreateBy(userId);
        this.baseMapper.insert(comActWorkGuideDO);
        //保存办事指南材料
        workGuideVO.getMaterials().forEach(material -> {
        workGuideDTO.getMaterials().forEach(material -> {
            ComActWorkGuideMaterialDO workGuideMaterialDO = new ComActWorkGuideMaterialDO();
            workGuideMaterialDO.setMaterialName(material.getMaterialName());
            workGuideMaterialDO.setWorkGuideId(comActWorkGuideDO.getId());
@@ -47,25 +62,25 @@
    }
    @Override
    public R editWorkGuide(ComActWorkGuideVO workGuideVO, Long userId) {
        if (workGuideVO.getId() == 0L) {
    public R editWorkGuide(ComActWorkGuideDTO workGuideDTO, Long userId) {
        if (workGuideDTO.getId() == 0L) {
            return R.fail("Id有误!");
        }
        ComActWorkGuideDO workGuideDO = this.baseMapper.selectById(workGuideVO.getId());
        ComActWorkGuideDO workGuideDO = this.baseMapper.selectById(workGuideDTO.getId());
        if (workGuideDO == null) {
            return R.fail("Id有误!");
        }
        BeanUtils.copyProperties(workGuideVO, workGuideDO);
        BeanUtils.copyProperties(workGuideDTO, workGuideDO);
        workGuideDO.setUpdateBy(userId);
        this.baseMapper.updateById(workGuideDO);
        if (workGuideVO.getMaterials().size() == 0) {//若编辑材料为null,执行删除操作
        if (workGuideDTO.getMaterials().size() == 0) {//若编辑材料为null,执行删除操作
            workGuideMaterialDAO.delete(new LambdaQueryWrapper<ComActWorkGuideMaterialDO>()
                    .eq(ComActWorkGuideMaterialDO::getWorkGuideId, workGuideVO.getId()));
                    .eq(ComActWorkGuideMaterialDO::getWorkGuideId, workGuideDTO.getId()));
        } else {//若材料不为空,但是少了数据条数,也要执行删除
            List<ComActWorkGuideMaterialDO> workGuideMaterialDOS = workGuideMaterialDAO.selectList(new LambdaQueryWrapper<ComActWorkGuideMaterialDO>()
                    .eq(ComActWorkGuideMaterialDO::getWorkGuideId, workGuideVO.getId()));
                    .eq(ComActWorkGuideMaterialDO::getWorkGuideId, workGuideDTO.getId()));
            //1、筛选vo里面和db里,需要删除的材料数据
            List<Long> voMaterialIds = workGuideVO.getMaterials().stream()
            List<Long> voMaterialIds = workGuideDTO.getMaterials().stream()
                    .filter(materialVo -> materialVo.getId() != null)
                    .map(ComActWorkGuideMaterialVO::getId)
                    .collect(Collectors.toList());
@@ -76,7 +91,7 @@
            if (deleteMaterialIds.size() != 0) {
                workGuideMaterialDAO.deleteBatchIds(deleteMaterialIds);
            }
            workGuideVO.getMaterials().forEach(materialsVO -> {
            workGuideDTO.getMaterials().forEach(materialsVO -> {
                if (materialsVO.getId() != null && materialsVO.getId() != 0L) {
                    ComActWorkGuideMaterialDO workGuideMaterialDO1 = workGuideMaterialDOS.stream().filter(workGuideMaterialDO -> workGuideMaterialDO.getId()
                            .equals(materialsVO.getId())).findFirst().orElse(null);
@@ -96,13 +111,23 @@
    }
    @Override
    public R detailWorkGuide(Long workGuideId) {
    public R detailWorkGuide(Long workGuideId, Long conmunityId) {
        ComActWorkGuideDO workGuideDO = this.baseMapper.selectById(workGuideId);
        if (workGuideDO == null) {
            return R.fail("Id有误!");
        }
        ComActWorkGuideVO vo = new ComActWorkGuideVO();
        BeanUtils.copyProperties(workGuideDO, vo);
        ComActDO comActDO = comActDAO.selectById(conmunityId);
        if (comActDO != null){
            vo.setAddress(comActDO.getAddress());
            vo.setPhone(comActDO.getContactsPhone());
        }
        List<String> list = getCoordinate(vo.getAddress());
        if (list !=null && list.size() > 0){
            vo.setLon(list.get(0));
            vo.setLat(list.get(1));
        }
        List<ComActWorkGuideMaterialDO> workGuideMaterialDOS = workGuideMaterialDAO.selectList(new LambdaQueryWrapper<ComActWorkGuideMaterialDO>()
                .eq(ComActWorkGuideMaterialDO::getWorkGuideId, workGuideId));
        workGuideMaterialDOS.forEach(workGuideMaterialDO -> {
@@ -138,4 +163,47 @@
                .eq(ComActWorkGuideMaterialDO::getWorkGuideId, workGuideId));
        return R.ok();
    }
    // 调用百度地图API根据地址,获取坐标
    public static List<String> getCoordinate(String address) {
        List<String> list = new ArrayList<>();
        if (address != null && !"".equals(address)) {
            address = address.replaceAll("\\s*", "").replace("#", "栋");
            String url = "http://api.map.baidu.com/geocoder/v3/?address=" + address + "&output=json&ak=" + AK;
            String json = loadJSON(url);
//            json = "{" + json;
//            json = json + "}";
            if (json != null && !"".equals(json)) {
                XMLSerializer xmlSerializer = new XMLSerializer();
                //将xml转为json(注:如果是元素的属性,会在json里的key前加一个@标识)
                String result = xmlSerializer.read(json).toString();
                JSONObject obj = JSONObject.parseObject(json);
                if ("0".equals(obj.getString("status"))) {
                    double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng"); // 经度
                    double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat"); // 纬度
                    DecimalFormat df = new DecimalFormat("#.######");
                    list.add(df.format(lng));
                    list.add(df.format(lat));
                    return list;
                }
            }
        }
        return null;
    }
    public static String loadJSON(String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {} catch (IOException e) {}
        return json.toString();
    }
}