fix
Helius
2021-09-15 7ab7943de217a9e5d4a489c22c7ae27a29692d55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.xzx.gc.system.service;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzx.gc.entity.HomeServiceInfo;
import com.xzx.gc.entity.HomeServiceRef;
import com.xzx.gc.model.order.PartnerFenceDTO;
import com.xzx.gc.model.system.HomeServiceAddDTO;
import com.xzx.gc.model.system.HomeServicePageDTO;
import com.xzx.gc.system.mapper.HomeServiceInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
@Transactional
public class HomeServiceInfoService {
 
    @Autowired
    private HomeServiceInfoMapper homeServiceInfoMapper;
 
    @Autowired
    private HomeServiceRefService homeServiceRefService;
 
    /**
     * 添加
     */
    public Long add(HomeServiceAddDTO homeServiceAddDTO){
 
        HomeServiceInfo homeServiceInfo=new HomeServiceInfo();
        BeanUtil.copyProperties(homeServiceAddDTO,homeServiceInfo);
        homeServiceInfo.setDelFlag(false);
        homeServiceInfo.setCreateTime(DateUtil.now());
        homeServiceInfo.setId(null);
        homeServiceInfoMapper.insertSelective(homeServiceInfo);
 
        //添加二级分类
        if(homeServiceAddDTO.getLevel().equals(Convert.toShort(2))){
            addRef(homeServiceAddDTO, homeServiceInfo.getId());
 
        }
        return homeServiceInfo.getId();
    }
 
    /**
     * 添加分类与合伙人或围栏的对应关系
     * @param homeServiceAddDTO
     * @param serviceId
     */
    private void addRef(HomeServiceAddDTO homeServiceAddDTO, Long serviceId) {
        List<PartnerFenceDTO> list = homeServiceAddDTO.getList();
 
        List<HomeServiceRef> listObj=CollUtil.newArrayList();
 
        for (PartnerFenceDTO partnerFenceDTO : list) {
            List<String> fenceId = partnerFenceDTO.getFenceIds();
            String partnerId = partnerFenceDTO.getPartnerId();
            if(CollUtil.isEmpty(fenceId)){
                HomeServiceRef homeServiceRef=new HomeServiceRef();
                homeServiceRef.setServiceId(serviceId);
                homeServiceRef.setRefType(Convert.toShort(1));
                homeServiceRef.setPartnerId(partnerId);
                listObj.add(homeServiceRef);
            }else {
                for (String s : fenceId) {
                    HomeServiceRef homeServiceRef=new HomeServiceRef();
                    homeServiceRef.setServiceId(serviceId);
                    homeServiceRef.setRefType(Convert.toShort(2));
                    homeServiceRef.setPartnerId(partnerId);
                    homeServiceRef.setFenceId(s);
                    listObj.add(homeServiceRef);
                }
 
            }
        }
 
        if(CollUtil.isNotEmpty(listObj)){
            homeServiceRefService.addBatch(listObj);
        }
    }
 
    /**
     * 根据父ID查询
     * @param parentId
     * @return
     */
    public List<HomeServiceInfo> findByParentId(Long parentId,Short showFlag){
        Example example=new Example(HomeServiceInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId",Convert.toLong(parentId));
        criteria.andEqualTo("delFlag",false);
        if(showFlag!=null){
            criteria.andEqualTo("showFlag",showFlag);
        }
        example.setOrderByClause("sort asc");
        return homeServiceInfoMapper.selectByExample(example);
    }
 
 
    public List<HomeServiceInfo> findByLevel(Short level){
        Example example=new Example(HomeServiceInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("level",level);
        criteria.andEqualTo("delFlag",false);
        example.setOrderByClause("create_time desc");
        return homeServiceInfoMapper.selectByExample(example);
    }
 
 
    /**
     * 分页查询
     * @param homeServicePageDTO
     * @return
     */
    public PageInfo<HomeServiceInfo> page(HomeServicePageDTO homeServicePageDTO) {
        PageHelper.startPage(homeServicePageDTO.getPageNo(),homeServicePageDTO.getPageSize());
        List<HomeServiceInfo> list=homeServiceInfoMapper.page(homeServicePageDTO);
        return new PageInfo<>(list);
    }
 
    public HomeServiceInfo findById(Long id){
        return homeServiceInfoMapper.selectByPrimaryKey(id);
    }
 
    public void deleteById(Long id){
        HomeServiceInfo homeServiceInfo=new HomeServiceInfo();
        homeServiceInfo.setId(id);
        homeServiceInfo.setDelFlag(true);
        homeServiceInfoMapper.updateByPrimaryKeySelective(homeServiceInfo);
    }
 
    public void updateById(HomeServiceInfo homeServiceInfo){
        homeServiceInfoMapper.updateByPrimaryKeySelective(homeServiceInfo);
    }
 
 
    public void update(HomeServiceAddDTO homeServiceAddDTO) {
        //先删除 再加
        deleteById(homeServiceAddDTO.getId());
        //删除关联关系
        if(homeServiceAddDTO.getLevel().intValue()==2){
            homeServiceRefService.deleteByServiceId(homeServiceAddDTO.getId());
        }
        Long add = add(homeServiceAddDTO);
 
        //更改关联关系
        if(homeServiceAddDTO.getLevel().intValue()==2) {
            updateParentByParentId(homeServiceAddDTO.getId(),add);
        }
 
//        HomeServiceInfo homeServiceInfo=new HomeServiceInfo();
//        BeanUtil.copyProperties(homeServiceAddDTO,homeServiceInfo);
//        updateById(homeServiceInfo);
//
//        List<PartnerFenceDTO> list = homeServiceAddDTO.getList();
//        if(CollUtil.isNotEmpty(list)){
//            homeServiceRefService.deleteByServiceId(homeServiceAddDTO.getId());
//            addRef(homeServiceAddDTO,homeServiceAddDTO.getId());
//        }
 
    }
 
    /**
     * 将父级id更改为新的
     * @param oldParentId
     * @param newParentId
     */
    public void updateParentByParentId(Long oldParentId,Long newParentId){
        Example example=new Example(HomeServiceInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId",oldParentId);
        HomeServiceInfo homeServiceInfo=new HomeServiceInfo();
        homeServiceInfo.setParentId(newParentId);
        homeServiceInfoMapper.updateByExampleSelective(homeServiceInfo,example);
    }
 
 
    public HomeServiceInfo findSecondById(Long id){
        HomeServiceInfo homeServiceInfo = homeServiceInfoMapper.selectByPrimaryKey(id);
        List<PartnerFenceDTO> list=new ArrayList<>();
        List<HomeServiceRef> byServiceId = homeServiceRefService.findByServiceId(id);
        if(CollUtil.isNotEmpty(byServiceId)){
            List<List<HomeServiceRef>> partnerId = CollUtil.groupByField(byServiceId, "partnerId");
            for (List<HomeServiceRef> homeServiceRefs : partnerId) {
                String partnerId1 = homeServiceRefs.get(0).getPartnerId();
                PartnerFenceDTO partnerFenceDTO=new PartnerFenceDTO();
                partnerFenceDTO.setPartnerId(partnerId1);
                List<String> fenceIds=homeServiceRefs.stream().map(x->x.getFenceId()).collect(Collectors.toList());
                partnerFenceDTO.setFenceIds(fenceIds);
                list.add(partnerFenceDTO);
            }
            homeServiceInfo.setList(list);
        }
        return homeServiceInfo;
    }
 
 
    public List<HomeServiceInfo> findSecond(String partnerId, String fenceId){
        return homeServiceInfoMapper.findSecond(partnerId,fenceId);
    }
}