package com.dxhy.base.service.fpcj;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.api.model.config.Tax;
import com.dxhy.base.dao.BaseFpcjhzLbDao;
import com.dxhy.base.model.BaseFpcjCjhzRespVO;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
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.dxhy.common.util.StringHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 *
 *
 * 发票采集业务实现类
 *
 * @author ld
 * @date 2019年4月25日
 *
 */

@SuppressWarnings("AlibabaUndefineMagicConstant")
@Service("baseIFpcjService")
public class BaseFpcjServiceImpl extends MpBaseServiceImpl<BaseFpcjhzLbDao, BaseFpcjCjhzRespVO>
    implements BaseIFpcjService {
    @Resource
    private TabulateRedisService tabulateRedisService;

    @Override
    public String selectByGfshAndCjrq(Map<String, Object> pramsMap, int curr, int size) {
        List<BaseFpcjCjhzRespVO> resultList = new ArrayList<>();
        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<Tax> orgList = (List<Tax>)pramsMap.get("userInfo");
        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<BaseFpcjCjhzRespVO>());
        } else {
            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (curr == 1) {
                String hjsl = "0";
                String hjje = "0.00";
                String hjse = "0.00";
                List<BaseFpcjCjhzRespVO> hjList = super.baseMapper.selectHjxxByGfshAndCjrq(pramsMap);
                BaseFpcjCjhzRespVO 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", hjsl);
                json.put("hjje", hjje);
                json.put("hjse", hjse);
                if ("0".equals(hjsl)) {
                    json.put("total", 0);
                    json.put("datalist", resultList);
                    return super.responseResult(json);
                }
            }

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

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

            // 用于页面展示
            resultList = pageInfo.getList();

            if (resultList != null && resultList.size() > 0) {
                int start = new Long(pageInfo.getStartRow()).intValue();

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

        return super.responseResult(json);
    }

    /**
     * 转换结果数据为页面可展示表格
     *
     * @param cjlb
     *            待转换对象
     * @param i
     *            序号
     * @param orgList
     *            购方信息列表
     * @return 返回结果
     *
     */
    private BaseFpcjCjhzRespVO convertToHzlbView(BaseFpcjCjhzRespVO cjlb, int i, List<Tax> orgList) {
        cjlb.setXh(String.valueOf(i));
        if(StringUtils.isNotEmpty(cjlb.getJe())){
            cjlb.setJe(AmountFormatUtil.fmtMicrometer(MathUtil.round(cjlb.getJe())));
        }
        if(StringUtils.isNotEmpty(cjlb.getSe())){
            cjlb.setSe(AmountFormatUtil.fmtMicrometer(MathUtil.round(cjlb.getSe())));
        }

        cjlb.setGfmc(this.getGfmcByGfsh(cjlb.getGfsh(), orgList));
        return cjlb;
    }

    public static void main(String[] args) {
        System.out.println(AmountFormatUtil.fmtMicrometer(MathUtil.round("")));
        System.out.println(AmountFormatUtil.fmtMicrometer(MathUtil.round("0")));
    }

    /**
     * 根据购方税号获取购方名称
     *
     * @param gfsh
     *            购方税号
     * @param orgList
     *            机构信息
     * @return 返回结果
     *
     */
    private String getGfmcByGfsh(String gfsh, List<Tax> orgList) {
        String gfmc = "";
        for (Tax org : orgList) {
            if (gfsh.equals(org.getTaxno())) {
                gfmc = org.getTaxname();
                if (StringHelper.isBlank(gfmc)) {
                    gfmc = "";
                }
                break;
            }
        }
        return gfmc;
    }
}
