package com.energy.manage.service.service.system.impl;


import com.alibaba.fastjson.JSONObject;
import com.energy.manage.common.base.BaseServiceImpl;
import com.energy.manage.common.base.Page;
import com.energy.manage.common.po.system.SysRolePO;
import com.energy.manage.common.po.system.SysUserInfoPO;
import com.energy.manage.common.po.system.SysUserRolePO;
import com.energy.manage.common.reponse.RespMsg;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.common.util.BCryptTools;
import com.energy.manage.service.domain.vo.system.UserRoleVo;
import com.energy.manage.service.domain.vo.system.UserVo;
import com.energy.manage.service.mappers.system.SysRoleMapper;
import com.energy.manage.service.mappers.system.SysUserInfoMapper;
import com.energy.manage.service.mappers.system.SysUserRoleMapper;
import com.energy.manage.service.service.system.SysUserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Slf4j
@Service
public class SysUserInfoServiceImpl extends BaseServiceImpl<SysUserInfoPO> implements SysUserInfoService {

    @Autowired
    private SysUserInfoMapper sysUserInfoMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;



    @Override
    public boolean addSystemUser(SysUserInfoPO userPO, Integer roleId) {
        int row = sysUserInfoMapper.insert(userPO);
        if (row != 1) {
            return false;
        }
        SysUserRolePO userRole = new SysUserRolePO();
        userRole.setUserId(userPO.getId());
        userRole.setRoleId(roleId);
        return userRoleMapper.insert(userRole) > 0;
    }

    @Override
    public SysUserInfoPO selectOne(SysUserInfoPO userPO) {
        return sysUserInfoMapper.selectOne(userPO);
    }

    @Override
    public Page<UserRoleVo> getUserByName(Integer pageNum, Integer pageSize, String userName, Integer roleId) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserRoleVo> list = sysUserInfoMapper.getUserByName(userName, roleId);
        Long total = CollectionUtils.isEmpty(list) ? 0L : new PageInfo<>(list).getTotal();
        return Page.build(pageNum, pageSize, total, list);
    }

    @Override
    public List<SysUserInfoPO> getUserList() {
        Example queryExample = new Example(SysUserInfoPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("state", 1);
        List<SysUserInfoPO> list = sysUserInfoMapper.selectByExample(queryExample);
        return list;
    }


    @Override
    public boolean updateUser(SysUserInfoPO userPO) {
        return sysUserInfoMapper.updateByPrimaryKeySelective(userPO) > 0;
    }

    @Override
    public boolean deleteUser(SysUserInfoPO userPO) {
        SysUserRolePO po = new SysUserRolePO();
        po.setUserId(userPO.getId());
        userRoleMapper.delete(po);
        return sysUserInfoMapper.delete(userPO) > 0;
    }


    @Override
    public boolean updateUserRole(SysUserInfoPO userPO, Integer roleId) {
        // 老权限
        SysRolePO roleOld = sysUserInfoMapper.getOldRoleByUserId(userPO.getId());
        if (null == roleOld) {
            return false;
        }
        // 新权限
        SysRolePO role = new SysRolePO();
        role.setId(roleId);
        role = sysRoleMapper.selectOne(role);
        if (null == role) {
            return false;
        }
        // 更改用户信息
        boolean flag = sysUserInfoMapper.updateByPrimaryKeySelective(userPO) >= 0;
        if (!flag) {
            return false;
        }
        flag = userRoleMapper.updateUserRole(userPO.getId(), roleId) >= 0;
        if (!flag) {
            return false;
        }
        return true;
    }

    @Override
    public UserRoleVo getUserById(Integer userId) {
        return sysUserInfoMapper.getUserById(userId);
    }

    @Override
    public UserVo getUserInfoByUserId(Integer userId) {
        UserVo userVo = sysUserInfoMapper.getUserInfoByUserId(userId);
        if (null == userVo) {
            return null;
        }
        return userVo;
    }

    @Override
    public ResultResp updateUserPWD(Integer userId, String oldPWD, String newPWD) {
        SysUserInfoPO userPO = new SysUserInfoPO();
        userPO.setId(userId);
        userPO = sysUserInfoMapper.selectOne(userPO);
        if (null == userPO) {
            return ResultResp.NOTEXISTSUSER();
        }
        if (StringUtils.isEmpty(oldPWD) || !BCryptTools.checkpw(oldPWD, userPO.getUserPassword())) {
            return ResultResp.FAIL("原密码输入错误");
        }
        if (BCryptTools.checkpw(newPWD, userPO.getUserPassword())) {
            return ResultResp.FAIL("新密码与旧密码不能相同");
        }
        userPO = new SysUserInfoPO();
        userPO.setId(userId);
        userPO.setUserPassword(BCryptTools.hashpw(newPWD));
        boolean flag = sysUserInfoMapper.updateByPrimaryKeySelective(userPO) > 0;
        if (flag) {
            return ResultResp.SUCCESS();
        }
        return ResultResp.FAIL();
    }

    @Override
    public List<UserVo> getNonBoundTeacherUser(String userName) {
        return sysUserInfoMapper.quertNonBoundTeacherUser( userName);
    }


}
