package com.ruoyi.oa.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.oa.domain.OaPaymentProgress;
import com.ruoyi.oa.domain.SysOaFinance;
import com.ruoyi.oa.domain.bo.OaPaymentProgressBo;
import com.ruoyi.oa.domain.bo.SysOaFinanceBo;
import com.ruoyi.oa.domain.vo.OaPaymentProgressVo;
import com.ruoyi.oa.domain.vo.SysOaFinanceVo;
import com.ruoyi.oa.service.IOaPaymentProgressService;
import com.ruoyi.oa.service.ISysOaFinanceService;
import liquibase.pro.packaged.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.List;

/**
 * 付款进度
 */
@RestController
@RequestMapping("/oa/payment-progress")
public class OaPaymentProgressController  extends BaseController {

    @Autowired
    private IOaPaymentProgressService progressService;
    @Autowired
    private ISysOaFinanceService financeService;


    /** 1. 创建付款进度 **/
    @PostMapping
    public R<Boolean> createProgress(@RequestBody OaPaymentProgressBo p) {

        return R.ok(progressService.insertByBo(p));
    }

    /** 2. 修改付款进度（不可改主键/项目ID） **/
    @PutMapping
    public R<Boolean>  updateProgress(@RequestBody OaPaymentProgressBo p) {

        return R.ok(progressService.updateByBo(p));
    }

    /**
     * 查询项目付款进度列表
     */
    @GetMapping("/list")
    public TableDataInfo<OaPaymentProgressVo> list(OaPaymentProgressBo bo, PageQuery pageQuery) {
        return progressService.queryPageList(bo, pageQuery);
    }

    /** 4. 查询某个进度下的所有明细（finance 记录） **/
    @GetMapping("/{progressId}/details")
    public R<List<SysOaFinanceVo>> listDetails(@PathVariable("progressId") Long progressId) {
        return R.ok(financeService.getByProgressId(progressId));
    }

    /** 5. 为某个进度添加明细 **/
    @PostMapping("/{progressId}/details")
    public R<Boolean> addDetail(@PathVariable("progressId") Long progressId,
                                     @RequestBody SysOaFinanceBo f) {
        f.setPaymentProgressId(progressId);

        return R.ok(financeService.insertByBo(f));
    }

    /** 6. 修改明细（不可改主键/进度ID） **/
    @PutMapping("/details")
    public R<Boolean> updateDetail(@RequestBody SysOaFinanceBo f) {
        SysOaFinanceVo exist = financeService.queryById(f.getFinanceId());
        f.setPaymentProgressId(exist.getPaymentProgressId());
        return R.ok(financeService.updateByBo(f));
    }

    /** 7. 删除某条明细（并级联删 detail 记录） **/
    @DeleteMapping("/details/{financeId}")
    public void deleteDetail(@PathVariable Long financeId) {
        financeService.deleteDetail(financeId);
    }

    /**
     * 删除项目付款进度
     *
     * @param paymentProgressIds 主键串
     */
    @DeleteMapping("/{paymentProgressIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] paymentProgressIds) {
        return toAjax(progressService.deleteWithValidByIds(Arrays.asList(paymentProgressIds), true));
    }
}
