package com.kcim.web;

import com.kcim.common.util.PageUtils;
import com.kcim.common.util.Result;
import com.kcim.common.util.UserContext;
import com.kcim.service.HospProfitAndLossService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@Api(tags = "全院损益相关")
@RestController
@RequestMapping("hospProfitAndLoss")
public class HospProfitAndLossController extends AbstractController {

    private final HospProfitAndLossService hospProfitAndLossService;

    public HospProfitAndLossController(HospProfitAndLossService hospProfitAndLossService) {
        this.hospProfitAndLossService = hospProfitAndLossService;
    }

    @ApiOperation("计算指定类型报表的全院损益")
    @PostMapping("/calc")
    public Result calcHospProfit(@RequestParam @ApiParam(name = "date", value = "yyyy-MM-dd") String date,
                                 @RequestParam(name = "reportType",required = false) Integer reportType) {
        if(ObjectUtils.isEmpty(reportType)){
            hospProfitAndLossService.calcByResponsibility(date, getHospId());
        }else{
            hospProfitAndLossService.calcHospProfit(date, getHospId(),reportType);
        }
        return Result.ok();
    }

    @ApiOperation("全院损益列表")
    @GetMapping("/getHospProfits")
    public Result getHospProfits(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 @RequestParam(value = "date")String date) {
        PageUtils pageUtils = hospProfitAndLossService.getHospProfits(current, pageSize, date, getHospId());
        return Result.ok(pageUtils);
    }
    @ApiOperation("全院损益列表-责任中心列表")
    @GetMapping("/getHospProfitResponsibilities")
    public Result getHospProfitResponsibilities(@RequestParam String responsibilityName) {        ;
        return Result.ok(hospProfitAndLossService.getHospProfitResponsibilities(responsibilityName));
    }

    @GetMapping("/getHospProfitList")
    @ApiOperation("全院损益列表-损益报表计算列表")
    public Result getHospProfitList(@RequestParam @ApiParam(name = "date", value = "yyyy-MM-dd") String computeDate,
                                       @RequestParam String reportType){
        Long hospId = UserContext.getHospId();
        return  Result.ok(hospProfitAndLossService.getHospProfitList(computeDate,hospId,reportType));
    }

    @GetMapping("/getBatchHospProfitList")
    @ApiOperation("多月份全院损益列表")
    public Result getBatchComputeProfitList(@RequestParam @ApiParam(name = "date", value = "yyyy-MM") String beginComputeDate,
                                            @RequestParam String endComputeDate,
                                            @RequestParam Integer reportType){
        Long hospId = UserContext.getHospId();
        return  Result.ok(hospProfitAndLossService.getBatchHospProfitList(beginComputeDate,endComputeDate,hospId,reportType));
    }


    @ApiOperation("全院损益计算输出")
    @PostMapping("/report")
    public Result hospProfitReport(@RequestParam String date) {
//        try {
            hospProfitAndLossService.hospProfitReport(date,getHospId());
//        } catch (Exception e) {
//            throw new CostException(500,"生成错误");
//        }

        return Result.ok();
    }

    @ApiOperation("全院经营报表列表")
    @GetMapping("/hospReports")
    public Result hospReports(@RequestParam(value = "current", defaultValue = "1") Integer current,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(value = "date") String date) {

        PageUtils pageUtils = hospProfitAndLossService.hospProfitReports(current, pageSize, getHospId(),date);
        return Result.ok(pageUtils);
    }
}
