package com.dxhy.extend.service.invoicelog;

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

import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.api.model.config.sys.DictdetaModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.InputStatusEnum;
import com.dxhy.common.enums.LogTypeEnum;
import com.dxhy.common.enums.QsfsEnum;
import com.dxhy.common.enums.RzfsEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.extend.dao.ExtInvoiceLogDao;
import com.dxhy.api.model.invoice.InvoiceLogEntity;
import com.dxhy.extend.entity.ExtInvoiceLogEntityView;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * @author dxhy
 */
@Service("extInvoiceLogService")
@Slf4j
public class ExtInvoiceLogServiceImpl extends MpBaseServiceImpl<ExtInvoiceLogDao, InvoiceLogEntity>
    implements ExtInvoiceLogService {

    @Resource
    DictdetaServcice dictdetaServcice;

    @Override
    public String getInvoiceLog(Map<String, String> pramsMap) {

        String[] titlelist = {"序号", "发票代码", "发票号码", "操作类型", "操作时间", "操作人", "操作状态", "签收方式", "认证方式", "备注"};
        String[] titleprop = {"xh", "invoiceCode", "invoiceNo", "type", "createDate", "inputName", "inputStatus",
            "qsType", "rzType", "remark"};

        // 需要设置返回列表标题时调用
        JSONObject json = super.setTitleList(titlelist, titleprop);

        DynamicContextHolder.push(DbConstant.BASICS_READ);
        QueryWrapper<InvoiceLogEntity> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(pramsMap.get("invoiceCode"))){
            queryWrapper.eq("invoice_code", pramsMap.get("invoiceCode"));
        }
        queryWrapper.eq("invoice_no", pramsMap.get("invoiceNo"));
        queryWrapper.orderByAsc("create_date");
        // 分页获取明细列表
        PageHelper.startPage(Integer.parseInt(pramsMap.get("curr")), Integer.parseInt(pramsMap.get("size")));
        List<InvoiceLogEntity> logList = super.baseMapper.selectList(queryWrapper);
        PageInfo<InvoiceLogEntity> pageInfo = new PageInfo<>(logList);
        List<InvoiceLogEntity> resultList = pageInfo.getList();

        // 设置记录总数
        json.put("total", pageInfo.getTotal());
        List<DictdetaModel> gxTypes = dictdetaServcice.selectDictdeta("GXTYPE");
        List<Object> returnList = new ArrayList<>();
        if (resultList != null && resultList.size() > 0) {
            int start = (int)pageInfo.getStartRow();
            for (int i = 0; i < resultList.size(); i++) {
                ExtInvoiceLogEntityView returnLog = this.convertToLogView(resultList.get(i), start + i, gxTypes);
                returnList.add(returnLog);
            }
        }
        json.put("datalist", returnList);
        return super.responseResult(json);

    }

    private ExtInvoiceLogEntityView convertToLogView(InvoiceLogEntity invoiceLog, int i,
        List<DictdetaModel> gxTypes) {
        ExtInvoiceLogEntityView returnLog = new ExtInvoiceLogEntityView();
        returnLog.setXh(String.valueOf(i));
        returnLog.setId(invoiceLog.getId());
        returnLog.setInvoiceCode(invoiceLog.getInvoiceCode());
        returnLog.setInvoiceNo(invoiceLog.getInvoiceNo());
        for (InputStatusEnum e : InputStatusEnum.values()) {
            if (e.getInputStatusDm().equals(invoiceLog.getInputStatus())) {
                returnLog.setInputStatus(e.getInputStatusMc(e.getIndex()));
            }
        }
        for (LogTypeEnum e : LogTypeEnum.values()) {
            if (e.getRzlxDm().equals(invoiceLog.getType())) {
                returnLog.setType(e.getRzlxMc(e.getIndex()));
            }
        }

        returnLog.setCreateDate(
            invoiceLog.getCreateDate() == null ? "" : DateUtils.dateToStrLong(invoiceLog.getCreateDate()));
        returnLog.setInputName(invoiceLog.getInputName());

        for (QsfsEnum e : QsfsEnum.values()) {
            if (e.getQsfsDm().equals(invoiceLog.getQsType())) {
                returnLog.setQsType(e.getQsfsMc(e.getIndex()));
            }
        }

        if (gxTypes != null && gxTypes.size() > 0) {
            for (DictdetaModel model : gxTypes) {
                if (model.getDictcode().equals(invoiceLog.getRzType())) {
                    returnLog.setRzType(model.getDictname());
                }
            }
        }
        return returnLog;
    }
}
