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

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.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import top.imwork.commons.core.utils.StringUtils;
import top.imwork.window.silos.dao.SysUserRoleDao;
import top.imwork.window.silos.entity.upms.SysUserRole;
import top.imwork.window.silos.pojo.bo.SysUserRoleBO;
import top.imwork.window.silos.pojo.bo.SysUserRoleResultBO;
import top.imwork.window.silos.pojo.dto.SysUserRoleDTO;
import top.imwork.window.silos.pojo.dto.SysUserRoleListDTO;
import top.imwork.window.silos.service.upms.ISysUserRoleService;
import top.imwork.window.silos.convert.SysUserRoleConvert;

import java.util.Date;

@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleDao, SysUserRole> implements ISysUserRoleService {
    @Resource
    private SysUserRoleDao sysUserRoleDao;

    /**
     * 根据id获取信息
     *
     * @param id 用户id
     * @return SysUserRoleBO 响应信息
     */
    @Override
    public SysUserRoleBO info(Long id) {
        SysUserRole sysUserRole = sysUserRoleDao.info(id);
        return SysUserRoleConvert.sysUserRoleDoToBo(sysUserRole);
    }

    /**
     * 新增/更新信息
     *
     * @param sysUserRoleDTO 信息
     * @return sysUserRoleBO 更新后信息
     */
    @Override
    public SysUserRoleBO save(SysUserRoleDTO sysUserRoleDTO) {
        SysUserRole sysUserRole = SysUserRoleConvert.sysUserRoleDtoToDo(sysUserRoleDTO);
        if (ObjectUtils.isEmpty(sysUserRole.getId())) {
            sysUserRoleDao.insert(sysUserRole);
        } else {
            sysUserRoleDao.updateById(sysUserRole);
        }
        return SysUserRoleConvert.sysUserRoleDoToBo(sysUserRole);
    }

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

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

    @Override
    public SysUserRoleResultBO queryPage(SysUserRoleListDTO sysUserRoleListDTO) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(sysUserRoleListDTO.getSysUserRoleName())) {
            queryWrapper.like("sysUserRole_name", sysUserRoleListDTO.getSysUserRoleName());
        }

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

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

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

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

        Page<SysUserRole> page = new Page<>(sysUserRoleListDTO.getPageNo(), sysUserRoleListDTO.getPageRows());
        IPage<SysUserRole> iPage = sysUserRoleDao.queryPage(page, queryWrapper);

        SysUserRoleResultBO resultBO = new SysUserRoleResultBO();
        resultBO.setPageNo(sysUserRoleListDTO.getPageNo());
        resultBO.setPageRows(sysUserRoleListDTO.getPageRows());
        resultBO.setPageCount((int) iPage.getPages());
        resultBO.setTotalRows((int) iPage.getTotal());
        resultBO.setDataList(SysUserRoleConvert.sysUserRoleListToSysUserRoleBOList(iPage.getRecords()));
        resultBO.setParams(sysUserRoleListDTO);

        return resultBO;
    }

    @Override
    public void allotRoles(SysUserRoleDTO sysUserRoleDTO) {
        for (Long roleId : sysUserRoleDTO.getRolesId()) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(sysUserRoleDTO.getUserId());
            sysUserRole.setRoleId(roleId);
            sysUserRole.setCreateTime(new Date());
            sysUserRoleDao.insert(sysUserRole);
        }
    }
}