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

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.api.model.cpy.cpyCustom.TDxRecordCustomCpyVo;
import com.dxhy.api.model.db.TdxCustomsRecordEntity;
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.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.core.service.export.CpyGxCustomsExportService;
import com.dxhy.jxpt.baseService.cpyAuthInvoice.dao.CpyRecordCustomDao;
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.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Description 成品油勾选导出
 * @Author wangruwei
 * @Date 2024/7/17 16:33
 **/
@SuppressWarnings("AlibabaMethodTooLong")
@Service("cpyGxCustomsExportService")
@Slf4j
public class CpyGxCustomsExportServiceImpl extends MpBaseServiceImpl<CpyRecordCustomDao, TdxCustomsRecordEntity> implements CpyGxCustomsExportService {

    @Resource
    private DictdetaServcice dictdetaServcice;

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

        try {
            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<TDxRecordCustomCpyVo>());
            } else {
                PageHelper.startPage(curr, size);
                List<TDxRecordCustomCpyVo> pageList = super.baseMapper.selectAuthPageList(pramsMap);
                PageInfo<TDxRecordCustomCpyVo> pageInfo = new PageInfo<>(pageList);
                //List<TDxRecordCustomCpyVo> resultList = pageInfo.getList();
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());
                // 用于页面返回list对象
                if (pageList != null && pageList.size() > 0) {
                    List<TDxRecordCustomCpyVo> resultList = pageInfo.getList();
                    // 用于页面返回list对象
                    if (resultList.size() > 0) {
                        int start = (int)pageInfo.getStartRow();
                        for (int i = 0; i < resultList.size(); i++) {
                            resultList.get(i).setXh(String.valueOf(i + start));

                            pageList.get(i)
                                    .setSelect(true);
                            //设置是否可勾选
                            String notAllowCheck = getCheckedStatus(resultList.get(i));
                            if(StringUtils.isNotBlank(notAllowCheck)){
                                pageList.get(i)
                                        .setSelect(false);
                                pageList.get(i)
                                        .setNotAllowCheckStr(notAllowCheck);
                            }

                            if(ObjectUtil.isNotEmpty(resultList.get(i).getYxTaxAmount())){
                                resultList.get(i).setYxTaxAmount(
                                        AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getYxTaxAmount()) ));
                            }

                            //重号锁定
                            resultList.get(i).setLqSfchsd(dictdetaServcice.queryDictName(DictConstant.CUSTOM_SFCHSD, resultList.get(i).getLqSfchsd()));

                            //消费税用途
                            resultList.get(i).setLqXfsyt(dictdetaServcice.queryDictName(DictConstant.XFSYT, resultList.get(i).getLqXfsyt()));

                            //用途处理状态
                            resultList.get(i).setGxclzt(dictdetaServcice.queryDictName(DictConstant.CPY_GXCLZT, resultList.get(i).getGxclzt()));
                        }
                    }
                    // 设置记录总数
                    json.put("total", pageInfo.getTotal());
                    json.put("datalist", resultList);
                }
            }
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }

        return json;
    }

    /**
     * @Description 获取当前缴款书可勾选状态
     * @param TDxRecordCustomCpyVo
     * @Return void
     * @Author wangruwei
     * @Date 2024/7/15 17:59
     **/
    private String getCheckedStatus(TDxRecordCustomCpyVo TDxRecordCustomCpyVo) {
        //不可选原因
        List<String> notSelMsg = new LinkedList();

        if("Y".equals(TDxRecordCustomCpyVo.getLqSfchsd())){
            notSelMsg.add("重号锁定");
        }

        String showStr = String.join(",", notSelMsg);
        if(StringUtils.isNotBlank(showStr)){
            showStr+="。不允许勾选库存";
        }
        return showStr;
    }
}
