package com.bonus.canteen.core.notice.notice.v2.controller;

import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.canteen.core.common.page.PageVO;
import com.bonus.canteen.core.notice.notice.v2.dto.InformHistoryAppletPageDTO;
import com.bonus.canteen.core.notice.notice.v2.dto.InformRangeUpdateWhetherDTO;
import com.bonus.canteen.core.notice.notice.v2.service.InformHistoryService;
import com.bonus.canteen.core.notice.notice.v2.service.InformRangeService;
import com.bonus.canteen.core.notice.notice.v2.vo.InformHistoryAppletPageVO;
import com.bonus.common.houqin.encrypt.RequiresGuest;
import com.bonus.common.houqin.utils.LeRequest;
import com.github.pagehelper.page.PageMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(
        value = "用户获取临时消息",
        tags = {"用户获取临时消息"}
)
@RequestMapping({"/api/v2/applet/inform"})
public class AppletInformController extends BaseController {
    @Resource
    @Lazy
    private InformHistoryService informHistoryService;
    @Resource
    @Lazy
    private InformRangeService informRangeService;

    @PostMapping({"/queryUserInform"})
    @RequiresGuest
    @ApiOperation("用户获取通知消息")
    public PageVO<InformHistoryAppletPageVO> queryUserInform(@RequestBody InformHistoryAppletPageDTO dto) {
        PageMethod.startPage(dto);
        List<InformHistoryAppletPageVO> historyList = this.informHistoryService.queryAppletPageList(dto);
        return PageVO.of(historyList);
    }

    @PostMapping({"/queryUnreadInformNum"})
    @RequiresGuest
    @ApiOperation("获取未读消息数量")
    public AjaxResult queryUnreadInformNum(@RequestBody InformHistoryAppletPageDTO dto) {
        return AjaxResult.success(this.informRangeService.queryUnreadInformNum(dto.getCustId()));
    }

    @PostMapping({"/updateStatus"})
    @RequiresGuest
    @ApiOperation("修改通知状态")
    public AjaxResult updateWhether(@RequestBody @Valid InformRangeUpdateWhetherDTO dto) {
        int count = this.informRangeService.updateWhether(dto);
        if (count == 0) {
            return AjaxResult.error("修改通知状态失败");
        }
        return AjaxResult.success("修改通知状态成功");
    }
}
