package com.yx.face.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.face.boot.restful.RestDTO;
import com.yx.face.boot.restful.ServiceException;
import com.yx.face.boot.uitls.PasswordUtils;
import com.yx.face.dao.AdminDao;
import com.yx.face.dao.SystemConfigDao;
import com.yx.face.model.dto.SystemConfigDTO;
import com.yx.face.model.entity.Admin;
import com.yx.face.model.entity.SystemConfig;
import com.yx.face.model.query.SystemConfigQu;
import com.yx.face.model.vo.SystemConfigVO;
import com.yx.face.service.SystemConfigService;
import org.springframework.stereotype.Service;

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


/**
 * @description: 系统配置表(SystemConfig)表服务实现类 <br>
 * @author: PWB <br>
 * @since: 1.0 <br>
 * @date: 2021-06-26 14:03:07 <br>
 */

@Service
public class SystemConfigServiceImpl implements SystemConfigService {
    @Resource
    private SystemConfigDao systemConfigDao;
    @Resource
    private AdminDao adminDao;


    @Override
    public Boolean add(SystemConfigDTO dto, Integer userId) {
        Admin admin = adminDao.selectByPrimaryKey(userId);
        if (admin == null) {
            throw new ServiceException("此账号无权限操作");
        }
        if (admin.getType() > 2) {
            throw new ServiceException("此账号无权限操作");
        }
        Admin primary = adminDao.selectByPrimaryKey(dto.getAdminId());
        if (primary == null) {
            throw new ServiceException("推送账号不存在");
        }
        SystemConfig config = new SystemConfig();
        dto.setPassword(PasswordUtils.buildPw(dto.getPassword()));
        Date date = new Date();
        config.setRemark(dto.getRemark());
        config.setStatus(dto.getStatus());
        config.setPassword(dto.getPassword());
        config.setAccount(primary.getUsername());
        config.setAccountId(primary.getId());
        config.setType(1);
        config.setAdminId(userId);
        if (dto.getId() != null) {
            config.setUpdateTime(date);
            config.setId(dto.getId());
            return systemConfigDao.updateByPrimaryKeySelective(config) == 1;
        }
        config.setCreateTime(date);
        config.setUpdateTime(date);

        Integer count = systemConfigDao.selectByPrimaryOen(primary.getId());
        if (count >= 1) {
            throw new ServiceException("所选账号已存在哦~");
        }
        return systemConfigDao.insert(config) == 1;
    }


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

    @Override
    public PageInfo<SystemConfigVO> getList(RestDTO<SystemConfigQu> dto, Integer adminId) {

        if (adminId == 1) {
            adminId = null;
        }
        SystemConfigQu data = dto.getData();
        if (data.getType() == null) {
            data.setType(1);
        }
        if (data.getStatus() == null) {
            data.setStatus(99);
        }
        Integer accountId = 0;
        if (data.getAdminId() == null) {
            accountId = 0;
        } else {
            accountId = data.getAdminId();
        }
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<SystemConfigVO> vo = systemConfigDao.selectList(accountId, data.getStatus(), data.getType(), adminId);
        return new PageInfo<>(vo);
    }
}

