package com.dxhy.base.service.invoicecheck;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.vo.DictdetaModel;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.base.dao.BaseTDxRecordInvoiceDao;
import com.dxhy.base.dao.BaseYgxfpqrlbDao;
import com.dxhy.base.entity.BaseTDxDkCount;
import com.dxhy.base.model.BaseYgxfpqrRespVO;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.FpqsjgEnum;
import com.dxhy.common.enums.QsfsEnum;
import com.dxhy.common.service.TabulateRedisService;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 已勾选发票service实现类
 *
 * @author kangzq
 * @date 2019年5月11日
 * @version 1.0
 */
@SuppressWarnings({"AlibabaLowerCamelCaseVariableNaming", "AlibabaUndefineMagicConstant"})
@Service
public class BaseYgxfpqrServiceImpl extends MpBaseServiceImpl<BaseYgxfpqrlbDao, BaseYgxfpqrRespVO>
    implements BaseYgxfpqrService {
    @Resource
    private BaseTDxRecordInvoiceDao baseTDxRecordInvoiceDao;
    @Resource
    private TabulateRedisService tabulateRedisService;
    @Resource
    private DictdetaServcice dictdetaServcice;

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

        String company = (String)pramsMap.get("company");
        String menuId = (String)pramsMap.get("menuId");
        JSONObject json = tabulateRedisService.getTabulateMenu(company, menuId);

        List<String> gfshList = (List<String>)pramsMap.get("gfsh");
        List<String> gfshList1 = new ArrayList<>();
        if (gfshList.size() > 0) {
            List<BaseTDxDkCount> curList = baseTDxRecordInvoiceDao.selectDksh(pramsMap);
            for (BaseTDxDkCount cur : curList) {
                String skssq = cur.getSkssq();
                String year = skssq.substring(0, 4);
                String mon = skssq.substring(4);
                LocalDate localDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(mon), 1);
                LocalDate localDate1 = localDate.plusMonths(1);
                LocalDate localDate2 = LocalDate.now();
                if ("0".equals(cur.getTjStatus()) || "3".equals(cur.getTjStatus()) || localDate2.isBefore(localDate1)) {
                    gfshList1.add(cur.getTaxno());
                }
            }
        } else {
            json.put("hjsl", "0");
            json.put("hjje", "0");
            json.put("hjse", "0");
            json.put("datalist", new ArrayList<>());
            return super.responseResult(json);
        }
        // 合计信息,仅第一页时需要统计，减少数据库压力
        if (curr == 1) {
            String hjsl = "0";
            String hjje = "0";
            String hjse = "0";
            List<BaseYgxfpqrRespVO> hjList = super.baseMapper.selectHjxxByGfshAndKprq(pramsMap);
            BaseYgxfpqrRespVO hjxx = hjList.get(0);
            hjsl = hjxx.getSl();
            if (!"0".equals(hjsl)) {
                hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getJe()));
                hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getSe()));
            }
            json.put("hjsl", AmountFormatUtil.fmtMicrometer(hjsl));
            json.put("hjje", hjje);
            json.put("hjse", hjse);
        }

        PageHelper.startPage(curr, size);
        List<BaseYgxfpqrRespVO> pageList = super.baseMapper.selectByGfshAndKprq(pramsMap);
        PageInfo<BaseYgxfpqrRespVO> pageInfo = new PageInfo<>(pageList);

        // 设置记录总数
        json.put("total", pageInfo.getTotal());

        // 用于页面展示
        List<BaseYgxfpqrRespVO> resultList = pageInfo.getList();
        if (resultList != null && resultList.size() > 0) {

            int start = new Long(pageInfo.getStartRow()).intValue();

            for (int i = 0; i < resultList.size(); i++) {
                // 页面展示需要信息
                BaseYgxfpqrRespVO xxlb = this.convertToHzlbView(resultList.get(i), start + i);
                xxlb.setSelect(gfshList1.contains(xxlb.getGfsh()));
                resultList.set(i, xxlb);
            }
        }
        json.put("datalist", resultList);

        return super.responseResult(json);
    }

    /**
     * 转换结果数据为页面可展示表格
     *
     * @param ygxfplb
     *            待转换对象
     * @param i
     *            序号
     * @return 返回
     *
     */
    private BaseYgxfpqrRespVO convertToHzlbView(BaseYgxfpqrRespVO ygxfplb, int i) {
        ygxfplb.setXh(String.valueOf(i));
        ygxfplb.setJe(AmountFormatUtil.fmtMicrometer(MathUtil.round(ygxfplb.getJe())));
        ygxfplb.setSe(AmountFormatUtil.fmtMicrometer(MathUtil.round(ygxfplb.getSe())));
        if (ygxfplb.getYxdkse() != null) {
            ygxfplb.setYxdkse(AmountFormatUtil.fmtMicrometer(MathUtil.round(ygxfplb.getYxdkse())));
        }
        //字典转换
        ygxfplb.setQszt(dictdetaServcice.queryDictName(DictConstant.QS_STATUS,ygxfplb.getQszt()));
        ygxfplb.setQsfs(dictdetaServcice.queryDictName(DictConstant.QS_TYPE,ygxfplb.getQsfs()));
        ygxfplb.setPaymentStatus(dictdetaServcice.queryDictName(DictConstant.FK_STATUS,ygxfplb.getPaymentStatus()));
        return ygxfplb;
    }

}
