张天森
2022-07-08 5f5ed6db2a76e626b8d11d60a09db22b238388a0
springcloud_k8s_panzhihuazhihuishequ/service_user/src/main/java/com/panzhihua/service_user/service/impl/UserServiceImpl.java
@@ -4,12 +4,18 @@
import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.common.model.dtos.community.microCommercialStreet.BindUserPhoneDTO;
@@ -146,6 +152,11 @@
    private SysAppConfigDao sysAppConfigDao;
    @Resource
    private SysTemplateConfigDao sysTemplateConfigDao;
    public static final Integer ALLCOUNTY=1;
    public static final Integer ALLSTREET=2;
    public static final Integer ALLCOMMUNITY=3;
    public static final Integer BYSTREET=4;
    // @Resource
    // private GridService gridService;
@@ -3288,4 +3299,181 @@
        }
        return R.fail("信息错误");
    }
    @Override
    public R checkExport(String account, String password,String oldPassword) {
        if(new BCryptPasswordEncoder().matches(password, oldPassword)){
            return R.ok();
        }
        return R.fail();
    }
    @Override
    public R newIndexData(Integer type, Long streetId) {
        IndexDataKanbanVO indexDataKanbanVO = userDao.selectIndexDataKanban(null);
        IndexDataAnalysisVO vo=new IndexDataAnalysisVO();
        if (ObjectUtils.isEmpty(type)){
            //不传默认为所有区县
            type=ALLCOUNTY;
        }
        if (type.equals(ALLCOUNTY) || type.equals(ALLCOMMUNITY) || type.equals(ALLSTREET)){
            BeanUtils.copyProperties(indexDataKanbanVO,vo);
        }else {
            IndexDataKanbanVO indexDataVO = userDao.dataKanBanStreet(streetId);
            BeanUtils.copyProperties(indexDataVO,vo);
        }
            List<UserProportion> userProportionsTotal = userDao.userAnalysis(type, null, streetId);
            Integer streetUser = userDao.streetUserCount(streetId);
            for (UserProportion userProportion : userProportionsTotal) {
                if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(userProportion)){
                    if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(userProportion.getUser())){
                        //计算占比
                        //userProportion.setAreaCode(null);
                        if (type.equals(BYSTREET)){
                            userProportion.setRate(getRate(streetUser,userProportion.getUser()));
                        }
                        userProportion.setRate(getRate(vo.getAllUser(),userProportion.getUser()));
                    }
                }
            }
            List<UserProportion> userProportionsMonth = userDao.userAnalysis(type, DateUtil.beginOfMonth(new Date()).toString(), streetId);
            for (UserProportion userProportion : userProportionsMonth) {
                if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(userProportion)){
                    if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(userProportion.getUser())){
                        //计算占比
                        //userProportion.setAreaCode(null);
                        if (type.equals(BYSTREET)){
                            userProportion.setRate(getRate(streetUser,userProportion.getUser()));
                        }
                        userProportion.setRate(getRate(vo.getAllUser(),userProportion.getUser()));
                    }
                }
            }
            vo.setUserProportionTotal(userProportionsTotal);
            vo.setUserProportionMonth(userProportionsMonth);
            List<RealUser> realUsersTotal = userDao.realUserCount(type, null, streetId);
            List<RealUser> realUsersMonth = userDao.realUserCount(type, DateUtil.beginOfMonth(new Date()).toString(), streetId);
            vo.setRealUsersTotal(realUsersTotal);
            vo.setRealUsersMonth(realUsersMonth);
            vo.setRealUser(getStreetRealUser(realUsersTotal));
            vo.setRealUserRate(getRate(vo.getAllUser(),vo.getRealUser()));
            vo.setUserActivityXAll(getTwentyDays());
            vo.setUserActivityXMonth(getMonthTwentyDays());
            List<String> addUserYAll=new ArrayList<>();
            List<String> addUserYMonth=new ArrayList<>();
            List<String> activeUserYAll=new ArrayList<>();
            List<String> activeUserYMonth=new ArrayList<>();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            //获取用户行为Y轴
            for (Date date : vo.getUserActivityXAll()) {
                //查询当天的新增和日活
                Map<String, Long> map = userDao.getDailyAddAndActive(DateUtil.parse(sdf.format(date)), streetId);
                addUserYAll.add(map.get("addUser").toString());
                activeUserYAll.add(map.get("activeUser").toString());
            }
            for (Date date : vo.getUserActivityXMonth()) {
                Map<String, Long> map = userDao.getDailyAddAndActive(DateUtil.parse(sdf.format(date)), streetId);
                addUserYMonth.add(map.get("addUser").toString());
                activeUserYMonth.add(map.get("activeUser").toString());
            }
            vo.setAddUserYAll(addUserYAll);
            vo.setAddUserYWeek(addUserYMonth);
            vo.setActiveUserYAll(activeUserYAll);
            vo.setActiveUserYMonth(activeUserYMonth);
            if (type.equals(BYSTREET) || type.equals(ALLCOMMUNITY) || type.equals(ALLSTREET)){
                //获取其他用户
                vo.setOtherUser(indexDataKanbanVO.getAllUser()-countUser(vo.getUserProportionTotal()));
                vo.setOtherUserRate(getRate(indexDataKanbanVO.getAllUser(),vo.getOtherUser()));
            }
        return R.ok(vo);
    }
    /**
     * 获取最近20天数据
     *
     * @return
     */
    private List<Date> getTwentyDays() {
        List<Date> dates = new ArrayList<>();
        Date date = new Date();
        for (int i = 19; i >= 0; i--) {
            Date date1 = DateUtils.addDays(date, -i);
            dates.add(date1);
        }
        return dates;
    }
    /**
     * 获取最近本月20天数据
     *
     * @return
     */
    private List<Date> getMonthTwentyDays() {
        List<Date> dates = new ArrayList<>();
        Date date = new Date();
        Long offset = DateUtil.between(new Date(), DateUtil.beginOfMonth(date), DateUnit.DAY);
        if (offset>=20){
            for (int i = 19; i >= 0; i--) {
                Date date1 = DateUtils.addDays(date, -i);
                dates.add(date1);
            }
        }else{
            //距离月初不足20天
            for (int i = offset.intValue(); i >= 0; i--) {
                Date date1 = DateUtils.addDays(date, -i);
                dates.add(date1);
            }
        }
        return dates;
    }
    /**
     * 计算占比
     * */
    private Double getRate(Integer total,Integer count){
        if (0 != total && null != total){
            if (0 != count && null != count){
                BigDecimal div = NumberUtil.div(count.toString(), total.toString(),2);
                return div.doubleValue();
            }
        }
        return null;
    }
    /**
     * 计算街道实名用户总数
     * */
    private Integer getStreetRealUser(List<RealUser> list){
        Integer count=0;
        if (list.size()>0){
            for (RealUser realUser : list) {
                if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(realUser)){
                    if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(realUser.getCount())){
                        count+=realUser.getCount();
                    }
                }
            }
        }
        return count;
    }
    /**
     * 计算当前统计范围内的所有用户数
     * */
    public Integer countUser(List<UserProportion> list){
        Integer count=0;
        if (list.size()>0){
            for (UserProportion proportion : list) {
                if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(proportion)){
                    if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(proportion.getUser())){
                        count+=proportion.getUser();
                    }
                }
            }
        }
        return count;
    }
}