package com.ygtx.gxt.controller.mobile;

import com.ygtx.common.constant.CacheConstants;
import com.ygtx.common.core.controller.BaseController;
import com.ygtx.common.core.domain.AjaxResult;
import com.ygtx.common.core.redis.RedisCache;
import com.ygtx.common.utils.DateUtils;
import com.ygtx.gxt.domain.GxtNotifyMessage;
import com.ygtx.gxt.domain.GxtRepairOrder;
import com.ygtx.gxt.domain.GxtWorkOrder;
import com.ygtx.gxt.domain.OrderScoreInfo;
import com.ygtx.gxt.service.IGxtOrderMineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工单Controller
 *
 * @date 2025-11-12
 */
@RestController
@RequestMapping("/mobile/notify")
@Api(tags = "移动端通知管理")
public class GxtMobileNotifyController extends BaseController
{
    @Autowired
    private IGxtOrderMineService qxtOrderMineService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 获取通知信息
     */
    @ApiOperation("获取通知信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getTask(@PathVariable("id") Long id)
    {
        try {
            System.out.println("请求" + DateUtils.getTime());
            if(id != null){
                GxtRepairOrder repairOrder = new GxtRepairOrder();
                GxtWorkOrder workOrder = new GxtWorkOrder();
                List<OrderScoreInfo> orderScoreInfos= qxtOrderMineService.selectUnionOrderListMyTodo(repairOrder, workOrder, id);
                boolean isSend = false;
                boolean isSendBadge = true;

                int taskNum = 0;
                Object taskNumStr = redisCache.lGet(CacheConstants.NOTIFY_STORAGE_KEY, id);
                taskNum = orderScoreInfos.size();
                if(taskNumStr!=null){
                    int prevTaskNum = Integer.parseInt(taskNumStr.toString());
                    if(taskNum != prevTaskNum){
                        isSend = true;
                    }else{
                        isSendBadge = false;
                    }
                }else{
                    isSend = true;
                }
                redisCache.lPush(CacheConstants.NOTIFY_STORAGE_KEY,id, taskNum+"");

                // 获取申诉中的工单数量
                int appealNum = 0;
                boolean isAppealChanged = false;
                boolean isAppealBadge = true;
                int appealNumPre = 0;
                int appealNumNow = 0;
                Object appealNumStr = redisCache.lPop(CacheConstants.NOTIFY_STORAGE_APPEAL_KEY, id);
                Object appealNumPreStr = redisCache.lGet(CacheConstants.NOTIFY_STORAGE_APPEAL_PRE, id);
                Object appealNumNowStr = redisCache.lGet(CacheConstants.NOTIFY_STORAGE_APPEAL_NOW, id);
                if (appealNumStr != null) {
                    appealNum = Integer.parseInt(appealNumStr.toString());
                }
                if (appealNum != 0) {
                    isAppealChanged = true;
                    redisCache.lPush(CacheConstants.NOTIFY_STORAGE_APPEAL_PRE,id, appealNum+"");
                }
                if (appealNumPreStr != null) {
                    appealNumPre = Integer.parseInt(appealNumPreStr.toString());
                }
                if (appealNumNowStr != null) {
                    appealNumNow = Integer.parseInt(appealNumNowStr.toString());
                }
                if (appealNumPre == appealNumNow) {
                    isAppealBadge = false;
                }
                GxtNotifyMessage notifyMessage = new GxtNotifyMessage();
                if((isSend && taskNum > 0) || (isAppealChanged && appealNum > 0)) {
                    notifyMessage.setTitle(DateUtils.getHourTime() + "收到消息");
                    if (taskNum > 0 && appealNum > 0) {
                        notifyMessage.setDesc("您有" + taskNum + "个任务待处理，" + appealNum + "个申诉待处理");
                    } else if (taskNum > 0) {
                        notifyMessage.setDesc("您有" + taskNum + "个任务待处理");
                    } else {
                        notifyMessage.setDesc("您有" + appealNum + "个申诉待处理");
                    }
                }
                // 角标数字现在包括任务数量和申诉数量
                int badgeNum = taskNum + appealNumNow;
                if(!isSendBadge && !isAppealBadge){
                    badgeNum = -1; // 总数未变，不更新角标
                }
                notifyMessage.setNum(badgeNum);
                return success(notifyMessage);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return AjaxResult.success();
    }
}
