package com.kcim.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcim.common.util.PageUtils;
import com.kcim.common.util.Result;
import com.kcim.common.util.UserContext;
import com.kcim.dao.model.CostAccountShare;
import com.kcim.dao.model.Responsibility;
import com.kcim.dao.model.dto.AccountShareCopyDto;
import com.kcim.dao.model.dto.CostAccountShareEditDto;
import com.kcim.dao.model.dto.CostAccountShareSaveDto;
import com.kcim.dao.model.dto.ShareParamEditDto;
import com.kcim.service.CostAccountShareService;
import com.kcim.vo.CostShareParamStatusVO;
import com.kcim.vo.ShareParamProportionVO;
import com.kcim.vo.ShareTargetMapVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


/**
 * 责任中心成本对照表
 *
 * @author KCYG
 * @date 2021-07-28 18:34:52
 */
@RestController
@RequestMapping("costaccountshare")
@Api(tags = "成本分摊参数对应相关操作")
public class CostAccountShareController {
    @Autowired
    private CostAccountShareService costAccountShareService;

    /**
     * 分页查询列表
     */
    @GetMapping("/list")
    @ApiOperation("分页获取成本分摊参数对应数据")
    public Result list(@RequestParam(defaultValue = "1", value = "current") Integer current,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "name", required = false) String name){
        Long hospId = UserContext.getHospId();
        PageUtils pageUtils = costAccountShareService.queryList(current,pageSize,name,hospId);
        return Result.ok(pageUtils);
    }


    /**
     * 获取相关的责任中心成本对照表数据
     */
    @GetMapping("/getCostAccountShare")
    @ApiOperation("根据成本分摊参数对应表的Id获取")
    public Result getCostAccountShare(Integer id){
		CostAccountShare costAccountShare = costAccountShareService.getById(id);
        return Result.ok(costAccountShare);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存成本分摊参数对应数据")
    public Result save(@RequestBody @Valid CostAccountShareSaveDto costAccountShareSaveDto){
		costAccountShareService.addCostAccountShare(costAccountShareSaveDto);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改成本分摊参数对应数据")
    public Result update(@RequestBody @Valid CostAccountShareEditDto costAccountShareEditDto){
		costAccountShareService.updateByCostAccountShare(costAccountShareEditDto);
        return Result.ok();
    }
    /**
     * 显示会计科目里面
     */
    /**
     * 在全部分摊参数里面显示当前责任中心已经存在的分摊参数
     */
    @GetMapping("/getAllShareParamStatusById")
    @ApiOperation("在全部分摊参数里面显示当前责任中心已经存在的分摊参数的状态")
    public Result getAllShareParamStatusById(@RequestParam(defaultValue = "1", value = "current") Integer current,
                                             @RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize,
                                             Integer id){
        Long hospId = UserContext.getHospId();
        List<CostShareParamStatusVO> costShareParamStatusVOList= costAccountShareService.getAllShareParamStatusById(id,hospId);
        Page<CostShareParamStatusVO> page = new Page<>(current, pageSize);
        page.setRecords(costShareParamStatusVOList);
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(page.getRecords());
        return Result.ok(pageUtils);
    }

    /**
     * 获取责任中心成本对应的分摊参数数据
     */
    @GetMapping("/getShareParamById")
    @ApiOperation("获取指定成本分摊参数对应数据的分摊参数集合")
    public Result getShareParamById(Integer id){
        Long hospId = UserContext.getHospId();
        List<ShareParamProportionVO> shareParamProportionVOList = costAccountShareService.selectShareParamById(id,hospId);
        return Result.ok(shareParamProportionVOList);
    }


    /**
     * 进行分摊参数设置
     */
    @PostMapping("/updateShareParam")
    @ApiOperation("添加分摊参数设置")
    public Result updateShareParam(@RequestBody @Valid ShareParamEditDto shareParamEditDto){
        costAccountShareService.updateShareParam(shareParamEditDto);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除成本分摊参数对应数据")
    public Result delete(@RequestBody Long[] ids){
        costAccountShareService.deleteByIds(Arrays.asList(ids));
        return Result.ok();
    }

    /**
     * 成本分摊参数设置对应复制
     */
    @PostMapping("/copyAccountShareData")
    @ApiOperation("复制成本分摊参数设置功能")
    public Result copyAccountShareData(@RequestBody @Valid AccountShareCopyDto accountShareCopyDto){
        Long hospId = UserContext.getHospId();
        costAccountShareService.copyAccountShareData(accountShareCopyDto,hospId);
        return Result.ok();
    }
    /**
     * 获取指定类型的责任中心集合 分开计算还是合并计算
     * @param accountShareId 分摊参数设置的Id
     */
    @GetMapping("/getResponsibilityCalType")
    @ApiOperation("获取指定类型的责任中心集合")
    public Result getResponsibilityCalType(Long accountShareId){
        Long hospId = UserContext.getHospId();
        List<Responsibility> responsibilityList= costAccountShareService.getResponsibilityCalType(accountShareId,hospId);
        return Result.ok(responsibilityList);
    }

    /**
     * 同步分摊参数设置
     * @param direction 1主表向明细表同步 2明细表向主表同步
     * @return
     */
    @PostMapping("/synCostAccountShare")
    @ApiOperation("同步分摊参数设置")
    public Result synCostAccountShare(@RequestParam(defaultValue = "1") Integer direction,
                                      @RequestParam(required = false) Long accountShareId){
        costAccountShareService.synCostAccountShare(direction,accountShareId);
        return Result.ok();
    }

    /**
     * 进行分摊参数设置
     */
    @PostMapping("/applyAccountShareTarget")
    @ApiOperation("添加分摊参数设置对应的目标责任中心")
    public Result applyAccountShareTarget(@RequestBody ShareTargetMapVo shareTargetMapVo){
        costAccountShareService.applyAccountShareTarget(shareTargetMapVo);
        return Result.ok();
    }
}
