package com.reyun.saas.web.controller.user;

import com.reyun.saas.common.constants.AfficheEnumType;
import com.reyun.saas.common.framework.ApiRestController;
import com.reyun.saas.common.restResult.RestResult;
import com.reyun.saas.common.restResult.ResultGenerator;
import com.reyun.saas.mob.user.domain.AfficheAck;
import com.reyun.saas.mob.user.service.AfficheSerivce;
import com.reyun.saas.security.utils.SecurityUtils;
import com.reyun.saas.web.aop.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: hanzepeng
 * @Date: 2022/03/04/10:21
 * @Description: 公告控制层
 */
@Slf4j
@Api(value = "/affiche", tags = "公告")
@ApiRestController("affiche")
public class AfficheController {

    @Autowired
    private AfficheSerivce afficheSerivce;

    @Log(value = 4, description = "展示公告列表", type = 10)
    @ApiOperation(value = "展示公告列表",  produces = "application/json")
    @GetMapping("afficheShow")
    public RestResult afficheShow(){
        Long loginUserId = SecurityUtils.getLoginUserId();
        List<AfficheAck> afficheAcks = afficheSerivce.findAckByUserId(loginUserId);
        //收集已确认的公告
        Set<String> acks
                = afficheAcks.stream().map(AfficheAck::getAfficheName).collect(Collectors.toSet());
        //收集公告确认状态
        Map<String, Boolean> result
                = Arrays.stream(AfficheEnumType.values())
                .collect(Collectors.toMap(AfficheEnumType::getName, v -> acks.contains(v.getName()), (v1, v2) -> v1));
        return ResultGenerator.genSuccessResult(result);
    }


    @Log(value = 4, description = "确认公告", type = 10)
    @ApiOperation(value = "确认公告",  produces = "application/json")
    @PostMapping("afficheAck/{afficheName}")
    public RestResult afficheAck(@PathVariable("afficheName")String afficheName,
                                 HttpServletRequest request){
        if(StringUtils.isEmpty(afficheName)){
            log.warn("affiche name is null! 公告名称为空!");
            return ResultGenerator.genSuccessResult();
        }
        Long loginUserId = SecurityUtils.getLoginUserId();
        afficheSerivce.createAfficheAck(loginUserId,afficheName,request);
        return ResultGenerator.genSuccessResult();
    }



}
