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.ApproveStatusEnum;
import com.storlead.sales.pojo.dto.finance.OrderReceivePlanQueryDTO;
import com.storlead.sales.pojo.entity.finance.ReimburseBillRecordEntity;
import com.storlead.sales.service.finance.ReimburseBillRecordService;
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:14
 */
@Api(tags = "财务: 报销")
@RestController
@RequestMapping("/finance/reimburse")
public class ReimburseBillApiController {

    @Resource
    private ReimburseBillRecordService reimburseBillRecordService;

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

    @ApiOperation(value = "保存报销记录")
    @PostMapping( "/save")
    public Result<?> save(@RequestBody ReimburseBillRecordEntity record) {
        if (Objects.isNull(record.getId())) {
            record.setDataCode(CodeGenerate.generate12Code(CodeGenerateInterface.ORDER_REIMBURSE_BILL_CODE));
        }
        reimburseBillRecordService.saveOrUpdate(record);
        return Result.ok();
    }

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


    @ApiOperation(value = "删除报销记录")
    @PostMapping( "/delete")
    public Result<?> delete(Long id) {
        ReimburseBillRecordEntity record = reimburseBillRecordService.getById(id);
        if (Objects.isNull(record)) {
            return Result.error("报销记录不存在");
        }
        if (ApproveStatusEnum.APPROVE_NO_PASS.getCode().equals(record.getApproveStatus())
                || ApproveStatusEnum.APPROVE_ING.getCode().equals(record.getApproveStatus())
                || ApproveStatusEnum.NOT_SUBMIT_APPROVE.getCode().equals(record.getApproveStatus())
        ) {
            reimburseBillRecordService.lgDelete(id);
        } else {
            return Result.error("报销记录已审批通过，不能删除");
        }
        return Result.ok();
    }

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