package com.dxhy.extend.service.messagecontrol;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.extend.dao.ExtMessageControlDao;
import com.dxhy.extend.dao.ExtSysMessagecontrolDao;
import com.dxhy.extend.entity.ExtSysMessagecontrol;
import com.dxhy.extend.model.ExtSysMessagecontrolVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang
 */
@Service("messageControlService")
@Slf4j
public class ExtMessageControlServiceImpl extends MpBaseServiceImpl<ExtMessageControlDao, ExtSysMessagecontrolVo>
    implements ExtMessageControlService {
    @Resource
    private ExtMessageControlDao extMessageControlDao;
    @Resource
    private ExtSysMessagecontrolDao extSysMessagecontrolDao;

    @Override
    public void insertMessage(ExtSysMessagecontrol vo) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        extSysMessagecontrolDao.insert(vo);
    }

    @Override
    public Object queryMessageContol(String userName, Integer curr, Integer size, String operationStatus) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        JSONObject json = new JSONObject();
        try {
            PageHelper.startPage(curr, size);
            List<ExtSysMessagecontrolVo> pageList = extMessageControlDao.queryMessageContol(userName, operationStatus);
            PageInfo<ExtSysMessagecontrolVo> pageInfo = new PageInfo<>(pageList);
            // 统一返回页面相同的分页返回结果
            json = super.pageResult(json, pageInfo);
            // 统一设置操作成功标识
            json = super.succResult(json);
        } catch (Exception e) {
            // 统一设置操作异常标识
           log.info("业务处理异常：{}", e);
            json = super.errorResult(e);
        }
        return json;

    }

    @Override
    public int queryCount(String userName) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return extMessageControlDao.queryCount(userName);
    }

    @Override
    public int updateOperationStatusById(ExtSysMessagecontrol vo) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        return extMessageControlDao.updateOperationStatusById(vo);
    }

    @Override
    public int updateOperationStatusAll(ExtSysMessagecontrol vo) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        return extMessageControlDao.updateOperationStatusAll(vo);
    }

}
