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.upms.OrganizationDao;
import top.imwork.window.silos.entity.upms.Organization;
import top.imwork.window.silos.pojo.upms.bo.OrganizationBO;
import top.imwork.window.silos.pojo.upms.bo.OrganizationResultBO;
import top.imwork.window.silos.pojo.upms.dto.OrganizationDTO;
import top.imwork.window.silos.pojo.upms.dto.OrganizationListDTO;
import top.imwork.window.silos.service.upms.IOrganizationService;
import top.imwork.window.silos.convert.upms.OrganizationConvert;

@Service("organizationService")
public class OrganizationServiceImpl extends ServiceImpl<OrganizationDao,Organization> implements IOrganizationService {
    @Resource
    private OrganizationDao organizationDao;

    /**
     * 根据id获取信息
     * @param id 用户id
     * @return OrganizationBO 响应信息
     */
    @Override
    public OrganizationBO info(Long id) {
        Organization organization = organizationDao.info(id);
        return OrganizationConvert.organizationDoToBo(organization);
    }

    /**
     * 新增/更新信息
     * @param organizationDTO 信息
     * @return organizationBO 更新后信息
     */
    @Override
    public OrganizationBO save(OrganizationDTO organizationDTO) {
        Organization organization = OrganizationConvert.organizationDtoToDo(organizationDTO);
        if (ObjectUtils.isEmpty(organization.getId())) {
            organizationDao.insert(organization);
        } else {
            organizationDao.updateById(organization);
        }
        return OrganizationConvert.organizationDoToBo(organization);
    }

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

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

    @Override
    public OrganizationResultBO queryPage(OrganizationListDTO organizationListDTO) {
        QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(organizationListDTO.getOrganizationName())) {
            queryWrapper.like("organization_name", organizationListDTO.getOrganizationName());
        }

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

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

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

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

        Page<Organization> page = new Page<>(organizationListDTO.getPageNo(), organizationListDTO.getPageRows());
        IPage<Organization> iPage = organizationDao.queryPage(page, queryWrapper);

        OrganizationResultBO resultBO = new OrganizationResultBO();
        resultBO.setPageNo(organizationListDTO.getPageNo());
        resultBO.setPageRows(organizationListDTO.getPageRows());
        resultBO.setPageCount((int) iPage.getPages());
        resultBO.setTotalRows((int) iPage.getTotal());
        resultBO.setDataList(OrganizationConvert.organizationListToOrganizationBOList(iPage.getRecords()));
        resultBO.setParams(organizationListDTO);

        return resultBO;
    }
}