package com.dxhy.extend.service.bb.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.extend.dao.ExtBbfpbjDao;
import com.dxhy.extend.entity.ExtTdxRecordInvoice;
import com.dxhy.extend.service.bb.ExtFpzhbjService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

@Service("fpzhbjService")
@Slf4j
public class ExtFpzhbjServiceImpl extends MpBaseServiceImpl<ExtBbfpbjDao, ExtTdxRecordInvoice> implements ExtFpzhbjService {

    @Override
    public String editorVoucherNumber(Map<String, Object> pramsMap) {
        JSONObject jsonObject = new JSONObject();
        try{
            String[] uuids = pramsMap.get("uuids").toString().split(",");
            String voucherNumber = (String)  pramsMap.get("voucherNumber");
            Arrays.stream(uuids).forEach(uuid->{
                UpdateWrapper<ExtTdxRecordInvoice> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("uuid",uuid);
                ExtTdxRecordInvoice recordInvoice = new ExtTdxRecordInvoice();
                recordInvoice.setVoucherNumber(voucherNumber);
                log.info("发票综合查询编辑凭证号操作---uuid{},recordInvoice:{}",uuid,JSONObject.toJSON(recordInvoice));
                DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_WRITE);
                super.update(recordInvoice, updateWrapper);
            });
            jsonObject.put("message", "执行成功");
        }catch (Exception e){
            // 统一设置操作异常标识
            jsonObject = super.errorResult(e);
        }
        return super.responseResult(jsonObject);
    }
}
