package com.yx.face.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.face.boot.restful.ServiceException;
import com.yx.face.dao.AdminDao;
import com.yx.face.dao.SystemPushConfigDao;
import com.yx.face.model.dto.SystemPushConfigDTO;
import com.yx.face.model.entity.Admin;
import com.yx.face.model.entity.SystemPushConfig;
import com.yx.face.model.vo.SystemPushConfigVO;
import com.yx.face.service.SystemPushConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: face-server
 * @Package: com.yx.face.service.impl
 * @ClassName: SystemPushConfigServiceImpl
 * @Author: 崔哥
 * @Description:
 * @Date: 2021/11/22 16:54
 * @Version: 1.0
 */
@Service
public class SystemPushConfigServiceImpl implements SystemPushConfigService {
    @Resource
    private AdminDao adminDao;

    @Resource
    private SystemPushConfigDao systemPushConfigDao;

    @Override
    public Boolean add(SystemPushConfigDTO dto, Integer userId) {
        Admin admin = adminDao.selectByPrimaryKey(userId);
        if (admin == null) {
            throw new ServiceException("此账号无权限操作");
        }
        if (admin.getType() != 1) {
            throw new ServiceException("此账号无权限操作");
        }
        Admin primary = adminDao.selectByPrimaryKey(dto.getAdminId());
        if (primary == null) {
            throw new ServiceException("推送账号不存在");
        }
        Date date = new Date();
        SystemPushConfig config = JSONObject.parseObject(JSON.toJSONString(dto), SystemPushConfig.class);
        config.setAccount(primary.getUsername());
        config.setAccountId(primary.getId());
        config.setAdminId(userId);
        config.setCreateTime(date);
        config.setUpdateTime(date);
        config.setId(null);
        systemPushConfigDao.insert(config);
        return true;
    }

    @Override
    public Boolean update(SystemPushConfigDTO dto, Integer userId) {
        Admin admin = adminDao.selectByPrimaryKey(userId);
        if (admin == null) {
            throw new ServiceException("此账号无权限操作");
        }
        if (admin.getType() != 1) {
            throw new ServiceException("此账号无权限操作");
        }
        Admin primary = adminDao.selectByPrimaryKey(dto.getAdminId());
        if (primary == null) {
            throw new ServiceException("推送账号不存在");
        }
        Date date = new Date();
        SystemPushConfig config = JSONObject.parseObject(JSON.toJSONString(dto), SystemPushConfig.class);
        config.setAccount(primary.getUsername());
        config.setAccountId(primary.getId());
        config.setAdminId(userId);
        config.setCreateTime(date);
        config.setUpdateTime(date);
        systemPushConfigDao.updateByPrimaryKeySelective(config);
        return true;
    }

    @Override
    public Boolean updateStatus(Integer id, Integer status) {
        SystemPushConfig config = new SystemPushConfig();
        config.setId(id);
        config.setStatus(status);
        systemPushConfigDao.updateByPrimaryKeySelective(config);
        return true;
    }

    @Override
    public PageInfo<SystemPushConfigVO> getList(Integer pageNum, Integer pageSize, Integer accountId, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        List<SystemPushConfigVO> vo = systemPushConfigDao.selectList(accountId,status);
        return new PageInfo<>(vo);
    }

    @Override
    public Boolean delete(Integer id) {
        return systemPushConfigDao.deleteByPrimaryKey(id) == 1;
    }
}
