package com.ruoyi.hywz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.hywz.entity.AdsMatHyxtInvoiceD;
import com.ruoyi.hywz.entity.AdsMatHyxtInvoiceFileD;
import com.ruoyi.hywz.service.IAdsMatHyxtInvoiceDService;
import com.ruoyi.hywz.service.IAdsMatHyxtInvoiceFileDService;
import com.ruoyi.hywz.utils.IdGen;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author: jxh
 * @description:
 * @create: 2024/7/4
 */
@RestController
@RequestMapping("adsMatHyxtInvoiceFileD")
@AllArgsConstructor
@Api(value = "adsMatHyxtInvoiceFileD", tags = "开票申请流程管控")
public class AdsMatHyxtInvoiceFileDController {

    private final IAdsMatHyxtInvoiceFileDService adsMatHyxtInvoiceFileDService;
    private final IAdsMatHyxtInvoiceDService adsMatHyxtInvoiceDService;

    @ApiOperation("发票分页")
    @PostMapping("/invoicePage")
    public R invoicePage(@RequestParam(defaultValue = "10") Long pageSize,
                         @RequestParam(defaultValue = "1") Long pageNo,
                         @RequestBody AdsMatHyxtInvoiceD param) {
        if (StringUtils.isBlank(param.getBusinessId())) {
            return R.fail("参数businessId不能为空");
        }
        Page<AdsMatHyxtInvoiceFileD> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);

        LambdaQueryWrapper<AdsMatHyxtInvoiceFileD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtInvoiceFileD::getApplyId, param.getBusinessId());
        queryWrapper.eq(AdsMatHyxtInvoiceFileD::getIsDeleted, 0);
        queryWrapper.orderByDesc(AdsMatHyxtInvoiceFileD::getCreateTime);
        return R.ok(adsMatHyxtInvoiceFileDService.page(page, queryWrapper));
    }

    @ApiOperation("发票删除")
    @PostMapping("/invoiceDelete")
    public R invoiceDelete(@RequestBody AdsMatHyxtInvoiceFileD param) {
        if (StringUtils.isBlank(param.getId())) {
            return R.fail("参数id不能为空");
        }
        LambdaUpdateWrapper<AdsMatHyxtInvoiceFileD> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(AdsMatHyxtInvoiceFileD::getId, param.getId());
        updateWrapper.set(AdsMatHyxtInvoiceFileD::getIsDeleted, 1);
        adsMatHyxtInvoiceFileDService.update(updateWrapper);
        return R.ok();
    }

    @ApiOperation("发票上传")
    @PostMapping("/invoiceSave")
    public R invoiceSave(@RequestBody AdsMatHyxtInvoiceFileD param) {
        if (StringUtils.isBlank(param.getBusinessId())) {
            return R.fail("参数businessId不能为空");
        }
        if (StringUtils.isBlank(param.getInvoiceCode())) {
            return R.fail("请填写发票代码！");
        }
        if (ObjectUtils.isEmpty(param.getMakeInvoiceDate())) {
            return R.fail("请填写开票日期！");
        }

        LambdaUpdateWrapper<AdsMatHyxtInvoiceD> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(AdsMatHyxtInvoiceD::getApplyId, param.getBusinessId());
        updateWrapper.set(AdsMatHyxtInvoiceD::getMakeInvoiceDate, param.getMakeInvoiceDate());
        updateWrapper.set(AdsMatHyxtInvoiceD::getInvoiceCode, param.getInvoiceCode());
        if (ObjectUtils.isNotEmpty(param.getMakeInvoiceDate()) || StringUtils.isNotBlank(param.getInvoiceCode())) {
            updateWrapper.set(AdsMatHyxtInvoiceD::getIsUploadInvoice, 1);
        }
        adsMatHyxtInvoiceDService.update(updateWrapper);

        param.setId(IdGen.get32UUID());
        param.setApplyId(param.getBusinessId());
        param.setCreateTime(new Date());
        param.setIsDeleted(0);
        adsMatHyxtInvoiceFileDService.save(param);

        return R.ok();
    }


}
