package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.vo.LawFirmListVO;
import shop.alien.entity.store.vo.LawFirmReconciliationVO;
import shop.alien.entity.store.vo.LawyerListVO;
import shop.alien.store.service.LawFirmReconciliationService;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 律所对账结算控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-律所对账结算"})
@ApiSort(21)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/firm/reconciliation")
@RequiredArgsConstructor
public class LawFirmReconciliationController {
    private final LawFirmReconciliationService lawFirmReconciliationService;

    @ApiOperation("获取所有律所的对账")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firmId", value = "律所ID（可选，不传则查询所有律所）", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "firmName", value = "律所名称（可选，模糊查询）", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "startDate", value = "开始日期（可选，格式：yyyy-MM-dd）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期（可选，格式：yyyy-MM-dd）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getOverview")
    public R<LawFirmReconciliationVO> getLawFirmReconciliation(
            @RequestParam(value = "firmId", required = false) Integer firmId,
            @RequestParam(value = "firmName", required = false) String firmName,
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        log.info("LawFirmReconciliationController.getLawFirmReconciliation?firmId={},firmName={},startDate={},endDate={}",
                firmId, firmName, startDate, endDate);
        return lawFirmReconciliationService.getLawFirmReconciliation(firmId, firmName, startDate, endDate);
    }

    @ApiOperation("获取所有律所的对账统计列表（分页）")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firmName", value = "律所名称（可选，模糊查询）", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "startDate", value = "开始日期（可选，格式：yyyy-MM-dd）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期（可选，格式：yyyy-MM-dd）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码（默认1）", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页容（默认10）", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getAllLawFirmList")
    public R<IPage<LawFirmListVO>> getAllLawFirmReconciliationList(
            @RequestParam(value = "firmName", required = false) String firmName,
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("LawFirmReconciliationController.getAllLawFirmReconciliationList?firmName={},startDate={},endDate={},pageNum={},pageSize={}",
                firmName, startDate, endDate, pageNum, pageSize);
        return lawFirmReconciliationService.getAllLawFirmReconciliationList(firmName, startDate, endDate, pageNum, pageSize);
    }

    @ApiOperation("获取律所下所有律师的对账统计列表（分页）")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firmId", value = "律所ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "startDate", value = "开始日期（可选，格式：yyyy-MM-dd）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期（可选，格式：yyyy-MM-dd）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码（默认1）", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页容（默认10）", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getLawyerList")
    public R<IPage<LawyerListVO>> getLawyerReconciliationList(
            @RequestParam(value = "firmId") Integer firmId,
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("LawFirmReconciliationController.getLawyerReconciliationList?firmId={},startDate={},endDate={},pageNum={},pageSize={}",
                firmId, startDate, endDate, pageNum, pageSize);
        return lawFirmReconciliationService.getLawyerReconciliationList(firmId, startDate, endDate, pageNum, pageSize);
    }

    @ApiOperation("获取律所下所有律师的对账统计列表（分页，支持律师名称模糊查询）")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firmId", value = "律所ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "lawyerName", value = "律师名称（可选，模糊查询）", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "startDate", value = "开始日期（可选，格式：yyyy-MM-dd）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期（可选，格式：yyyy-MM-dd）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码（默认1）", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页容（默认10）", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getLawyerListWithName")
    public R<IPage<LawyerListVO>> getLawyerReconciliationListWithName(
            @RequestParam(value = "firmId") Integer firmId,
            @RequestParam(value = "lawyerName", required = false) String lawyerName,
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("LawFirmReconciliationController.getLawyerReconciliationListWithName?firmId={},lawyerName={},startDate={},endDate={},pageNum={},pageSize={}",
                firmId, lawyerName, startDate, endDate, pageNum, pageSize);
        return lawFirmReconciliationService.getLawyerReconciliationListWithName(firmId, lawyerName, startDate, endDate, pageNum, pageSize);
    }

    @ApiOperation("获取律师的已完成订单列表（分页）")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lawyerId", value = "律师ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageNum", value = "页码（默认1）", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页容（默认10）", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getLawyerOrderList")
    public R<IPage<LawFirmReconciliationVO>> getLawyerOrderList(
            @RequestParam(value = "lawyerId") Integer lawyerId,
            @RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize) {
        log.info("LawFirmReconciliationController.getLawyerOrderList?lawyerId={},pageNum={},pageSize={}",
                lawyerId, pageNum, pageSize);
        return lawFirmReconciliationService.getLawyerOrderList(lawyerId, pageNum, pageSize);
    }

    @ApiOperation("导出所有律所的对账统计列表到Excel（基于/getAllLawFirmList接口数据）")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firmName", value = "律所名称（可选，模糊查询）", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "startDate", value = "开始日期（可选，格式：yyyy-MM-dd）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期（可选，格式：yyyy-MM-dd）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码（可选，不传或传0则导出全部，传值则导出本页）", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页容（可选，与pageNum配合使用）", dataType = "Integer", paramType = "query")
    })
    @GetMapping(value = "/exportAllLawFirmList", produces = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    public void exportAllLawFirmList(
            HttpServletResponse response,
            @RequestParam(value = "firmName", required = false) String firmName,
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) throws Exception {
        log.info("LawFirmReconciliationController.exportAllLawFirmList?firmName={},startDate={},endDate={},pageNum={},pageSize={}",
                firmName, startDate, endDate, pageNum, pageSize);
        try {
            lawFirmReconciliationService.exportAllLawFirmList(response, firmName, startDate, endDate, pageNum, pageSize);
        } catch (Exception e) {
            log.error("导出律所对账列表异常", e);
            if (!response.isCommitted()) {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().write("{\"code\":500,\"msg\":\"导出失败：" + e.getMessage() + "\"}");
            }
            throw e;
        }
    }

    @ApiOperation("导出律师对账统计列表到Excel（基于/getLawyerListWithName接口数据）")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firmId", value = "律所ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "lawyerName", value = "律师名称（可选，模糊查询）", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "startDate", value = "开始日期（可选，格式：yyyy-MM-dd）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束日期（可选，格式：yyyy-MM-dd）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码（可选，不传或传0则导出全部，传值则导出本页）", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页容（可选，与pageNum配合使用）", dataType = "Integer", paramType = "query")
    })
    @GetMapping(value = "/exportLawyerListWithName", produces = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    public void exportLawyerListWithName(
            HttpServletResponse response,
            @RequestParam(value = "firmId") Integer firmId,
            @RequestParam(value = "lawyerName", required = false) String lawyerName,
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) throws Exception {
        log.info("LawFirmReconciliationController.exportLawyerListWithName?firmId={},lawyerName={},startDate={},endDate={},pageNum={},pageSize={}",
                firmId, lawyerName, startDate, endDate, pageNum, pageSize);
        try {
            lawFirmReconciliationService.exportLawyerListWithName(response, firmId, lawyerName, startDate, endDate, pageNum, pageSize);
        } catch (Exception e) {
            log.error("导出律师对账列表异常", e);
            if (!response.isCommitted()) {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().write("{\"code\":500,\"msg\":\"导出失败：" + e.getMessage() + "\"}");
            }
            throw e;
        }
    }
}
