package com.dxhy.base.service.invoicecheck;

import java.math.BigDecimal;
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;

/**
 *
 *
 * 根据id更新勾选状态
 *
 * @author kangzq @date 2019年4月24日
 *
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong", "AlibabaCollectionInitShouldAssignCapacity",
    "AlibabaLowerCamelCaseVariableNaming"})
@Service("baseInvoiceCheckService")
public class BaseInvoiceCheckServiceImpl extends MpBaseServiceImpl<BaseTDxRecordInvoiceDao, BaseTDxRecordInvoice>
    implements BaseInvoiceCheckService {
    @Resource
    private BaseTDxTaxCurrentDao baseTDxTaxCurrentDao;
    @Resource
    private InvoiceLogService invoiceLogService;

    @Override
    public String updateInvoiceCheck(Map<String, Object> pramsMap) {
        JSONObject json = new JSONObject();
        String userAccount = (String)pramsMap.get("userAccount");
        String[] ids = pramsMap.get("ids").toString().split(",");
        String[] ses = null;
        if (pramsMap.get("ses") != null) {
            ses = pramsMap.get("ses").toString().split(",");
        }
        String hjje = "0";
        String hjse = "0";
        String hjdk = "0";
        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();
        boolean flag = false;
        boolean flag1 = false;
        sb.append("以下发票号码的发票抵扣税额大于税额不可勾选： \n ");
        sb1.append("以下税号已被锁定不可勾选： \n ");
        List<String> taxList = new ArrayList<>();
        int total = 0;
        List<InvoiceLog> logList = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            try {
                QueryWrapper<BaseTDxRecordInvoice> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id", ids[i]);
                DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
                BaseTDxRecordInvoice recordInvoice = super.baseMapper.selectOne(queryWrapper);
                if (recordInvoice.getTaxAmount().compareTo(new BigDecimal(ses[i])) < 0) {
                    sb.append(recordInvoice.getInvoiceNo()).append(" \n ");
                    flag = true;
                    continue;
                }
                QueryWrapper<BaseTDxTaxCurrent> currentWrapper = new QueryWrapper<>();
                currentWrapper.eq("taxno", recordInvoice.getGfTaxNo());
                BaseTDxTaxCurrent current = baseTDxTaxCurrentDao.selectOne(currentWrapper);
                if ("1".equals(current.getLatchStatus())) {
                    if (!taxList.contains(recordInvoice.getGfTaxNo())) {
                        sb1.append(recordInvoice.getGfTaxNo()).append(" \n ");
                        taxList.add(recordInvoice.getGfTaxNo());
                    }
                    flag1 = true;
                    continue;
                }

                Map<String, Object> pramaps = new HashMap<>();
                pramaps.put("gxType", pramsMap.get("gxType"));
                pramaps.put("dkTaxAmount", new BigDecimal(ses[i]));
                pramaps.put("userAccount", userAccount);
                pramaps.put("userName", pramsMap.get("userName"));
                pramaps.put("id", ids[i]);
                pramaps.put("bdkStatus", "1");
                DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_WRITE);
                super.baseMapper.updateGx(pramaps);
                hjje = MathUtil.round(MathUtil.add(hjje, recordInvoice.getInvoiceAmount().toPlainString()));
                hjse = MathUtil.round(MathUtil.add(hjse, recordInvoice.getTaxAmount().toPlainString()));
                hjdk = MathUtil.round(MathUtil.add(hjdk, ses[i]));
                total = total + 1;
                InvoiceLog invoiceLog = new InvoiceLog();
                invoiceLog.setInvoiceCode(recordInvoice.getInvoiceCode());
                invoiceLog.setInvoiceNo(recordInvoice.getInvoiceNo());
                invoiceLog.setType("2");
                invoiceLog.setCreateDate(new Date());
                invoiceLog.setInputName((String)pramsMap.get("userName"));
                invoiceLog.setInputStatus("13");
                invoiceLog.setRzType((String)pramsMap.get("gxType"));
                logList.add(invoiceLog);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (logList.size() > 0) {
            invoiceLogService.toInvoiceLog(logList);
        }
        String message = "";
        if (flag) {
            message = sb.toString();
        }
        if (flag1) {
            message = message + sb1.toString() + "如需勾选，请到企业税务信息菜单解除锁定";
        }
        json.put("message", message);
        json.put("total", total);
        json.put("hjje", AmountFormatUtil.fmtMicrometer(hjje));
        json.put("hjse", AmountFormatUtil.fmtMicrometer(hjse));
        json.put("hjdk", AmountFormatUtil.fmtMicrometer(hjdk));
        return super.responseResult(json);
    }

}
