package org.dromara.work.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.dto.RoleDTO;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.SysDept;
import org.dromara.system.domain.SysRoleDept;
import org.dromara.system.domain.SysUser;
import org.dromara.system.mapper.SysDeptMapper;
import org.dromara.system.mapper.SysRoleDeptMapper;
import org.dromara.system.mapper.SysRoleMapper;
import org.dromara.system.mapper.SysUserMapper;
import org.dromara.work.domain.*;
import org.dromara.work.domain.bo.OrderRankingBo;
import org.dromara.work.domain.bo.TpOrderBo;
import org.dromara.work.domain.vo.*;
import org.dromara.work.mapper.TpClientMapper;
import org.dromara.work.mapper.TpMonthMapper;
import org.dromara.work.mapper.TpOrderMapper;
import org.dromara.work.mapper.TpYearMapper;
import org.dromara.work.service.ITpNewOrderService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: Maosw
 */
@RequiredArgsConstructor
@Service
public class TpNewOrderServiceImpl extends MPJBaseServiceImpl<TpOrderMapper, TpOrder> implements ITpNewOrderService {

    private final TpOrderMapper baseMapper;

    private final SysUserMapper sysUserMapper;

    private final SysDeptMapper deptMapper;

    private final SysRoleDeptMapper roleDeptMapper;

    private final TpClientMapper clientMapper;

    private final TpMonthMapper monthMapper;

    private final TpYearMapper yearMapper;


    private MPJLambdaWrapper<TpOrder> buildQueryRankingMPJWrapper(OrderRankingBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        MPJLambdaWrapper<TpOrder> lqw = new MPJLambdaWrapper<>();
        if(bo.getDeptIds()!= null && bo.getDeptIds().size() > 0){
            if(bo.getType() == 1){
                lqw.in(TpOrder::getDeptId,bo.getDeptIds());
            } else if (bo.getType() == 2) {
                lqw.in(TpOrder::getDeptIdJs,bo.getDeptIds());
            }
        }else {
            int scope = getDataScope();
            if(scope == 2){
                List<Long> deptIds = getDeptIds();
                if(bo.getType() == 1){
                    lqw.in(TpOrder::getDeptId,deptIds);
                } else if (bo.getType() == 2) {
                    lqw.in(TpOrder::getDeptIdJs,deptIds);
                }
            }else if(scope == 3){
                if(bo.getType() == 1){
                    lqw.eq(TpOrder::getDeptId,loginUser.getDeptId());
                } else if (bo.getType() == 2) {
                    lqw.eq(TpOrder::getDeptIdJs,loginUser.getDeptId());
                }
            }else if(scope == 4){
                List<Long> deptIds = deptMapper.selectList(new LambdaQueryWrapper<SysDept>().like(SysDept::getAncestors, loginUser.getDeptId())).stream().map(SysDept::getDeptId).collect(Collectors.toList());
                deptIds.add(loginUser.getDeptId());
                if(bo.getType() == 1){
                    lqw.in(TpOrder::getDeptId,deptIds);
                } else if (bo.getType() == 2) {
                    lqw.in(TpOrder::getDeptIdJs,deptIds);
                }
            }else if(scope == 5){
                if(bo.getType() == 1){
                    lqw.eq(TpOrder::getSid,loginUser.getUserId());
                } else if (bo.getType() == 2) {
                    lqw.eq(TpOrder::getBid,loginUser.getUserId());
                }
            }
        }
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), SysUser::getNickName, bo.getUserName());
        lqw.like(StringUtils.isNotBlank(bo.getRealName()), SysUser::getRealName, bo.getRealName());
        lqw.between(bo.getStartTime() != null && bo.getEndTime() != null,
            TpOrder::getAddTime ,bo.getStartTime(), bo.getEndTime());
        return lqw;
    }

    private MPJLambdaWrapper<TpOrder> buildKHRankingMPJWrapper(OrderRankingBo bo) {
        MPJLambdaWrapper<TpOrder> lqw = new MPJLambdaWrapper<>();
        lqw.eq(StringUtils.isNotBlank(bo.getUserName()), TpClient::getCode, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getRealName()), TpClient::getName, bo.getRealName());
        lqw.between(bo.getStartTime() != null && bo.getEndTime() != null,
            TpOrder::getAddTime ,bo.getStartTime(), bo.getEndTime());
        return lqw;
    }

    /**
     * 客服排行榜
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<OrderRankingVo> rankingList(OrderRankingBo bo, PageQuery pageQuery) {
        /*DateTime dateTime = new DateTime(bo.getMonth(), DatePattern.NORM_MONTH_FORMAT);
        Month month1 = dateTime.monthEnum();
        int lastDay = month1.getLastDay(false);
        bo.setStartTime(bo.getMonth() + "-01 00:00:00");
        bo.setEndTime(bo.getMonth() + "-" + lastDay + " 23:59:59");*/

        MPJLambdaWrapper<TpOrder> wrapper = buildQueryRankingMPJWrapper(bo)
            .selectAs(SysUser::getNickName,OrderRankingVo::getUserName)
            .selectAs(SysUser::getRealName,OrderRankingVo::getRealName)
            .selectAs(SysDept::getDeptName,OrderRankingVo::getDeptName)
            .selectAs("count(1)",OrderRankingVo::getOrderNum)
            .selectAs("sum(price)",OrderRankingVo::getTotalPerformance)
            .selectAs("sum(pay_price - kfpay)",OrderRankingVo::getCompletionAmount)
            .selectAs("sum(end_price)",OrderRankingVo::getUnpaidAmount)
            .selectAs("concat(round(sum(pay_price)/(SELECT CASE WHEN m.gongsi = 0 THEN m.geren ELSE m.gongsi END AS result_field FROM tp_month m WHERE m.sid = t.sid AND m.`month` = '" + bo.getMonth() + "') * 100),'%')",OrderRankingVo::getTargetCompletionRate)
            .selectAs("concat(round(sum(end_price)/sum(price) * 100),'%')",OrderRankingVo::getDeadOrderRate)
            .leftJoin(SysUser.class,SysUser::getUserId,TpOrder::getSid)
            .leftJoin(SysDept.class,SysDept::getDeptId,SysUser::getDeptId)
            .eq(TpOrder::getIsDel,1)
//            .ne(TpOrder::getPayState,1)
            .groupBy(TpOrder::getSid);

        wrapper.orderBy(bo.getSort() == 1,bo.getIsAsc(),OrderRankingVo::getOrderNum);
        wrapper.orderBy(bo.getSort() == 2,bo.getIsAsc(),OrderRankingVo::getTotalPerformance);
        wrapper.orderBy(bo.getSort() == 3,bo.getIsAsc(),OrderRankingVo::getCompletionAmount);
        wrapper.orderBy(bo.getSort() == 4,bo.getIsAsc(),OrderRankingVo::getUnpaidAmount);
        wrapper.orderBy(bo.getSort() == 5,bo.getIsAsc(),OrderRankingVo::getTargetCompletionRate);
        wrapper.orderBy(bo.getSort() == 6,bo.getIsAsc(),OrderRankingVo::getDeadOrderRate);

        IPage<OrderRankingVo> listPage = baseMapper.selectJoinPage(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()), OrderRankingVo.class, wrapper);
        return TableDataInfo.build(listPage);
    }

    /**
     * 技术排行榜
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<OrderRankingVo> rankingListJS(OrderRankingBo bo, PageQuery pageQuery) {
        MPJLambdaWrapper<TpOrder> wrapper = buildQueryRankingMPJWrapper(bo)
            .selectAs(SysUser::getNickName,OrderRankingVo::getUserName)
            .selectAs(SysUser::getRealName,OrderRankingVo::getRealName)
            .selectAs(SysDept::getDeptName,OrderRankingVo::getDeptName)
            .selectAs("count(1)",OrderRankingVo::getOrderNum)
            .selectAs("sum(js_price)",OrderRankingVo::getTotalPerformance)
            .selectAs("sum(js_pay_price)",OrderRankingVo::getCompletionAmount)
            .selectAs("sum(js_price) - sum(js_pay_price)",OrderRankingVo::getUnpaidAmount)
            .selectAs("concat(round(sum(js_pay_price)/sum(js_price) * 100),'%')",OrderRankingVo::getTargetCompletionRate)
            .selectAs("concat(round((sum(js_price) - sum(js_pay_price))/sum(js_price) * 100),'%')",OrderRankingVo::getDeadOrderRate)
            .leftJoin(SysUser.class,SysUser::getUserId,TpOrder::getBid)
            .leftJoin(SysDept.class,SysDept::getDeptId,SysUser::getDeptId)
            .eq(TpOrder::getIsDel,1)
            .groupBy(TpOrder::getBid);

        wrapper.isNotNull(TpOrder::getBid);
        wrapper.orderBy(bo.getSort() == 1,bo.getIsAsc(),OrderRankingVo::getOrderNum);
        wrapper.orderBy(bo.getSort() == 2,bo.getIsAsc(),OrderRankingVo::getTotalPerformance);
        wrapper.orderBy(bo.getSort() == 3,bo.getIsAsc(),OrderRankingVo::getCompletionAmount);
        wrapper.orderBy(bo.getSort() == 4,bo.getIsAsc(),OrderRankingVo::getUnpaidAmount);
        wrapper.orderBy(bo.getSort() == 5,bo.getIsAsc(),OrderRankingVo::getTargetCompletionRate);
        wrapper.orderBy(bo.getSort() == 6,bo.getIsAsc(),OrderRankingVo::getDeadOrderRate);

        IPage<OrderRankingVo> listPage = baseMapper.selectJoinPage(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()), OrderRankingVo.class, wrapper);
        return TableDataInfo.build(listPage);
    }

    /**
     * 客服排行榜统计
     *
     * @param bo
     * @return
     */
    @Override
    public OrderRankingSumVo rankingListKFSum(OrderRankingBo bo) {
        Long totalNum = sysUserMapper.selectCount(new LambdaQueryWrapper<SysUser>().eq(SysUser::getIdentity, 3));
        MPJLambdaWrapper<TpOrder> wrapper = buildQueryRankingMPJWrapper(bo)
            .selectAs("count(1)",OrderRankingSumVo::getTotalOrderNum)
            .selectAs("sum(price)",OrderRankingSumVo::getTotalPerformance)
            .selectAs("sum(pay_price - kfpay)",OrderRankingSumVo::getTotalPaidAmount)
            .selectAs("concat(round(sum(pay_price - kfpay)/sum(price) * 100),'%')",OrderRankingSumVo::getTotalCompletionRate)
            .leftJoin(SysUser.class,SysUser::getUserId,TpOrder::getSid)
            .leftJoin(SysDept.class,SysDept::getDeptId,SysUser::getDeptId)
            .eq(TpOrder::getIsDel,1);
//            .ne(TpOrder::getPayState,1);

        OrderRankingSumVo orderRankingSumVo = baseMapper.selectJoinOne(OrderRankingSumVo.class, wrapper);
        orderRankingSumVo.setTotalNum(totalNum);
        return orderRankingSumVo;
    }

    /**
     * 技术排行榜统计
     *
     * @param bo
     * @return
     */
    @Override
    public OrderRankingSumVo rankingListJSSum(OrderRankingBo bo) {
        Long totalNum = sysUserMapper.selectCount(new LambdaQueryWrapper<SysUser>().eq(SysUser::getIdentity, 2));
        MPJLambdaWrapper<TpOrder> wrapper = buildQueryRankingMPJWrapper(bo)
            .selectAs("count(1)",OrderRankingSumVo::getTotalOrderNum)
            .selectAs("sum(js_price)",OrderRankingSumVo::getTotalPerformance)
            .selectAs("sum(js_pay_price)",OrderRankingSumVo::getTotalPaidAmount)
            .selectAs("concat(round(sum(js_pay_price)/sum(js_price) * 100),'%')",OrderRankingSumVo::getTotalCompletionRate)
            .leftJoin(SysUser.class,SysUser::getUserId,TpOrder::getBid)
            .leftJoin(SysDept.class,SysDept::getDeptId,SysUser::getDeptId)
            .eq(TpOrder::getIsDel,1)
            .isNotNull(TpOrder::getBid);
//            .ne(TpOrder::getPayState,1);

        OrderRankingSumVo orderRankingSumVo = baseMapper.selectJoinOne(OrderRankingSumVo.class, wrapper);
        orderRankingSumVo.setTotalNum(totalNum);
        return orderRankingSumVo;
    }


    /**
     * 客户下单排行榜
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<OrderRankingVo> khRankingList(OrderRankingBo bo, PageQuery pageQuery) {
        MPJLambdaWrapper<TpOrder> wrapper = buildKHRankingMPJWrapper(bo)
            .selectAs(TpClient::getCode,OrderRankingVo::getUserName)
            .selectAs(TpClient::getName,OrderRankingVo::getRealName)
//            .selectAs(SysDept::getDeptName,OrderRankingVo::getDeptName)
            .selectAs("count(1)",OrderRankingVo::getOrderNum)
            .selectAs("sum(t.price)",OrderRankingVo::getTotalPerformance)
            .selectAs("sum(t.khpay)",OrderRankingVo::getCompletionAmount)
            .selectAs("sum(t.price) - sum(t.khpay)",OrderRankingVo::getUnpaidAmount)
            .selectAs("concat(round(sum(t.khpay)/sum(t.price) * 100),'%')",OrderRankingVo::getTargetCompletionRate)
            .leftJoin(TpClient.class,TpClient::getId,TpOrder::getKid)
//            .leftJoin(SysDept.class,SysDept::getDeptId,SysUser::getDeptId)
            .eq(TpOrder::getIsDel,1)
//            .ne(TpOrder::getPayState,1)
            .groupBy(TpOrder::getKid);
//            .orderByDesc("sum(js_price)");

        wrapper.orderBy(bo.getSort() == 1,bo.getIsAsc(),OrderRankingVo::getOrderNum);
        wrapper.orderBy(bo.getSort() == 2,bo.getIsAsc(),OrderRankingVo::getTotalPerformance);
        wrapper.orderBy(bo.getSort() == 3,bo.getIsAsc(),OrderRankingVo::getCompletionAmount);
        wrapper.orderBy(bo.getSort() == 4,bo.getIsAsc(),OrderRankingVo::getUnpaidAmount);
        wrapper.orderBy(bo.getSort() == 5,bo.getIsAsc(),OrderRankingVo::getTargetCompletionRate);

        IPage<OrderRankingVo> listPage = baseMapper.selectJoinPage(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()), OrderRankingVo.class, wrapper);
        return TableDataInfo.build(listPage);
    }

    /**
     * 客户下单排行榜统计
     *
     * @param bo
     * @return
     */
    @Override
    public OrderRankingSumVo khRankingListSum(OrderRankingBo bo) {
        Long totalNum = clientMapper.selectCount(new LambdaQueryWrapper<TpClient>().eq(TpClient::getIsDel, 1));
        MPJLambdaWrapper<TpOrder> wrapper = buildKHRankingMPJWrapper(bo)
            .selectAs("count(1)",OrderRankingSumVo::getTotalOrderNum)
            .selectAs("sum(t.price)",OrderRankingSumVo::getTotalPerformance)
            .selectAs("sum(t.khpay)",OrderRankingSumVo::getTotalPaidAmount)
            .selectAs("concat(round(sum(t.khpay)/sum(t.price) * 100),'%')",OrderRankingSumVo::getTotalCompletionRate)
            .leftJoin(TpClient.class,TpClient::getId,TpOrder::getKid)
            .eq(TpOrder::getIsDel,1);
//            .ne(TpOrder::getPayState,1);

        OrderRankingSumVo orderRankingSumVo = baseMapper.selectJoinOne(OrderRankingSumVo.class, wrapper);
        orderRankingSumVo.setTotalNum(totalNum);
        return orderRankingSumVo;
    }

    public int getDataScope() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<String> ids = loginUser != null ?
            loginUser.getRoles().stream().filter(Objects::nonNull).map(role -> role.getDataScope()).filter(Objects::nonNull).collect(Collectors.toList()) :
            Collections.emptyList();

        if (ids.contains("1")) {
            return 1;
        } else if (ids.contains("2")) {
            return 2;
        } else if (ids.contains("3")) {
            return 3;
        } else if (ids.contains("4")) {
            return 4;
        } else if (ids.contains("5")) {
            return 5;
        }
        return 0;
    }

    public List<Long> getDeptIds(){
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<Long> roleIds = loginUser.getRoles().stream().map(RoleDTO::getRoleId).collect(Collectors.toList());
        List<Long> getDeptIds = roleDeptMapper.selectList(new LambdaQueryWrapper<SysRoleDept>().in(SysRoleDept::getRoleId, roleIds)).stream().map(SysRoleDept::getDeptId).collect(Collectors.toList());
        return getDeptIds.stream().distinct().collect(Collectors.toList());
    }


    /**
     * 分页查询订单管理列表
     *
     * @param bo        订单管理
     * @param pageQuery
     * @return 订单管理
     */
    @Override
    public IPage<TpOrderVo> selectPageList(TpOrderBo bo, PageQuery pageQuery) {
        bo.setIsDel(1);
        //和Mybatis plus一致，MPJLambdaWrapper的泛型必须是主表的泛型，并且要用主表的Mapper来调用
        MPJLambdaWrapper<TpOrder> wrapper = buildQueryMPJWrapper(bo)
            .selectAll(TpOrder.class)
            .selectAs("s.nick_name",TpOrderVo::getSname)
            .selectAs("f.nick_name",TpOrderVo::getFname)
            .selectAs("b.nick_name",TpOrderVo::getBname)
//            .selectAs(TpClient::getName,TpOrderVo::getCname)
//            .selectAs(TpWechat::getCode,TpOrderVo::getWname)
            .selectAs("concat(round(t.pay_price/t.price * 100),'%')",TpOrderVo::getBili)
            .leftJoin(SysUser.class,"s",SysUser::getUserId,TpOrder::getSid)
            .leftJoin(SysUser.class,"f",SysUser::getUserId,TpOrder::getFid)
            .leftJoin(SysUser.class,"b",SysUser::getUserId,TpOrder::getBid);
//            .leftJoin(TpClient.class,TpClient::getId,TpOrder::getKid)
//            .leftJoin(TpWechat.class,TpWechat::getId,TpOrder::getWid)
        if(bo.getOrderBy() == 1){
            wrapper.orderByDesc(TpOrder::getAddTime);
        }else if(bo.getOrderBy() == 2){
            wrapper.orderByDesc(TpOrder::getPrice);
        }else if(bo.getOrderBy() == 3){
            wrapper.orderByAsc(TpOrder::getPrice);
        }
        //分页查询 （需要启用 mybatis plus 分页插件）
        return baseMapper.selectJoinPage(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()), TpOrderVo.class, wrapper);
    }

    /**
     * 根据id查询订单详情
     *
     * @param id
     * @return
     */
    @Override
    public TpOrderVo queryById(Long id) {
        MPJLambdaWrapper<TpOrder> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(TpOrder.class);
        wrapper.selectAs("s.nick_name",TpOrderVo::getSname)
            .selectAs("f.nick_name",TpOrderVo::getFname)
            .selectAs("b.nick_name",TpOrderVo::getBname)
            .leftJoin(SysUser.class,"s",SysUser::getUserId,TpOrder::getSid)
            .leftJoin(SysUser.class,"f",SysUser::getUserId,TpOrder::getFid)
            .leftJoin(SysUser.class,"b",SysUser::getUserId,TpOrder::getBid);
        wrapper.eq(id != null, TpOrder::getId, id);
        return baseMapper.selectJoinOne(TpOrderVo.class,wrapper);
    }

    private MPJLambdaWrapper<TpOrder> buildQueryMPJWrapper(TpOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        MPJLambdaWrapper<TpOrder> lqw = new MPJLambdaWrapper<>();
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), TpOrder::getOrderId, bo.getOrderId());
        lqw.eq(bo.getCategoryId() != null, TpOrder::getCategoryId, bo.getCategoryId());
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), TpOrder::getCategoryName, bo.getCategoryName());
        lqw.eq(bo.getFlag() != null, TpOrder::getFlag, bo.getFlag());
        lqw.like(StringUtils.isNotBlank(bo.getScode()), "s.nick_name", bo.getScode());
        lqw.like(StringUtils.isNotBlank(bo.getPhone()), "s.phonenumber", bo.getPhone());
        lqw.like(StringUtils.isNotBlank(bo.getBcode()), "b.nick_name", bo.getBcode());
        lqw.like(StringUtils.isNotBlank(bo.getFcode()), "f.nick_name", bo.getFcode());
        if(bo.getAssignState() != null){
            lqw.isNull(bo.getAssignState() == 1, TpOrder::getBid);
            lqw.isNotNull(bo.getAssignState() == 2, TpOrder::getBid);
        }
        lqw.eq(bo.getStyle() != null, TpOrder::getStyle, bo.getStyle());
        lqw.like(StringUtils.isNotBlank(bo.getSpace()), TpOrder::getSpace, bo.getSpace());
        lqw.eq(bo.getQuality() != null, TpOrder::getQuality, bo.getQuality());
        lqw.eq(bo.getType() != null, TpOrder::getType, bo.getType());
        lqw.eq(bo.getState() != null, TpOrder::getState, bo.getState());
        lqw.like(StringUtils.isNotBlank(bo.getRemark()), TpOrder::getRemark, bo.getRemark());
        lqw.like(StringUtils.isNotBlank(bo.getJsRemark()), TpOrder::getJsRemark, bo.getJsRemark());
        lqw.eq(bo.getPayState() != null, TpOrder::getPayState, bo.getPayState());
        lqw.eq(bo.getIsDel() != null, TpOrder::getIsDel, bo.getIsDel());
        lqw.between(params.get("addBeginTime") != null && params.get("addEndTime") != null,
            TpOrder::getAddTime, params.get("addBeginTime"), params.get("addEndTime"));
        lqw.between(params.get("dtBeginTime") != null && params.get("dtEndTime") != null,
            TpOrder::getDtTime, params.get("dtBeginTime"), params.get("dtEndTime"));
        lqw.eq(bo.getIsC() != null, TpOrder::getIsC, bo.getIsC());
        lqw.eq(bo.getIsCd() != null, TpOrder::getIsCd, bo.getIsCd());
        return lqw;
    }

    /**
     * 客服首页统计
     * @return
     */
    @Override
    public IndexSumVo getKfTarget() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        DateTime now = DateTime.now();
        int year = now.year();
        String month = DateUtil.format(new Date(), "yyyy-MM");
        String day = DateUtil.format(new Date(), "yyyy-MM-dd");

        IndexSumVo indexSumVo = new IndexSumVo();
        TpYearVo yearVo = yearMapper.selectVoOne(new LambdaQueryWrapper<TpYear>().eq(TpYear::getSid, loginUser.getUserId()).eq(TpYear::getYear, year));
        TpMonthVo monthVo = monthMapper.selectVoOne(new LambdaQueryWrapper<TpMonth>().eq(TpMonth::getSid, loginUser.getUserId()).eq(TpMonth::getMonth, month+"-01"));

        if(ObjectUtil.isNotEmpty(yearVo)){
            indexSumVo.setGrnTarget(yearVo.getGeren());
            indexSumVo.setGsnTarget(yearVo.getGongsi());
        }
        if(ObjectUtil.isNotEmpty(monthVo)){
            indexSumVo.setGryTarget(monthVo.getGeren());
            indexSumVo.setGsyTarget(monthVo.getGongsi());
        }

        LambdaQueryWrapper<TpOrder> lqw = new LambdaQueryWrapper<TpOrder>()
                .select(TpOrder::getPrice,TpOrder::getPayPrice)
                .eq(TpOrder::getIsDel,1)
                .eq(TpOrder::getSid,loginUser.getUserId())
                .ge(TpOrder::getAddTime,year);
        List<TpOrder> list = baseMapper.selectList(lqw);
        BigDecimal ywcYSum = list.stream().map(f -> new BigDecimal(String.valueOf(f.getPrice()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal ydkYSum = list.stream().map(f -> new BigDecimal(String.valueOf(f.getPayPrice()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        indexSumVo.setYwcYSum(String.valueOf(ywcYSum));
        indexSumVo.setYdkYSum(String.valueOf(ydkYSum));

        LambdaQueryWrapper<TpOrder> lqw1 = new LambdaQueryWrapper<TpOrder>()
                .select(TpOrder::getPrice,TpOrder::getPayPrice)
                .eq(TpOrder::getIsDel,1)
                .eq(TpOrder::getSid,loginUser.getUserId())
                .ge(TpOrder::getAddTime,month+"-01");
        List<TpOrder> list1 = baseMapper.selectList(lqw1);
        BigDecimal ywcMSum = list1.stream().map(f -> new BigDecimal(String.valueOf(f.getPrice()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal ydkMSum = list1.stream().map(f -> new BigDecimal(String.valueOf(f.getPayPrice()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        indexSumVo.setYwcMSum(String.valueOf(ywcMSum));
        indexSumVo.setYdkMSum(String.valueOf(ydkMSum));

        LambdaQueryWrapper<TpOrder> lqw2 = new LambdaQueryWrapper<TpOrder>()
                .select(TpOrder::getPrice,TpOrder::getPayPrice)
                .eq(TpOrder::getIsDel,1)
                .eq(TpOrder::getSid,loginUser.getUserId())
                .ge(TpOrder::getAddTime,day);
        List<TpOrder> list2 = baseMapper.selectList(lqw2);
        BigDecimal ywcDSum = list2.stream().map(f -> new BigDecimal(String.valueOf(f.getPrice()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal ydkDSum = list2.stream().map(f -> new BigDecimal(String.valueOf(f.getPayPrice()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        indexSumVo.setYwcDSum(String.valueOf(ywcDSum));
        indexSumVo.setYdkDSum(String.valueOf(ydkDSum));
        return indexSumVo;
    }

    /**
     * 技术首页统计
     * @return
     */
    @Override
    public IndexSumVo getJsTarget() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        DateTime now = DateTime.now();
        int year = now.year();
        String month = DateUtil.format(new Date(), "yyyy-MM");
        String day = DateUtil.format(new Date(), "yyyy-MM-dd");
        String endDay = day + " 09:00:00";
        //昨天
        String yesterday = DateUtil.formatDate(DateUtil.yesterday());

        IndexSumVo indexSumVo = new IndexSumVo();
        LambdaQueryWrapper<TpOrder> lqw = new LambdaQueryWrapper<TpOrder>()
                .select(TpOrder::getJsPrice,TpOrder::getJsPayPrice)
                .eq(TpOrder::getIsDel,1)
                .eq(TpOrder::getBid,loginUser.getUserId())
                .between(TpOrder::getAddTime,year+"-01-01 00:00:00",endDay);
        List<TpOrder> list = baseMapper.selectList(lqw);
        BigDecimal ywcYSum = list.stream().map(f -> new BigDecimal(String.valueOf(f.getJsPrice()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal ydkYSum = list.stream().map(f -> new BigDecimal(String.valueOf(f.getJsPayPrice()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        indexSumVo.setYwcYSum(String.valueOf(ywcYSum));
        indexSumVo.setYdkYSum(String.valueOf(ydkYSum));
//        indexSumVo.setYwcYSum(getMinMultipleOfOneThousand(ywcYSum.intValue())+"以上");
//        indexSumVo.setYdkYSum(getMinMultipleOfOneThousand(ydkYSum.intValue())+"以上");

        LambdaQueryWrapper<TpOrder> lqw1 = new LambdaQueryWrapper<TpOrder>()
                .select(TpOrder::getJsPrice,TpOrder::getJsPayPrice)
                .eq(TpOrder::getIsDel,1)
                .eq(TpOrder::getBid,loginUser.getUserId())
                .between(TpOrder::getAddTime,month+"-01 00:00:00",endDay);
        List<TpOrder> list1 = baseMapper.selectList(lqw1);
        BigDecimal ywcMSum = list1.stream().map(f -> new BigDecimal(String.valueOf(f.getJsPrice()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal ydkMSum = list1.stream().map(f -> new BigDecimal(String.valueOf(f.getJsPayPrice()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        indexSumVo.setYwcMSum(String.valueOf(ywcMSum));
        indexSumVo.setYdkMSum(String.valueOf(ydkMSum));

//        indexSumVo.setYwcMSum(getMinMultipleOfOneThousand(ywcMSum.intValue())+"以上");
//        indexSumVo.setYdkMSum(getMinMultipleOfOneThousand(ydkMSum.intValue())+"以上");

        LambdaQueryWrapper<TpOrder> lqw2 = new LambdaQueryWrapper<TpOrder>()
                .select(TpOrder::getJsPrice,TpOrder::getJsPayPrice)
                .eq(TpOrder::getIsDel,1)
                .eq(TpOrder::getBid,loginUser.getUserId())
                .between(TpOrder::getAddTime,yesterday+" 00:00:00",endDay);
        List<TpOrder> list2 = baseMapper.selectList(lqw2);
        BigDecimal ywcDSum = list2.stream().map(f -> new BigDecimal(String.valueOf(f.getJsPrice()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal ydkDSum = list2.stream().map(f -> new BigDecimal(String.valueOf(f.getJsPayPrice()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        indexSumVo.setYwcDSum(String.valueOf(ywcDSum));
        indexSumVo.setYdkDSum(String.valueOf(ydkDSum));

//        indexSumVo.setYwcDSum(getMinMultipleOfOneThousand(ywcDSum.intValue())+"以上");
//        indexSumVo.setYdkDSum(getMinMultipleOfOneThousand(ydkDSum.intValue())+"以上");
        return indexSumVo;
    }
}
