package com.jwipc.finance.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jwipc.finance.entity.AccountStatement;
import com.jwipc.finance.entity.AccountStatementDetail;
import com.jwipc.finance.mapper.AccountStatementDetailMapper;
import com.jwipc.finance.model.po.FastStatementItemPO;
import com.jwipc.finance.model.po.InvoicedApplyItemPO;
import com.jwipc.finance.model.po.StatementInvoicedFlagPO;
import com.jwipc.finance.model.po.StatementItemPO;
import com.jwipc.finance.model.query.AccountStatementDetailQuery;
import com.jwipc.finance.model.query.CreateInvoicedApplyPageQuery;
import com.jwipc.finance.model.query.CreateInvoicedApplyQuery;
import com.jwipc.finance.service.IAccountStatementDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwipc.finance.service.IReceiveVoucherService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.baomidou.dynamic.datasource.support.DdConstants.SLAVE;

/**
 * <p>
 * 对账模块-对账单明细表 服务实现类
 * </p>
 *
 * @author shenyang.ou
 * @since 2024-03-04
 */
@Service
@DS(SLAVE)
public class AccountStatementDetailServiceImpl extends ServiceImpl<AccountStatementDetailMapper, AccountStatementDetail> implements IAccountStatementDetailService {

    @Override
    public List<StatementItemPO> findByStatementId(Long asId) {
        return baseMapper.findByStatementId(asId);
    }

    @Override
    public List<StatementItemPO> findByStatementIdAndFactoryCode(Long asId, String factoryCode) {
        return baseMapper.findByStatementIdAndFactoryCode(asId,factoryCode);
    }

    @Override
    public boolean deleteByAsIdList(List<Long> asIdList) {
        List<AccountStatementDetail> asdList = baseMapper.findByAsId(asIdList);
        for(AccountStatementDetail asd: asdList){
            asd.setDeleteFlag(1);
        }

        if(CollectionUtils.isNotEmpty(asdList)){
            return this.updateBatchById(asdList);
        }

        return true;
    }

    @Override
    public List<AccountStatementDetail> selectDataByQuery(Page<AccountStatementDetail> toPage, AccountStatementDetailQuery query) {
        if(null == toPage){
            return baseMapper.selectDataByQuery(query);
        }
        return baseMapper.selectDataByQuery(toPage, query);
    }

    @Override
    public void updateDetailItemNo(Long asId) {
        AccountStatementDetailQuery query = new AccountStatementDetailQuery();
        query.setAsId(asId);
        final List<AccountStatementDetail> list = baseMapper.selectDataByQuery(query);

        List<AccountStatementDetail> updateList = Lists.newArrayList();
        int i = 1;
        for(AccountStatementDetail asd: list){
            AccountStatementDetail updateAsd = new AccountStatementDetail();
            updateAsd.setId(asd.getId());
            updateAsd.setItemNo(StringUtils.leftPad(String.valueOf(i*10), 5, "0"));
            updateList.add(updateAsd);
            i++;
        }

        this.updateBatchById(updateList);
    }

    @Override
    public List<FastStatementItemPO> selectFastDataByQuery(Page<AccountStatement> toPage, AccountStatementDetailQuery query) {
        if(null == toPage){
            return baseMapper.selectFastDataByQuery(query);
        }
        return baseMapper.selectFastDataByQuery(toPage, query);
    }

    /**
     * 提供创建开票申请时获取对账单明细数据
     *
     * @param query 查询条件
     * @return 结果
     */
    @Override
    public List<InvoicedApplyItemPO> provideCreateInvoicedApply(CreateInvoicedApplyQuery query) {
        if(StringUtils.isEmpty(query.getSupplierCode())){
            return Lists.newArrayList();
        }
        if(StringUtils.isEmpty(query.getCompanyCode())){
            return Lists.newArrayList();
        }
        if(StringUtils.isEmpty(query.getFactoryCode())){
            return Lists.newArrayList();
        }
        if(StringUtils.isEmpty(query.getCurrencyCode())){
            return Lists.newArrayList();
        }

        // 允许用于创建开票申请的对账单明细ID
        List<Long> allowUseAsdIdList = Lists.newArrayList();
        if(null != query.getTempDelAsdIds() && StringUtils.isNotEmpty(query.getTempDelAsdIds())){
            final String[] tempDelAsdArr = StringUtils.split(query.getTempDelAsdIds(), ",");
            for(String id: tempDelAsdArr){
                allowUseAsdIdList.add(Long.parseLong(id));
            }
        }

        final List<AccountStatementDetail> asdList = baseMapper.findAllowUseAsd(query);
        for(AccountStatementDetail asd: asdList){
            allowUseAsdIdList.add(asd.getId());
        }
        if(CollectionUtils.isEmpty(allowUseAsdIdList)){
            return Lists.newArrayList();
        }

        List<InvoicedApplyItemPO> invoicedApplyItemDtos = baseMapper.provideCreateInvoicedApply(allowUseAsdIdList);
        if (CollectionUtils.isNotEmpty(invoicedApplyItemDtos)){
            invoicedApplyItemDtos.forEach(invoicedApplyItemDto -> {
                //如果工厂为9001或9002，财务设为已确认
                if ("9001".equals(invoicedApplyItemDto.getFactoryCode())||"9002".equals(invoicedApplyItemDto.getFactoryCode())){
                    invoicedApplyItemDto.setFinConf(1);
                }
            });
        }
        return invoicedApplyItemDtos;
    }

    /**
     * 提供创建开票申请时获取对账单明细数据(根据对账单id分页)
     *
     * @param dbPage
     * @param query  对账单id
     * @return 结果
     */
    @Override
    public List<InvoicedApplyItemPO> provideCreateInvoicedApplyPage(Page<AccountStatementDetail> dbPage, CreateInvoicedApplyPageQuery query) {
        if (dbPage==null){
            return baseMapper.provideCreateInvoicedApplyPage(query);
        }
        return baseMapper.provideCreateInvoicedApplyPage(dbPage,query);
    }

    /**
     * 根据对账单ID获取对账单明细开票完成标识的情况
     *
     * @param asIdList 对账单ID集合
     * @return 结果
     */
    @Override
    public List<StatementInvoicedFlagPO> findStatementInvoicedFlag(List<Long> asIdList) {
        return baseMapper.findStatementInvoicedFlag(asIdList);
    }
}
