xuhy
2023-04-10 1b6c4cd60f3882c400d30af367bd4c5960fbd4a1
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.stylefeng.guns.modular.system.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.controller.resp.TBranchOfficeResp;
import com.stylefeng.guns.modular.system.dao.*;
import com.stylefeng.guns.modular.system.enums.StatusEnum;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.ITBranchOfficeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.service.ITRegionService;
import org.apache.poi.hdf.extractor.TC;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * <p>
 * 分公司 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2023-02-21
 */
@Service
public class TBranchOfficeServiceImpl extends ServiceImpl<TBranchOfficeMapper, TBranchOffice> implements ITBranchOfficeService {
 
    @Autowired
    private TBranchOfficeMapper tBranchOfficeMapper;
 
    @Autowired
    private TCouponMapper tCouponMapper;
    @Autowired
    private TUserToCouponMapper tUserToCouponMapper;
 
    @Autowired
    private TOrderMapper tOrderMapper;
    @Autowired
    private TDriverMapper tDriverMapper;
    @Autowired
    private ITRegionService tRegionService;
    @Autowired
    private TAgentMapper tAgentMapper;
 
    @Override
    public void tBranchOfficeDetail(Integer tBranchOfficeId, Model model) {
        // 分公司信息
        TBranchOffice tBranchOffice = tBranchOfficeMapper.selectById(tBranchOfficeId);
        model.addAttribute("principal",tBranchOffice.getPrincipal());
        model.addAttribute("createTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tBranchOffice.getCreateTime()));
        model.addAttribute("email",tBranchOffice.getEmail());
        model.addAttribute("area",tBranchOffice.getProvinceName()+tBranchOffice.getCityName()+tBranchOffice.getDistrictName());
 
        // 统计时间
        model.addAttribute("startToEndTime",new SimpleDateFormat("yyyy.MM.dd").format(tBranchOffice.getCreateTime())+"-"+
                new SimpleDateFormat("yyyy.MM.dd").format(new Date()));
 
        // 订单数据
        List<TOrder> orders = tOrderMapper.selectList(new EntityWrapper<TOrder>().eq("branchOfficeId", tBranchOfficeId));
        model.addAttribute("orderCount",orders.size());// 订单数量
        List<TOrder> effectiveOrder = orders.stream().filter(order -> order.getPayMoney().compareTo(new BigDecimal("15")) > 0).collect(Collectors.toList());
        model.addAttribute("effectiveOrderCount",effectiveOrder.size());// 有效订单
 
        // 优惠券数据,,,通过订单找到该区域的下单人,找出优惠券信息
        List<Integer> userIds = orders.stream().map(TOrder::getUserId).collect(Collectors.toList());
        List<TUserToCoupon> tUserToCoupons = tUserToCouponMapper.selectList(new EntityWrapper<TUserToCoupon>().eq("objectId", tBranchOfficeId)
                .eq("roleType",2));
        // 优惠券有效数量
        int validCount = tUserToCoupons.stream().mapToInt(TUserToCoupon::getValidCount).sum();
        // 过期数量
        int expireCount = tUserToCoupons.stream().mapToInt(TUserToCoupon::getExpireCount).sum();
        // 总数量
        int totalCount = tUserToCoupons.stream().mapToInt(TUserToCoupon::getCouponTotal).sum();
        // 已使用优惠券;总数量减去有效数量
        model.addAttribute("usedCount",totalCount-validCount-expireCount);
 
        // 已发放优惠券
        model.addAttribute("totalCount",totalCount);
 
        BigDecimal orderPriceCount = new BigDecimal("0");
 
        // 累计优惠券金额
        for (TUserToCoupon tUserToCoupon : tUserToCoupons) {
            TCoupon tCoupon = tCouponMapper.selectById(tUserToCoupon.getCouponId());
            BigDecimal price = tCoupon.getCouponPreferentialAmount().multiply(new BigDecimal(tUserToCoupon.getCouponTotal()));
            orderPriceCount = orderPriceCount.add(price);
        }
        model.addAttribute("orderPriceCount",orderPriceCount);
        // 司机数量
        Integer driverCount = tDriverMapper.selectCount(new EntityWrapper<TDriver>().eq("branchOfficeId", tBranchOfficeId));
        model.addAttribute("driverCount",driverCount);
    }
 
    @Override
    public List<TBranchOfficeResp> getPageList(String branchOfficeName, String principal, String principalPhone, Integer operatingBusiness, Integer status) {
        EntityWrapper<TBranchOffice> wrapper = new EntityWrapper<>();
        // 分公司名称
        if(StringUtils.hasLength(branchOfficeName)){
            wrapper.like("branchOfficeName",branchOfficeName);
        }
        // 负责人
        if(StringUtils.hasLength(principal)){
            wrapper.like("principal",principal);
        }
        // 负责人电话
        if(StringUtils.hasLength(principalPhone)){
            wrapper.like("principalPhone",principalPhone);
        }
        // 经营业务
        if(Objects.nonNull(operatingBusiness)){
            wrapper.eq("operatingBusiness",operatingBusiness);
        }
        // 状态
        if(Objects.nonNull(status)){
            wrapper.eq("status",status);
        }
        wrapper.ne("status", StatusEnum.DELETE.getCode());
        wrapper.orderBy("createTime",false);
        // 判断代理商 分公司
        Integer roleType = Objects.requireNonNull(ShiroKit.getUser()).getRoleType();
        Integer objectId = Objects.requireNonNull(ShiroKit.getUser()).getObjectId();
        if(2 == roleType){
            // 分公司
            wrapper.eq("id",objectId);
        }
        if(3 == roleType){
            // 代理商
            wrapper.eq("agentId",objectId);
        }
        List<TBranchOffice> tBranchOffices = tBranchOfficeMapper.selectList(wrapper);
        List<TBranchOfficeResp> tBranchOfficeRespList = new ArrayList<>(tBranchOffices.size());
        for (TBranchOffice tBranchOffice : tBranchOffices) {
            TBranchOfficeResp tBranchOfficeResp = new TBranchOfficeResp();
            BeanUtils.copyProperties(tBranchOffice,tBranchOfficeResp);
            tBranchOfficeRespList.add(tBranchOfficeResp);
        }
        return tBranchOfficeRespList;
    }
 
    @Override
    public void queryOtherInfo(List<TBranchOfficeResp> tBranchOfficeRespList) {
        List<TOrder> orders = tOrderMapper.selectList(new EntityWrapper<TOrder>());
 
        for (TBranchOfficeResp tBranchOfficeResp : tBranchOfficeRespList) {
 
            List<TOrder> orderList = orders.stream().filter(t -> tBranchOfficeResp.getId().equals(t.getBranchOfficeId())).collect(Collectors.toList());
 
            if(!CollectionUtils.isEmpty(orderList)){
 
                // 订单数据
                tBranchOfficeResp.setOrderCount(orderList.size());// 订单数量
                List<TOrder> effectiveOrder = orderList.stream().filter(order -> Objects.nonNull(order.getPayMoney()) && order.getPayMoney().compareTo(new BigDecimal("15")) > 0).collect(Collectors.toList());
                tBranchOfficeResp.setEffectiveOrderCount(effectiveOrder.size());// 有效订单
 
                // 优惠券数据,,,通过订单找到该区域的下单人,找出优惠券信息
                List<Integer> userIds = orderList.stream().map(TOrder::getUserId).collect(Collectors.toList());
                List<TUserToCoupon> tUserToCoupons = tUserToCouponMapper.selectList(new EntityWrapper<TUserToCoupon>().eq("objectId", tBranchOfficeResp.getId())
                        .eq("roleType",2));
                // 优惠券有效数量
                int validCount = tUserToCoupons.stream().mapToInt(TUserToCoupon::getValidCount).sum();
                // 过期数量
                int expireCount = tUserToCoupons.stream().mapToInt(TUserToCoupon::getExpireCount).sum();
                // 总数量
                int totalCount = tUserToCoupons.stream().mapToInt(TUserToCoupon::getCouponTotal).sum();
                // 已使用优惠券;总数量减去有效数量
                tBranchOfficeResp.setUsedCount(totalCount-validCount-expireCount);
 
                // 已发放优惠券
                tBranchOfficeResp.setTotalCount(totalCount);
 
                BigDecimal orderPriceCount = new BigDecimal("0");
 
                // 累计优惠券金额
                for (TUserToCoupon tUserToCoupon : tUserToCoupons) {
                    TCoupon tCoupon = tCouponMapper.selectById(tUserToCoupon.getCouponId());
                    BigDecimal price = tCoupon.getCouponPreferentialAmount().multiply(new BigDecimal(tUserToCoupon.getCouponTotal()));
                    orderPriceCount = orderPriceCount.add(price);
                }
                tBranchOfficeResp.setOrderPriceCount(orderPriceCount);
            }
            // 司机数量
            Integer driverCount = tDriverMapper.selectCount(new EntityWrapper<TDriver>().eq("branchOfficeId", tBranchOfficeResp.getId()));
            tBranchOfficeResp.setDriverCount(driverCount);
        }
 
    }
 
    @Override
    public Object addOrUpdate(TBranchOffice tBranchOffice) {
        // 对省市区做处理
        String[] split = tBranchOffice.getAreaId().split("/");
        // 查询省市
        // 黑龙江省/大兴安岭地区
        // 702/852
        TRegion province = tRegionService.selectById(split[0]);
        tBranchOffice.setProvinceName(province.getName());
        tBranchOffice.setProvinceCode(province.getCode());
 
        TRegion city = tRegionService.selectById(split[1]);
        if(Objects.isNull(city)){
            city = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("name",split[1]));
        }
        tBranchOffice.setCityName(city.getName());
        tBranchOffice.setCityCode(city.getCode());
 
        if(split.length>2){
            TRegion area = tRegionService.selectById(split[2]);
            if(Objects.isNull(area)){
                area = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("name",split[2]));
            }
            tBranchOffice.setDistrictName(area.getName());
            tBranchOffice.setDistrictCode(area.getCode());
        }else {
            tBranchOffice.setDistrictName("");
            tBranchOffice.setDistrictCode("");
        }
 
        // 通过省市查询代理商
        List<TAgent> tAgent = tAgentMapper.selectList(new EntityWrapper<TAgent>().eq("provinceCode", province.getCode())
                .eq("cityCode", city.getCode())
                .eq("status", 1)
                .last("LIMIT 1"));
        if(!CollectionUtils.isEmpty(tAgent)){
            tBranchOffice.setAgentId(tAgent.get(0).getId());
        }else {
            return new SuccessTip(500, "该区域代理商被冻结或不存在");
        }
        return null;
    }
}