package xiaoqu.home.open.task;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import xiaoqu.home.open.model.db1.Toilet;
import xiaoqu.home.open.model.db1.UserProper;
import xiaoqu.home.open.model.db2.*;
import xiaoqu.home.open.publicBean.MessageNotification;
import xiaoqu.home.open.service.RedisService4;
import xiaoqu.home.open.service.db1.ToiletService;
import xiaoqu.home.open.service.db1.UserProperService;
import xiaoqu.home.open.service.db2.*;
import xiaoqu.home.open.utils.GeTuiUtils;
import xiaoqu.home.open.utils.WrapperUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j(topic = "task")
public class TaskPushMessage {
    @Autowired
    private RedisService4 redisService4;
    @Autowired
    private TaskUserService taskUserService;
    @Autowired
    private TaskScenesService taskScenesService;

    @Autowired
    private UserProperService userProperService;
    @Autowired
    private UserPushService userPushService;
    @Autowired
    private PushLogService pushLogService;
    @Autowired
    private ToiletService toiletService;
    @Autowired
    private UserToServiceAreaService userToServiceAreaService;

//  任务状态-1=不生成任务仅通知，0=已生成,1=已匹配,2=待抢单,3=已接单,4=作业中,5=按时完成,6=超时完成,7=人工取消,8=系统取消,9=未完成")

    @Scheduled(cron = "* * * * * ?")
    public void taskPushTask() {
        String pushJson = redisService4.rightPop("taskPushQueue");
        while (pushJson != null) {
            log.info("【taskPushTask】" + pushJson);
            MessageNotification taskPushInfo = JSONObject.parseObject(pushJson, new TypeReference<MessageNotification>() {
            });

            List<Long> userIds = new ArrayList<>();
            if (taskPushInfo.getStatus() > 0) {
                QueryWrapper<TaskUser> tuwrapper = WrapperUtils.getWrapper(false);
                tuwrapper.eq("task_id", taskPushInfo.getTaskId());
                List<TaskUser> list = taskUserService.list(tuwrapper);
                //需修改
                if (!list.isEmpty()) {
                    if (list.size() == 1 && list.get(0).getUserId() == 0) {
                        QueryWrapper<UserProper> wrapperUP = WrapperUtils.getWrapper(false);
                        wrapperUP.eq("district_id", taskPushInfo.getDistrictId());
                        wrapperUP.in("type", 0, 1);// 保洁和物业
                        List<UserProper> userPropers = userProperService.list(wrapperUP);
                        for (UserProper userProper : userPropers) {
                            userIds.add(userProper.getUserId());
                        }
                    } else {
                        for (TaskUser taskUser : list) {
                            userIds.add(taskUser.getUserId());
                        }
                    }
                }
            } else if (taskPushInfo.getStatus() == -1) {
                QueryWrapper<UserToServiceArea> wrapperUTSA = WrapperUtils.getWrapper(false);
                wrapperUTSA.eq("service_type",1);
                wrapperUTSA.eq("service_area_id",taskPushInfo.getTaskId());
                wrapperUTSA.eq("grid_scenes_level",1);
                wrapperUTSA.eq("district_id",taskPushInfo.getDistrictId());
                List<UserToServiceArea> list = userToServiceAreaService.list(wrapperUTSA);
                if(!list.isEmpty()){
                    for (UserToServiceArea user:list                         ) {
                        userIds.add(user.getUserId());
                    }
                }

            }


            QueryWrapper<UserPush> wrapperUserPush = WrapperUtils.getWrapper(false);
            Date date = new Date();

            if (!userIds.isEmpty()) {
                wrapperUserPush.in("user_id", userIds);
            } else {
                //无通知人
                pushJson = redisService4.rightPop("taskPushQueue");
                continue;
            }
            List<UserPush> userPushList = userPushService.list(wrapperUserPush);
            String headString = null;
            StringBuffer text = new StringBuffer();
            switch (taskPushInfo.getStatus()) {
                case 1:
                    headString = "【保洁任务】新任务消息";
                    break;
                case 2:
                    headString = "【保洁任务】抢单池";
                    text.append("【待抢单】").append(taskPushInfo.getTaskName());
                    QueryWrapper<TaskScenes> tswrapper2 = WrapperUtils.getWrapper(false);
                    tswrapper2.eq("task_id", taskPushInfo.getTaskId());
                    List<TaskScenes> tslist2 = taskScenesService.list(tswrapper2);
                    if (!tslist2.isEmpty()) {
                        setText(text, tslist2);
                    }

                    break;
                case 3:
                    headString = "【保洁任务】新任务消息";
                    text.append("您有新任务工单，").append(taskPushInfo.getTaskName());
                    QueryWrapper<TaskScenes> tswrapper3 = WrapperUtils.getWrapper(false);
                    tswrapper3.eq("task_id", taskPushInfo.getTaskId());
                    List<TaskScenes> tslist3 = taskScenesService.list(tswrapper3);
                    if (!tslist3.isEmpty()) {
                        setText(text, tslist3);
                    }
                    break;
                case 7:
                    headString = "【保洁任务】任务取消消息";
                    text.append("【已取消】").append(taskPushInfo.getTaskName());
                    QueryWrapper<TaskScenes> tswrapper7 = WrapperUtils.getWrapper(false);
                    tswrapper7.eq("task_id", taskPushInfo.getTaskId());
                    List<TaskScenes> tslist7 = taskScenesService.list(tswrapper7);
                    if (!tslist7.isEmpty()) {
                        setText(text, tslist7);
                    }
                    break;
                case 8:
                    headString = "【保洁任务】任务取消消息";
                    text.append("【已取消】").append(taskPushInfo.getTaskName());
                    QueryWrapper<TaskScenes> tswrapper8 = WrapperUtils.getWrapper(false);
                    tswrapper8.eq("task_id", taskPushInfo.getTaskId());
                    List<TaskScenes> tslist8 = taskScenesService.list(tswrapper8);
                    if (!tslist8.isEmpty()) {
                        setText(text, tslist8);
                    }
                    break;
                case -1:
                    headString = "新消息提醒";
                    text.append("您有一个").append(taskPushInfo.getTaskName()).append("提醒");
                    //厕所id
                    Toilet toilet = toiletService.getById(taskPushInfo.getTaskId());
                    if (toilet != null) {
                        text.append(",").append(toilet.getSuperiorGridName()).append("->").append(toilet.getName()).append("。");
                    }

                    break;
            }

            for (UserPush userPush : userPushList) {
                GeTuiUtils.uniPushOnLine(headString, text.substring(0, text.length() - 1), null, "", userPush.getClientId());
                PushLog pushLog = new PushLog();
                pushLog.setClientId(userPush.getClientId());
                pushLog.setUserId(userPush.getUserId());
                pushLog.setCreateTime(date);
                pushLog.setDistrictId(taskPushInfo.getDistrictId());
                pushLog.setMemo(text.toString());
                pushLog.setTitle(headString);
                pushLog.setStatus(0);
                pushLogService.save(pushLog);
            }
            pushJson = redisService4.rightPop("taskPushQueue");
        }

    }

    private void setText(StringBuffer text, List<TaskScenes> tslist8) {
        text.append("->");
        for (TaskScenes taskScenes : tslist8) {
            text.append(taskScenes.getName()).append(",");
        }
    }

}
