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.OrderReceiveRecordEntity;
import com.storlead.sales.service.finance.OrderReceiveRecordService;
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 14:31
 */
@Api(tags = "财务: 回款记录")
@RestController
@RequestMapping("/finance/receive")
public class OrderReceiveRecordApiController {
    @Resource
    private OrderReceiveRecordService receiveRecordService;

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

    @ApiOperation(value = "保存汇款记录")
    @PostMapping( "/save")
    public Result<?> save(@RequestBody OrderReceiveRecordEntity record) {
        if (Objects.isNull(record.getId())) {
            record.setDataCode(CodeGenerate.generate12Code(CodeGenerateInterface.ORDER_RECEIVE_CODE));
        }
        receiveRecordService.saveOrUpdate(record);
        return Result.ok();
    }

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


    @ApiOperation(value = "删除回款记录")
    @PostMapping( "/delete")
    public Result<?> delete(Long id) {
        OrderReceiveRecordEntity record = receiveRecordService.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())
        ) {
            receiveRecordService.lgDelete(id);
        } else {
            return Result.error("回款记录已审批通过，不能删除");
        }
        return Result.ok();
    }

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

}
