package com.dxhy.core.service.export.impl;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.api.model.config.sys.DictdetaModel;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.MathUtil;
import com.dxhy.core.dao.CoreFpxxZhDao;
import com.dxhy.core.model.AuthenticationCheckListVO;
import com.dxhy.core.model.FpRzRespVO;
import com.dxhy.core.service.export.RzFpxxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 发票认证综合查询
 *
 * @author user
 */
@SuppressWarnings({"AlibabaMethodTooLong", "DuplicatedCode"})
@Service("rzFpxxService")
@Slf4j

public class RzFpxxServiceImpl extends MpBaseServiceImpl<CoreFpxxZhDao, FpRzRespVO> implements RzFpxxService {
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public JSONObject selectByPramsMap(Map<String, Object> pramsMap, int curr, int size) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        JSONObject json = new JSONObject();
        try {
            List<String> gfshList = (List<String>)pramsMap.get("gfsh");
            String inAccountStatus = (String)pramsMap.get("inAccountStatus");
            if (gfshList.size() == 0) {
                // 未从getUserInfo()获取到购方税号信息
                json.put("hjsl", "0");
                json.put("hjje", "0.00");
                json.put("hjse", "0.00");
                json.put("total", "0");
                json.put("datalist", new ArrayList<FpRzRespVO>());
            } else {
                PageHelper.startPage(curr, size);
                List<FpRzRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
                PageInfo<FpRzRespVO> pageInfo = new PageInfo<>(pageList);
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());

                List<FpRzRespVO> resultList = pageInfo.getList();
                if (resultList != null && resultList.size() > 0) {
                    for (int i = 0; i < pageList.size(); i++) {
                        pageList.get(i).setXh((i + 1)+"");
                        // 字典转换
                        pageList.get(i).setInAccountStatus(dictdetaServcice.queryDictName(DictConstant.DK_STATUS,
                            pageList.get(i).getInAccountStatus()));
                        pageList.get(i)
                                .setInvoiceType(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL, pageList.get(i).getInvoiceType()));
                        String invoiceStatus = pageList.get(i).getInvoiceStatus();
                        pageList.get(i).setInvoiceStatus(
                            dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS, invoiceStatus));
                        String qsStatus = pageList.get(i).getQszt();
                        pageList.get(i).setQszt(dictdetaServcice.queryDictName(DictConstant.QS_STATUS, qsStatus));
                        pageList.get(i)
                            .setQsfs(dictdetaServcice.queryDictName(DictConstant.QS_TYPE, pageList.get(i).getQsfs()));
                        pageList.get(i).setRzzt(
                            dictdetaServcice.queryDictName(DictConstant.DEDU_STATUS, pageList.get(i).getRzzt()));
                        pageList.get(i).setRzclzt(
                            dictdetaServcice.queryDictName(DictConstant.AUTH_STATUS_ALL, pageList.get(i).getRzclzt()));
                        pageList.get(i).setRzlx(
                            dictdetaServcice.queryDictName(DictConstant.BDK_STATUS, pageList.get(i).getBdkStatus()));
                        pageList.get(i).setCxrzStatus(
                            dictdetaServcice.queryDictName(DictConstant.CXRZ_STATUS, pageList.get(i).getCxrzStatus()));
                        pageList.get(i).setPaymentStatus(
                            dictdetaServcice.queryDictName(DictConstant.FK_STATUS, pageList.get(i).getPaymentStatus()));

                        if(StringUtils.isNotEmpty(pageList.get(i).getInvoiceAmount())){
                            pageList.get(i).setInvoiceAmount(MathUtil.round(pageList.get(i).getInvoiceAmount()));
                        }
                        if(StringUtils.isNotEmpty(pageList.get(i).getTaxAmount())){
                            pageList.get(i).setTaxAmount(MathUtil.round(pageList.get(i).getTaxAmount()));
                        }
                        if(StringUtils.isNotEmpty(pageList.get(i).getDkTaxAmount())){
                            pageList.get(i).setDkTaxAmount(MathUtil.round(pageList.get(i).getDkTaxAmount()));
                        }
                    }
                }
                json.put("datalist", pageList);
            }
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }

        return json;
    }

    @Override
    public JSONObject getTaxRebateList(Map<String, Object> pramsMap, int curr, int size) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        JSONObject json = new JSONObject();
        try {
            String buyerTaxNoString = (String)pramsMap.get("buyerTaxNo");
            List<String> buyerTaxNoList = Arrays.asList(buyerTaxNoString.split(","));

            if (buyerTaxNoList.isEmpty()) {
                // 未从getUserInfo()获取到购方税号信息
                json.put("datalist", new ArrayList<FpRzRespVO>());
            } else {
                pramsMap.put("taxNoList", buyerTaxNoList);
                PageHelper.startPage(curr, size);
                List<AuthenticationCheckListVO> pageList = super.baseMapper.selectTaxRebateByPramsMap(pramsMap);
                PageInfo<AuthenticationCheckListVO> pageInfo = new PageInfo<>(pageList);
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());
                // 收集数据
                List<FpRzRespVO> returnList = new ArrayList<>();
                if (!pageList.isEmpty()) {
                    // List<DictdetaModel> fplxModels = dictdetaServcice.selectDictdeta("INVOICE_TYPE");
                    // List<DictdetaModel> fpztModels = dictdetaServcice.selectDictdeta("INVOICE_STATUS");
                    // List<DictdetaModel> qsztModels = dictdetaServcice.selectDictdeta("qsStatus");
                    // List<DictdetaModel> qsfsModels = dictdetaServcice.selectDictdeta("qsType");
                    // List<DictdetaModel> rzclztModels = dictdetaServcice.selectDictdeta("authStatus");
                    // List<DictdetaModel> rzztModels = dictdetaServcice.selectDictdeta("rzhYesorno");
                    List<DictdetaModel> rzlxModels = dictdetaServcice.selectDictdeta("BDK_STATUS");
                    int start = new Long(pageInfo.getStartRow()).intValue();
                    for (AuthenticationCheckListVO vo : pageList) {
                        FpRzRespVO fpVo = new FpRzRespVO();
                        fpVo.setOrder(String.valueOf(pageList.indexOf(vo) + start));
                        fpVo.setInvoiceCode(vo.getInvoiceCode());
                        fpVo.setInvoiceNo(vo.getInvoiceNo());
                        // 字典转换
                        fpVo.setInvoiceType(
                            dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL, vo.getInvoiceType()));
                        fpVo.setInvoiceStatus(
                            dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS, vo.getInvoiceStatus()));
                        fpVo.setSignInStatus(
                            dictdetaServcice.queryDictName(DictConstant.QS_STATUS, vo.getSignInStatus()));
                        fpVo.setSignInType(dictdetaServcice.queryDictName(DictConstant.QS_TYPE, vo.getSignInType()));
                        fpVo.setAuthenticationDetailStatus(dictdetaServcice.queryDictName(DictConstant.AUTH_STATUS_ALL,
                            vo.getAuthenticationDetailStatus()));
                        fpVo.setAuthenticationStatus(
                            dictdetaServcice.queryDictName(DictConstant.DEDU_STATUS, vo.getAuthenticationStatus()));

                        fpVo.setBuyerTax(vo.getBuyerTax());
                        fpVo.setBuyerName(vo.getBuyerName());
                        fpVo.setInvoiceDate(vo.getInvoiceDate());
                        fpVo.setSellerName(vo.getSellerName());
                        fpVo.setSellerTax(vo.getSellerTax());
                        if(StringUtils.isNotEmpty(vo.getInvoiceAmount())){
                            fpVo.setInvoiceAmount(MathUtil.round(vo.getInvoiceAmount()));
                        }
                        if(StringUtils.isNotEmpty(vo.getTaxAmount())){
                            fpVo.setTaxAmount(MathUtil.round(vo.getTaxAmount()));
                        }
                        fpVo.setSignInDate(vo.getSignInDate());
                        fpVo.setSignInPeople(vo.getSignInPeople());
                        fpVo.setAuthenticationDate(vo.getAuthenticationDate());
//                        fpVo.setAuthenticationType("退税认证");
                        fpVo.setAuthenticationType(
                                dictdetaServcice.queryDictName(DictConstant.INVOICE_RZLX, vo.getAuthenticationType()));
                        fpVo.setAuthenticationPerson(vo.getAuthenticationPerson());
                        fpVo.setTaxPeriod(vo.getTaxPeriod());
                        fpVo.setBusinessName(vo.getDeptId());
                        returnList.add(fpVo);
                    }
                }
                json.put("datalist", returnList);
            }
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return json;
    }
}
