package com.dxhy.customs.service.impl;

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

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.customs.dao.CustomsLogDao;
import com.dxhy.api.model.custom.SysCustomsLog;
import com.dxhy.customs.model.SysCustomsLogEntityView;
import com.dxhy.customs.service.CustomsCusLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * @author dxhy
 */
@Service("customsCusLogService")
@Slf4j
public class CustomsCusLogServiceImpl extends MpBaseServiceImpl<CustomsLogDao, SysCustomsLog>
    implements CustomsCusLogService {

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

        String[] titlelist = {"序号", "缴款书号码", "操作类型", "操作时间", "操作人", "操作状态", "签收方式", "认证方式"};
        String[] titleprop =
            {"xh", "customsCode", "type", "createDate", "inputName", "inputStatus", "qsType", "rzType"};

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

        DynamicContextHolder.push(DbConstant.BASICS_READ);
        QueryWrapper<SysCustomsLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customs_code", pramsMap.get("customsCode"));
        queryWrapper.orderByDesc("create_date");
        // 分页获取明细列表
        PageHelper.startPage(Integer.parseInt(pramsMap.get("curr")), Integer.parseInt(pramsMap.get("size")));
        List<SysCustomsLog> logList = super.baseMapper.selectList(queryWrapper);
        PageInfo<SysCustomsLog> pageInfo = new PageInfo<>(logList);
        List<SysCustomsLog> resultList = pageInfo.getList();

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

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

            for (int i = 0; i < resultList.size(); i++) {
                SysCustomsLogEntityView returnLog = this.convertToLogView(resultList.get(i), start + i);
                returnList.add(returnLog);
            }
        }
        json.put("datalist", returnList);
        return super.responseResult(json);

    }

    private SysCustomsLogEntityView convertToLogView(SysCustomsLog customsLog, int i) {
        SysCustomsLogEntityView returnLog = new SysCustomsLogEntityView();
        returnLog.setXh(String.valueOf(i));
        returnLog.setId(customsLog.getId());
        returnLog.setCustomsCode(customsLog.getCustomsCode());
        for (InputStatusEnum e : InputStatusEnum.values()) {
            if (e.getInputStatusDm().equals(customsLog.getInputStatus())) {
                returnLog.setInputStatus(e.getInputStatusMc(e.getIndex()));
            }
        }
        for (LogTypeEnum e : LogTypeEnum.values()) {
            if (e.getRzlxDm().equals(customsLog.getType())) {
                returnLog.setType(e.getRzlxMc(e.getIndex()));
            }
        }

        returnLog
            .setCreateDate(customsLog.getCreateDate() == null ? "" : DateUtils.dateToStr(customsLog.getCreateDate()));
        returnLog.setInputName(customsLog.getInputName());

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

        for (RzfsEnum e : RzfsEnum.values()) {
            if (e.getRzfsDm().equals(customsLog.getRzType())) {
                returnLog.setRzType(e.getRzfsMc(e.getIndex()));
            }
        }
        return returnLog;
    }
}
