package com.izouma.dingdong.web;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.izouma.dingdong.domain.MoneyRecord;
import com.izouma.dingdong.service.MoneyRecordService;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.MoneyRecordRepo;
import com.izouma.dingdong.utils.ObjUtils;
import com.izouma.dingdong.utils.SecurityUtils;
import com.izouma.dingdong.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/moneyRecord")
@AllArgsConstructor
public class MoneyRecordController extends BaseController {
    private MoneyRecordService moneyRecordService;
    private MoneyRecordRepo moneyRecordRepo;

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


    //@PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<MoneyRecord> all(PageQuery pageQuery) {
        return moneyRecordRepo.findAll(toSpecification(pageQuery, MoneyRecord.class), toPageRequest(pageQuery));
    }

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

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

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

    @GetMapping("/my")
    public Page<MoneyRecord> my(PageQuery pageQuery) {
//        Map<String, Object> query = pageQuery.getQuery();
//        query.put("time","2020-05-14 00:00:00,2020-05-14 13:59:59");
        Long userId = SecurityUtils.getAuthenticatedUser().getId();
        List<MoneyRecord> all = moneyRecordRepo.findAll(toSpecification(pageQuery, MoneyRecord.class));
        if (CollUtil.isEmpty(all)) {
            return null;
        }
        List<MoneyRecord> my = all.stream().filter(a ->
                a.getUserId().equals(userId)).collect(Collectors.toList());
        return new PageImpl<>(my, toPageRequest(pageQuery), pageQuery.getSize());
    }
}

