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


import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.Base64Encoding;
import com.dxhy.core.dao.mailGather.EmailMaintainDao;
import com.dxhy.core.model.mailGather.EmailMaintainVo;
import com.dxhy.core.service.mailGather.EmailMaintainService;
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.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EmailMaintainServiceImpl implements EmailMaintainService {

    @Resource
    private EmailMaintainDao emailMaintainDao;

    /**
     * 通过ID查询单条数据
     * @return 实例对象
     */
    @Override
    public List<EmailMaintainVo> queryAll() {
        return this.emailMaintainDao.queryAll();
    }

    /**
     * 分页查询
     * @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<EmailMaintainVo> emailMaintainVos = emailMaintainDao.queryAllByLimit(pramsMap);
        if(emailMaintainVos !=null && emailMaintainVos.size() > 0) {
            for (int i = 0; i < emailMaintainVos.size(); i++) {
                EmailMaintainVo emailMaintainVo = emailMaintainVos.get(i);
                emailMaintainVo.setXh(i + 1 + "");
                emailMaintainVo.setEmailPassword(Base64Encoding.decodeToString(emailMaintainVo.getEmailPassword()));
            }
        }
        PageInfo<EmailMaintainVo> 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;
    }

    /**
     * 新增数据
     *
     * @param emailMaintain 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(EmailMaintainVo emailMaintain) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        if(StringUtils.isEmpty(emailMaintain.getId())){
            emailMaintain.setId(IdUtil.nanoId());
        }
        if(StringUtils.isEmpty(emailMaintain.getDelStatus())){
            emailMaintain.setDelStatus("0");
        }
        emailMaintain.setCreateTime(new Date());
        emailMaintain.setModifyTime(new Date());
        int insert = this.emailMaintainDao.insert(emailMaintain);
        return insert > 0;
    }

    /**
     * 修改数据
     *
     * @param emailMaintain 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(EmailMaintainVo emailMaintain) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        emailMaintain.setModifyTime(new Date());
        int update = this.emailMaintainDao.update(emailMaintain);
        return update > 0 ;
    }

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

    @Override
    public String getUserName(String userCode) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return emailMaintainDao.getUserName(userCode);
    }


    @Override
    public String getUserByUsername(String username) {
        DynamicContextHolder.push("business_itax_read");
        return emailMaintainDao.getUserByUsername(username);
    }

    @Override
    public int insertBatch(List<EmailMaintainVo> entities) {
        return emailMaintainDao.insertBatch(entities);
    }

    @Override
    public int selectByEmail(String email) {
        return emailMaintainDao.selectByEmail(email);
    }

    @Override
    public EmailMaintainVo getEmailMaintainVo(EmailMaintainVo emailMaintain) {
        return emailMaintainDao.getEmailMaintainVo(emailMaintain);
    }

    //根据用户名进行查询
    public void getdeptInfo(String deptId){
        DynamicContextHolder.push(DbConstant.BASICS_READ);

    }
}
