package com.jeeplus.test.roster.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.constant.enums.LogTypeEnum;
import com.jeeplus.test.oss.service.OssService;
import com.jeeplus.test.rank.domain.Rank;
import com.jeeplus.test.rank.domain.RankRRs;
import com.jeeplus.test.rank.domain.RankSequence;
import com.jeeplus.test.rank.service.RankRRSService;
import com.jeeplus.test.rank.service.RankSequenceService;
import com.jeeplus.test.rank.service.RankService;
import com.jeeplus.test.rank.service.dto.RankDTO;
import com.jeeplus.test.rank.service.dto.RankSequenceDTO;
import com.jeeplus.test.rank.service.mapstruct.RankSequenceWrapper;
import com.jeeplus.test.rank.service.mapstruct.RankWrapper;
import com.jeeplus.test.roster.domain.RosterBase;
import com.jeeplus.test.roster.service.RosterBaseService;
import com.jeeplus.test.roster.service.dto.RosterBaseDTO;
import com.jeeplus.test.roster.service.mapstruct.RosterBaseWrapper;
import com.jeeplus.test.user.service.dto.FileUrlDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api("花名册")
@RestController
@RequestMapping(value = "/roster/base")
public class RosterBaseController {

    @Autowired
    private RosterBaseService rosterBaseService;

    @Autowired
    private OssService ossService;

    /**
     * 查询花名册列表
     * @param rosterBaseDTO
     * @param page
     * @return
     * @throws Exception
     */
    @ApiLog("查询花名册列表")
    @PreAuthorize("hasAuthority('roster:base:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<RosterBaseDTO>> data(RosterBaseDTO rosterBaseDTO, Page<RosterBaseDTO> page) throws Exception {
        RosterBase rosterBase = RosterBaseWrapper.INSTANCE.toEntity(rosterBaseDTO);
        QueryWrapper<RosterBase> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( rosterBase, RosterBase.class );
        IPage<RosterBaseDTO> result = rosterBaseService.findList (page,queryWrapper);
        return ResponseEntity.ok (result);
    }

    /**
     * 查询花名册数据
     * @param id
     * @return
     */
    @ApiLog("查询花名册详情")
    @PreAuthorize ("hasAnyAuthority('roster:base:view','roster:base:add','roster:base:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        RosterBaseDTO rosterBaseDTO = rosterBaseService.queryById ( id );
        return ResponseEntity.ok (rosterBaseDTO);
    }

    /**
     * 添加花名册
     * @param rosterBaseDTO
     * @return
     */
    @ApiLog(value = "修改/新增花名册", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('roster:base:save','roster:base:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody RosterBaseDTO rosterBaseDTO, HttpServletRequest request) {
        return rosterBaseService.saveRoster(rosterBaseDTO);
    }

    /**
     * 删除花名册
     * @param ids
     * @return
     */
    @ApiLog(value = "删除花名册", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('roster:base:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids,HttpServletRequest request) {
        String idArray[] =ids.split(",");
        rosterBaseService.removeByIds (Lists.newArrayList (idArray));
        return ResponseEntity.ok ("删除花名册成功");
    }

    /**
     * 上传文件
     * @return
     * @throws IOException
     */
    @ApiOperation("上传文件")
    @RequestMapping("webUpload/upload")
    public ResponseEntity<FileUrlDto> webUpload(HttpServletRequest request, MultipartFile file) throws IOException {
        String dir = "roster";
        FileUrlDto dto = ossService.webUpload(file,dir);
        return ResponseEntity.ok(dto);
    }

}
