package com.dxhy.base.service.tdxrecordinvoice;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedHashMap;
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 org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.base.dao.BaseTDxRecordInvoiceDao;
import com.dxhy.base.dao.BaseYqfpxxDao;
import com.dxhy.base.entity.BaseTDxDkCount;
import com.dxhy.base.model.BaseYqrzRespVO;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.RzztEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.vo.Tax;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @author ld
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong", "AlibabaLowerCamelCaseVariableNaming"})
@Service("baseIYqfprzService")
public class BaseYqfprzServiceImpl extends MpBaseServiceImpl<BaseYqfpxxDao, BaseYqrzRespVO>
    implements BaseIYqfprzService {
    @Resource
    private BaseTDxRecordInvoiceDao baseTDxRecordInvoiceDao;
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public String selectByYqcxtj(Map<String, Object> pramsMap, int curr, int size) {
        String[] titlelist = {"序号", "发票代码", "发票号码", "开票日期", "购方名称", "购方税号", "销方税号", "金额", "税额", "抵扣税额"};
        String[] titleprop = {"xh", "fpdm", "fphm", "kprq", "gfmc", "gfsh", "xfsh", "je", "se", "dkse"};
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        // 需要设置返回列表标题时调用
        JSONObject json = super.setTitleList(titlelist, titleprop);
        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("total", 0);
            json.put("datalist", new ArrayList<>());
            return super.responseResult(json);
        }

        if (curr == 1) {
            BaseYqrzRespVO baseYqrzRespVO = super.baseMapper.selectHjByYqcxtj(pramsMap);
            String hjje = "0";
            String hjse = "0";
            String total = "0";
            // 设置记录总数
            if (baseYqrzRespVO != null) {
                total = baseYqrzRespVO.getXh();
                if (!"0".equals(total)) {
                    hjje = MathUtil.add(hjje, MathUtil.round(baseYqrzRespVO.getJe()));
                    hjse = MathUtil.add(hjse, MathUtil.round(baseYqrzRespVO.getSe()));
                }
            }
            json.put("total", AmountFormatUtil.fmtMicrometer(total));
            json.put("hjje", AmountFormatUtil.fmtMicrometer(hjje));
            json.put("hjse", AmountFormatUtil.fmtMicrometer(hjse));
        }

        PageHelper.startPage(curr, size);
        List<BaseYqrzRespVO> pageList = super.baseMapper.selectByYqcxtj(pramsMap);
        PageInfo<BaseYqrzRespVO> pageInfo = new PageInfo<>(pageList);
        // 用于页面展示
        List<BaseYqrzRespVO> resultList = pageInfo.getList();
        List<Object> returnList = new ArrayList<>();
        if (resultList != null && resultList.size() > 0) {
            List<Tax> taxList = (List<Tax>)pramsMap.get("taxList");
            List<String> taxnoList = new ArrayList<>();
            for (Tax t : taxList) {
                String upgradeStatus = t.getUpgradeStatus();
                if ("1".equals(upgradeStatus)) {
                    taxnoList.add(t.getTaxno());
                }
            }
            int start = new Long(pageInfo.getStartRow()).intValue();

            for (int i = 0; i < resultList.size(); i++) {
                // 页面展示需要信息
                BaseYqrzRespVO vo = this.convertToYqlbView(resultList.get(i), start + i);
                if (taxnoList.contains(vo.getGfsh())) {
                    vo.setUpgradeStatus("1");
                } else {
                    vo.setUpgradeStatus("0");
                }
                vo.setSelect(true);
                if (!"0".equals(vo.getAuthStatus()) && !"5".equals(vo.getAuthStatus())
                    && !"6".equals(vo.getAuthStatus())) {
                    vo.setSelect(false);
                }
                if (!gfshList1.contains(vo.getGfsh())) {
                    vo.setSelect(false);
                }
                returnList.add(vo);
            }
        }
        json.put("total", pageInfo.getTotal());
        json.put("datalist", returnList);
        return super.responseResult(json);
    }

    private BaseYqrzRespVO convertToYqlbView(BaseYqrzRespVO baseYqrzRespVo, int i) {
        BaseYqrzRespVO yqxx = new BaseYqrzRespVO();
        if (baseYqrzRespVo.getDkse() != null) {
            yqxx.setDkse(AmountFormatUtil.fmtMicrometer(MathUtil.round(baseYqrzRespVo.getDkse())));
        } else {
            yqxx.setDkse(AmountFormatUtil.fmtMicrometer(MathUtil.round(baseYqrzRespVo.getSe())));
        }
        yqxx.setFpdm(baseYqrzRespVo.getFpdm());
        yqxx.setFphm(baseYqrzRespVo.getFphm());
        yqxx.setGfmc(baseYqrzRespVo.getGfmc());
        yqxx.setGfsh(baseYqrzRespVo.getGfsh());
        yqxx.setId(baseYqrzRespVo.getId());
        yqxx.setJe(AmountFormatUtil.fmtMicrometer(MathUtil.round(baseYqrzRespVo.getJe())));
        yqxx.setKprq(baseYqrzRespVo.getKprq());
        yqxx.setRzsj(baseYqrzRespVo.getRzsj());
        yqxx.setSe(AmountFormatUtil.fmtMicrometer(MathUtil.round(baseYqrzRespVo.getSe())));
//        yqxx.setSfrz(SFRZ_MAP.get(baseYqrzRespVo.getSfrz()));
        yqxx.setSfrz(dictdetaServcice.queryDictName(DictConstant.DEDU_STATUS,baseYqrzRespVo.getSfrz()));
        yqxx.setUuid(baseYqrzRespVo.getUuid());
        yqxx.setXfsh(baseYqrzRespVo.getXfsh());
        yqxx.setXh(String.valueOf(i));
        yqxx.setInvoiceType(baseYqrzRespVo.getInvoiceType());
        yqxx.setAuthStatus(baseYqrzRespVo.getAuthStatus());
        return yqxx;
    }

//    /**
//     * 认证状态类型放入map集合中
//     */
//    public static final Map<String, String> SFRZ_MAP = new LinkedHashMap<String, String>() {
//
//        private static final long serialVersionUID = 1L;
//
//        {
//            for (RzztEnum en : RzztEnum.values()) {
//                this.put(en.getRzztDm(), en.getRzztMc(en.getIndex()));
//            }
//        }
//    };

}
