package com.izouma.wenlvju.web.performance;

import com.izouma.wenlvju.domain.performance.Programme;
import com.izouma.wenlvju.dto.ArrangeProgrammeDTO;
import com.izouma.wenlvju.dto.PageQuery;
import com.izouma.wenlvju.dto.ProgrammeDTO;
import com.izouma.wenlvju.dto.ProgrammeScoreDTO;
import com.izouma.wenlvju.enums.SignedIn;
import com.izouma.wenlvju.exception.BusinessException;
import com.izouma.wenlvju.repo.performance.ProgrammeRepo;
import com.izouma.wenlvju.service.performance.ProgrammeService;
import com.izouma.wenlvju.utils.ObjUtils;
import com.izouma.wenlvju.utils.SecurityUtils;
import com.izouma.wenlvju.utils.excel.ExcelUtils;
import com.izouma.wenlvju.web.BaseController;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/programme")
@AllArgsConstructor
public class ProgrammeController extends BaseController {
    private ProgrammeService programmeService;
    private ProgrammeRepo    programmeRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public Programme save(@RequestBody Programme record) {
        if (record.getId() != null) {
            Programme orig = programmeRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return programmeRepo.save(orig);
        }
        return programmeRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<Programme> all(@RequestBody PageQuery pageQuery) {
        return programmeService.all(pageQuery);
    }

    @PostMapping("/backAll")
    public Page<ProgrammeDTO> backAll(@RequestBody PageQuery pageQuery) {
        return programmeService.backAll(pageQuery);
    }

    @GetMapping("/get/{id}")
    public Programme get(@PathVariable Long id) {
        return programmeRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        programmeRepo.softDelete(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<ProgrammeDTO> data = programmeService.toDTOList(all(pageQuery).getContent());
        ExcelUtils.export(response, data);
    }

    @GetMapping(value = "/excelTemp", produces = "application/vnd.ms-excel;charset=utf-8")
    public void excelTemp(HttpServletResponse response) throws IOException {
        ExcelUtils.export1(response, ProgrammeDTO.class);
//        programmeService.excelTemp(response);
    }

    @PostMapping("/upload")
    public void uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            programmeService.upload(file, SecurityUtils.getAuthenticatedUser().getId());
        } catch (IOException e) {
            log.error("上传失败", e);
            throw new BusinessException("上传失败", e.getMessage());
        }
    }

    @ApiOperation("移出分组")
    @PostMapping("/removeGroup/{id}")
    public void removeGroup(@PathVariable Long id) {
        programmeService.removeArrange(id);
    }

    @ApiOperation("加入分组")
    @PostMapping("/intoGroup")
    public void intoGroup(@RequestParam Long id, @RequestParam Long arrangeId) {
        programmeService.add(id, arrangeId);
    }

    @ApiOperation("未分组")
    @PostMapping("/unGrouped")
    public List<Programme> unGrouped(@RequestParam Long performanceId) {
        return programmeService.ungrouped(performanceId);
    }

    @ApiOperation("签到列表")
    @PostMapping("/byArrange")
    public Page<ArrangeProgrammeDTO> byArrange(@RequestBody PageQuery pageQuery) {
        pageQuery.setSort("allSigned,asc;arrangeId,desc;gradingOrganizationId,asc;organizationId,asc;");
//        Map<String, Object> query = pageQuery.getQuery();
//        query.put("status", 2);
        return programmeService.byArrange(pageQuery);
    }

    @ApiOperation("签到")
    @PostMapping("/signIn")
    public void signIn(@RequestParam Long id, @RequestParam SignedIn signedIn, String description) {
        programmeService.signIn(id, signedIn, description);
    }

    @PostMapping("/byExpert")
    @ApiOperation("专家手机端查看")
    public Page<ProgrammeScoreDTO> byExpert(@RequestBody PageQuery pageQuery) {
        return programmeService.byExpert(pageQuery, SecurityUtils.getAuthenticatedUser().getId());
    }


    @PostMapping("/byScore")
    @ApiOperation("专家电脑端查看")
    public Page<ProgrammeScoreDTO> byScore(@RequestBody PageQuery pageQuery) {
        return programmeService.byScore(pageQuery, SecurityUtils.getAuthenticatedUser().getId());
    }

    @PostMapping("/getDTO/{id}")
    @ApiOperation("专家手机端查看详情")
    public ProgrammeScoreDTO getDTO(@PathVariable Long id) {
        return programmeService.getDTO(id, SecurityUtils.getAuthenticatedUser().getId());
    }
}

