package com.yonge.cooleshow.student.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.microsvc.toolkit.common.response.paging.PageInfo;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.microsvc.toolkit.common.response.template.R;
import com.yonge.cooleshow.biz.dal.entity.SysSuggestionType;
import com.yonge.cooleshow.biz.dal.service.SysSuggestionTypeService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.wrapper.SysSuggestionTypeWrapper;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.student:}/sysSuggestionType")
@Api(tags = "平台建议类型表")
public class SysSuggestionTypeController {

    @Autowired
    private SysSuggestionTypeService sysSuggestionTypeService;

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "详情", notes = "平台建议类型表-根据详情ID查询单条, 传入id")
    @GetMapping("/detail/{id}")
    public R<SysSuggestionType> detail(@PathVariable("id") Long id) {

        SysSuggestionType wrapper = sysSuggestionTypeService.detail(id);

        return R.from(wrapper);
    }

    @ApiOperation(value = "查询分页", notes = "平台建议类型表- 传入 SysSuggestionTypeWrapper.SysSuggestionTypeQuery")
    @PostMapping("/page")
    public R<PageInfo<SysSuggestionTypeWrapper.SysSuggestionType>> page(@RequestBody SysSuggestionTypeWrapper.SysSuggestionTypeQuery query) {

        IPage<SysSuggestionType> pages = sysSuggestionTypeService.selectPage(QueryInfo.getPage(query), query);

        Map<Long, com.yonge.cooleshow.biz.dal.entity.SysUser> idEmpMap = new HashMap<>();
        List<Long> updateIds = pages.getRecords().stream().map(SysSuggestionType::getUpdateBy)
            .filter(Objects::nonNull).distinct().collect(Collectors.toList());
        if (!updateIds.isEmpty()) {
            Map<Long, com.yonge.cooleshow.biz.dal.entity.SysUser> idEmpMapTmp = sysUserService.getMapByIds(updateIds);
            idEmpMap.putAll(idEmpMapTmp);
        }
        List<SysSuggestionTypeWrapper.SysSuggestionType> rows = pages.getRecords().stream().map(next -> {
            SysSuggestionTypeWrapper.SysSuggestionType type =
                JSON.parseObject(JSON.toJSONString(next), SysSuggestionTypeWrapper.SysSuggestionType.class);
            if (type.getUpdateBy() != null) {
                type.setOperator(idEmpMap.getOrDefault(type.getUpdateBy(), new com.yonge.cooleshow.biz.dal.entity.SysUser()).getUsername());
            }
            return type;
        }).collect(Collectors.toList());
        return R.from(QueryInfo.pageInfo(pages, rows));
    }

}
