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

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.workorder.OrderBaseCountBean;
import com.gree.mall.manager.bean.workorder.OrderCount7DayBean;
import com.gree.mall.manager.commonmapper.workorder.OrderBaseCMapper;
import com.gree.mall.manager.enums.OrderStatusEnum;
import com.gree.mall.manager.enums.workorder.OrderBaseStatusEnum;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.PgOrderBase;
import com.gree.mall.manager.plus.service.PgOrderBaseService;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class OrderBaseCountLogic {

    private final OrderBaseCMapper orderBaseCMapper;
    private final PgOrderBaseService pgOrderBaseService;
    private final CommonLogic commonLogic;

    /**
     * 首页工单统计
     * @return
     */
    public OrderBaseCountBean baseCountBean(){
        AdminUserCom adminUser = commonLogic.getAdminUser();
        OrderBaseCountBean orderBaseCountBean = orderBaseCMapper
                .countOrderBaseIndex(adminUser.getCompanyWechatId(), adminUser.getAdminWebsitIds(),adminUser.getType());

        List<PgOrderBase> pgOrderBaseList = pgOrderBaseService.lambdaQuery()
                .select(PgOrderBase::getOrderType, PgOrderBase::getOrderTypeText, PgOrderBase::getCreateTime, PgOrderBase::getId
                        , PgOrderBase::getOrderStatus,PgOrderBase::getAppointmentTime)
                .between(PgOrderBase::getCreateTime, DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -30)), new Date())
                .in(CollectionUtils.isNotEmpty(adminUser.getCompanyWechatIds()),PgOrderBase::getCompanyWechatId,adminUser.getCompanyWechatId())
                .and(CollectionUtils.isNotEmpty(adminUser.getAdminWebsitIds()),
                        v -> v.in(PgOrderBase::getWebsitId,adminUser.getAdminWebsitIds()).or().in(PgOrderBase::getCreateWebsitId,adminUser.getAdminWebsitIds()))
                .list();

        Map<String,List<Integer>> qtddqs = new HashMap<>();

        List<Integer> xzgdList = new ArrayList<>();
        List<Integer> wgdList = new ArrayList<>();
        List<Integer> jxzList = new ArrayList<>();
        List<Integer> qtsmList = new ArrayList<>();
        for (int i = 6; i >= 0; i--) {
            Integer day = i*-1;
            int xzgd = pgOrderBaseList.stream().filter(item -> item.getCreateTime().before(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), (day)))) &&
                    item.getCreateTime().after(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), (day))))
            ).collect(Collectors.toList()).size();
            xzgdList.add(xzgd);
            int wgd = pgOrderBaseList.stream().filter(item -> item.getCreateTime().before(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), (day)))) &&
                    item.getCreateTime().after(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), (day))))
                    && (item.getOrderStatus().equals(OrderBaseStatusEnum.YWG.getKey()) || item.getOrderStatus().equals(OrderBaseStatusEnum.YJS.getKey()))

            ).collect(Collectors.toList()).size();
            wgdList.add(wgd);
            int jxz = pgOrderBaseList.stream().filter(item -> item.getCreateTime().before(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), (day)))) &&
                    item.getCreateTime().after(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), (day))))
                    && !(item.getOrderStatus().equals(OrderBaseStatusEnum.YWG.getKey()) || item.getOrderStatus().equals(OrderBaseStatusEnum.YJS.getKey())
                    || item.getOrderStatus().equals(OrderBaseStatusEnum.YQX.getKey()))
            ).collect(Collectors.toList()).size();
            jxzList.add(jxz);



        }

        for (int i = 0; i < 7; i++) {
            Integer day = i;
            int qtsm = pgOrderBaseList.stream().filter(item -> item.getAppointmentTime() != null && item.getAppointmentTime().before(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), (day)))) &&
                    item.getAppointmentTime().after(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), (day))))

            ).collect(Collectors.toList()).size();

            qtsmList.add(qtsm);

        }
        qtddqs.put("新增工单",xzgdList);
        qtddqs.put("完工工单",wgdList);
        qtddqs.put("进行中",jxzList);

        orderBaseCountBean.setQtddqs(qtddqs);
        orderBaseCountBean.setQtsm(qtsmList);

        List<String> collect = pgOrderBaseList.stream().map(PgOrderBase::getOrderTypeText).distinct().collect(Collectors.toList());

        Map<String,List<Integer>> wglxfb = new HashMap<>();
        for (String orderTypeText : collect) {

            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < 30; i++) {
                Integer day = i*-1;

                int num = pgOrderBaseList.stream().filter(item -> item.getCreateTime().before(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), (day)))) &&
                        item.getCreateTime().after(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), (day))))
                        && item.getOrderTypeText().equals(orderTypeText)
                ).collect(Collectors.toList()).size();
                list.add(num);
            }

            Collections.reverse(list);

            wglxfb.put(orderTypeText,list);

        }


        orderBaseCountBean.setWglxfb(wglxfb);

        return orderBaseCountBean;
    }

    /**
     * 统计近7天的订单趋势和需上门数量
     * @return
     */
//    public OrderCount7DayBean last7DayCount(){
//        AdminUserCom adminUser = commonLogic.getAdminUser();
//        OrderCount7DayBean orderCount7DayBean = new OrderCount7DayBean();
//
//        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
//        //最近7天的日期
//        orderCount7DayBean.setDates(
//            Lists.newArrayList(
//                    sdf.format(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(),0))),
//                    sdf.format(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(),1))),
//                    sdf.format(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(),2))),
//                    sdf.format(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(),3))),
//                    sdf.format(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(),4))),
//                    sdf.format(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(),5))),
//                    sdf.format(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(),6))))
//        );
//
//        //最近7天的制单时间 key = time,total
//        List<Map<String,Long>> countList1 = orderBaseCMapper.countLast7Day1(adminUser.getCompanyWechatId(), adminUser.getAdminWebsitIds());
//        List<Map<String,Long>> countList2 = orderBaseCMapper.countLast7Day1(adminUser.getCompanyWechatId(), adminUser.getAdminWebsitIds());
//
//        return orderCount7DayBean;
//    }

    /**
     * 未来7天需上门数量
     * @return
     */
//    public OrderCount7DayBean feature7DayCount(){
//        AdminUserCom adminUser = commonLogic.getAdminUser();
//        OrderCount7DayBean orderCount7DayBean = new OrderCount7DayBean();
//
//        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
//        return orderCount7DayBean;
//    }

}
