package com.huiju.estateagents.property.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.property.dao.MessageMapper;
import com.huiju.estateagents.property.model.Message;
import com.huiju.estateagents.property.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 物业端消息表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-03-05
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Override
    public ResponseBean messageLiset(String parameter, Integer communityId,Integer user) {
        ResponseBean response= new  ResponseBean();
        JSONObject object = JSONObject.parseObject(parameter);
        //待办消息类型(-1:论坛，-2:工单，-3:缴费)
        String  messageType = object.getString("messageType");
        //0:无效，1:有效
        String  status = object.getString("status");
        Date createDate = object.getDate("createDate");

        String  messageContent = object.getString("messageContent");
        Integer pageNum = object.getInteger("pageNum");
        Integer pageSize = object.getInteger("pageSize");

        Page<Message> page = new Page<>();
        page.setSize(pageSize == null ? 10 : pageSize);
        page.setCurrent(pageNum == null ? 1 : pageNum);
        IPage<Message> pageInfo = messageMapper.messageLiset(page,messageType,status,createDate,messageContent,user);
        List<Message> ticketList = pageInfo.getRecords();
        HashMap map=new HashMap<>();
        map.put("list",ticketList);
        map.put("total", pageInfo.getTotal());
        map.put("pageNum", pageInfo.getCurrent());
        map.put("pageSize", pageInfo.getSize());
        response.addSuccess(map);
        return response;
    }

    @Override
    public ResponseBean myMessageList(String parameter, Integer communityId, Integer user) {
        ResponseBean response= new  ResponseBean();
        JSONObject object = JSONObject.parseObject(parameter);
        //待办消息类型(-1:论坛，-2:工单，-3:缴费)
        String  messageType = object.getString("messageType");
        //0:无效，1:有效
        String  readStatus = object.getString("readStatus");
        Date createDate = object.getDate("createDate");

        String  messageContent = object.getString("messageContent");
        Integer pageNum = object.getInteger("pageNum");
        Integer pageSize = object.getInteger("pageSize");

        Page<Message> page = new Page<>();
        page.setSize(pageSize == null ? 10 : pageSize);
        page.setCurrent(pageNum == null ? 1 : pageNum);
        IPage<Message> pageInfo = messageMapper.myMessageList(page,messageType,readStatus,createDate,messageContent,user);
        List<Message> ticketList = pageInfo.getRecords();
        HashMap map=new HashMap<>();
        map.put("list",ticketList);
        map.put("total", pageInfo.getTotal());
        map.put("pageNum", pageInfo.getCurrent());
        map.put("pageSize", pageInfo.getSize());
        response.addSuccess(map);
        return response;
    }

    @Override
    public ResponseBean delectRead(String parameter, Integer userID) {
        ResponseBean response = new ResponseBean();
        JSONObject parseObject = JSONObject.parseObject(parameter);

        JSONArray message = parseObject.getJSONArray("id");
        Integer[] messageListID = message.toArray(new Integer[]{});
        for (Integer id : messageListID) {
            Message mes= new Message();
                    mes.setId(id);
                    mes.setReadStatus("1");
                    mes.setUpdateDate(LocalDateTime.now());
                    mes.setUpdateUser(userID);
                    messageMapper.updateById(mes);
        }
                response.addSuccess("成功");
                return response;
    }
}
