package com.kcim.web;

import com.kcim.common.util.PageUtils;
import com.kcim.common.util.Result;
import com.kcim.dao.model.dto.CopyShareParamValueDTO;
import com.kcim.dao.model.dto.ShareParamValueEditDTO;
import com.kcim.dao.model.dto.ShareParamValueSaveDTO;
import com.kcim.service.ShareParamValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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


/**
 * 成本分摊参数值设置
 *
 * @author huangrui
 * @date 2021-08-17 10:03:48
 */
@Api(tags = "成本分摊参数值管理")
@RestController
@RequestMapping("/shareParamValue")
public class ShareParamValueController extends AbstractController {

    private final ShareParamValueService shareParamValueService;

    public ShareParamValueController(ShareParamValueService shareParamValueService) {
        this.shareParamValueService = shareParamValueService;
    }

    @ApiOperation("添加分摊参数值")
    @PostMapping("/addData")
    public Result addData(@RequestBody @Valid ShareParamValueSaveDTO shareParamValueSaveDTO) {
        if (shareParamValueSaveDTO.getDate().length() == 6) {
            shareParamValueSaveDTO.setDate(shareParamValueSaveDTO.getDate()+"01");
        }
        shareParamValueService.addData(shareParamValueSaveDTO, getHospId());
        return Result.ok();
    }

    @ApiOperation("成本分摊参数值列表")
    @GetMapping("/list")
    public Result list(@RequestParam(value = "current", defaultValue = "1") Integer current,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "date") String date,
                       @RequestParam(value = "shareParamCode", required = false) String shareParamCode,
                       @RequestParam(value = "responsibilityCode", required = false) String responsibilityCode) {
        PageUtils pageUtils = shareParamValueService.selectList(current, pageSize, date + "01", shareParamCode, responsibilityCode, getHospId());
        return Result.ok(pageUtils);
    }

    @ApiOperation("复制数据")
    @PostMapping("/copyData")
    public Result copyData(@RequestBody @Valid CopyShareParamValueDTO copyShareParamValueDTO) {
        if (copyShareParamValueDTO.getFromDate().length() == 6) {
            copyShareParamValueDTO.setFromDate(copyShareParamValueDTO.getFromDate() + "01");
        }
        if (copyShareParamValueDTO.getToDate().length() == 6) {
            copyShareParamValueDTO.setToDate(copyShareParamValueDTO.getToDate() + "01");
        }
        shareParamValueService.copyData(copyShareParamValueDTO, getHospId());
        return Result.ok();
    }


    @ApiOperation("修改")
    @PostMapping("/edit")
    public Result update(@RequestBody @Valid ShareParamValueEditDTO shareParamValueEditDTO) {
        if (shareParamValueEditDTO.getDate().length() == 6) {
            shareParamValueEditDTO.setDate(shareParamValueEditDTO.getDate() + "01");
        }
        shareParamValueService.editParamValue(shareParamValueEditDTO,getHospId());
        return Result.ok();
    }

    @ApiOperation("删除某条数据")
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids) {
        shareParamValueService.deleteByIds(Arrays.asList(ids));
        return Result.ok();
    }

    @ApiOperation("计算数值")
    @PostMapping("/calcData")
    public Result calcData(@RequestParam String date) {
        if (date.length() == 6) {
            date = date.concat("01");
        }
        shareParamValueService.calcData(date, getHospId());
        return Result.ok();
    }

}
