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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.api.model.config.sys.DictdetaModel;
import com.dxhy.api.model.config.sys.SysEnterpriseConfigEntity;
import com.dxhy.base.dao.BaseSysEnterpriseConfigDao;
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.core.dao.CoreQyswxxDao;
import com.dxhy.core.model.QyswxxRespVO;
import com.dxhy.core.service.export.IQyswxxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 税务信息业务实现类
 *
 * @author user
 */
@SuppressWarnings("AlibabaMethodTooLong")
@Service("qyswxxService")
@Slf4j

public class QyswxxServiceImpl extends MpBaseServiceImpl<CoreQyswxxDao, QyswxxRespVO> implements IQyswxxService {

    @Resource
    private DictdetaServcice dictdetaServcice;

    @Autowired
    private BaseSysEnterpriseConfigDao sysEnterpriseConfigDao;

    @Override
    public JSONObject selectByGfshAndXydj(Map<String, Object> pramsMap, int curr, int size) {
        JSONObject json = new JSONObject();

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        PageHelper.startPage(curr, size);
        List<QyswxxRespVO> pageList = super.baseMapper.selectByGfshAndXydj(pramsMap);
        PageInfo<QyswxxRespVO> pageInfo = new PageInfo<>(pageList);
        json.put("hasNext", pageInfo.isHasNextPage());
        json.put("nextPage", pageInfo.getNextPage());
        // 用于页面展示
        List<QyswxxRespVO> resultList = pageInfo.getList();
        List<String[]> returnList = new ArrayList<>();
        if (resultList != null && resultList.size() > 0) {
            int i = 0;
            for (QyswxxRespVO hzxx : resultList) {
                // 页面展示需要信息
                i++;
                hzxx.setXh(String.valueOf((curr - 1) * size + i));
                //字典转换
                hzxx.setCompanyType(dictdetaServcice.queryDictName(DictConstant.COMPANY_TYPE,hzxx.getCompanyType()));
                hzxx.setOilType(dictdetaServcice.queryDictName(DictConstant.OIL_TYPE,hzxx.getOilType()));
                hzxx.setTaxpayerType(dictdetaServcice.queryDictName(DictConstant.TAXPAYER_TYPE,hzxx.getTaxpayerType()));

                List<DictdetaModel> sbzts = dictdetaServcice.selectDictdeta("sbzt");
                String periodIndexInfo = hzxx.getPeriodIndexInfo();
                String monthIndexInfo = hzxx.getMonthIndexInfo();
                if (StringUtils.isNotBlank(periodIndexInfo)) {
                    String period = periodIndexInfo.substring(periodIndexInfo.lastIndexOf("_") + 1);
                    for (DictdetaModel model : sbzts) {
                        if (model.getDictcode().equals(period)) {
                            hzxx.setPeriodIndexInfo(periodIndexInfo.substring(0, periodIndexInfo.lastIndexOf("_") + 1)
                                + model.getDictname());
                        }
                    }
                }
                if (StringUtils.isNotBlank(monthIndexInfo)) {
                    String month = monthIndexInfo.substring(monthIndexInfo.lastIndexOf("_") + 1);
                    for (DictdetaModel model : sbzts) {
                        if (model.getDictcode().equals(month)) {
                            hzxx.setMonthIndexInfo(
                                monthIndexInfo.substring(0, monthIndexInfo.lastIndexOf("_") + 1) + model.getDictname());
                        }
                    }
                }
                hzxx.setSbzq(getSbzq(hzxx.getSbzq()));
                if ("0".equals(hzxx.getSdzt())) {
                    hzxx.setSdzt("否");
                }
                if ("1".equals(hzxx.getSdzt())) {
                    hzxx.setSdzt("是");
                }
                SysEnterpriseConfigEntity sysEnterpriseConfigEntity = sysEnterpriseConfigDao.selectOne(new QueryWrapper<SysEnterpriseConfigEntity>().eq("nsrsbh", hzxx.getGfsh()));
                if (sysEnterpriseConfigEntity != null&&"1".equals(sysEnterpriseConfigEntity.getJrfs())) {
                    //置空勾选开始，结束，所属期结束日期
                    hzxx.setGxksrq("");
                    hzxx.setGxjsrq("");
                    hzxx.setSsqjsrq("");
                }
            }
        }
        json.put("datalist", resultList);

        return json;
    }

    public String getSbzq(String sbzq) {
        String sbztmsc = "";
        if (StringUtils.isNotBlank(sbzq)) {
            switch (sbzq) {
                case "1":
                    sbztmsc = "月报";
                    break;
                case "3":
                    sbztmsc = "季报";
                    break;
                default:
                    break;
            }
        }
        return sbztmsc;
    }
}
