package com.kcim.web;

import com.kcim.common.util.PageUtils;
import com.kcim.common.util.Result;
import com.kcim.common.util.UserContext;
import com.kcim.dao.model.CostShareParam;
import com.kcim.dao.model.dto.CostShareParamAccountDto;
import com.kcim.dao.model.dto.CostShareParamEditDto;
import com.kcim.dao.model.dto.CostShareParamSaveDto;
import com.kcim.vo.CostShareParamVO;
import com.kcim.service.CostShareParamService;
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 09:23:28
 */
@RestController
@RequestMapping("costshareparam")
@Api(tags = "成本参数操作")
public class CostShareParamController {
    @Autowired
    private CostShareParamService costShareParamService;

    /**
     * 分页查询列表
     */
    @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){
//        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Long hospId = UserContext.getHospId();
        PageUtils pageUtils= costShareParamService.queryList(current,pageSize,name,hospId);
        return Result.ok(pageUtils);
    }

    @GetMapping("/getById")
    @ApiOperation("根据Id获取指定成本参数")
    public Result getById(Integer id){
//        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Long hospId = UserContext.getHospId();
        CostShareParamVO costShareParamVO = costShareParamService.getByHospIdAndAPramId(id, hospId);
        return Result.ok(costShareParamVO);
    }


    @GetMapping("/getAll")
    @ApiOperation("获取所有的分摊参数的信息")
    public Result getAll(){
//        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Long hospId = UserContext.getHospId();
        List<CostShareParamVO> costShareParamVOList= costShareParamService.getAll(hospId);
        return Result.ok(costShareParamVOList);
    }


    /**
     * 添加对应成本科目
     * 接受传递过来的数组集合
     */
    @PostMapping("/updateCostShareParamByAccountId")
    @ApiOperation("为成本参数添加对应会计科目信息")
    public Result updateCostShareParamByAccountId(@RequestBody CostShareParamAccountDto costShareParamAccountDto){
//        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Long hospId = UserContext.getHospId();
        costShareParamAccountDto.setHospId(hospId);
		CostShareParam costShareParam = costShareParamService.updateCostShareParamByAccountId(costShareParamAccountDto);
        return Result.ok(costShareParam);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加分摊参数信息")
    public Result save(@RequestBody @Valid CostShareParamSaveDto costShareParamSaveDto){
//        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Long hospId = UserContext.getHospId();
        costShareParamSaveDto.setHospId(hospId);
		costShareParamService.addCostShareParam(costShareParamSaveDto);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改参数信息")
    public Result update(@RequestBody @Valid  CostShareParamEditDto costShareParamEditDto){
//        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Long hospId = UserContext.getHospId();
        costShareParamEditDto.setHospId(hospId);
		costShareParamService.updateCostShareParam(costShareParamEditDto);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除分摊参数信息")
    public Result delete(@RequestBody Integer[] ids){
        costShareParamService.deleteByIds(Arrays.asList(ids));
        return Result.ok();
    }


}
