package com.dxhy.jxpt.baseService.poolPush.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.api.model.db.TDxZzsDkdjWspzEntity;
import com.dxhy.api.model.db.TdxCustomsRecordEntity;
import com.dxhy.api.model.db.TdxPoolLogEntity;
import com.dxhy.api.model.db.TdxRecordInvoiceEntity;
import com.dxhy.jxpt.baseService.dao.TdxDbCustomsRecordDao;
import com.dxhy.jxpt.baseService.dao.TdxPoolLogDao;
import com.dxhy.jxpt.baseService.dao.TdxRecordInvoiceDao;
import com.dxhy.jxpt.baseService.dao.ZzsDkdjWspzDao;
import com.dxhy.jxpt.baseService.poolPush.PoolLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @Description 推送票池业务
* @Author wangruwei
* @Date 2024/10/16 10:51
**/
@Service
@Slf4j
public class PoolLogServiceImpl extends ServiceImpl<TdxPoolLogDao, TdxPoolLogEntity> implements PoolLogService {

    @Autowired
    private TdxPoolLogDao tdxPoolLogDao;
    @Autowired
    private ZzsDkdjWspzDao zzsDkdjWspzDao;
    @Autowired
    private TdxDbCustomsRecordDao tdxCustomsRecordDao;
    @Autowired
    private TdxRecordInvoiceDao tdxRecordInvoiceDao;

    /**
     * 推送票池开关
     **/
    @Value("${jxpt.poolPush.switch:0}")
    private String pushPoolSwitch;

    /**
     * @Description 记录变更数据到pool_log表
     * @param sjlx   票据类型：1.进项发票，2.缴款书，3.代扣代缴完税凭
     * @param uuids  数据唯一标识：
     * sjlx为1发票时为发票uuid，
     * sjlx2为缴款书时为缴款书号码，
     * sjlx3为代扣代缴完税凭证时为代扣代缴完税凭证号
     * bglx为4时，为纳税人识别号@税款所属期（91110000100010433L@202409）
     * @param bglx   变更类型：1.数据变更，2.状态变更，3.源文件变更，4、属期状态变更
     * @Return void
     * @Author wangruwei
     * @Date 2024/10/16 11:03
     **/
    @Override
    public void pushChangeToPool(String sjlx, List<String> uuids, String bglx) {
        if("1".equals(pushPoolSwitch)){
            if(!CollectionUtils.isEmpty(uuids)){
                log.info("1、更新历史插入pool_log的数据sfyx状态为 无效");
                tdxPoolLogDao.update(null, new LambdaUpdateWrapper<TdxPoolLogEntity>()
                        .set(TdxPoolLogEntity::getSfyx, "0")
                        .set(TdxPoolLogEntity::getUpdateTime,new Date())

                        .in(TdxPoolLogEntity::getUuid, uuids)
                        .eq(StringUtils.isNotBlank(sjlx),TdxPoolLogEntity::getSjlx, sjlx)
                        .eq(TdxPoolLogEntity::getClzt, "0")
                );
                log.info("2、插入数据变动的uuids数据");
                uuids.stream().forEach(item->{
                    TdxPoolLogEntity insertItem = new TdxPoolLogEntity();
                    insertItem.setUuid(item);
                    insertItem.setSjlx(sjlx);
                    insertItem.setBglx(bglx);
                    insertItem.setClzt("0");
                    insertItem.setSfyx("1");
                    insertItem.setCreateTime(new Date());
                    tdxPoolLogDao.insert(insertItem);
                });
                //更新底账的票池推送状态为1
                if("1".equals(sjlx)&&"1".equals(bglx)){
                    //发票更新为1
                    LambdaUpdateWrapper<TdxRecordInvoiceEntity> updateWrapper = new LambdaUpdateWrapper<>();
                    updateWrapper.set(TdxRecordInvoiceEntity::getPushPool, "1");
                    updateWrapper.in(TdxRecordInvoiceEntity::getUuid, uuids);
                    tdxRecordInvoiceDao.update(null,updateWrapper);
                }
                if("2".equals(sjlx)&&"1".equals(bglx)){
                    //缴款书更新为1
                    LambdaUpdateWrapper<TdxCustomsRecordEntity> updateWrapper = new LambdaUpdateWrapper<>();
                    updateWrapper.set(TdxCustomsRecordEntity::getPushPool, "1");
                    updateWrapper.in(TdxCustomsRecordEntity::getCustomsCode, uuids);
                    tdxCustomsRecordDao.update(null,updateWrapper);
                }
                if("3".equals(sjlx)&&"1".equals(bglx)){
                    //代扣代缴更新为1
                    LambdaUpdateWrapper<TDxZzsDkdjWspzEntity> updateWrapper = new LambdaUpdateWrapper<>();
                    updateWrapper.set(TDxZzsDkdjWspzEntity::getPushPool, "1");
                    updateWrapper.in(TDxZzsDkdjWspzEntity::getDkdjwspzh, uuids);
                    zzsDkdjWspzDao.update(null,updateWrapper);
                }
            }
        }
    }

    @Override
    public void pushChangeToPool(String sjlx, String uuid, String bglx) {
        if("1".equals(pushPoolSwitch)){
            pushChangeToPool(sjlx, Arrays.asList(uuid), bglx);
        }
    }


}
