package com.gree.mall.manager.logic.big;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.big.*;
import com.gree.mall.manager.enums.IsYesNoEnum;
import com.gree.mall.manager.enums.UserTypeEnum;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.*;
import com.gree.mall.manager.utils.HttpUtils;
import com.gree.mall.manager.utils.RedisUtil;
import com.gree.mall.manager.utils.StringUtil;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class BigLogic {

    @Autowired
    LbsAmapService lbsAmapService;

    @Autowired
    AdminCompanyWechatService adminCompanyWechatService;

    @Autowired
    UserLogService userLogService;

    @Autowired
    UserService userService;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    PgOrderBaseService pgOrderBaseService;

    @Autowired
    PgOrderProductService pgOrderProductService;

    @Autowired
    OrderInfoService orderInfoService;

    @Autowired
    PgIncreOrderService pgIncreOrderService;

    @Autowired
    WebsitSalesService websitSalesService;

    @Autowired
    WebsitSalesRetService websitSalesRetService;

    @Autowired
    WebsitPartsRetService websitPartsRetService;

    @Autowired
    WebsitPartsRetItemService websitPartsRetItemService;

    @Autowired
    OrderSmallTypeService orderSmallTypeService;

    @Autowired
    SettlementOrderService settlementOrderService;

    @Autowired
    AdminUserService adminUserService;

    @Autowired
    RedisUtil redisUtil;


    public String getRegion(String country, String province, String city, String area) {

        if (!StringUtil.isEmpty(area)) {
            LbsAmap areaV = lbsAmapService.lambdaQuery().eq(LbsAmap::getProvinceName, province)
                    .eq(LbsAmap::getCityName, city)
                    .eq(LbsAmap::getAreaName, area).last("limit 1").one();
            return HttpUtil.get("https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=" + areaV.getAdCode() + "_full");
        }


        if (!StringUtil.isEmpty(city)) {
            LbsAmap areaV = lbsAmapService.lambdaQuery().eq(LbsAmap::getProvinceName, province)
                    .eq(LbsAmap::getCityName, city)
                    .last("limit 1").one();
            return HttpUtil.get("https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=" + areaV.getAdCode() + "_full");
        }


        if (!StringUtil.isEmpty(province)) {
            LbsAmap areaV = lbsAmapService.lambdaQuery().eq(LbsAmap::getProvinceName, province)
                    .last("limit 1").one();
            return HttpUtil.get("https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=" + areaV.getAdCode() + "_full");
        }


        if (!StringUtil.isEmpty(country)) {

            return HttpUtil.get("https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=100000_full");
        }

        return null;
    }

    public LargeOneBean getLarge1(String province) {
        List<AdminCompanyWechat> adminCompanyWechatList = adminCompanyWechatService.lambdaQuery()
                .eq(!StringUtil.isEmpty(province), AdminCompanyWechat::getProvince, province)
                .select(AdminCompanyWechat::getCompanyWechatId).list();

        List<String> companyIds = adminCompanyWechatList.stream().map(AdminCompanyWechat::getCompanyWechatId).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(adminCompanyWechatList))
            return new LargeOneBean();

        Integer loginComapnyNum = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.YES.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(StringUtil.isEmpty(province) && !CollectionUtils.isEmpty(adminCompanyWechatList), UserLog::getCompanyWechatId, companyIds)
                .isNotNull(UserLog::getCompanyWechatId)
                .between(UserLog::getCreateTime, DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date())).count();

        Integer loginComapnyNumBefore = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.YES.getKey())
                .in(StringUtil.isEmpty(province) && !CollectionUtils.isEmpty(adminCompanyWechatList), UserLog::getCompanyWechatId, companyIds)
                .eq(UserLog::getType, "LOGIN")
                .isNotNull(UserLog::getCompanyWechatId)
                .between(UserLog::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -1), DateUtil.offsetDay(DateUtil.endOfDay(new Date()), -1))
                .count();

        Integer allWorker = userService.lambdaQuery().eq(User::getType, UserTypeEnum.WORKER.getKey())
                .in(StringUtil.isEmpty(province) && !CollectionUtils.isEmpty(adminCompanyWechatList), User::getCompanyWechatId, companyIds)
                .count();


        Integer allWorkerLogin = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.NO.getKey())
                .eq(UserLog::getMiniType, UserTypeEnum.WORKER.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(StringUtil.isEmpty(province) && !CollectionUtils.isEmpty(adminCompanyWechatList), UserLog::getCompanyWechatId, companyIds)
                .between(UserLog::getCreateTime, DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date())).count();

        Integer allWorkerLoginBefore = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.NO.getKey())
                .eq(UserLog::getMiniType, UserTypeEnum.WORKER.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(StringUtil.isEmpty(province) && !CollectionUtils.isEmpty(adminCompanyWechatList), UserLog::getCompanyWechatId, companyIds)
                .between(UserLog::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -1), DateUtil.offsetDay(DateUtil.endOfDay(new Date()), -1)).count();


        Integer allService = userService.lambdaQuery().eq(User::getType, UserTypeEnum.SERVICE.getKey())
                .in(StringUtil.isEmpty(province) && !CollectionUtils.isEmpty(adminCompanyWechatList), User::getCompanyWechatId, companyIds).count();


        Integer allServiceLogin = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.NO.getKey())
                .eq(UserLog::getMiniType, UserTypeEnum.SERVICE.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(StringUtil.isEmpty(province) && !CollectionUtils.isEmpty(adminCompanyWechatList), UserLog::getCompanyWechatId, companyIds)
                .between(UserLog::getCreateTime, DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date())).count();

        Integer allServiceBefore = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.NO.getKey())
                .eq(UserLog::getMiniType, UserTypeEnum.SERVICE.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(StringUtil.isEmpty(province) && !CollectionUtils.isEmpty(adminCompanyWechatList), UserLog::getCompanyWechatId, companyIds)
                .between(UserLog::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -1), DateUtil.offsetDay(DateUtil.endOfDay(new Date()), -1)).count();


        Integer allUser = userService.lambdaQuery().eq(User::getType, UserTypeEnum.GENERAL.getKey())
                .in(StringUtil.isEmpty(province) && !CollectionUtils.isEmpty(adminCompanyWechatList), User::getCompanyWechatId, companyIds).count();


        Integer allUserLogin = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.NO.getKey())
                .eq(UserLog::getMiniType, UserTypeEnum.GENERAL.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(StringUtil.isEmpty(province) && !CollectionUtils.isEmpty(adminCompanyWechatList), UserLog::getCompanyWechatId, companyIds)
                .between(UserLog::getCreateTime, DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date())).count();

        Integer allUserBefore = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.NO.getKey())
                .eq(UserLog::getMiniType, UserTypeEnum.GENERAL.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(StringUtil.isEmpty(province) && !CollectionUtils.isEmpty(adminCompanyWechatList), UserLog::getCompanyWechatId, companyIds)
                .between(UserLog::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -1), DateUtil.offsetDay(DateUtil.endOfDay(new Date()), -1)).count();

        LargeOneBean largeOneBean = new LargeOneBean();
        largeOneBean.setCompanyNum(adminCompanyWechatList.size());
        largeOneBean.setLoginCompanyNum(loginComapnyNum);
        BigDecimal cPer = loginComapnyNumBefore == 0 ? Convert.toBigDecimal(loginComapnyNum).multiply(Convert.toBigDecimal("100")) :
                Convert.toBigDecimal(loginComapnyNum).divide(Convert.toBigDecimal(loginComapnyNumBefore), 3, BigDecimal.ROUND_HALF_UP).multiply(Convert.toBigDecimal("100"));
        largeOneBean.setLoginCompanyNumPer(cPer);

        largeOneBean.setAllWorker(allWorker);
        largeOneBean.setLoginAllWorker(allWorkerLogin);
        BigDecimal wPer = allWorkerLoginBefore == 0 ? Convert.toBigDecimal(allWorkerLogin).multiply(Convert.toBigDecimal("100")) :
                Convert.toBigDecimal(allWorkerLogin).divide(Convert.toBigDecimal(allWorkerLoginBefore), 3, BigDecimal.ROUND_HALF_UP).multiply(Convert.toBigDecimal("100"));
        largeOneBean.setLoginAllWorkerPer(wPer);

        largeOneBean.setAllService(allService);
        largeOneBean.setLoginAllService(allServiceLogin);
        BigDecimal sPer = allServiceBefore == 0 ? Convert.toBigDecimal(allServiceBefore).multiply(Convert.toBigDecimal("100")) :
                Convert.toBigDecimal(allServiceLogin).divide(Convert.toBigDecimal(allServiceBefore), 3, BigDecimal.ROUND_HALF_UP).multiply(Convert.toBigDecimal("100"));
        largeOneBean.setLoginAllServicePer(sPer);


        largeOneBean.setAllUser(allUser);
        largeOneBean.setLoginAllUser(allUserLogin);
        BigDecimal uPer = allUserBefore == 0 ? Convert.toBigDecimal(allUserBefore).multiply(Convert.toBigDecimal("100")) :
                Convert.toBigDecimal(allUserLogin).divide(Convert.toBigDecimal(allUserBefore), 3, BigDecimal.ROUND_HALF_UP).multiply(Convert.toBigDecimal("100"));
        largeOneBean.setLoginAllUserPer(uPer);

        if (!StringUtil.isEmpty(province)) {

            List<String> orderBaseId = pgOrderProductService.lambdaQuery().select(PgOrderProduct::getOrderBaseId)
                    .list().stream().map(PgOrderProduct::getOrderBaseId).collect(Collectors.toList());

            List<PgOrderBase> pgOrderBases = pgOrderBaseService.lambdaQuery().in(PgOrderBase::getId, orderBaseId).select(PgOrderBase::getOrderStatus, PgOrderBase::getId).list();

            List<String> never = new ArrayList<>();
            never.add("YWG");
            never.add("YJS");
            never.add("YQX");

            largeOneBean.setAllWorkerOrderNum(pgOrderBases.size());
            int neverNum = pgOrderBases.stream().filter(item -> never.contains(item.getOrderStatus())).collect(Collectors.toList()).size();
            largeOneBean.setNeverWorkerNum(neverNum);

            Integer count = orderInfoService.lambdaQuery().count();
            largeOneBean.setSalesOrderNum(count);

            Integer count1 = pgIncreOrderService.lambdaQuery().count();

            largeOneBean.setIncreOrderNum(count1);
        }


        return largeOneBean;
    }

    public LargeOneBean getLarge2() {

        AdminUserCom adminUser = commonLogic.getAdminUser();


        List<String> companyIds = adminUser.getCompanyWechatIds();



        Integer loginComapnyNum = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.YES.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(!CollectionUtils.isEmpty(companyIds), UserLog::getCompanyWechatId, companyIds)
                .isNotNull(UserLog::getCompanyWechatId)
                .between(UserLog::getCreateTime, DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date())).count();

        Integer loginComapnyNumBefore = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.YES.getKey())
                .in(!CollectionUtils.isEmpty(companyIds), UserLog::getCompanyWechatId, companyIds)
                .eq(UserLog::getType, "LOGIN")
                .isNotNull(UserLog::getCompanyWechatId)
                .between(UserLog::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -1), DateUtil.offsetDay(DateUtil.endOfDay(new Date()), -1))
                .count();

        Integer allWorker = userService.lambdaQuery().eq(User::getType, UserTypeEnum.WORKER.getKey())
                .in(!CollectionUtils.isEmpty(companyIds), User::getCompanyWechatId, companyIds)
                .count();


        Integer allWorkerLogin = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.NO.getKey())
                .eq(UserLog::getMiniType, UserTypeEnum.WORKER.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(!CollectionUtils.isEmpty(companyIds), UserLog::getCompanyWechatId, companyIds)
                .between(UserLog::getCreateTime, DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date())).count();

        Integer allWorkerLoginBefore = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.YES.getKey())
                .eq(UserLog::getMiniType, UserTypeEnum.WORKER.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(!CollectionUtils.isEmpty(companyIds), UserLog::getCompanyWechatId, companyIds)
                .between(UserLog::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -1), DateUtil.offsetDay(DateUtil.endOfDay(new Date()), -1)).count();


        Integer allService = userService.lambdaQuery().eq(User::getType, UserTypeEnum.SERVICE.getKey())
                .in(!CollectionUtils.isEmpty(companyIds), User::getCompanyWechatId, companyIds).count();


        Integer allServiceLogin = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.NO.getKey())
                .eq(UserLog::getMiniType, UserTypeEnum.SERVICE.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(!CollectionUtils.isEmpty(companyIds), UserLog::getCompanyWechatId, companyIds)
                .between(UserLog::getCreateTime, DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date())).count();

        Integer allServiceBefore = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.YES.getKey())
                .eq(UserLog::getMiniType, UserTypeEnum.SERVICE.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(!CollectionUtils.isEmpty(companyIds), UserLog::getCompanyWechatId, companyIds)
                .between(UserLog::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -1), DateUtil.offsetDay(DateUtil.endOfDay(new Date()), -1)).count();


        Integer allUser = userService.lambdaQuery().eq(User::getType, UserTypeEnum.GENERAL.getKey())
                .in(!CollectionUtils.isEmpty(companyIds), User::getCompanyWechatId, companyIds).count();


        Integer allUserLogin = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.NO.getKey())
                .eq(UserLog::getMiniType, UserTypeEnum.GENERAL.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(!CollectionUtils.isEmpty(companyIds), UserLog::getCompanyWechatId, companyIds)
                .between(UserLog::getCreateTime, DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date())).count();

        Integer allUserBefore = userLogService.lambdaQuery().eq(UserLog::getLoginType, IsYesNoEnum.YES.getKey())
                .eq(UserLog::getMiniType, UserTypeEnum.GENERAL.getKey())
                .eq(UserLog::getType, "LOGIN")
                .in(!CollectionUtils.isEmpty(companyIds), UserLog::getCompanyWechatId, companyIds)
                .between(UserLog::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -1), DateUtil.offsetDay(DateUtil.endOfDay(new Date()), -1)).count();

        LargeOneBean largeOneBean = new LargeOneBean();
        largeOneBean.setCompanyNum(companyIds.size());
        largeOneBean.setLoginCompanyNum(loginComapnyNum);
        BigDecimal cPer = loginComapnyNumBefore == 0 ? Convert.toBigDecimal(loginComapnyNum).multiply(Convert.toBigDecimal("100")) :
                Convert.toBigDecimal(loginComapnyNum).divide(Convert.toBigDecimal(loginComapnyNumBefore), 3, BigDecimal.ROUND_HALF_UP)
                        .multiply(Convert.toBigDecimal("100"));
        largeOneBean.setLoginCompanyNumPer(cPer);

        largeOneBean.setAllWorker(allWorker);
        largeOneBean.setLoginAllWorker(allWorkerLogin);
        BigDecimal wPer = allWorkerLoginBefore == 0 ? Convert.toBigDecimal(allWorkerLogin).multiply(Convert.toBigDecimal("100")) :
                Convert.toBigDecimal(allWorkerLogin).divide(Convert.toBigDecimal(allWorkerLoginBefore), 3, BigDecimal.ROUND_HALF_UP)
                        .multiply(Convert.toBigDecimal("100"));
        largeOneBean.setLoginAllWorkerPer(wPer);

        largeOneBean.setAllService(allService);
        largeOneBean.setLoginAllService(allServiceLogin);
        BigDecimal sPer = allServiceBefore == 0 ? Convert.toBigDecimal(allServiceBefore).multiply(Convert.toBigDecimal("100")) :
                Convert.toBigDecimal(allServiceLogin).divide(Convert.toBigDecimal(allServiceBefore), 3, BigDecimal.ROUND_HALF_UP)
                        .multiply(Convert.toBigDecimal("100"));
        largeOneBean.setLoginAllServicePer(sPer);


        largeOneBean.setAllUser(allUser);
        largeOneBean.setLoginAllUser(allUserLogin);
        BigDecimal uPer = allUserBefore == 0 ? Convert.toBigDecimal(allUserBefore).multiply(Convert.toBigDecimal("100")) :
                Convert.toBigDecimal(allUserLogin).divide(Convert.toBigDecimal(allUserBefore), 3, BigDecimal.ROUND_HALF_UP)
                        .multiply(Convert.toBigDecimal("100"));
        largeOneBean.setLoginAllUserPer(uPer);



        return largeOneBean;
    }

    public List<largeSeeLogin> getLarge3() {

        AdminUserCom adminUser = commonLogic.getAdminUser();

        DateTime dateTime = DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -30);

        List<UserLog> userLogs = userLogService.lambdaQuery().between(UserLog::getCreateTime, dateTime, new Date())
                .eq(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), UserLog::getCompanyWechatId, adminUser.getCompanyWechatId())
                .select(UserLog::getType, UserLog::getCreateTime).list();

        List<largeSeeLogin> largeSeeLogins = new ArrayList<>();

        for (int i = 29; i > -1; i--) {

            largeSeeLogin largeSeeLogin = new largeSeeLogin();

            DateTime startTime = DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -1 * i);
            DateTime endTime = DateUtil.endOfDay(startTime);
            List<UserLog> userloginList = userLogs.stream().filter(item -> item.getCreateTime().before(endTime) && item.getCreateTime().after(startTime)
                    && item.getType().equals("LOGIN")
            ).collect(Collectors.toList());

            List<UserLog> userSeeList = userLogs.stream().filter(item -> item.getCreateTime().before(endTime) && item.getCreateTime().after(startTime)
                    && item.getType().equals("SEE")
            ).collect(Collectors.toList());

            String format = DateUtil.format(startTime, "yyyy-MM-dd");

            largeSeeLogin.setDStr(format);
            largeSeeLogin.setNum(userSeeList.size());
            largeSeeLogin.setUserNum(userloginList.size());

            largeSeeLogins.add(largeSeeLogin);


        }

        return largeSeeLogins;
    }

    public LargeMpBean getLarge4() {

        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<WebsitSales> websitSales = websitSalesService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), WebsitSales::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(WebsitSales::getPayFlag, "YES")
                .ne(WebsitSales::getFlag, "IMPORT_ERR")
                .between(WebsitSales::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -30), new Date())
                .select(WebsitSales::getSalesId, WebsitSales::getGoodsType, WebsitSales::getPayFlag).list();
        LargeMpBean largeMpBean = new LargeMpBean();

        largeMpBean.setFcxs(Convert.toInt(websitSales.stream().filter(item ->
                item.getGoodsType().equals("M")
        ).count()));

        largeMpBean.setPjxs(Convert.toInt(websitSales.stream().filter(item ->
                item.getGoodsType().equals("P")
        ).count()));


        List<WebsitSalesRet> websitSalesRets = websitSalesRetService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), WebsitSalesRet::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(WebsitSalesRet::getRetState, "OK")
                .between(WebsitSalesRet::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -30), new Date())
                .select(WebsitSalesRet::getSalesRetId, WebsitSalesRet::getGoodsType, WebsitSalesRet::getRetState).list();

        largeMpBean.setFcth(Convert.toInt(websitSalesRets.stream().filter(item ->
                item.getGoodsType().equals("M")
        ).count()));


        largeMpBean.setPjth(Convert.toInt(websitSalesRets.stream().filter(item ->
                item.getGoodsType().equals("P")
        ).count()));


        List<WebsitPartsRet> websitPartsRets = websitPartsRetService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), WebsitPartsRet::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(WebsitPartsRet::getFlag, "SUBMIT")
                .between(WebsitPartsRet::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -30), new Date())
                .select(WebsitPartsRet::getFlag, WebsitPartsRet::getGoodsType, WebsitPartsRet::getPartsRetId, WebsitPartsRet::getType)
                .list();

        List<String> partRetIds = websitPartsRets.stream().map(WebsitPartsRet::getPartsRetId).collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(partRetIds)) {

            List<WebsitPartsRetItem> websitPartsRetItemList = websitPartsRetItemService.lambdaQuery()
                    .in(WebsitPartsRetItem::getPartsRetId, partRetIds)
                    .select(WebsitPartsRetItem::getNewRefundQty, WebsitPartsRetItem::getOldRefundQty, WebsitPartsRetItem::getPartsRetId).list();


            List<String> partRetNewIds = websitPartsRets.stream()
                    .filter(item -> item.getType().equals("NEW"))
                    .map(WebsitPartsRet::getPartsRetId).collect(Collectors.toList());

            largeMpBean.setXjdfh(websitPartsRetItemList.stream().filter(item -> partRetNewIds.contains(item.getPartsRetId()))
                    .map(WebsitPartsRetItem::getNewRefundQty).reduce(BigDecimal::add).orElse(Convert.toBigDecimal("0"))
            );

            List<String> partRetOldIds = websitPartsRets.stream()
                    .filter(item -> item.getType().equals("OLD"))
                    .map(WebsitPartsRet::getPartsRetId).collect(Collectors.toList());

            largeMpBean.setJjdfh(websitPartsRetItemList.stream().filter(item -> partRetOldIds.contains(item.getPartsRetId()))
                    .map(WebsitPartsRetItem::getOldRefundQty).reduce(BigDecimal::add).orElse(Convert.toBigDecimal("0"))
            );


            List<String> partRetFaIds = websitPartsRets.stream()
                    .filter(item -> item.getType().equals("FACTORY"))
                    .map(WebsitPartsRet::getPartsRetId).collect(Collectors.toList());

            largeMpBean.setJjdfc(websitPartsRetItemList.stream().filter(item -> partRetFaIds.contains(item.getPartsRetId()))
                    .map(WebsitPartsRetItem::getOldRefundQty).reduce(BigDecimal::add).orElse(Convert.toBigDecimal("0"))
            );

        }


        return largeMpBean;

    }

    public LargeWorkerOrderBean getLarge5() {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        LargeWorkerOrderBean largeWorkerOrderBean = new LargeWorkerOrderBean();

        List<PgOrderBase> pgOrderBases = pgOrderBaseService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), PgOrderBase::getCompanyWechatId, adminUser.getCompanyWechatId())
                .ne(PgOrderBase::getOrderStatus, "YQX")
                .between(PgOrderBase::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -30), new Date())
                .select(PgOrderBase::getOrderSmallTypeText, PgOrderBase::getId, PgOrderBase::getTotalNum, PgOrderBase::getUndoneNum,
                        PgOrderBase::getSaleType).list();

        if (CollectionUtils.isEmpty(pgOrderBases))
            return new LargeWorkerOrderBean();


        Integer integer = pgOrderBases.stream().map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0);

        largeWorkerOrderBean.setAzgd(pgOrderBases.stream().filter(item -> item.getOrderSmallTypeText().equals("安装"))
                .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
        );


        largeWorkerOrderBean.setAzgdzb(Convert.toBigDecimal(largeWorkerOrderBean.getAzgd()).divide(Convert.toBigDecimal(integer), 3, BigDecimal.ROUND_HALF_UP)
                .multiply(Convert.toBigDecimal("100"))
        );

        largeWorkerOrderBean.setWxgd(pgOrderBases.stream().filter(item -> item.getOrderSmallTypeText().equals("维修")
                        && (item.getSaleType().equals(1) || item.getSaleType().equals(2))
                )
                        .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
        );


        largeWorkerOrderBean.setWxgdzb(Convert.toBigDecimal(largeWorkerOrderBean.getWxgd()).divide(Convert.toBigDecimal(integer), 3, BigDecimal.ROUND_HALF_UP)
                .multiply(Convert.toBigDecimal("100"))
        );


        largeWorkerOrderBean.setQxgd(pgOrderBases.stream().filter(item -> item.getOrderSmallTypeText().equals("清洗")
                )
                        .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
        );


        largeWorkerOrderBean.setQxgdzb(Convert.toBigDecimal(largeWorkerOrderBean.getQxgd()).divide(Convert.toBigDecimal(integer), 3, BigDecimal.ROUND_HALF_UP)
                .multiply(Convert.toBigDecimal("100"))
        );


        largeWorkerOrderBean.setZzgd(pgOrderBases.stream().filter(item -> item.getSaleType().equals(3)
                )
                        .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
        );


        largeWorkerOrderBean.setZzgdzb(Convert.toBigDecimal(largeWorkerOrderBean.getZzgd()).divide(Convert.toBigDecimal(integer), 3, BigDecimal.ROUND_HALF_UP)
                .multiply(Convert.toBigDecimal("100"))
        );


        largeWorkerOrderBean.setWbgd(pgOrderBases.stream().filter(item -> item.getSaleType().equals(4)
                )
                        .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
        );


        largeWorkerOrderBean.setWbgdzb(Convert.toBigDecimal(largeWorkerOrderBean.getWbgd()).divide(Convert.toBigDecimal(integer), 3, BigDecimal.ROUND_HALF_UP)
                .multiply(Convert.toBigDecimal("100"))
        );

        return largeWorkerOrderBean;

    }

    public List<LargeWorkerOrderDateBean> getLarge6() {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<OrderSmallType> orderSmallTypes = orderSmallTypeService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), OrderSmallType::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(OrderSmallType::getStatus, true)
                .groupBy(OrderSmallType::getOrderSmallTypeText).list();


        List<PgOrderBase> pgOrderBases = pgOrderBaseService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), PgOrderBase::getCompanyWechatId, adminUser.getCompanyWechatId())
                .ne(PgOrderBase::getOrderStatus, "YQX")
                .between(PgOrderBase::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -30), new Date())
                .select(PgOrderBase::getOrderSmallTypeText, PgOrderBase::getId, PgOrderBase::getTotalNum, PgOrderBase::getUndoneNum, PgOrderBase::getCreateTime,
                        PgOrderBase::getSaleType).list();

        List<LargeWorkerOrderDateBean> largeWorkerOrderDateBeans = new ArrayList<>();

        List<String> status = new ArrayList<>();
        status.add("YWG");
        status.add("YJS");


        for (OrderSmallType orderSmallType : orderSmallTypes) {

            LargeWorkerOrderDateBean largeWorkerOrderDateBean = new LargeWorkerOrderDateBean();
            largeWorkerOrderDateBean.setLx(orderSmallType.getOrderSmallTypeText());

            List<Integer> unitNum = new ArrayList<>();
            List<Integer> unitNumWwg = new ArrayList<>();

            for (int i = 29; i > -1; i--) {

                DateTime startDay = DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -1 * i));

                DateTime endDay = DateUtil.endOfDay(startDay);

                Integer integer = pgOrderBases.stream().filter(item -> item.getCreateTime().before(endDay)
                        && item.getCreateTime().after(startDay) && status.contains(item.getOrderStatus())
                        && item.getOrderSmallTypeText().equals(largeWorkerOrderDateBean.getLx())
                )
                        .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0);


                Integer integerWwg = pgOrderBases.stream().filter(item -> item.getCreateTime().before(endDay)
                        && item.getCreateTime().after(startDay) && !status.contains(item.getOrderStatus())
                        && item.getOrderSmallTypeText().equals(largeWorkerOrderDateBean.getLx())
                )
                        .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0);

                unitNum.add(integer);
                unitNumWwg.add(integerWwg);
            }

            largeWorkerOrderDateBean.setUnitNum(unitNum);
            largeWorkerOrderDateBean.setUnitNumWwg(unitNumWwg);

            largeWorkerOrderDateBeans.add(largeWorkerOrderDateBean);

        }

        return largeWorkerOrderDateBeans;

    }

    public LargeOrderBean getLarge7() {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<OrderInfo> orderInfoList = orderInfoService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), OrderInfo::getCompanyWechatId, adminUser.getCompanyWechatId())
                .notIn(OrderInfo::getOrderStatus, "NOPAY", "DQR", "CLOSE", "TIMEOUT")
                .between(OrderInfo::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -30), new Date())
                .select(OrderInfo::getOrderId, OrderInfo::getPayAmount).list();

        LargeOrderBean largeOrderBean = new LargeOrderBean();
        largeOrderBean.setSxdds(orderInfoList.size());
        largeOrderBean.setSxddje(orderInfoList.stream().map(OrderInfo::getPayAmount).reduce(BigDecimal::add).orElse(Convert.toBigDecimal("0")));


        List<SettlementOrder> settlementOrderList = settlementOrderService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), SettlementOrder::getCompanyWechatId, adminUser.getCompanyWechatId())
                .between(SettlementOrder::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -30), new Date())
                .select(SettlementOrder::getOrderId, SettlementOrder::getTotalAmount, SettlementOrder::getGoodsType)
                .list();

        List<SettlementOrder> settlementOrderList1 = settlementOrderList.stream().filter(item -> item.getGoodsType().equals("M"))
                .collect(Collectors.toList());
        largeOrderBean.setFcdds(settlementOrderList1.size()
        );
        largeOrderBean.setFcddje(settlementOrderList1.stream()
                .filter(item -> item.getGoodsType().equals("M"))
                .map(SettlementOrder::getTotalAmount).reduce(BigDecimal::add).orElse(Convert.toBigDecimal("0")));


        List<SettlementOrder> settlementOrderListP = settlementOrderList.stream().filter(item -> item.getGoodsType().equals("P"))
                .collect(Collectors.toList());
        largeOrderBean.setPjdds(settlementOrderListP.size()
        );
        largeOrderBean.setPjddje(settlementOrderList.stream()
                .filter(item -> item.getGoodsType().equals("P"))
                .map(SettlementOrder::getTotalAmount).reduce(BigDecimal::add).orElse(Convert.toBigDecimal("0")));


        List<PgIncreOrder> pgIncreOrders = pgIncreOrderService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), PgIncreOrder::getCompanyWechatId, adminUser.getCompanyWechatId())
                .in(PgIncreOrder::getPayStatus, "PAID", "EXPIRE")
                .between(PgIncreOrder::getCreateTime, DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -30), new Date())
                .select(PgIncreOrder::getAmount, PgIncreOrder::getId).list();

        largeOrderBean.setZzdds(pgIncreOrders.size()
        );
        largeOrderBean.setZzddje(pgIncreOrders.stream()
                .map(PgIncreOrder::getAmount).reduce(BigDecimal::add).orElse(Convert.toBigDecimal("0")));


        return largeOrderBean;
    }

    public List<LargeOrderBean> getLarge8() {


        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<OrderInfo> orderInfoList = orderInfoService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), OrderInfo::getCompanyWechatId, adminUser.getCompanyWechatId())
                .notIn(OrderInfo::getOrderStatus, "NOPAY", "DQR", "CLOSE", "TIMEOUT")
                .between(OrderInfo::getCreateTime, DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -30)), new Date())
                .select(OrderInfo::getOrderId, OrderInfo::getPayAmount, OrderInfo::getCreateTime).list();


        List<SettlementOrder> settlementOrderList = settlementOrderService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), SettlementOrder::getCompanyWechatId, adminUser.getCompanyWechatId())
                .between(SettlementOrder::getCreateTime, DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -30)), new Date())
                .select(SettlementOrder::getOrderId, SettlementOrder::getTotalAmount, SettlementOrder::getGoodsType, SettlementOrder::getCreateTime)
                .list();


        List<PgIncreOrder> pgIncreOrders = pgIncreOrderService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), PgIncreOrder::getCompanyWechatId, adminUser.getCompanyWechatId())
                .in(PgIncreOrder::getPayStatus, "PAID", "EXPIRE")
                .between(PgIncreOrder::getCreateTime, DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -30)), new Date())
                .select(PgIncreOrder::getAmount, PgIncreOrder::getId, PgIncreOrder::getCreateTime).list();

        List<LargeOrderBean> largeOrderBeans = new ArrayList<>();


        for (int i = 29; i > -1; i--) {
            DateTime startDay = DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -1 * i));

            DateTime endDay = DateUtil.endOfDay(startDay);

            List<OrderInfo> orderInfos = orderInfoList.stream().filter(item -> item.getCreateTime().before(endDay) && item.getCreateTime().after(startDay))
                    .collect(Collectors.toList());

            LargeOrderBean largeOrderBean = new LargeOrderBean();
            largeOrderBean.setSxdds(orderInfos.size());
            largeOrderBean.setSxddje(orderInfos.stream().map(OrderInfo::getPayAmount).reduce(BigDecimal::add).orElse(Convert.toBigDecimal("0")));


            List<SettlementOrder> settlementOrderList1 = settlementOrderList.stream().filter(item -> item.getGoodsType().equals("M")
                    && item.getCreateTime().before(endDay) && item.getCreateTime().after(startDay)
            )
                    .collect(Collectors.toList());

            largeOrderBean.setFcdds(settlementOrderList1.size()
            );
            largeOrderBean.setFcddje(settlementOrderList1.stream()
                    .filter(item -> item.getGoodsType().equals("M"))
                    .map(SettlementOrder::getTotalAmount).reduce(BigDecimal::add).orElse(Convert.toBigDecimal("0")));


            List<SettlementOrder> settlementOrderListP = settlementOrderList.stream().filter(item -> item.getGoodsType().equals("P")
                    && item.getCreateTime().before(endDay) && item.getCreateTime().after(startDay)
            )
                    .collect(Collectors.toList());
            largeOrderBean.setPjdds(settlementOrderListP.size()
            );
            largeOrderBean.setPjddje(settlementOrderListP.stream()
                    .filter(item -> item.getGoodsType().equals("P"))
                    .map(SettlementOrder::getTotalAmount).reduce(BigDecimal::add).orElse(Convert.toBigDecimal("0")));

            List<PgIncreOrder> pgIncreOrderList = pgIncreOrders.stream().filter(item ->
                    item.getCreateTime().before(endDay) && item.getCreateTime().after(startDay)
            )
                    .collect(Collectors.toList());

            largeOrderBean.setZzdds(pgIncreOrderList.size()
            );
            largeOrderBean.setZzddje(pgIncreOrderList.stream()
                    .map(PgIncreOrder::getAmount).reduce(BigDecimal::add).orElse(Convert.toBigDecimal("0")));


            largeOrderBeans.add(largeOrderBean);


        }

        return largeOrderBeans;

    }

    public IPage<LargeWorkerOrderList> list(HttpServletRequest request, String city, String province, String area, Integer pageNum, Integer pageSize) {

        AdminUserCom adminUser = commonLogic.getAdminUser();
        Page<PgOrderBase> pgOrderBasePage = pgOrderBaseService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), PgOrderBase::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(PgOrderBase::getCity, city)
                .eq(PgOrderBase::getProvince, province)
                .eq(PgOrderBase::getArea, area)
                .ne(PgOrderBase::getOrderStatus, "YQX")
                .groupBy(PgOrderBase::getStreet, PgOrderBase::getCity, PgOrderBase::getProvince)
                .select(PgOrderBase::getArea, PgOrderBase::getProvince, PgOrderBase::getCity,PgOrderBase::getStreet).page(new Page<>(pageNum, pageSize));

        List<PgOrderBase> pgOrderBaseList = pgOrderBaseService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), PgOrderBase::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(PgOrderBase::getCity, city)
                .eq(PgOrderBase::getProvince, province)
                .eq(PgOrderBase::getArea, area)
                .ne(PgOrderBase::getOrderStatus, "YQX")
                .select(PgOrderBase::getArea, PgOrderBase::getProvince, PgOrderBase::getCity, PgOrderBase::getOrderStatus,PgOrderBase::getStreet,
                        PgOrderBase::getOrderSmallTypeText, PgOrderBase::getId, PgOrderBase::getTotalNum, PgOrderBase::getUndoneNum, PgOrderBase::getCreateTime,
                        PgOrderBase::getSaleType).list();

        if (CollectionUtils.isEmpty(pgOrderBaseList))
            return new Page<>();


        IPage<LargeWorkerOrderList> largeWorkerOrderListIPage = new Page<>();

        largeWorkerOrderListIPage.setTotal(pgOrderBasePage.getTotal());
        largeWorkerOrderListIPage.setCurrent(pgOrderBasePage.getCurrent());
        largeWorkerOrderListIPage.setSize(pgOrderBasePage.getSize());
        largeWorkerOrderListIPage.setPages(pgOrderBasePage.getPages());

        List<LargeWorkerOrderList> largeWorkerOrderLists = new ArrayList<>();

        List<String> status = new ArrayList<>();
        status.add("YWG");
        status.add("YJS");

        for (PgOrderBase record : pgOrderBasePage.getRecords()) {

            LargeWorkerOrderList largeWorkerOrderList = new LargeWorkerOrderList();

            largeWorkerOrderList.setStreetName(record.getStreet());

            List<PgOrderBase> pgOrderBasesStreet = pgOrderBaseList.stream().filter(item ->
                    item.getStreet().equals(record.getStreet())).collect(Collectors.toList());

            largeWorkerOrderList.setAzgd(pgOrderBasesStreet.stream().filter(item -> item.getOrderSmallTypeText().equals("安装")
                            && status.contains(item.getOrderStatus())
                    )
                            .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
            );


            largeWorkerOrderList.setAzgdwwg(pgOrderBasesStreet.stream().filter(item -> item.getOrderSmallTypeText().equals("安装")
                            && !status.contains(item.getOrderStatus())
                    )
                            .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
            );

            largeWorkerOrderList.setWxgd(pgOrderBasesStreet.stream().filter(item -> item.getOrderSmallTypeText().equals("维修")
                            && status.contains(item.getOrderStatus())
                    )
                            .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
            );


            largeWorkerOrderList.setWxgdwwg(pgOrderBasesStreet.stream().filter(item -> item.getOrderSmallTypeText().equals("维修")
                            && !status.contains(item.getOrderStatus())
                    )
                            .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
            );


            largeWorkerOrderList.setQxgd(pgOrderBasesStreet.stream().filter(item -> item.getOrderSmallTypeText().equals("清洗")
                            && status.contains(item.getOrderStatus())
                    )
                            .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
            );


            largeWorkerOrderList.setQxgdwwg(pgOrderBasesStreet.stream().filter(item -> item.getOrderSmallTypeText().equals("清洗")
                            && !status.contains(item.getOrderStatus())
                    )
                            .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
            );


            largeWorkerOrderList.setZzgd(pgOrderBasesStreet.stream().filter(item -> item.getSaleType().equals(3)
                            && status.contains(item.getOrderStatus())
                    )
                            .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
            );


            largeWorkerOrderList.setZzgdwwg(pgOrderBasesStreet.stream().filter(item -> item.getSaleType().equals(3)
                            && !status.contains(item.getOrderStatus())
                    )
                            .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
            );


            largeWorkerOrderList.setWbgd(pgOrderBasesStreet.stream().filter(item -> item.getSaleType().equals(4)
                            && status.contains(item.getOrderStatus())
                    )
                            .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
            );


            largeWorkerOrderList.setWbgdwwg(pgOrderBasesStreet.stream().filter(item -> item.getSaleType().equals(4)
                            && !status.contains(item.getOrderStatus())
                    )
                            .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0)
            );


            largeWorkerOrderLists.add(largeWorkerOrderList);
        }

        largeWorkerOrderListIPage.setRecords(largeWorkerOrderLists);


        return largeWorkerOrderListIPage;

    }

    public List<String> getLarge9() {
        List<AdminCompanyWechat> list = adminCompanyWechatService.lambdaQuery().select(AdminCompanyWechat::getProvince).groupBy(AdminCompanyWechat::getProvince).list();

        List<String> p = new ArrayList<>();
        for (AdminCompanyWechat adminCompanyWechat : list) {

            p.add(adminCompanyWechat.getProvince());
        }

        return p;
    }


    public void addUserLog(String userId) {

        AdminUser adminUser = adminUserService.getById(userId);


        if (userLogService.lambdaQuery().eq(UserLog::getUserId, adminUser.getAdminUserId())
                .between(UserLog::getCreateTime, DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date())).eq(UserLog::getType, "LOGIN").count() < 1
        ) {
            UserLog userLog = new UserLog();
            userLog.setType("LOGIN");
            userLog.setCompanyWechatId(adminUser.getCompanyWechatId());
            userLog.setCompanyName(adminUser.getCompanyName());
            userLog.setUserId(adminUser.getAdminUserId());
            userLog.setLoginType(IsYesNoEnum.YES.getKey());
            userLog.insert();

        }


        UserLog userLog = new UserLog();
        userLog.setType("SEE");
        userLog.setCompanyWechatId(adminUser.getCompanyWechatId());
        userLog.setCompanyName(adminUser.getCompanyName());
        userLog.setUserId(adminUser.getAdminUserId());
        userLog.setLoginType(IsYesNoEnum.YES.getKey());

        userLog.insert();

    }

    public List<LargeNumCity> getLarge10(String city, String province) {

        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<PgOrderBase> pgOrderBases = pgOrderBaseService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), PgOrderBase::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(PgOrderBase::getCity, city)
                .eq(PgOrderBase::getProvince, province)
                .ne(PgOrderBase::getOrderStatus, "YQX")
                .groupBy(PgOrderBase::getArea, PgOrderBase::getCity)
                .select(PgOrderBase::getArea, PgOrderBase::getProvince, PgOrderBase::getCity).list();


        List<PgOrderBase> pgOrderBaseList = pgOrderBaseService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), PgOrderBase::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(PgOrderBase::getCity, city)
                .eq(PgOrderBase::getProvince, province)
                .ne(PgOrderBase::getOrderStatus, "YQX")
                .select(PgOrderBase::getArea, PgOrderBase::getProvince, PgOrderBase::getCity, PgOrderBase::getOrderStatus,
                        PgOrderBase::getOrderSmallTypeText, PgOrderBase::getId, PgOrderBase::getTotalNum, PgOrderBase::getUndoneNum, PgOrderBase::getCreateTime,
                        PgOrderBase::getSaleType).list();

        List<LargeNumCity> largeNumCities = new ArrayList<>();

        List<String> status = new ArrayList<>();
        status.add("YWG");
        status.add("YJS");


        for (PgOrderBase pgOrderBase : pgOrderBases) {
            LargeNumCity largeNumCity = new LargeNumCity();

            List<PgOrderBase> pgOrderBasesStreet = pgOrderBaseList.stream()
                    .filter(item -> item.getArea().equals(pgOrderBase.getArea())
                            && item.getCreateTime().after(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), -8)))
                            && item.getCreateTime().before(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), -1)))
                            && status.contains(item.getOrderStatus())
                    ).collect(Collectors.toList());

            Integer integer = pgOrderBasesStreet.stream()
                    .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0);


            List<PgOrderBase> pgOrderBasesStreetNum = pgOrderBaseList.stream()
                    .filter(item -> item.getArea().equals(pgOrderBase.getArea())
                            && !status.contains(item.getOrderStatus())
                    ).collect(Collectors.toList());

            Integer streetNum = pgOrderBasesStreetNum.stream()
                    .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0);

            BigDecimal divide = integer == 0 ? Convert.toBigDecimal(streetNum) : Convert.toBigDecimal(streetNum).divide(Convert.toBigDecimal(integer).divide(Convert.toBigDecimal("7"), 2, BigDecimal.ROUND_HALF_UP), 2, BigDecimal.ROUND_HALF_UP);


            largeNumCity.setNum(divide);
            largeNumCity.setArea(pgOrderBase.getArea());

            largeNumCities.add(largeNumCity);
        }

        return largeNumCities;
    }

    public LargeNumCity getLarge11(String city, String province) {


        AdminUserCom adminUser = commonLogic.getAdminUser();


        List<PgOrderBase> pgOrderBaseList = pgOrderBaseService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), PgOrderBase::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(PgOrderBase::getCity, city)
                .eq(PgOrderBase::getProvince, province)
                .ne(PgOrderBase::getOrderStatus, "YQX")
                .select(PgOrderBase::getArea, PgOrderBase::getProvince, PgOrderBase::getCity, PgOrderBase::getOrderStatus,
                        PgOrderBase::getOrderSmallTypeText, PgOrderBase::getId, PgOrderBase::getTotalNum, PgOrderBase::getUndoneNum, PgOrderBase::getCreateTime,
                        PgOrderBase::getSaleType).list();

        List<LargeNumCity> largeNumCities = new ArrayList<>();

        List<String> status = new ArrayList<>();
        status.add("YWG");
        status.add("YJS");


        LargeNumCity largeNumCity = new LargeNumCity();

        List<PgOrderBase> pgOrderBasesStreet = pgOrderBaseList.stream()
                .filter(item -> item.getCity().equals(city)
                        && status.contains(item.getOrderStatus())
                ).collect(Collectors.toList());

        Integer integer = pgOrderBasesStreet.stream()
                .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0);


        List<PgOrderBase> pgOrderBasesStreetNum = pgOrderBaseList.stream()
                .filter(item -> item.getCity().equals(city)
                        && !status.contains(item.getOrderStatus())
                ).collect(Collectors.toList());

        Integer streetNum = pgOrderBasesStreetNum.stream()
                .map(PgOrderBase::getTotalNum).reduce(Integer::sum).orElse(0);



        largeNumCity.setNum(Convert.toBigDecimal(integer));
        largeNumCity.setCity(city);
        largeNumCity.setNumWwg(Convert.toBigDecimal(streetNum));




        return largeNumCity;
    }

    public Map<String, Object> getWater(String city) {

        String ymd = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String redisKey = "water:"+city+ymd;
        if(redisUtil.get(redisKey) != null){
            return JSONObject.parseObject(redisUtil.get(redisKey).toString(),Map.class);
        }
        Map<String,String> map = new HashMap<>();
        map.put("Authorization","APPCODE efa35f50d7474bc0b9cbb78ca9e9c0f4");
        map.put("Content-Type", "application/json; charset=UTF-8");

        Map<String,String> param = new HashMap<>();
        param.put("city",city);
        String s = HttpUtils.requestGet("http://jisuqgtq.market.alicloudapi.com/weather/query",param,map);
        redisUtil.set(redisKey,s,24 * 60 * 60);
        return JSONObject.parseObject(s,Map.class);
    }
}
