package com.izouma.wenlvju.web;

import cn.hutool.core.util.ObjectUtil;
import com.izouma.wenlvju.domain.TrainingInstitution;
import com.izouma.wenlvju.dto.PageQuery;
import com.izouma.wenlvju.dto.TrainingInstitutionDTO;
import com.izouma.wenlvju.exception.BusinessException;
import com.izouma.wenlvju.repo.TrainingInstitutionRepo;
import com.izouma.wenlvju.service.TrainingInstitutionService;
import com.izouma.wenlvju.utils.ObjUtils;
import com.izouma.wenlvju.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
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.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/trainingInstitution")
@AllArgsConstructor
public class TrainingInstitutionController extends BaseController {
    private TrainingInstitutionService trainingInstitutionService;
    private TrainingInstitutionRepo    trainingInstitutionRepo;

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

            orig.setSubmit(true);
            if (ObjectUtil.isNull(orig.getFirstWrite())) {
                orig.setFirstWrite(LocalDateTime.now());
            }
            return trainingInstitutionRepo.save(orig);
        }
        return trainingInstitutionRepo.save(record);
    }


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

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

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

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

    @GetMapping("/excelWrite")
    @ResponseBody
    public void excelWrite(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<TrainingInstitution> data = trainingInstitutionService.fillIn(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @GetMapping("/excel1")
    @ResponseBody
    public void excel1(HttpServletResponse response) throws IOException {
        List<TrainingInstitution> data = trainingInstitutionRepo.findAllByPhoneIsNull();
        List<TrainingInstitutionDTO> dtos = data.stream()
                .map(TrainingInstitutionDTO::new)
                .collect(Collectors.toList());
        ExcelUtils.export(response, dtos);
    }

    @GetMapping("/batchSend")
    public void batchSend() throws InterruptedException {
        List<String> phones = trainingInstitutionRepo.findAllBySubmitFalseAndPhoneIsNotNull();
        trainingInstitutionService.batchSend(phones);
    }

    @PostMapping("/upload")
    public void uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        trainingInstitutionService.upload(file);
    }

    @PostMapping("/name")
    public Page<String> name(@RequestBody PageQuery pageQuery) {
        return trainingInstitutionService.all(pageQuery).map(TrainingInstitution::getName);
    }

    @PostMapping("/byUscc")
    public TrainingInstitution byUscc(@RequestParam String name, @RequestParam String uscc) {
        return trainingInstitutionService.byUscc(name, uscc);
    }
}

