package com.kcim.web;

import com.kcim.common.util.PageUtils;
import com.kcim.common.util.Result;
import com.kcim.dao.model.dto.CopyReportDTO;
import com.kcim.dao.model.dto.ReportFormEditDTO;
import com.kcim.dao.model.dto.ReportFormSaveDTO;
import com.kcim.dao.model.dto.ReportRelationDTO;
import com.kcim.service.ReportFormService;
import com.kcim.service.ReportRelationService;
import com.kcim.vo.RelationVO;
import com.kcim.vo.ReportFormVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 报表项目
 *
 * @author huangrui
 * @date 2021-08-03 13:53:52
 */
@Api(tags = "报表项目")
@RestController
@RequestMapping("reportForm")
public class ReportFormController extends AbstractController {

    private final ReportFormService reportFormService;
    private final ReportRelationService reportRelationService;

    public ReportFormController(ReportFormService reportFormService, ReportRelationService reportRelationService) {
        this.reportFormService = reportFormService;
        this.reportRelationService = reportRelationService;
    }

    /**
     * 按报表类型查询报表项目
     */
    @ApiOperation("按报表类型查询报表项目列表")
    @GetMapping("/list")
    public Result list(@RequestParam Integer reportType) {
        List<ReportFormVO> list = reportFormService.selectList(reportType, getUser());
        PageUtils pageUtils = new PageUtils(list, 0, 0, 0);
        return Result.ok(pageUtils);
    }

    /**
     * 保存
     */
    @ApiOperation("保存报表项目")
    @PostMapping("/save")
    public Result save(@RequestBody @Valid ReportFormSaveDTO reportFormDTO) {
        reportFormService.saveReport(reportFormDTO, getHospId());
        return Result.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改报表项目设置")
    @PostMapping("/edit")
    public Result update(@RequestBody @Valid ReportFormEditDTO formEditDTO) {
        reportFormService.updateReport(formEditDTO);
        return Result.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除某个报表项目")
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids) {
        reportFormService.removeByIds(Arrays.asList(ids));
        reportRelationService.removeByReportIds(Arrays.asList(ids));
        return Result.ok();
    }


    @ApiOperation("编辑对应的报表项目和对应的会计或者分摊参数")
    @PostMapping("/saveReportRelation")
    public Result saveReportRelation(@RequestBody @Valid ReportRelationDTO reportRelationDTO) {
        reportRelationService.saveReportRelation(reportRelationDTO,getHospId());
        return Result.ok();
    }

    @ApiOperation("获取已绑定的关系列表")
    @GetMapping("/getRelations")

    public Result getRelations(@RequestParam Long reportId, @RequestParam Integer relation) {
        List<RelationVO> list = new ArrayList<>();
        Long hospId = getHospId();
        if (relation == 1) {
            list = reportRelationService.getAccountRelation(reportId, hospId);
        }
        if (relation == 2) {
            list = reportRelationService.getShareLevel(reportId, hospId);
        }
        if (relation == 3) {
            list = reportRelationService.getResponsibilities(reportId, hospId);
        }
        return Result.ok(list);
    }


    /**
     *
     * @since 2021-8-6 10:50:59
     * @param reportId
     * @param relation
     * @return 只返回已经选择过的数据列表
     */
    @ApiOperation("根据关系类型，获取可绑定的关系数据（包含回显）")
    @GetMapping("/getRelationList")

    public Result getRelationList(@RequestParam Long reportId, @RequestParam Integer relation) {
        List<RelationVO> list = reportRelationService.getRelationList(reportId, relation, getHospId());
        return Result.ok(list);
    }

    @ApiOperation("复制数据")
    @PostMapping("/copyReport")
    public Result copyReport(@RequestBody @Valid CopyReportDTO copyReportDTO) {
        reportFormService.copyReport(copyReportDTO, getHospId());
        return Result.ok();
    }

    @ApiOperation("报表设置-获取责任中心（同报表同计算方式互斥）")
    @GetMapping("getResponsibilities")
    public Result getResponsibilities(@RequestParam String reportType,
                                      @RequestParam Long reportId){
        return Result.ok(reportFormService.getResponsibilities(reportType,reportId));

    }


}
