package com.dayou.service.impl;

import com.dayou.common.BaseEntity;
import com.dayou.entity.FinanceInvoice;
import com.dayou.enums.FinanceInvoiceState;
import com.dayou.enums.ReportStatus;
import com.dayou.exception.ErrorCode;
import com.dayou.mapper.FinanceInvoiceMapper;
import com.dayou.service.IFinanceInvoiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.utils.LoginContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 财务开票 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-12-08
 */
@Service
public class FinanceInvoiceServiceImpl extends ServiceImpl<FinanceInvoiceMapper, FinanceInvoice> implements IFinanceInvoiceService {

    @Autowired
    private FinanceInvoiceMapper financeInvoiceMapper;

    @Override
    @SuppressWarnings("unchecked")
    public Page<FinanceInvoice> selectPage(Page page,FinanceInvoice financeInvoice,String keyword){
        Page<FinanceInvoice> ret = financeInvoiceMapper.getPage(page,financeInvoice,keyword);
        return ret;
    }


    @Override
    public FinanceInvoice detail(Long id){
        return this.getById(id);
    }

    @Override
    public Boolean add(FinanceInvoice financeInvoice){
        financeInvoice.setApplyId(LoginContext.getCurrentUserId());
        return  this.save(financeInvoice);
    }

    @Override
    public Boolean update(FinanceInvoice financeInvoice){
        financeInvoice.setCheckerId(LoginContext.getCurrentUserId());
        financeInvoice.setMakerId(LoginContext.getCurrentUserId());
        financeInvoice.setState(FinanceInvoiceState.已开票.name());
        return  this.updateById(financeInvoice);
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    @Override
    public List<FinanceInvoice> getList(Long id) {
        List<FinanceInvoice> list = this.list(new LambdaQueryWrapper<FinanceInvoice>().eq(FinanceInvoice::getOrderFundId, id).orderByDesc(BaseEntity::getCreated));
        return list;
    }

    @Override
    public Boolean cancellation(Long id) {
        FinanceInvoice invoice = this.getById(id);
        if (invoice.getState().equals(FinanceInvoiceState.已开票.name())){
            ErrorCode.throwBusinessException(ErrorCode.CUSTOM_ERROR,"此开票申请已开票，无法作废。请联系财务同事确认。");
        }

        return this.update(new LambdaUpdateWrapper<FinanceInvoice>().set(FinanceInvoice::getState, FinanceInvoiceState.作废).eq(BaseEntity::getId,id));
    }
}
