package top.imwork.window.silos.service.upms.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import jakarta.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import top.imwork.commons.core.utils.StringUtils;
import top.imwork.window.silos.dao.SysUserDao;
import top.imwork.window.silos.entity.upms.AllotRoles;
import top.imwork.window.silos.entity.upms.SysUser;
import top.imwork.window.silos.pojo.bo.SysUserBO;
import top.imwork.window.silos.pojo.bo.SysUserResultBO;
import top.imwork.window.silos.pojo.dto.SysUserDTO;
import top.imwork.window.silos.pojo.dto.SysUserListDTO;
import top.imwork.window.silos.service.upms.ISysUserService;
import top.imwork.window.silos.convert.SysUserConvert;

import java.util.Date;
import java.util.List;

@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao,SysUser> implements ISysUserService {
    @Resource
    private SysUserDao sysUserDao;

    /**
     * 根据id获取信息
     * @param id 用户id
     * @return SysUserBO 响应信息
     */
    @Override
    public SysUserBO info(Long id) {
        SysUser sysUser = sysUserDao.info(id);
        return SysUserConvert.sysUserDoToBo(sysUser);
    }

    /**
     * 新增/更新信息
     * @param sysUserDTO 信息
     * @return sysUserBO 更新后信息
     */
    @Override
    public SysUserBO save(SysUserDTO sysUserDTO) {
        SysUser sysUser = SysUserConvert.sysUserDtoToDo(sysUserDTO);
        if (ObjectUtils.isEmpty(sysUser.getId())) {
            sysUser.setLoginPassword(new BCryptPasswordEncoder().encode("123456"));
            sysUser.setDelFlag("N");
            sysUser.setCreator("admin");
            sysUser.setCreateTime(new Date());
            sysUser.setUpdateTime(new Date());
            sysUser.setModifier("admin");
            sysUserDao.insert(sysUser);
        } else {
            sysUser.setUpdateTime(new Date());
            sysUserDao.updateById(sysUser);
        }
        return SysUserConvert.sysUserDoToBo(sysUser);
    }

    /**
     * 根据id删除信息
     * @param id id
     * @return true/false 是否删除成功
     */
    @Override
    public Boolean delete(Long id) {
        return sysUserDao.deleteById(id) == 1 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean deleteByIds(String[] ids) {
        return sysUserDao.deleteByIds(ids) > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public SysUserResultBO queryPage(SysUserListDTO sysUserListDTO) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(sysUserListDTO.getUserName())) {
            queryWrapper.like("user_name", sysUserListDTO.getUserName());
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getLoginName())) {
            queryWrapper.like("login_name", sysUserListDTO.getLoginName());
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getUserMobilePhone())) {
            queryWrapper.like("user_mobil_phone", sysUserListDTO.getUserMobilePhone());
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getUserEmail())) {
            queryWrapper.like("user_email", sysUserListDTO.getUserEmail());
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getUserNickName())) {
            queryWrapper.like("user_nick_name", sysUserListDTO.getUserNickName());
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getUserGender())) {
            queryWrapper.like("user_gender", sysUserListDTO.getUserGender());
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getUserStatus())) {
            queryWrapper.like("user_status", sysUserListDTO.getUserStatus());
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getUserType())) {
            queryWrapper.like("user_type", sysUserListDTO.getUserType());
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getUserIdCard())) {
            queryWrapper.like("user_id_card", sysUserListDTO.getUserIdCard());
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getDncnet())) {
            queryWrapper.like("dncnet", sysUserListDTO.getDncnet());
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getDelFlag())) {
            queryWrapper.like("del_flag", sysUserListDTO.getDelFlag());
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getCreateBeginTime())) {
            queryWrapper.apply("create_time >= '" + sysUserListDTO.getCreateBeginTime() + "'");
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getCreateEndTime())) {
            queryWrapper.apply("create_time <= '" + sysUserListDTO.getCreateEndTime() + "'");
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getUpdateBeginTime())) {
            queryWrapper.apply("update_time >= '" + sysUserListDTO.getUpdateBeginTime() + "'");
        }

        if (!StringUtils.isEmpty(sysUserListDTO.getUpdateEndTime())) {
            queryWrapper.apply("update_time <= '" + sysUserListDTO.getUpdateEndTime() + "'");
        }
        queryWrapper.apply("del_flag = 'N'");

        PageHelper.startPage(sysUserListDTO.getPageNo(), sysUserListDTO.getPageRows());
        List<SysUser> list = sysUserDao.selectList(queryWrapper);
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);
        SysUserResultBO resultBO = new SysUserResultBO();
        resultBO.setPageNo(sysUserListDTO.getPageNo());
        resultBO.setPageRows(sysUserListDTO.getPageRows());
        resultBO.setPageCount(pageInfo.getPages());
        resultBO.setTotalRows((int) pageInfo.getTotal());
        resultBO.setDataList(SysUserConvert.sysUserListToSysUserBOList(list));
        resultBO.setParams(sysUserListDTO);

        return resultBO;
    }
    /**
     * 根据用户id查询所拥有的角色
     * @param id
     * @return
     */
    @Override
    public List<AllotRoles> queryAllotRoles(Long id) {
        return sysUserDao.queryAllotRoles(id);
    }
}