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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dxhy.common.constant.DbConstant;
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.LqCustomFpycxxDao;
import com.dxhy.core.dao.LqFpycxxDao;
import com.dxhy.core.model.TdxLqDkCustomFpycxx;
import com.dxhy.core.model.TdxLqDkFpycxx;
import com.dxhy.core.service.export.LqFpycxxService;
import com.dxhy.lqchannel.constants.DateUtils;
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.List;
import java.util.Map;

/**
 * 发票认证综合查询
 *
 * @author user
 */
@SuppressWarnings("DuplicatedCode")
@Service("lqfpycxxService")
@Slf4j

public class LqFpycxxServiceImpl extends MpBaseServiceImpl<LqFpycxxDao, TdxLqDkFpycxx> implements LqFpycxxService {
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Resource
    private LqCustomFpycxxDao lqCustomFpycxxDao;

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


            LambdaQueryWrapper<TdxLqDkFpycxx> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.in(TdxLqDkFpycxx::getNsrsbh, (List<String>)pramsMap.get("gfsh"));
            lambdaQueryWrapper.eq(TdxLqDkFpycxx::getSkssq, pramsMap.get("skssq"));
            PageHelper.startPage(curr, size);
            List<TdxLqDkFpycxx> pageList = super.baseMapper.selectList(lambdaQueryWrapper);
            PageInfo<TdxLqDkFpycxx> pageInfo = new PageInfo<>(pageList);
            json.put("hasNext", pageInfo.isHasNextPage());
            json.put("nextPage", pageInfo.getNextPage());
            List<String[]> returnList = new ArrayList<>();
            // 用于页面返回list对象
            if (pageList.size() > 0) {
                for (int i = 0; i < pageList.size(); i++) {
                    String[] dataArray = new String[12];
                    dataArray[0] = DateUtils.dateToStr(DateUtils.strToDate(pageList.get(i).getKprq(), "yyyyMMdd"), "yyyy-MM-dd");
                    dataArray[1] = pageList.get(i).getFpdm();
                    dataArray[2] = pageList.get(i).getFphm();
                    dataArray[3] = pageList.get(i).getSkssq();
                    dataArray[4] = pageList.get(i).getCkznxzmbh();
                    dataArray[5] = DateUtils.dateToStr(DateUtils.strToDate(pageList.get(i).getGxsj(), "yyyyMMdd"), "yyyy-MM-dd");
                    dataArray[6] = getYcfpZt(pageList.get(i).getFpzt());
                    dataArray[7] = getFailMsgBySfycpz(pageList.get(i).getSfycpz());
                    dataArray[8] = getFailMsgBySdzt(pageList.get(i).getSdzt());

                    returnList.add(dataArray);
                }
            }

            json.put("datalist", returnList);

        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }

        return json;
    }

    @Override
    public JSONObject selectCustomYcxxByPramsMap(Map<String, Object> pramsMap, int curr, int size) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        JSONObject json = new JSONObject();
        try {
            LambdaQueryWrapper<TdxLqDkCustomFpycxx> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.in(TdxLqDkCustomFpycxx::getNsrsbh, (List<String>)pramsMap.get("gfsh"));
            lambdaQueryWrapper.eq(TdxLqDkCustomFpycxx::getSkssq, pramsMap.get("skssq"));
            PageHelper.startPage(curr, size);
            List<TdxLqDkCustomFpycxx> pageList = lqCustomFpycxxDao.selectList(lambdaQueryWrapper);
            PageInfo<TdxLqDkCustomFpycxx> pageInfo = new PageInfo<>(pageList);
            json.put("hasNext", pageInfo.isHasNextPage());
            json.put("nextPage", pageInfo.getNextPage());
            List<String[]> returnList = new ArrayList<>();
            // 用于页面返回list对象
            if (pageList.size() > 0) {
                for (int i = 0; i < pageList.size(); i++) {
                    String[] dataArray = new String[12];
                    dataArray[0] = pageList.get(i).getJkshm();
                    dataArray[1] = pageList.get(i).getTfrq();
                    dataArray[2] = pageList.get(i).getCkznxzmbh();
                    dataArray[3] = pageList.get(i).getSkssq();
                    dataArray[4] = pageList.get(i).getGxsj();
                    dataArray[5] = getYcztMsg(pageList.get(i).getYcztdm());

                    returnList.add(dataArray);
                }
            }

            json.put("datalist", returnList);

        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }

        return json;
    }

    private String getYcztMsg(String ycztdm) {
        String msg = "";
        switch (ycztdm){
            case "01":
                msg = "对方重号,对方申请核对";
                break;
            case "02":
                msg = "核查后允许该企业使用";
                break;
            case "03":
                msg = "核查后不允许该企业使用";
                break;
        }
        return msg;
    }

    private String getYcfpZt(String fpzt) {
        if(StringUtils.isEmpty(fpzt)){
            return "";
        }
        String name = "";
        switch (fpzt){
            case "0":
                name = "正常";
                break;
            case "1":
                name = "失控";
                break;
            case "2":
                name = "作废";
                break;
            case "3":
                name = "已红冲";
                break;
            case "7":
                name = "部分冲红";
                break;
            case "8":
                name = "全额红冲";
                break;
        }

        return name;

    }

    private String getFailMsgBySfycpz(String sfycpz) {
        String msg = "";
        if ("01".equals(sfycpz)) {
            msg = "正常";
        }
        if ("02".equals(sfycpz)) {
            msg = "异常凭证";
        }
        if ("03".equals(sfycpz)) {
            msg = "疑似异常凭证";
        }
        return msg;
    }

    private String getFailMsgBySdzt(String sdzt) {
        String msg = "";
        if ("01".equals(sdzt)) {
            msg = "未锁定";
        }
        if ("02".equals(sdzt)) {
            msg = "已锁定";
        }
        return msg;
    }

}
