package com.storlead.sales.controller.finance;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.common.constant.CodeGenerateInterface;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.common.util.generate.CodeGenerate;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.finance.OrderReceivePlanQueryDTO;
import com.storlead.sales.pojo.entity.finance.OrderInvoiceRecordEntity;
import com.storlead.sales.service.finance.OrderInvoiceRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 javax.annotation.Resource;
import java.util.Arrays;
import java.util.Objects;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-07-31 11:08
 */
@Api(tags = "财务: 发票")
@RestController
@RequestMapping("/finance/invoice")
public class OrderInvoiceRecordApiController {

    @Resource
    private OrderInvoiceRecordService invoiceRecordService;

    @ApiOperation(value = "获取发票记录")
    @PostMapping( "/list_page")
    public Result<?> list(@RequestBody OrderReceivePlanQueryDTO dto) {
        IPage<OrderInvoiceRecordEntity> page = new Page<>(dto.getPageIndex(),dto.getPageSize());
        LambdaQueryWrapper<OrderInvoiceRecordEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(OrderInvoiceRecordEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        IPage<OrderInvoiceRecordEntity> pageList = invoiceRecordService.page(page,queryWrapper);
        return Result.result(pageList);
    }

    @ApiOperation(value = "保存发票")
    @PostMapping( "/save")
    public Result<?> save(@RequestBody OrderInvoiceRecordEntity record) {
        if (Objects.isNull(record.getId())) {
            record.setDataCode(CodeGenerate.generate12Code(CodeGenerateInterface.ORDER_RECEIVE_CODE));
        }
        invoiceRecordService.saveOrUpdate(record);
        return Result.ok();
    }

    @ApiOperation(value = "获取详情")
    @PostMapping( "/get_detial")
    public Result<?> getDetail(Long id) {
        OrderInvoiceRecordEntity record = invoiceRecordService.getById(id);
        if (Objects.isNull(record)) {
            return Result.error("发票记录不存在");
        }
        return Result.ok(record);
    }

    @ApiOperation(value = "删除发票")
    @PostMapping( "/delete")
    public Result<?> delete(Long id) {
        invoiceRecordService.lgDelete(id);
        return Result.ok();
    }

    @ApiOperation(value = "批量除发票")
    @PostMapping( "/deleteIds")
    public Result<?> deleteIds(Long [] ids) {
        invoiceRecordService.lgDelete(Arrays.asList(ids));
        return Result.ok();
    }

}
