package com.kcim.web;

import com.kcim.common.util.Result;
import com.kcim.common.util.UserContext;
import com.kcim.service.*;
import com.kcim.vo.SessionUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @program: CostAccount
 * @description: 成本核算controller
 * @author: Wang.YS
 * @create: 2023-10-23 16:08
 **/
@RestController
@Slf4j
@Api(tags = "成本核算控制类")
@RequestMapping("calculate")
@AllArgsConstructor
public class CostCalculateController {

    //单位人事成本计算
    EmpCostCalculateService empCostCalculateService;

    @ApiOperation("成本核算-单位人事成本计算-获取列表")
    @GetMapping("empCostCalculateList")
    public Result getEmpCostCalculateList(@RequestParam(name = "computeDate") String computeDate,
                                          @RequestParam(name = "filter", required = false) String filter) {
        return Result.ok(empCostCalculateService.getEmpCostCalculateList(computeDate, filter));
    }

    @ApiOperation("成本核算-单位人事成本计算-计算")
    @PostMapping("empCostCalculate")
    public Result empCostCalculate(@RequestParam(name = "computeDate") String computeDate) {
        empCostCalculateService.empCostCalculate(computeDate);
        Result result = new Result();
        result.setStatus(200);
        result.setMsg("计算完成");
        return result;
    }

    /**
     * 收费项目成本计算
     */
    ComputeItemCostService computeItemCostService;

    @ApiOperation("成本核算-收费项目成本计算-获取列表")
    @GetMapping("itemCostCalculateList")
    public Result getItemCostCalculateList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                           @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                           @RequestParam(name = "computeDate") String computeDate,
                                           @RequestParam(name = "itemType", required = false) String itemType,
                                           @RequestParam(name = "itemName", required = false) String itemName) {
        return Result.ok(computeItemCostService.getItemCostCalculateList(current, pageSize, computeDate, itemType, itemName));
    }

    @ApiOperation("成本核算-收费项目成本计算-计算")
    @PostMapping("itemCostCalculate")
    public Result itemCostCalculate(@RequestParam(name = "computeDate") String computeDate,
                                    @RequestParam String type) {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        SessionUserVO currentUser = UserContext.getCurrentUser();

        CompletableFuture<Result> future = CompletableFuture.supplyAsync(() -> {
            try {
                long timeMillis = System.currentTimeMillis();
                computeItemCostService.itemCostCalculate(computeDate, currentUser, requestAttributes, type);
                long timeMillis1 = System.currentTimeMillis();
                Result result = new Result();
                result.setStatus(200);
                result.setMsg("计算完成");
                result.setData(timeMillis1 - timeMillis1);
                return result;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, executor);
        try {
            executor.shutdown();
            return future.get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }


//
//        future.thenAccept((x)->{
//
//        });
//        future.exceptionally((y)->{
//           System.out.println("2");
//            Result result = new Result();
//            result.setStatus(500);
//            result.setMsg("计算中止"+y.getMessage());
//            return result;
//        });

    }

    /**
     * 病人成本计算接口
     */
    ComputePatientCostService computePatientCostService;

    @ApiOperation("成本核算-病人成本计算-获取列表")
    @GetMapping("patientCostCalculateList")
    public Result patientCostCalculateList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                           @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                           @RequestParam(name = "computeDate") String computeDate,
                                           @RequestParam(name = "departmentName", required = false) String departmentName,
                                           @RequestParam(name = "patientName", required = false) String patientName) {
        return Result.ok(computePatientCostService.patientCostCalculateList(current, pageSize, computeDate, departmentName, patientName));
    }

    @ApiOperation("成本核算-病人成本计算-计算")
    @PostMapping("patientCostCalculate")
    public Result patientCostCalculate(@RequestParam(name = "computeDate") String computeDate) {
        computePatientCostService.patientCostCalculate(computeDate);
        Result result = new Result();
        result.setStatus(200);
        result.setMsg("计算完成");
        return result;
    }

    /**
     * 病种成本计算
     */
    ComputeDiseaseCostService computeDiseaseCostService;

    @ApiOperation("成本核算-病种成本计算-获取列表")
    @GetMapping("diseaseCostCalculateList")
    public Result diseaseCostCalculateList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                           @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                           @RequestParam(name = "computeDate") String computeDate,
                                           @RequestParam(name = "departmentName", required = false) String departmentName,
                                           @RequestParam(name = "diseaseName", required = false) String diseaseName) {
        return Result.ok(computeDiseaseCostService.diseaseCostCalculateList(current, pageSize, computeDate, departmentName, diseaseName));
    }

    @ApiOperation("成本核算-病种成本计算-计算")
    @PostMapping("diseaseCostCalculate")
    public Result diseaseCostCalculate(@RequestParam(name = "computeDate") String computeDate) {
        computeDiseaseCostService.diseaseCostCalculate(computeDate);
        Result result = new Result();
        result.setStatus(200);
        result.setMsg("计算完成");
        return result;
    }

    /**
     * DRG/DIP成本计算
     */
    ComputeGroupCostService computeGroupCostService;

    @ApiOperation("成本核算-DRG/DIP成本计算-获取列表")
    @GetMapping("groupCostCalculateList")
    public Result groupCostCalculateList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                         @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                         @RequestParam(name = "computeDate") String computeDate,
                                         @RequestParam(name = "departmentName", required = false) String departmentName,
                                         @RequestParam(name = "groupName", required = false) String groupName) {
        return Result.ok(computeGroupCostService.groupCostCalculateList(current, pageSize, computeDate, departmentName, groupName));
    }

    @ApiOperation("成本核算-DRG/DIP成本计算-计算")
    @PostMapping("groupCostCalculate")
    public Result groupCostCalculate(@RequestParam(name = "computeDate") String computeDate) {
        computeGroupCostService.groupCostCalculate(computeDate);
        Result result = new Result();
        result.setStatus(200);
        result.setMsg("计算完成");
        return result;
    }

    ComputeClinicalPathwayCostService computeClinicalPathwayCostService;

    @ApiOperation("成本核算-临床路径-获取列表")
    @GetMapping("clinicalPathwayCostCalculateList")
    public Result clinicalPathwayCostCalculateList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                                   @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                                   @RequestParam(name = "computeDate") String computeDate,
//                                         @RequestParam(name = "departmentName",required = false)String departmentName,
                                                   @RequestParam(name = "clinicalName", required = false) String clinicalName) {
        return Result.ok(computeClinicalPathwayCostService.clinicalPathwayCostCalculateList(current, pageSize, computeDate, clinicalName));
    }

    @ApiOperation("成本核算-临床路径-计算")
    @PostMapping("clinicalPathwayCostCalculate")
    public Result clinicalPathwayCostCalculate(@RequestParam(name = "computeDate") String computeDate) {
        computeClinicalPathwayCostService.clinicalPathwayCostCalculate(computeDate);
        Result result = new Result();
        result.setStatus(200);
        result.setMsg("计算完成");
        return result;
    }

    OrderCostService orderCostService;

    @ApiOperation("成本核算-医嘱项目成本-获取列表")
    @GetMapping("orderCostCalculateList")
    public Result orderCostCalculateList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                         @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                         @RequestParam(name = "computeDate") String computeDate,
                                         @RequestParam(name = "orderName", required = false) String orderName) {
        return Result.ok(orderCostService.orderCostCalculateList(current, pageSize, computeDate, orderName));
    }

    @ApiOperation("成本核算-医嘱项目成本-计算")
    @PostMapping("orderCostCalculate")
    public Result orderCostCalculate(@RequestParam(name = "computeDate") String computeDate) {
        orderCostService.orderCostCalculate(computeDate);
        Result result = new Result();
        result.setStatus(200);
        result.setMsg("计算完成");
        return result;
    }

    DrugMaterialCalculateService drugMaterialCalculateService;

    @ApiOperation("成本核算-药材成本-获取列表")
    @GetMapping("drugMaterialCostCalculateList")
    public Result drugMaterialCostCalculateList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                                @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                                @RequestParam(name = "computeDate") String computeDate,
                                                @RequestParam(name = "responsibilityCode", required = false) String responsibilityCode,
                                                @RequestParam(name = "filter", required = false) String filter) {
        return Result.ok(drugMaterialCalculateService.drugMaterialCostCalculateList(current, pageSize, computeDate, responsibilityCode, filter));
    }

    @ApiOperation("成本核算-药材成本-计算")
    @PostMapping("drugMaterialCostCalculate")
    public Result drugMaterialCostCalculate(@RequestParam(name = "computeDate") String computeDate) {
        drugMaterialCalculateService.drugMaterialCostCalculate(computeDate);
        Result result = new Result();
        result.setStatus(200);
        result.setMsg("计算完成");
        return result;
    }

    /**
     * 完全成本法项目计算
     */
    ProjectCostService projectCostService;

    @ApiOperation("成本核算-项目成本计算-获取列表")
    @GetMapping("getItemCostList")
    public Result getItemCostList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                  @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                  @RequestParam String computeDate,
                                  @RequestParam(required = false) String itemType,
                                  @RequestParam(required = false) String responsibility,
                                  @RequestParam(required = false) String itemFilter) {
        return Result.ok(projectCostService.getItemCostList(current, pageSize, computeDate, itemType, responsibility, itemFilter));

    }

    @ApiOperation("成本核算-项目成本计算-计算")
    @PostMapping("computeItemCost")
    public Result computeItemCost(@RequestParam String computeDate) {
        projectCostService.computeItemCost(computeDate);
        return Result.ok();
    }

    /**
     * 项目分摊参数计算
     */
    ShareParamService shareParamService;

    @ApiOperation("成本核算-项目分摊参数计算-获取列表")
    @GetMapping("getShareParamCostList")
    public Result getShareParamCostList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                        @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                        @RequestParam String computeDate,
                                        @RequestParam(required = false) String itemType,
                                        @RequestParam(required = false) String responsibility,
                                        @RequestParam(required = false) String itemFilter) {
        return Result.ok(shareParamService.getShareParamCostList(current,pageSize,computeDate, itemType, responsibility, itemFilter));

    }

    @ApiOperation("成本核算-项目分摊参数计算-计算")
    @PostMapping("computeShareParamCost")
    public Result computeShareParamCost(@RequestParam String computeDate) {
        shareParamService.computeShareParamCost(computeDate);
        return Result.ok();
    }

    @ApiOperation("成本核算-患者项目成本计算-获取列表")
    @GetMapping("getPatientItemCostList")
    public Result getPatientItemCostList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                        @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                        @RequestParam String computeDate,
                                        @RequestParam(required = false) String departmentFilter,
                                        @RequestParam(required = false) String patientFilter) {
        return Result.ok(projectCostService.getPatientItemCostList(current,pageSize,computeDate,  departmentFilter, patientFilter));

    }

    @ApiOperation("成本核算-患者项目成本计算-计算")
    @PostMapping("computePatientItemCost")
    public Result computePatientItemCost(@RequestParam String computeDate) {
        projectCostService.computePatientItemCost(computeDate);
        return Result.ok();
    }

    @ApiOperation("成本核算-标准项目分摊参数计算-获取列表")
    @GetMapping("getStandShareParamCostList")
    public Result getStandShareParamCostList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                        @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                        @RequestParam String computeDate,
                                        @RequestParam(required = false) String itemType,
                                        @RequestParam(required = false) String responsibility,
                                        @RequestParam(required = false) String itemFilter) {
        return Result.ok(shareParamService.getStandShareParamCostList(current,pageSize,computeDate, itemType, responsibility, itemFilter));

    }

    @ApiOperation("成本核算-标准项目分摊参数计算-计算")
    @PostMapping("computeStandShareParamCost")
    public Result computeStandShareParamCost(@RequestParam String computeDate) {
        shareParamService.computeStandShareParamCost(computeDate);
        return Result.ok();
    }

    @ApiOperation("成本核算-标准项目成本计算-获取列表")
    @GetMapping("getStandItemCostList")
    public Result getStandItemCostList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                  @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                  @RequestParam String computeDate,
                                  @RequestParam(required = false) String itemType,
                                  @RequestParam(required = false) String responsibility,
                                  @RequestParam(required = false) String itemFilter) {
        return Result.ok(projectCostService.getStandItemCostList(current, pageSize, computeDate, itemType, responsibility, itemFilter));

    }

    @ApiOperation("成本核算-标准项目成本计算-计算")
    @PostMapping("computeStandItemCost")
    public Result computeStandItemCost(@RequestParam String computeDate) {
        projectCostService.computeStandItemCost(computeDate);
        return Result.ok();
    }


    @ApiOperation("成本核算-患者标准项目成本计算-获取列表")
    @GetMapping("getPatientStandItemCostList")
    public Result getPatientStandItemCostList(@RequestParam(defaultValue = "1", name = "current", required = false) Integer current,
                                         @RequestParam(defaultValue = "10", name = "pageSize", required = false) Integer pageSize,
                                         @RequestParam String computeDate,
                                         @RequestParam(required = false) String departmentFilter,
                                         @RequestParam(required = false) String patientFilter) {
        return Result.ok(projectCostService.getPatientStandItemCostList(current,pageSize,computeDate,  departmentFilter, patientFilter));

    }

    @ApiOperation("成本核算-患者标准项目成本计算-计算")
    @PostMapping("computeStandPatientItemCost")
    public Result computeStandPatientItemCost(@RequestParam String computeDate) {
        projectCostService.computeStandPatientItemCost(computeDate);
        return Result.ok();
    }


}
