package com.sqx.modules.message.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Query;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.controller.app.AppLoginController;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserVip;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.message.dao.MessageInfoDao;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.orders.entity.Orders;
import com.sqx.modules.utils.HttpClientUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
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;

/**
 * 消息
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageInfoDao, MessageInfo> implements MessageService {

    @Autowired
    private AppLoginController loginController;

    @Resource
    private MessageInfoDao messageInfoDao;
    @Autowired
    private UserService userService;
    @Autowired
    private CommonInfoService commonInfoService;

    @Override
    public IPage<MessageInfo> selectMessageList(Integer page, Integer limit, MessageInfo messageInfo) {
        Page<MessageInfo> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<MessageInfo> selectPage ;
        if(!messageInfo.getState().equals("0")){
            selectPage = baseMapper.selectPage(pages, new QueryWrapper<>(messageInfo).orderByDesc("create_at"));
        }else {
            selectPage = baseMapper.selectPage(pages, new QueryWrapper<MessageInfo>().orderByDesc("create_at"));
        }

        for(MessageInfo messageInfo1:selectPage.getRecords()){
            if(StringUtils.isNotEmpty(messageInfo1.getIsSee()) && "0".equals(messageInfo1.getIsSee())){
                messageInfo1.setIsSee("2");
                baseMapper.updateById(messageInfo1);
            }
        }
        return selectPage;
    }

    @Override
    public int saveBody(MessageInfo messageInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        if(messageInfo.getUserId()!=null){
            UserEntity userEntity = userService.getById(messageInfo.getUserId());
            messageInfo.setUserName(userEntity.getUserName());
        }
        messageInfo.setCreateAt(sdf.format(now));
        return messageInfoDao.insert(messageInfo);
    }

    @Override
    public int update(MessageInfo messageInfo) {
        return messageInfoDao.updateById(messageInfo);
    }

    @Override
    public int delete(Long id) {
        return messageInfoDao.deleteById(id);
    }

    @Override
    public MessageInfo selectMessageById(Long id) {
        return messageInfoDao.selectById(id);
    }

    @Override
    public int updateSendState(Long userId,String state) {
        return messageInfoDao.updateSendState(userId,state);
    }

    @Override
    public Result getOrderMessage(Long userId) {
        List<MessageInfo> selectList = baseMapper.selectList(new QueryWrapper<MessageInfo>().eq("user_id", userId).eq("is_see", "0").eq("state", 7));
        for (MessageInfo messageInfo : selectList) {
            messageInfo.setIsSee("2");
            baseMapper.updateById(messageInfo);
        }
        return Result.success().put("data", selectList.size());


    }

    @Override
    public Result auditMessage(String ids, Integer status, String auditContent) {
        for (String id : ids.split(",")) {
            MessageInfo messageInfo = baseMapper.selectById(Long.parseLong(id));
            messageInfo.setUrl(String.valueOf(status));
            baseMapper.updateById(messageInfo);
        }
        return Result.success();
    }

    //重置腾讯地图key状态
    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨0点0分0秒执行
    public void userVipCheck() {
        baseMapper.update(null, Wrappers.<MessageInfo>lambdaUpdate()
                .set(MessageInfo::getIsSee,1)
                .set(MessageInfo::getPlatform,"")
                .eq(MessageInfo::getState,12));
    }

}
