package com.ghy.quartz.service.impl;

import com.ghy.common.enums.OrderStatus;
import com.ghy.common.enums.WxMsgEnum;
import com.ghy.common.utils.WechatMsgUtils;
import com.ghy.customer.domain.CustomerAddress;
import com.ghy.customer.service.CustomerAddressService;
import com.ghy.order.domain.OrderMaster;
import com.ghy.order.service.OrderMasterService;
import com.ghy.quartz.service.OrderAlertTaskService;
import com.ghy.worker.domain.Worker;
import com.ghy.worker.domain.WorkerArea;
import com.ghy.worker.service.WorkerAreaService;
import com.ghy.worker.service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class OrderAlertTaskServiceImpl implements OrderAlertTaskService {

    @Autowired
    private OrderMasterService orderMasterService;

    @Autowired
    private CustomerAddressService customerAddressService;

    @Autowired
    private WorkerAreaService workerAreaService;

    @Autowired
    private WorkerService workerService;

    @Override
    public void getSuitableOrderAlertWorker() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startTime = format.format(new Date()) + ":00:00";
        String endTime = format.format(new Date()) + ":59:59";
        // 找到合适时间的订单
        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setDeptId(101L);
        orderMaster.setOrderStatus(OrderStatus.RECEIVE.code());
        orderMaster.setWorkerId(-1L);
        orderMaster.setCreateTimeStart(LocalDateTime.parse(startTime, df));
        orderMaster.setCreateTimeEnd(LocalDateTime.parse(endTime, df));
        List<OrderMaster> orderMasters = orderMasterService.selectOrderMasterList(orderMaster);
        if(CollectionUtils.isNotEmpty(orderMasters)){
            List<Long> addressList = orderMasters.stream().map(OrderMaster::getAddressId).collect(Collectors.toList());
            CustomerAddress customerAddress = new CustomerAddress();
            customerAddress.setCustomerAddressIds(addressList);
            List<CustomerAddress> customerAddresses = customerAddressService.getCustomerAddressList(customerAddress);
            if(CollectionUtils.isNotEmpty(customerAddresses)){
                List<Long> cityIds = customerAddresses.stream().map(CustomerAddress::getCityId).collect(Collectors.toList());
                WorkerArea workerArea = new WorkerArea();
                workerArea.setCityIds(cityIds);
                List<WorkerArea> workerAreaList = workerAreaService.getWorkerAreaList(workerArea);
                List<Long> workIds = workerAreaList.stream().map(WorkerArea::getWorkerId).distinct().collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(workIds)){
                    workIds.forEach(model->{
                        // 通知师傅新订单
                        try {
                            Worker worker = workerService.selectById(model);
                            // 消息组装。
                            Map<String, Object> paramsNew = new HashMap<>();
                            // 订单编号
                            paramsNew.put("thing21", "订单大厅/需求大厅有新订单");
                            // 名称
                            paramsNew.put("thing31", "请进入【需求大厅】查看或操作处理");
                            paramsNew.put("thing14", "newOrer");
                            // 预约时间
                            paramsNew.put("time15", com.ghy.common.utils.DateUtils.parseDateToStr("yyyy年MM月dd日 HH:mm", new Date()));
                            WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), worker.getWxOpenId(), WxMsgEnum.DEFAULT_ORDER, paramsNew);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                }
            }
        }


        orderMaster.setCreateTimeStart(null);
        orderMaster.setCreateTimeEnd(null);
        orderMaster.setUpdateTimeStart(LocalDateTime.parse(startTime, df));
        orderMaster.setUpdateTimeEnd(LocalDateTime.parse(endTime, df));
        List<OrderMaster> updateOrderMasters = orderMasterService.selectOrderMasterList(orderMaster);
        if(CollectionUtils.isNotEmpty(updateOrderMasters)){
            List<Long> addressList = updateOrderMasters.stream().map(OrderMaster::getAddressId).collect(Collectors.toList());
            CustomerAddress customerAddress = new CustomerAddress();
            customerAddress.setCustomerAddressIds(addressList);
            List<CustomerAddress> customerAddresses = customerAddressService.getCustomerAddressList(customerAddress);
            if(CollectionUtils.isNotEmpty(customerAddresses)){
                List<Long> cityIds = customerAddresses.stream().map(CustomerAddress::getCityId).collect(Collectors.toList());
                WorkerArea workerArea = new WorkerArea();
                workerArea.setCityIds(cityIds);
                List<WorkerArea> workerAreaList = workerAreaService.getWorkerAreaList(workerArea);
                List<Long> workIds = workerAreaList.stream().map(WorkerArea::getWorkerId).distinct().collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(workIds)){
                    workIds.forEach(model->{
                        // 通知师傅新订单
                        try {
                            Worker assignWorker = workerService.selectById(model);
                            // 消息组装。
                            Map<String, Object> paramsNew = new HashMap<>();
                            // 订单编号
                            paramsNew.put("character_string6", "newOrder");
                            // 名称
                            paramsNew.put("thing14", "需求大厅有新订单哦,请尽快查看");
                            // 预约时间
                            paramsNew.put("time4", com.ghy.common.utils.DateUtils.parseDateToStr("yyyy年MM月dd日 HH:mm", new Date()));
                            // 消息推送
                            WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), assignWorker.getWxOpenId(), WxMsgEnum.TEXT, paramsNew);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                }
            }
        }
    }
}
