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

import com.alibaba.fastjson.JSONObject;
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.util.MathUtil;
import com.dxhy.core.dao.ViolationMapper;
import com.dxhy.core.model.BbfpzhcxRespVO;
import com.dxhy.core.model.EnterpriseInfoVO;
import com.dxhy.core.model.RiskSupplierVO;
import com.dxhy.core.model.ViolationRespVO;
import com.dxhy.core.service.export.ViolationSearchService;
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Gy Chen
 * @date 2021/1/25
 */
@Service
@Slf4j
public class ViolationSearchServiceImpl implements ViolationSearchService {
    @Resource
    ViolationMapper violationMapper;
    @Resource
    DictdetaServcice dictdetaServcice;

    @Override
    public JSONObject selectViolation(Map<String, Object> pramsMap, int curr, int size) {
        JSONObject json = new JSONObject();
        String company = (String)pramsMap.get("company");
        if (StringUtils.isBlank(company)) {
            // 未从getUserInfo()获取到购方税号信息
            json.put("datalist", new ArrayList<ViolationRespVO>());
        } else {
            DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
            PageHelper.startPage(curr, size);
            List<ViolationRespVO> pageList = violationMapper.selectViolation(pramsMap);
            PageInfo<ViolationRespVO> pageInfo = new PageInfo<>(pageList);
            json.put("hasNext", pageInfo.isHasNextPage());
            json.put("nextPage", pageInfo.getNextPage());
            // 用于页面展示
            List<ViolationRespVO> resultList = pageInfo.getList();
            if (resultList != null && resultList.size() > 0) {
                int i = 1;
                for (ViolationRespVO hzxx : resultList) {
                    hzxx.setXh(String.valueOf((curr - 1) * size + i));
                    i++;
                }
            }
            json.put("datalist", resultList);
        }

        return json;
    }

    @Override
    public JSONObject riskSupplier(Map<String, Object> pramsMap, int curr, int size) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        try {
            @SuppressWarnings("unchecked")
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
            String date = df.format(new Date());
            pramsMap.put("currentTime", date);
            List<String> gfshList = (List<String>)pramsMap.get("gfsh");
            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<BbfpzhcxRespVO>());
            } else {
                PageHelper.startPage(curr, size);
                List<RiskSupplierVO> pageList = violationMapper.selectRiskSupplier(pramsMap);
                PageInfo<RiskSupplierVO> pageInfo = new PageInfo<RiskSupplierVO>(pageList);
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());
                List<String[]> returnList = new ArrayList<String[]>();

                // 用于页面返回list对象
                if (pageList != null && pageList.size() > 0) {
                    for (int i = 0; i < pageList.size(); i++) {
                        String[] dataArray = new String[21];
                        dataArray[0] = String.valueOf((curr - 1) * size + i + 1);
                        dataArray[1] = pageList.get(i).getTaxNo();
                        dataArray[2] = pageList.get(i).getTaxName();
                        dataArray[3] = pageList.get(i).getInvoiceNum();
                        dataArray[4] = MathUtil.round(pageList.get(i).getInvoiceAmount());
                        returnList.add(dataArray);
                    }
                }
                json.put("datalist", returnList);
            }
        } catch (Exception e) {
            // 统一设置操作异常标识
           log.info("业务处理异常：{}", e);
        }
        return json;
    }

    @Override
    public JSONObject riskInvoice(Map<String, Object> pramsMap, int curr, int size) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        try {
            @SuppressWarnings("unchecked")
            List<String> gfshList = (List<String>)pramsMap.get("gfsh");
            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<BbfpzhcxRespVO>());
            } else {
                PageHelper.startPage(curr, size);
                List<BbfpzhcxRespVO> pageList = violationMapper.selectRiskInvoice(pramsMap);
                PageInfo<BbfpzhcxRespVO> pageInfo = new PageInfo<BbfpzhcxRespVO>(pageList);
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());
                List<String[]> returnList = new ArrayList<String[]>();

                // 用于页面返回list对象
                if (pageList != null && pageList.size() > 0) {
//                    List<DictdetaModel> fplxModels = dictdetaServcice.selectDictdeta("INVOICE_TYPE");
//                    List<DictdetaModel> fpztModels = dictdetaServcice.selectDictdeta("INVOICE_STATUS");
//                    List<DictdetaModel> glztModels = dictdetaServcice.selectDictdeta("GLZT");
//                    List<DictdetaModel> rzlxModels = dictdetaServcice.selectDictdeta("BDK_STATUS");
//                    List<DictdetaModel> rzztModels = dictdetaServcice.selectDictdeta("rzhYesorno");
                    for (int i = 0; i < pageList.size(); i++) {
                        BbfpzhcxRespVO vo = pageList.get(i);
                        String[] dataArray = new String[18];
                        dataArray[0] = String.valueOf((curr - 1) * size + i + 1);
                        StringBuffer sb = new StringBuffer("");
                        boolean flag = false;
                        if (!"0".equals(vo.getInvoiceStatus())) {
                            flag = true;
                            sb.append("发票状态异常");
                        }
                        if ("1".equals(vo.getGlzt()) || "2".equals(vo.getGlzt())) {
                            if (flag) {
                                sb.append("；管理状态异常");
                            } else {
                                flag = true;
                                sb.append("管理状态异常");
                            }
                        }
                        if ("1".equals(vo.getIllegalSupplier())) {
                            if (flag) {
                                sb.append("；供应商违规");
                            } else {
                                flag = true;
                                sb.append("供应商违规");
                            }
                        }
                        if ("1".equals(vo.getIllegalEnterprise())) {
                            if (flag) {
                                sb.append("；触发企业管控规则");
                            } else {
                                sb.append("触发企业管控规则");
                            }
                        }
                        dataArray[1] = sb.toString();
                        dataArray[2] = dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL,vo.getInvoiceType());
                        dataArray[3] = vo.getInvoiceCode();
                        dataArray[4] = vo.getInvoiceNo();
                        dataArray[5] = vo.getEleInvoiceNo();
                        dataArray[6] = dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS,vo.getInvoiceStatus());
                        dataArray[7] = dictdetaServcice.queryDictName(DictConstant.GLZT,vo.getGlzt());
                        if (StringUtils.isBlank(vo.getGlzt())) {
                            dataArray[7] = "正常";
                        }

                        dataArray[8] = vo.getInvoiceDate();
                        dataArray[9] = vo.getXfmc();
                        dataArray[10] = vo.getXfsh();
                        dataArray[11] = MathUtil.round(vo.getInvoiceAmount());
                        dataArray[12] = MathUtil.round(vo.getTaxAmount());
                        dataArray[13] = MathUtil.round(vo.getTotalAmount());
                        dataArray[14] = vo.getRemark();
                        dataArray[15] = MathUtil.round(vo.getDkTaxAmount());
                        dataArray[16] = dictdetaServcice.queryDictName(DictConstant.DEDU_STATUS,vo.getRzzt());
                        dataArray[17] = dictdetaServcice.queryDictName(DictConstant.BDK_STATUS,vo.getBdkStatus());
                        returnList.add(dataArray);
                    }
                }
                json.put("datalist", returnList);
            }
        } catch (Exception e) {
            // 统一设置操作异常标识
           log.info("业务处理异常：{}", e);
        }
        return json;
    }

    @Override
    public JSONObject riskEnterpriseInvoice(Map<String, Object> params, int curr, int size) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(params.get("dbName") + DbConstant.BUSINESS_READ);

        try {
            @SuppressWarnings("unchecked")
            List<String> gfshList = (List<String>)params.get("gfsh");
            if (gfshList.size() == 0) {
                json.put("datalist", new ArrayList<EnterpriseInfoVO>());
            } else {
                PageHelper.startPage(curr, size);
                List<EnterpriseInfoVO> pageList = violationMapper.selectRiskEnterpriseInvoice(params);
                PageInfo<EnterpriseInfoVO> pageInfo = new PageInfo<EnterpriseInfoVO>(pageList);
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());
                List<String[]> returnList = new ArrayList<String[]>();

                // 用于页面返回list对象
                if (pageList != null && pageList.size() > 0) {
                    for (int i = 0; i < pageList.size(); i++) {
                        EnterpriseInfoVO vo = pageList.get(i);
                        String[] dataArray = new String[6];
                        dataArray[0] = String.valueOf((curr - 1) * size + i + 1);
                        dataArray[1] = dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL,vo.getInvoiceType());
                        dataArray[2] = vo.getInvoiceCode();
                        dataArray[3] = vo.getInvoiceNo();
                        dataArray[4] = vo.getRuleMessage();
                        dataArray[5] = dictdetaServcice.queryDictName(DictConstant.IS_FORCE,vo.getIsForce());
                        returnList.add(dataArray);
                    }
                }
                json.put("datalist", returnList);
            }
        } catch (Exception e) {
            // 统一设置操作异常标识
           log.info("业务处理异常：{}", e);
        }
        return json;
    }
}
