package com.kcim.web;

import com.kcim.common.exception.CostException;
import com.kcim.common.util.PageUtils;
import com.kcim.common.util.Result;
import com.kcim.dao.model.dto.StartDTO;
import com.kcim.vo.AfterAllocationFormVO;
import com.kcim.service.AllocationService;
import com.kcim.service.CostCostingGroupService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

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


/**
 * 成本归集
 *
 * @author KCYG
 * @date 2021-08-12 11:00:23
 */
@Api(tags = "成本分摊")
@RestController
@RequestMapping("costcostinggroup")
public class CostCostingGroupController extends AbstractController {
    private final CostCostingGroupService costCostingGroupService;

    private final AllocationService allocationService;

    public CostCostingGroupController(CostCostingGroupService costCostingGroupService, AllocationService allocationService) {
        this.costCostingGroupService = costCostingGroupService;
        this.allocationService = allocationService;
    }

    @ApiOperation("成本分摊前数据列表")
    @GetMapping("/queryStartAllocation")

    public Result queryStartAllocation(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                       @RequestParam(value = "responsibilityCode", required = false) String responsibilityCode,
                                       @RequestParam(value = "accountCode", required = false) String accountCode,
                                       @RequestParam(value = "date") String date,
                                       @RequestParam String departmentCode,
                                       @RequestParam(required = false)String filter) {
        // 简单校验时间格式 这里要求的是yyyy-MM-dd
        if (date.length() != 7) {
            throw new CostException("时间格式不正确，这里要求yyyy-MM 格式");
        }
        PageUtils pageUtils = costCostingGroupService.queryStartAllocation(current, pageSize, responsibilityCode, accountCode, date, getHospId(),departmentCode,filter);
        return Result.ok(pageUtils);
    }

    @ApiOperation("成本分摊列表")
    @GetMapping("/allocationList")

    public Result allocationList(@RequestParam(value = "current", defaultValue = "1", required = false) Integer current,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 @RequestParam(value = "date", required = false) String date) {
        PageUtils pageUtils = costCostingGroupService.queryAllocation(current, pageSize, date, getHospId());
        return Result.ok(pageUtils);
    }

    @ApiOperation("撤销分摊")
    @PostMapping("/cancelAllocation")
    public Result cancelAllocation(@RequestParam Integer year, @RequestParam Integer month) {
        costCostingGroupService.cancelAllocation(year, month,getHospId());
        return Result.ok();
    }

    @ApiOperation("开始分摊")
    @PostMapping("/startAllocation")
    public Result startAllocation(@RequestBody @Valid StartDTO startDTO) {

//        costCostingGroupService.startAllocation(startDTO, getHospId());
        allocationService.startAllocation(startDTO, getHospId());
        return Result.ok();
    }

    @ApiOperation("分摊后列表")
    @GetMapping("/queryAfterAllocation")

    public Result queryAfterAllocation(@RequestParam(value = "year", required = false) String year,
                                       @RequestParam(value = "responsibilityCode", required = false) String responsibilityCode,
                                       @RequestParam(value = "current", defaultValue = "1", required = false) Integer current,
                                       @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        PageUtils pageUtils = allocationService.queryAfterAllocation(year, responsibilityCode, current, pageSize, getHospId());
        return Result.ok(pageUtils);
    }

    @ApiOperation("分摊后报表")
    @GetMapping("/queryAfterAllocationForm")

    public Result queryAfterAllocationForm(@RequestParam(value = "year", required = false) String year,
                                           @RequestParam(value = "responsibilityCode", required = false) String responsibilityCode) {
        return Result.ok(allocationService.queryAfterAllocationForm(year, responsibilityCode, getHospId()));
    }

    @ApiOperation("分摊后报表输出")
    @GetMapping("/afterAllocationFormList")
    public Result afterAllocationFormList(@RequestParam(value = "date", required = false)
                                          @ApiParam(name = "date", value = "年月日（yyyy-MM-dd）") String date) {
        List<AfterAllocationFormVO> list = allocationService.afterAllocationFormList(date, getHospId());
        PageUtils pageUtils = new PageUtils(list, 0, 0, 0);
        return Result.ok(pageUtils);
    }

}
