puzhibing
2023-08-16 d5b3e5a413bcfccba294793ee093722f31b2448a
management/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/TBranchOfficeServiceImpl.java
@@ -1,16 +1,31 @@
package com.stylefeng.guns.modular.system.service.impl;
import com.stylefeng.guns.modular.system.dao.TCouponMapper;
import com.stylefeng.guns.modular.system.dao.TOrderMapper;
import com.stylefeng.guns.modular.system.dao.TSystemConfigMapper;
import com.stylefeng.guns.modular.system.model.TBranchOffice;
import com.stylefeng.guns.modular.system.dao.TBranchOfficeMapper;
import com.stylefeng.guns.modular.system.model.TSystemConfig;
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>
@@ -28,17 +43,207 @@
    @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",tBranchOffice.getCreateTime());
        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());
            List<TUserToCoupon> tUserToCoupons = tUserToCouponMapper.selectList(new EntityWrapper<TUserToCoupon>().eq("objectId", tBranchOfficeResp.getId())
                    .eq("roleType",2));
            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());
            }
            // 优惠券有效数量
            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 = BigDecimal.ZERO;
            // 累计优惠券金额
            for (TUserToCoupon tUserToCoupon : tUserToCoupons) {
                TCoupon tCoupon = tCouponMapper.selectById(tUserToCoupon.getCouponId());
                if(Objects.nonNull(tCoupon)){
                    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(city.getName());
            tBranchOffice.setDistrictCode(city.getCode());
        }*/
        // 通过省市查询代理商
        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;
    }
}