package com.bweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
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.ResponseData;
import com.hai.model.UserInfoModel;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    Logger log = LoggerFactory.getLogger(ApiProductController.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 = "jumpType", required = false) Integer jumpType,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(name = "pageNum", required = true) Integer pageNum,
            @RequestParam(name = "pageSize", required = true) Integer pageSize, HttpServletRequest request
    ) {
        try {

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

            Map<String, Object> map = new HashMap<>(5);

            map.put("type", type);
            map.put("jumpType", jumpType);
            map.put("msgType", 2);
            map.put("title", title);
            map.put("companyId" , userInfoModel.getBsCompany().getId());

            PageHelper.startPage(pageNum,pageSize);

            List<BsMsg> list = bsMsgService.getMsgByList(map);
            return ResponseMsgUtil.success(new PageInfo<>(list));

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

    @RequestMapping(value = "insertMsg" , method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增站内信")
    public ResponseData insertMsg(@RequestBody BsMsg bsMsg , HttpServletRequest request) {
        try {

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

            bsMsg.setCompanyId(userInfoModel.getBsCompany().getId());
            bsMsg.setOpId(userInfoModel.getSecUser().getId());
            bsMsg.setOpName(userInfoModel.getSecUser().getUserName());
            bsMsg.setMsgType(2);
            bsMsg.setStatus(1);
            bsMsgService.insertMsg(bsMsg , null);
            return ResponseMsgUtil.success("新增成功");

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

    @RequestMapping(value = "/pushMsg", method = RequestMethod.GET)
    @ApiOperation(value = "发布站内信")
    @ResponseBody
    public ResponseData pushMsg(@RequestParam(value = "id", required = true) Long id) {
        try {

            BsMsg bsMsg = bsMsgService.findMsg(id);
            if (bsMsg == null) {
                throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR, "为查询到相关内容！");
            }

            if (bsMsg.getStatus() !=1) {
                throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR , "当前状态错误");
            }

            bsMsg.setStatus(2);
            bsMsg.setUpdateTime(new Date());
            bsMsgService.updateMsg(bsMsg);
            return ResponseMsgUtil.success("发布成功");
        } catch (Exception e) {
            log.error("CmsContentController --> delContent() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }
}
