package com.ruoyi.web.v2.v1.controller;


import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.datasource.DynamicDataSourceService;
import com.ruoyi.web.v2.v1.entity.PayInvoice;
import com.ruoyi.web.v2.v1.entity.PayRecord;
import com.ruoyi.web.v2.v1.entity.request.PayInvoiceRequest;
import com.ruoyi.web.v2.v1.service.IPayInvoiceService;
import com.ruoyi.web.v2.v1.service.IPayRecordService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-12-25
 */
@RestController
@RequestMapping("/pay-invoice")
public class PayInvoiceController {
    @Autowired
    private IPayInvoiceService payInvoiceService;
    @Autowired
    private IPayRecordService payRecordService;
    @Autowired
    private DynamicDataSourceService dynamicDataSourceService;
    @ApiOperation("添加")
    @PostMapping("/add")
    public AjaxResult register(@RequestBody PayInvoice payInvoice)   {
        payInvoiceService.save(payInvoice);
        String recordIds = payInvoice.getRecordIds();
        PayRecord payRecord = new PayRecord();
        payRecord.setInvoiceStatus(1);
        for (String s : recordIds.split(",")) {
            payRecord.setId(Integer.parseInt(s));
            payRecordService.updateById(payRecord);
        }

        return success();
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody PayInvoice payInvoice)   {
        payInvoiceService.updateById(payInvoice);
        String recordIds = payInvoice.getRecordIds();
        PayRecord payRecord = new PayRecord();
        payRecord.setInvoiceStatus(payInvoice.getInvoiceStatus());
        for (String s : recordIds.split(",")) {
            payRecord.setId(Integer.parseInt(s));
            payRecordService.updateById(payRecord);
        }
        return success();
    }


    @ApiOperation("分页查询")
    @PostMapping("/page")
    public AjaxResult page(@RequestBody PayInvoiceRequest payInvoice)   {
        return payInvoiceService.getListByPage(payInvoice);

    }

    @ApiOperation("查询全部")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody PayInvoiceRequest payInvoice) {

        return payInvoiceService.getList(payInvoice);
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    @Transactional
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap){
        String ids = paramsMap.get("ids");
        if (StrUtil.isBlank(ids)) {
            return AjaxResult.error("请求参数为空");
        }
        for (String s : ids.split(",")) {
            payInvoiceService.removeById(s);
        }

        return success("删除成功");


    }


    @ApiOperation("根据id查询")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap){
        String id = paramsMap.get("id");
        return success(payInvoiceService.getById(id));
    }

}
