package com.cweb.controller;


import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.BsMsg;
import com.hai.model.HighUserModel;
import com.hai.model.ResponseData;
import com.hai.service.BsMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author sum1dream
 */
@Controller
@RequestMapping(value = "/bsMsg")
@Api(value = "站内信")
public class BsMsgController {

    Logger log = LoggerFactory.getLogger(BsMsgController.class);

    @Resource
    private BsMsgService bsMsgService;

    @Autowired
    private UserCenter userCenter;

    @RequestMapping(value = "/getMsgByList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询站内信列表")
    public ResponseData getMsgByList(
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "companyId", required = true) String companyId , HttpServletRequest request
    ) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();


            // 查询个人信息
            Map<String, Object> mapUser = new HashMap<>();
            mapUser.put("msgType" , 1);
            mapUser.put("type" , type);
            mapUser.put("userId" , userInfoModel.getHighUser().getId());
            List<BsMsg> listUser = bsMsgService.queryMsgByList(mapUser);

            // 查询全局信息
            Map<String, Object> map = new HashMap<>();
            map.put("companyId" , companyId);
            map.put("type" , type);
            map.put("msgType" , 2);
            List<BsMsg> list = bsMsgService.queryMsgByList(map);

            list.addAll(listUser);

            list = list.stream().sorted(Comparator.comparing(BsMsg::getCreateTime).reversed())
                    .collect(Collectors.toList());

            return ResponseMsgUtil.success(list);

        } catch (Exception e) {
            log.error("BsMsgController --> getMsgByList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/checkMsg", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查看站内信")
    public ResponseData checkMsg(@RequestParam(value = "msgId", required = false) Long msgId, HttpServletRequest request) {
        try {

            // 用户
            SessionObject sessionObject = userCenter.getSessionObject(request);
            HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();

            bsMsgService.checkMsg(userInfoModel.getHighUser(), msgId);

            return ResponseMsgUtil.success(null);

        } catch (Exception e) {
            log.error("HighOrderController -> addOrder() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
