package com.zcloud.basic.info.persistence.repository.impl;

import com.alibaba.cola.dto.MultiResponse;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jjb.saas.framework.auth.model.SSOUser;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.zcloud.basic.info.domain.enums.MenuEnum;
import com.zcloud.basic.info.persistence.dataobject.DepartmentDO;
import com.zcloud.basic.info.persistence.dataobject.PostDO;
import com.zcloud.basic.info.persistence.mapper.PostMapper;
import com.zcloud.basic.info.persistence.repository.PostRepository;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.dto.PageResponse;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * web-infrastructure
 *
 * @Author SondonYong
 * @Date 2025-10-31 17:24:04
 */
@Service
@RequiredArgsConstructor
public class PostRepositoryImpl extends BaseRepositoryImpl<PostMapper, PostDO> implements PostRepository {
    private final PostMapper postMapper;

    @Override
    public PageResponse<PostDO> listPage(Map<String, Object> params) {

        if(params.get("eqCorpinfoId") == null){
            params.put("eqCorpinfoId", AuthContext.getTenantId());
        }
        if(params.get("eqDepartmentId") != null){
            Long eqDepartmentId = (Long) params.get("eqDepartmentId");
            if(eqDepartmentId == 0){
                params.put("eqDepartmentId", null);
            }
        }

        IPage<PostDO> iPage = new Query<PostDO>().getPage(params);
        QueryWrapper<PostDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, params);
        queryWrapper.orderByDesc("create_time");
        String menuPerms = "";
        if (!org.springframework.util.ObjectUtils.isEmpty(params.get("menuPath"))){
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        queryWrapper.eq("delete_enum", "FALSE");
        IPage<PostDO> result = postMapper.selectPostPage(iPage, queryWrapper,menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public MultiResponse<PostDO> list(Map<String, Object> params) {

        if(params.get("eqCorpinfoId") == null){
            params.put("eqCorpinfoId", AuthContext.getTenantId());
        }

        Long[] ids = (Long[])params.get("postIds");
        Long departmentId = (Long)params.get("departmentId");
        Long corpinfoId = (Long)params.get("corpinfoId");
        QueryWrapper<PostDO> queryWrapper = new QueryWrapper<>();
        if(ids != null && ids.length > 0) {
            queryWrapper.in("id", ids);
        }
        if(departmentId != null && departmentId > 0) {
            queryWrapper.eq("department_id", departmentId);
        }
        if(corpinfoId != null && corpinfoId > 0) {
            queryWrapper.eq("corpinfo_id", corpinfoId);
        }
        queryWrapper.orderByDesc("create_time");
        List<PostDO> postDOS = postMapper.selectList(queryWrapper);
        return MultiResponse.of(postDOS);
    }

    @Override
    public SingleResponse<PostDO> getInfoById(Long id) {
        return SingleResponse.of(postMapper.selectById(id));
    }

    @Override
    public Long getCountByCorpInfoId(Long id) {
        QueryWrapper<PostDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corpinfo_id", id);
        return postMapper.selectCount(queryWrapper);
    }

    @Override
    public List<PostDO> listByCorpInfoId(Long corpInfoId) {
        QueryWrapper<PostDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corpinfo_id", corpInfoId);
        queryWrapper.eq("delete_enum", "FALSE");
        return list(queryWrapper);
    }
    @Override
    public Boolean checkPostDepartment(Long departmentId) {
        QueryWrapper<PostDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", departmentId);
        queryWrapper.eq("delete_enum", "FALSE");
        return postMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public void deleteByCorpInfoId(Long corpInfoId) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("corpinfo_id", corpInfoId);
        updateWrapper.set("delete_enum", "TRUE");
        update(updateWrapper);
    }
}

