package cn.pluss.platform.controller;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.constants.ProjectConstant;
import cn.pluss.platform.entity.Notice;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.notice.NoticeService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notice")
@Transactional
@Api(value = "Notice控制类", tags = {"Notice接口"})
public class NoticeController {

    @Resource
    private NoticeService noticeService;
    @Resource
    private UserAppService userAppService;

    @GetMapping
    @ApiOperation(tags = {"页面-收益通知", "页面-审核通知"}, value = "获取全部", notes = "返回分页过后的数据", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询页码", paramType = "query", dataType = "Integer", defaultValue = "0"),
            @ApiImplicitParam(name = "size", value = "每页数据量", paramType = "query", dataType = "Integer", defaultValue = "0"),
            @ApiImplicitParam(name = "typeFirst", value = "通知类型", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "userId", value = "指定人员的Id", paramType = "query", dataType = "String", required = true)})
    public Result<PageInfo<Notice>> list(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer size, Integer typeFirst, String userId) {
        UserApp userApp = userAppService.queryUserAppByToken();
        userId = userApp.getUserId() + "";
        
        if (size == null || size == 0) {
            size = ProjectConstant.PAGE_SIZE;
        }
        if (page == null || page == 0) {
            page = 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", size);
        map.put("offset", (page - 1) * size);
        map.put("typeFirst", typeFirst);
        map.put("userId", userId);
        List<Notice> noticeList = noticeService.queryNoticePage(map);
        for (Notice notice : noticeList) {
            notice.setIsDeal(1);
            noticeService.updateNotice(notice);

            if (notice.getTypeSecond().equals(Notice.TYPE_MER_OUT_LINE) || notice.getTypeSecond().equals(Notice.TYPE_MER_EXCEPTION) || notice.getTypeSecond().equals(Notice.TYPE_MER_FREEZE)) {
                notice.setTypeSecond(-1);
            }
        }

        Integer count = noticeService.queryNoticePageCount(map);
        // 返回参数
        PageInfo pager = new PageInfo<>(count.longValue(), (long) StringUtil.getPageCount(count, size), page.longValue(), size.longValue(), noticeList);
        return ResultGenerator.genSuccessResult(pager);
    }

    /**
     * queryIsDeal:(查询当前用户的审核消息未读数量). <br/>
     *
     * @return
     * @author Administrator
     * @since JDK 1.8
     */
    @PostMapping("/queryIsDeal")
    @ApiOperation(value = "查询当前用户的审核消息未读数量", notes = "查询当前用户的审核消息未读数量", httpMethod = "POST")
    public Result queryIsDeal(@RequestBody Notice notice) {
        Map<String, Object> result = new HashMap<>(16);
        try {
            notice.setIsDeal(0);
            notice.setTypeFirst(notice.getTypeFirst() == null ? 1 : notice.getTypeFirst());
            notice.setUserId(notice.getUserId());
            List<Notice> queryNoticeList = noticeService.queryNoticeList(notice);
            result.put("count", queryNoticeList.size());
            return ResultGenerator.genSuccessResult(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult(result);
        }
    }
}
