package com.dxhy.base.service.tdxtaxcurrent;

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

import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.service.DictdetaServcice;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxhy.base.dao.BaseTDxTaxHandlelogDao;
import com.dxhy.base.entity.BaseTDxTaxHandlelog;
import com.dxhy.base.model.BaseQyswxxCzlsRespVO;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.CzmcEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.utils.DateUtils;

import javax.annotation.Resource;

/**
 *
 *
 * 企业信息操作历史业务处理类
 *
 * @author zhanghong
 * @date 2019年5月22日
 *
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaClassNamingShouldBeCamel"})
@Service("baseITDxTaxHandlelogService")
public class BaseTDxTaxHandlelogServiceImpl extends MpBaseServiceImpl<BaseTDxTaxHandlelogDao, BaseTDxTaxHandlelog>
    implements BaseITDxTaxHandlelogService {
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public String selectHandlelogByGfsh(Map<String, Object> pramsMap, int curr, int size, int count) {
        String[] titlelist = {"序号", "操作名称", "操作账户", "操作人", "操作时间", "税款所属期", "税号"};
        String[] titleprop = {"xh", "czmc", "czzh", "czr", "czsj", "skssq", "gfsh"};
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        // 需要设置返回列表标题时调用
        JSONObject json = super.setTitleList(titlelist, titleprop);

        Page<BaseTDxTaxHandlelog> page = new Page<>(curr, size);

        QueryWrapper<BaseTDxTaxHandlelog> queryWrapper = new QueryWrapper<>();
        if (pramsMap.containsKey("gfsh") && !StringHelper.isBlank(String.valueOf(pramsMap.get("gfsh")))) {
            queryWrapper.eq("taxno", pramsMap.get("gfsh"));
        }
        queryWrapper.orderByDesc("handle_time");
        IPage<BaseTDxTaxHandlelog> pageInfo = super.page(page, queryWrapper);
        List<BaseTDxTaxHandlelog> resultList = pageInfo.getRecords();
        List<Object> returnList = new ArrayList<>();
        if (resultList != null && resultList.size() > 0) {
            int c = (int)pageInfo.getCurrent();
            int s = (int)pageInfo.getSize();
            int xh = (c - 1) * s;
            for (int i = 0; i < resultList.size(); i++) {
                BaseQyswxxCzlsRespVO qylb = this.convertToCzlslbView(resultList.get(i), xh + 1 + i);
                returnList.add(qylb);
            }

        }
        json.put("datalist", returnList);
        // 设置记录总数
        json.put("total", pageInfo.getTotal());
        return super.responseResult(json);
    }

    /**
     * 操作名称统一放入map集合中
     */
//    public static final Map<String, String> CZMC_MAP = new LinkedHashMap<String, String>() {
//
//        private static final long serialVersionUID = 1L;
//
//        {
//            for (CzmcEnum en : CzmcEnum.values()) {
//                this.put(en.getCzmcDm(), en.getCzMc(en.getIndex()));
//            }
//        }
//    };

    private BaseQyswxxCzlsRespVO convertToCzlslbView(BaseTDxTaxHandlelog tdxtaxHandlelog, int i) {
        BaseQyswxxCzlsRespVO czls = new BaseQyswxxCzlsRespVO();
        czls.setCzr(tdxtaxHandlelog.getUserName());
        if (tdxtaxHandlelog.getHandleTime() != null) {
            czls.setCzsj(DateUtils.dateToStrLong(tdxtaxHandlelog.getHandleTime()));
        }
        czls.setCzzh(tdxtaxHandlelog.getUserAccount());
        czls.setGfsh(tdxtaxHandlelog.getTaxno());
        czls.setId(tdxtaxHandlelog.getId().toString());
        czls.setSkssq(tdxtaxHandlelog.getPeriod());
        czls.setXh(String.valueOf(i));

//        czls.setCzmc(CZMC_MAP.get(tdxtaxHandlelog.getHandleType()));
        czls.setCzmc(dictdetaServcice.queryDictName(DictConstant.QYSWXX_STATUS,tdxtaxHandlelog.getHandleType()));
        return czls;
    }

}
