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.enums.ReimburseStatusEnum;
import com.storlead.sales.pojo.dto.finance.OrderReceivePlanQueryDTO;
import com.storlead.sales.pojo.entity.finance.OrderCostRecordEntity;
import com.storlead.sales.service.finance.OrderCostRecordService;
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 15:18
 */
@Api(tags = "财务: 费用")
@RestController
@RequestMapping("/finance/rost")
public class OrderCostApiController {


    @Resource
    private OrderCostRecordService costRecordService;

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

    @ApiOperation(value = "保存费用记录")
    @PostMapping( "/save")
    public Result<?> save(@RequestBody OrderCostRecordEntity record) {
        if (Objects.isNull(record.getId())) {
            record.setDataCode(CodeGenerate.generate12Code(CodeGenerateInterface.ORDER_COST_CODE));
        }
        costRecordService.saveOrUpdate(record);
        return Result.ok();
    }

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

    @ApiOperation(value = "删除费用记录")
    @PostMapping( "/delete")
    public Result<?> delete(Long id) {
        OrderCostRecordEntity record = costRecordService.getById(id);
        if (Objects.isNull(record)) {
            return Result.error("费用记录不存在");
        }
        if (ReimburseStatusEnum.NOT_REIMBURSEED.getCode().equals(record.getReimburseStatus())) {
            costRecordService.lgDelete(id);
        } else {
            return Result.error("费用记录已报销，不能删除");
        }
        return Result.ok();
    }

    @ApiOperation(value = "批量删除费用记录")
    @PostMapping( "/deleteIds")
    public Result<?> deleteIds(Long [] ids) {
        costRecordService.lgDelete(Arrays.asList(ids));
        return Result.ok();
    }
}
