package com.gz.service.system.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gz.common.DataGlobalVariable;
import com.gz.dto.system.AdminDTO;
import com.gz.dto.system.DeptDTO;
import com.gz.mapper.system.AdminMapper;
import com.gz.rvo.system.AdminRVO;
import com.gz.service.system.AdminService;
import com.gz.utils.JwtUtils;
import com.gz.utils.PasswordUtils;
import com.gz.vo.system.AdminVO;
import org.springframework.stereotype.Service;
import sun.net.util.IPAddressUtil;
import tk.mybatis.mapper.entity.Example;

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

/**
 * @author LiuchangLan
 * @date 2020/9/4 14:42
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminMapper adminMapper;

    @Override
    public Integer insert(AdminDTO adminDTO) {
        // 盐
        adminDTO.setSalt(IdUtil.simpleUUID());
        // 加密后密码
        adminDTO.setPassword(PasswordUtils.encryption(adminDTO.getPassword(), adminDTO.getSalt()));
        // 创建人
        adminDTO.setCreated(JwtUtils.getCurrentUserJwtPayload().getId());
        // 执行入库
        return adminMapper.insertSelective(adminDTO);
    }

    @Override
    public Integer delete(Integer id) {
        return adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer update(AdminDTO adminDTO) {
        // 修改人
        adminDTO.setUpdated(JwtUtils.getCurrentUserJwtPayload().getId());
        // 修改时间
        adminDTO.setUpdateTime(DateUtil.now());
        // 执行入库
        return adminMapper.updateByPrimaryKeySelective(adminDTO);
    }

    @Override
    public PageInfo<AdminDTO> select(AdminVO adminVO) {
        // 包含分页参数执行分页
        if (adminVO.getPageNum() != null && adminVO.getPageSize() != null) {
            PageHelper.startPage(adminVO.getPageNum(), adminVO.getPageSize());
        }
        // 查询未删除的数据
        return new PageInfo<>(adminMapper.selectByPage(adminVO));
    }

    @Override
    public AdminDTO selectByPrimaryKey(String id) {
        AdminDTO adminDTO = new AdminDTO();
        adminDTO.setId(id);
        return adminMapper.selectOne(adminDTO);
    }

    @Override
    public Integer updateStatus(String id) {
        AdminDTO adminDTO = adminMapper.selectByPrimaryKey(id);
        if (adminDTO.getStatus().equals(0)) {
            adminDTO.setStatus(1);
        } else {
            adminDTO.setStatus(0);
        }
        return adminMapper.updateByPrimaryKeySelective(adminDTO);
    }

    @Override
    public Integer resetPassword(String id) {
        AdminDTO adminDTO = selectByPrimaryKey(id);
        // 盐
        adminDTO.setSalt(IdUtil.simpleUUID());
        // 加密后密码
        adminDTO.setPassword(PasswordUtils.encryption("123456", adminDTO.getSalt()));
        return update(adminDTO);
    }

    @Override
    public List<AdminDTO> selectAll() {
        return adminMapper.selectAll();
    }

    @Override
    public AdminRVO selectByPrimaryKeyDept(String id) {
        return adminMapper.selectByPrimaryKeyDept(id);
    }
}
