package com.kcim.web;

import com.kcim.common.constants.NumberConstant;
import com.kcim.common.util.PageUtils;
import com.kcim.common.util.Result;
import com.kcim.common.util.UserContext;
import com.kcim.dao.model.CostDepartmentProfit;
import com.kcim.service.ComputeMedicalDepartmentProfitService;
import com.kcim.service.CostDepartmentProfitService;
import com.kcim.service.HospProfitAndLossService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;


/**
 * 科室损益计算
 *
 * @author KCYG
 * @date 2021-08-24 16:24:08
 */
@RestController
@RequestMapping("costdepartmentprofit")
@Api(tags = "科室损益计算")
@Slf4j
@AllArgsConstructor
public class CostDepartmentProfitController {

    private CostDepartmentProfitService costDepartmentProfitService;

    private  HospProfitAndLossService hospProfitAndLossService;

    private ComputeMedicalDepartmentProfitService computeMedicalDepartmentProfitService;

    /**
     * 分页查询列表
     * 查询的是
     */
    @GetMapping("/list")
    @ApiOperation("科室损益计算查询")
    public Result list(@RequestParam(value = "current", defaultValue = "1") Integer current,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "responsibilityCode",required = false) String responsibilityCode,
                       @RequestParam(value = "date") String date){
        Long hospId = UserContext.getHospId();
        PageUtils pageUtils = costDepartmentProfitService.queryList(current,pageSize,responsibilityCode,date,hospId);
        return Result.ok(pageUtils);
    }

    /**
     * 科室损益计算导出
     */
    @SneakyThrows
    @ApiOperation("科室损益计算输出")
    @PostMapping("/getDepartProfitReport")
    public Result getDepartProfitReport(@RequestParam String date,@RequestParam Integer reportType) {
//        try {
            Long hospId = UserContext.getHospId();
            costDepartmentProfitService.getDepartProfitReport(date,hospId,reportType);
//        } catch (Exception e) {
//            log.error(e.getMessage());
//            throw new CostException(500,"生成错误"+e.getMessage());
//        }

        return Result.ok();
    }

    /**
     * 科室损益计算
     */
    @PostMapping("/setDepartmentProfit")
    @ApiOperation("进行科室损益计算")
    public Result setDepartmentProfit(@RequestParam @ApiParam(name = "date", value = "yyyy-MM-dd") String date){
        Long hospId = UserContext.getHospId();
        costDepartmentProfitService.setDepartmentProfit(date,hospId);
        return  Result.ok();
    }





    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
		CostDepartmentProfit costDepartmentProfit = costDepartmentProfitService.getById(id);
        return Result.ok(costDepartmentProfit);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody CostDepartmentProfit costDepartmentProfit){
		costDepartmentProfitService.save(costDepartmentProfit);
        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody CostDepartmentProfit costDepartmentProfit){
		costDepartmentProfitService.updateById(costDepartmentProfit);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
        costDepartmentProfitService.deleteByIds(Arrays.asList(ids));
        return Result.ok();
    }


    @PostMapping("/computeProfit")
    @ApiOperation("损益计算")
    public Result computeProfit(@RequestParam @ApiParam(name = "date", value = "yyyy-MM-dd") String computeDate,
                                @RequestParam String reportType){
        Long hospId = UserContext.getHospId();
        if(reportType.equals(NumberConstant.THREE_S)){
            hospProfitAndLossService.calcByResponsibility(computeDate, hospId);
        }else {
            costDepartmentProfitService.computeProfit(computeDate,hospId,reportType);
        }
        return  Result.ok();
    }

    @GetMapping("/computeProfitReport")
    @ApiOperation("损益报表数据")
    public Result computeProfitReport(@RequestParam @ApiParam(name = "date", value = "yyyy-MM-dd") String computeDate,
                                @RequestParam String reportType){
        Long hospId = UserContext.getHospId();
        return  Result.ok(costDepartmentProfitService.computeProfitReport(computeDate,hospId,reportType));
    }

    @GetMapping("/getProfitResponsibility")
    @ApiOperation("损益报表责任中心数据")
    public Result getProfitResponsibility(@RequestParam String reportType,
                                          @RequestParam(required = false) String responsibilityName){
        return  Result.ok(costDepartmentProfitService.getProfitResponsibility(reportType,responsibilityName));
    }

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

    @GetMapping("getLastComputeDate")
    @ApiOperation("获取最后一次损益计算月份")
    public Result getLastComputeDate(){
        return Result.ok(costDepartmentProfitService.getLastComputeDate());
    }




    @GetMapping("/getBatchComputeProfitList")
    @ApiOperation("多月损益报表计算列表")
    public Result getBatchComputeProfitList(@RequestParam @ApiParam(name = "date", value = "yyyy-MM") String beginComputeDate,
                                      @RequestParam String endComputeDate,
                                       @RequestParam String reportType,
                                       @RequestParam String responsibilityCode){
        Long hospId = UserContext.getHospId();
        return  Result.ok(costDepartmentProfitService.getBatchComputeProfitList(beginComputeDate,endComputeDate,hospId,reportType,responsibilityCode));
    }

    @GetMapping("/getBatchComputeProfitListByYear")
    @ApiOperation("多年份损益报表计算列表")
    public Result getBatchComputeProfitListByYear(@RequestParam String startYear,
                                            @RequestParam String endYear,
                                            @RequestParam String reportType,
                                            @RequestParam String responsibilityCode){
        return  Result.ok(costDepartmentProfitService.getBatchComputeProfitListByYear(startYear,endYear,reportType,responsibilityCode));
    }



    @GetMapping("/getRedirectData")
    @ApiOperation("下钻数据列表")
    public Result getRedirectData(@RequestParam @ApiParam(name = "date", value = "yyyy-MM") String computeDate,
                                            @RequestParam Long reportId,
                                            @RequestParam String reportType,
                                            @RequestParam String responsibilityCode){
        Long hospId = UserContext.getHospId();
        return  Result.ok(costDepartmentProfitService.getRedirectData(computeDate,reportId,hospId,reportType,responsibilityCode));
    }


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

    @PostMapping("/calcMedicalProfit")
    @ApiOperation("计算科室门住损益计算")
    public Result calcMedicalProfit(@RequestParam  String computeDate,
                                    @RequestParam String reportType){
        Long hospId = UserContext.getHospId();
        computeMedicalDepartmentProfitService.calcMedicalProfit(computeDate,hospId,reportType);
        return  Result.ok();
    }


    @GetMapping("/getMedicalProfit")
    @ApiOperation("获取科室门住损益计算列表")
    public Result getMedicalProfit(@RequestParam String computeDate,
                                   @RequestParam String reportType,
                                   @RequestParam String responsibilityCode,
                                   @RequestParam Integer responsibilityType){
        Long hospId = UserContext.getHospId();
        return  Result.ok(computeMedicalDepartmentProfitService.getMedicalProfit(computeDate,hospId,reportType,responsibilityCode,responsibilityType));
    }

    @GetMapping("/getMedicalResponsibility")
    @ApiOperation("获取科室门住损益责任中心列表")
    public Result getMedicalResponsibility(@RequestParam String computeDate,
                                           @RequestParam String reportType){
        Long hospId = UserContext.getHospId();
        return  Result.ok(computeMedicalDepartmentProfitService.getMedicalResponsibility(computeDate,hospId,reportType));
    }





}
