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

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.SysRoleDao;
import top.imwork.window.silos.entity.upms.SysRole;
import top.imwork.window.silos.pojo.bo.SysRoleBO;
import top.imwork.window.silos.pojo.bo.SysRoleResultBO;
import top.imwork.window.silos.pojo.dto.SysRoleDTO;
import top.imwork.window.silos.pojo.dto.SysRoleListDTO;
import top.imwork.window.silos.service.upms.ISysRoleService;
import top.imwork.window.silos.convert.SysRoleConvert;

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

@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements ISysRoleService {
    @Resource
    private SysRoleDao sysRoleDao;

    /**
     * 根据id获取信息
     *
     * @param id 用户id
     * @return SysRoleBO 响应信息
     */
    @Override
    public SysRoleBO info(Long id) {
        SysRole sysRole = sysRoleDao.info(id);
        return SysRoleConvert.sysRoleDoToBo(sysRole);
    }

    /**
     * 新增/更新信息
     *
     * @param sysRoleDTO 信息
     * @return sysRoleBO 更新后信息
     */
    @Override
    public SysRoleBO save(SysRoleDTO sysRoleDTO) {
        SysRole sysRole = SysRoleConvert.sysRoleDtoToDo(sysRoleDTO);
        if (ObjectUtils.isEmpty(sysRole.getId())) {
            sysRole.setDelFlag("N");
            sysRole.setCreator("admin");
            sysRole.setCreateTime(new Date());
            sysRole.setUpdateTime(new Date());
            sysRole.setModifier("admin");

            sysRoleDao.insert(sysRole);
        } else {
            sysRole.setUpdateTime(new Date());
            sysRoleDao.updateById(sysRole);
        }
        return SysRoleConvert.sysRoleDoToBo(sysRole);
    }

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

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

    @Override
    public SysRoleResultBO queryPage(SysRoleListDTO sysRoleListDTO) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(sysRoleListDTO.getRoleName())) {
            queryWrapper.like("role_name", sysRoleListDTO.getRoleName());
        }

        if (!StringUtils.isEmpty(sysRoleListDTO.getRoleType())) {
            queryWrapper.like("role_type", sysRoleListDTO.getRoleType());
        }

        if (!StringUtils.isEmpty(sysRoleListDTO.getEnabled())) {
            queryWrapper.like("enabled", sysRoleListDTO.getEnabled());
        }

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

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

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

        if (!StringUtils.isEmpty(sysRoleListDTO.getUpdateEndTime())) {
            queryWrapper.apply("update_time <= '" + sysRoleListDTO.getUpdateEndTime() + "'");
        }
        PageHelper.startPage(sysRoleListDTO.getPageNo(), sysRoleListDTO.getPageRows());
        List<SysRole> list = sysRoleDao.selectList(queryWrapper);
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);

        SysRoleResultBO resultBO = new SysRoleResultBO();
        resultBO.setPageNo(sysRoleListDTO.getPageNo());
        resultBO.setPageRows(sysRoleListDTO.getPageRows());
        resultBO.setPageCount(pageInfo.getPages());
        resultBO.setTotalRows((int) pageInfo.getTotal());
        resultBO.setDataList(SysRoleConvert.sysRoleListToSysRoleBOList(list));
        resultBO.setParams(sysRoleListDTO);

        return resultBO;
    }

    @Override
    public List<SysRoleBO> selectAll() {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("del_flag='N'");
        List<SysRole> list = sysRoleDao.selectList(queryWrapper);
        return SysRoleConvert.sysRoleListToSysRoleBOList(list);
    }

}