package com.njzscloud.supervisory.dispose.controller;

import com.njzscloud.common.core.http.constant.Mime;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.mvc.util.FileResponseUtil;
import com.njzscloud.supervisory.dispose.pojo.AuditParam;
import com.njzscloud.supervisory.dispose.pojo.DisposeRecordEntity;
import com.njzscloud.supervisory.dispose.pojo.DisposeRecordPagingParam;
import com.njzscloud.supervisory.dispose.pojo.DisposeRecordReportParam;
import com.njzscloud.supervisory.dispose.service.DisposeRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 处置记录
 */
@Slf4j
@RestController
@RequestMapping("/dispose_record")
@RequiredArgsConstructor
public class DisposeRecordController {

    private final DisposeRecordService disposeRecordService;

    /**
     * 新增
     *
     * @param disposeRecordEntity 数据
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody DisposeRecordEntity disposeRecordEntity) {
        disposeRecordService.add(disposeRecordEntity);
        return R.success();
    }

    /**
     * 修改
     *
     * @param disposeRecordEntity 数据
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody DisposeRecordEntity disposeRecordEntity) {
        disposeRecordService.modify(disposeRecordEntity);
        return R.success();
    }

    /**
     * 删除
     *
     * @param ids Ids
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        disposeRecordService.del(ids);
        return R.success();
    }

    /**
     * 详情
     *
     * @param id Id
     * @return DisposeRecordEntity 结果
     */
    @GetMapping("/detail")
    public R<DisposeRecordEntity> detail(@RequestParam Long id) {
        return R.success(disposeRecordService.detail(id));
    }

    @PostMapping("/audit")
    public R<DisposeRecordEntity> audit(@RequestBody AuditParam auditParam) {
        return R.success(disposeRecordService.audit(auditParam));
    }

    @GetMapping("/export")
    public void export(@RequestParam List<LocalDate> date, HttpServletResponse response) {
        byte[] bytes = disposeRecordService.export(date);
        FileResponseUtil.download(response, bytes, Mime.XLSX, "处置记录.xlsx");
    }

    @GetMapping("/statistics")
    public R<Map<String, Object>> statistics(@RequestParam String date) {
        return R.success(disposeRecordService.statistics(date));
    }

    /**
     * 分页查询
     *
     * @param disposeRecordPagingParam 筛选条件
     * @param pageParam                分页参数
     * @return PageResult&lt;DisposeRecordEntity&gt; 分页结果
     */
    @GetMapping("/paging")
    public R<PageResult<DisposeRecordEntity>> paging(PageParam pageParam, DisposeRecordPagingParam disposeRecordPagingParam) {
        return R.success(disposeRecordService.paging(pageParam, disposeRecordPagingParam));
    }

    @PostMapping("/report")
    public R<?> report(@RequestParam(required = false, defaultValue = "1") Integer action, @RequestBody DisposeRecordReportParam disposeRecordReportParam) {
        disposeRecordService.report(action, disposeRecordReportParam);
        return R.success();
    }
}
