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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.StringHelper;
import com.dxhy.core.dao.TDxExcelExportlogDao;
import com.dxhy.core.entity.TDxExcelExportlog;
import com.dxhy.core.service.singletable.IExcelExportlogService;
import com.dxhy.oss.service.FileService;

import lombok.extern.slf4j.Slf4j;

/**
 * excel导出记录表业务处理实现类
 *
 * @author
 */

@Service("exportlogService")
@Slf4j

public class ExcelExportlogServiceImpl extends MpBaseServiceImpl<TDxExcelExportlogDao, TDxExcelExportlog>
    implements IExcelExportlogService {
    @Resource
    private FileService fileService;

    @Override
    public void deleteExportLog() {
        DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
        List<TDxExcelExportlog> logList = this.baseMapper.selectExportLogToDel();
        for (TDxExcelExportlog exportlog : logList) {
            try {
                String filepath = exportlog.getFilepath();
                if (StringUtils.isNotBlank(filepath)) {
                    fileService.deleteFile(filepath);
                }
                DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
                this.baseMapper.deleteById(exportlog.getId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        this.baseMapper.deleteMessagecontrol();
    }

    @Override
    public List<TDxExcelExportlog> getNeedExportLog(String dbName, String userName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        // 锁定记录
        super.baseMapper.lockExportlog(userName);

        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
        // 返回需要导出的申请记录
        return super.baseMapper.selectNeedExportLog(userName);
    }

    @Override
    public void updateStart(long id, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        super.baseMapper.updateStart(id);
    }

    @Override
    public void updateSucc(long id, String excelFile, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        Map<String, Object> pramsMap = new HashMap<>(4);
        pramsMap.put("id", id);
        pramsMap.put("excelFile", excelFile);
        super.baseMapper.updateSucc(pramsMap);
    }

    @Override
    public void updateFail(long id, String errmsg, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        Map<String, Object> pramsMap = new HashMap<>(4);
        pramsMap.put("id", id);
        pramsMap.put("errmsg", errmsg);
        super.baseMapper.updateFail(pramsMap);
    }

    @Override
    public TDxExcelExportlog excelExportApply(Map<String, Object> pramsMap) {

        String dbName = StringHelper.trim(pramsMap.get("dbName").toString(), "");
        log.debug("dbName:" + dbName);
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);

        TDxExcelExportlog exportLog = new TDxExcelExportlog();
        exportLog.setUserAccount(pramsMap.get("userId").toString());
        exportLog.setUserName(pramsMap.get("userName").toString());
        exportLog.setServiceType(Integer.valueOf(pramsMap.get("serviceType").toString()));
        exportLog.setConditions(pramsMap.get("conditions").toString());
        exportLog.setGfTaxNo(pramsMap.get("gfTaxNo").toString());
        exportLog.setDbName(dbName);
        super.baseMapper.insert(exportLog);
        return exportLog;
    }

    @Override
    public TDxExcelExportlog selectById(Long id) {
        TDxExcelExportlog tDxExcelExportlog = super.baseMapper.selectById(id);
        return tDxExcelExportlog;
    }

}
