package com.izouma.wenlvju.web;

import cn.hutool.core.util.ObjectUtil;
import com.izouma.wenlvju.domain.ArtType;
import com.izouma.wenlvju.domain.PerformanceApply;
import com.izouma.wenlvju.dto.PageQuery;
import com.izouma.wenlvju.enums.ApplyStatus;
import com.izouma.wenlvju.exception.BusinessException;
import com.izouma.wenlvju.repo.ArtTypeRepo;
import com.izouma.wenlvju.repo.PerformanceApplyRepo;
import com.izouma.wenlvju.service.PerformanceApplyService;
import com.izouma.wenlvju.utils.ObjUtils;
import com.izouma.wenlvju.utils.SecurityUtils;
import com.izouma.wenlvju.utils.excel.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/performanceApply")
@AllArgsConstructor
public class PerformanceApplyController extends BaseController {
    private final PerformanceApplyService performanceApplyService;
    private final PerformanceApplyRepo    performanceApplyRepo;
    private final ArtTypeRepo             artTypeRepo;

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


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

    @GetMapping("/get/{id}")
    public PerformanceApply get(@PathVariable Long id, String phone) {
        PerformanceApply apply = performanceApplyRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        if (ObjectUtil.isNotNull(apply.getArtTypeId())) {
            ArtType artType = artTypeRepo.findById(apply.getArtTypeId()).orElseThrow(new BusinessException("无此专业"));
            apply.setArtType(artType.getName());
        }
        performanceApplyService.getAuth(id, phone, apply.getPhone());
        return apply;
    }

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

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

    @PostMapping("/audit")
    @ApiOperation("提现审核")
    public void audit(Long id, Boolean pass) {
        performanceApplyService.audit(id, pass);
    }

    @PostMapping("/autoArrangement")
    @ApiOperation("自动编排")
    public List<PerformanceApply> autoArrangement(@RequestParam Long performanceId) {
        return performanceApplyService.autoArrangement1(performanceId);
    }

    @PostMapping("/saveArrangement")
    @ApiOperation("保存编排")
    public void saveArrangement(String applies) {
        performanceApplyService.saveArrangement(applies);
    }

    @PostMapping("/autoAwards")
    @ApiOperation("自动评奖")
    public void autoAwards(@RequestParam Long performanceId) {
        performanceApplyService.autoAwards(performanceId);
    }

    @PostMapping("/score")
    @ApiOperation("打分")
    public void score(@RequestParam Long id, @RequestParam int score) {
        performanceApplyService.score(id, score, SecurityUtils.getAuthenticatedUser().getId());
    }

    @ApiOperation("签到")
    @PostMapping("/signIn")
    public PerformanceApply signIn(@RequestParam Long id) {
        return performanceApplyService.signIn(id, SecurityUtils.getAuthenticatedUser().getId());
    }

    @ApiOperation("获取手机号/token")
    @PostMapping("/getAuth")
    public Map<String, String> getAuth(@RequestParam Long id, @RequestParam String phone) {
        PerformanceApply performanceApply = performanceApplyRepo.findById(id)
                .orElseThrow(new BusinessException("无此表演"));
        return performanceApplyService.getAuth(id, phone, performanceApply.getPhone());
    }

    @ApiOperation("获取当前节目后续列表")
    @PostMapping("/related")
    public List<PerformanceApply> related(Long id) {
        return performanceApplyService.related(id);
    }

    @ApiOperation("获取当前活动列表")
    @PostMapping("/relatedByPer")
    public List<PerformanceApply> relatedByPer(@RequestParam Long performanceId) {
        return performanceApplyRepo.findAllByStatusAndPerformanceId(ApplyStatus.PASS, performanceId)
                .stream()
                .filter(apply -> ObjectUtil.isNotNull(apply.getSignInAt()) && ObjectUtil.isNotNull(apply.getSignInBy()))
                .collect(Collectors.toList());
    }
}

