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


import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.util.UUIDUtils;
import com.dxhy.common.utils.Base64Encoding;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.core.dao.mailGather.MailGatherLogDao;
import com.dxhy.core.model.mailGather.EmailMaintainVo;
import com.dxhy.core.model.mailGather.MailGatherLogVo;
import com.dxhy.core.service.mailGather.MailGatherLogService;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MailGatherLogServiceImpl implements MailGatherLogService {

    @Resource
    private MailGatherLogDao mailGatherLogDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MailGatherLogVo queryById(String id) {
        return this.mailGatherLogDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param pramsMap 筛选条件
     * @return 查询结果
     */
    @Override
    public String queryByPage(Map<String, Object> pramsMap) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        JSONObject json = new JSONObject();
        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        PageHelper.startPage(curr, size);
        List<MailGatherLogVo> emailMaintainVos = mailGatherLogDao.queryAllByLimit(pramsMap);
        if(emailMaintainVos !=null && emailMaintainVos.size() > 0) {
            for (int i = 0; i < emailMaintainVos.size(); i++) {
                MailGatherLogVo mailGatherLogVo = emailMaintainVos.get(i);
                mailGatherLogVo.setCjsj(DateUtils.dateToStrLong(mailGatherLogVo.getCreateTime()));
                mailGatherLogVo.setXh(i + 1 + "");
                String ocrType = mailGatherLogVo.getOcrType();
                log.info("ocrType={}",ocrType);
                if("0".equals(ocrType)){
                    mailGatherLogVo.setErrorMsg("");
                }

            }
        }
        PageInfo<MailGatherLogVo> pageInfo = new PageInfo<>(emailMaintainVos);
        // 设置记录总数
        json.put("total", pageInfo.getTotal());
        json.put("datalist", pageInfo.getList());
        String jsonString = null;
        if (json != null) {
            log.debug("返回结果：{}", json.toJSONString());
            jsonString = Base64.encode(json.toJSONString());
        }
        return jsonString;
    }

    public static void main(String[] args) {
        String s = DateUtils.dateToStrLong(new Date());
        System.out.println();
    }

    /**
     * 新增数据
     *
     * @param mailGatherLog 实例对象
     * @return 实例对象
     */
    @Override
    public MailGatherLogVo insert(MailGatherLogVo mailGatherLog) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        if(StringUtils.isEmpty(mailGatherLog.getId())){
            mailGatherLog.setId(UUIDUtils.generateShortUuid());
        }
        mailGatherLog.setCreateTime(new Date());
        this.mailGatherLogDao.insert(mailGatherLog);
        return mailGatherLog;
    }

    /**
     * 修改数据
     *
     * @param mailGatherLog 实例对象
     * @return 实例对象
     */
    @Override
    public MailGatherLogVo update(MailGatherLogVo mailGatherLog) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        this.mailGatherLogDao.update(mailGatherLog);
        return this.queryById(mailGatherLog.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.mailGatherLogDao.deleteById(id) > 0;
    }
}
