package com.dxhy.base.service.invoicecheck;

import java.util.*;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.base.dao.BaseTDxRecordInvoiceDao;
import com.dxhy.base.dao.BaseTDxTaxCurrentDao;
import com.dxhy.base.entity.BaseTDxRecordInvoice;
import com.dxhy.base.entity.BaseTDxTaxCurrent;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.InvoiceLogService;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.vo.InvoiceLog;

/**
 * 撤销已勾选发票service实现类
 *
 * @author kangzq
 * @date 2019年5月11日
 * @version 1.0
 */
@SuppressWarnings({"AlibabaCollectionInitShouldAssignCapacity", "AlibabaLowerCamelCaseVariableNaming"})
@Service("baseCxygxfpService")
public class BaseCxygxfpServiceImpl extends MpBaseServiceImpl<BaseTDxRecordInvoiceDao, BaseTDxRecordInvoice>
    implements BaseCxygxfpService {
    @Resource
    private InvoiceLogService invoiceLogService;
    @Resource
    private BaseTDxTaxCurrentDao baseTDxTaxCurrentDao;

    @Override
    public String updateByIds(Map<String, Object> paramMap) {
        JSONObject json = new JSONObject();
        String[] cxids = paramMap.get("cxids").toString().split(",");
        String hjje = "0";
        String hjse = "0";
        String hjdk = "0";
        int hjsl = 0;
        StringBuilder sb = new StringBuilder();
        boolean flag = false;
        sb.append("以下税号已被锁定不可撤销勾选： \n ");
        List<String> taxList = new ArrayList<>();
        List<InvoiceLog> logList = new ArrayList<>();
        for (String cxid : cxids) {
            try {
                DynamicContextHolder.push(paramMap.get("dbName") + DbConstant.BUSINESS_READ);
                BaseTDxRecordInvoice recordInvoice = super.baseMapper.selectById(cxid);
                QueryWrapper<BaseTDxTaxCurrent> currentWrapper = new QueryWrapper<>();
                currentWrapper.eq("taxno", recordInvoice.getGfTaxNo());
                BaseTDxTaxCurrent current = baseTDxTaxCurrentDao.selectOne(currentWrapper);
                if ("1".equals(current.getLatchStatus())) {
                    if (!taxList.contains(current.getTaxno())) {
                        sb.append(current.getTaxno()).append(" \n ");
                        taxList.add(current.getTaxno());
                    }
                    flag = true;
                    continue;
                }
                DynamicContextHolder.push(paramMap.get("dbName") + DbConstant.BUSINESS_WRITE);
                Map<String, Object> pramaps = new HashMap<>();
                pramaps.put("taxAmount", recordInvoice.getTaxAmount());
                pramaps.put("id", cxid);
                super.baseMapper.updateCxGx(pramaps);
                hjsl++;
                hjje = MathUtil.add(hjje, MathUtil.round(recordInvoice.getInvoiceAmount().toPlainString()));
                hjse = MathUtil.add(hjse, MathUtil.round(recordInvoice.getTaxAmount().toPlainString()));
                hjdk = MathUtil.add(hjdk, MathUtil.round(recordInvoice.getDkTaxAmount().toPlainString()));
                InvoiceLog invoiceLog = new InvoiceLog();
                invoiceLog.setInvoiceCode(recordInvoice.getInvoiceCode());
                invoiceLog.setInvoiceNo(recordInvoice.getInvoiceNo());
                invoiceLog.setType("2");
                invoiceLog.setCreateDate(new Date());
                invoiceLog.setInputName((String)paramMap.get("userName"));
                invoiceLog.setInputStatus("18");
                logList.add(invoiceLog);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (logList.size() > 0) {
            invoiceLogService.toInvoiceLog(logList);
        }
        String message = "";
        if (flag) {
            message = sb.toString() + "如需撤销勾选，请到企业税务信息菜单解除锁定";
        }
        json.put("message", message);
        json.put("total", hjsl);
        json.put("hjje", AmountFormatUtil.fmtMicrometer(hjje));
        json.put("hjse", AmountFormatUtil.fmtMicrometer(hjse));
        json.put("hjdk", AmountFormatUtil.fmtMicrometer(hjdk));
        return super.responseResult(json);

    }
}
