package me.zhengjie.modules.dm.statement.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.dm.order.repository.DmOrderRecordRepository;
import me.zhengjie.modules.dm.order.service.DmOrderRecordService;
import me.zhengjie.modules.dm.order.service.dto.DmOrderRecordQueryCriteria;
import me.zhengjie.modules.dm.statement.repository.DmStatementRepository;
import me.zhengjie.modules.dm.statement.service.DmStatementService;
import me.zhengjie.modules.dm.statement.service.dto.DmStatementDto;
import me.zhengjie.modules.dm.statement.service.dto.DmStatementQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequiredArgsConstructor
@Api(tags = "statement管理")
@RequestMapping("/api/dmStatement")
public class DmStatementController {
    private final DmStatementService dmStatementService;
    private final DmOrderRecordService dmOrderRecordService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dmStatement:list')")
    public void download(HttpServletResponse response,  String stateDate, String endDate, String type, Pageable pageable) throws IOException {
        Map<String,Object>  map = dmStatementService.selectOrderRecord(stateDate,endDate,type,pageable);
        List<DmStatementDto> all = (List<DmStatementDto>)map.get("content");
        dmStatementService.download(all, response);
    }


    @GetMapping(value = "/selectStatement")
    @Log("查询部门消费")
    @ApiOperation("查询部门消费")
//    @PreAuthorize("@el.check('dmStatement:list')")
    public ResponseEntity<Object> selectStatement(String stateDate, String endDate, String type, Pageable pageable){
        return new ResponseEntity<>(dmStatementService.selectOrderRecord(stateDate,endDate,type,pageable), HttpStatus.OK);
    }
}
