package com.cf.imes.module.system.dal.mysql.dept;

import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.framework.organ.core.context.OrganContextHolder;
import com.cf.imes.framework.security.core.LoginUser;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.module.system.controller.admin.dept.vo.post.PostPageReqVO;
import com.cf.imes.module.system.dal.dataobject.dept.PostDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

@Mapper
public interface PostMapper extends BaseMapperX<PostDO> {

    default List<PostDO> selectList(Collection<Long> ids, Collection<Integer> statuses, Long organId) {
        return selectList(new LambdaQueryWrapperX<PostDO>()
                .eqIfPresent(PostDO::getOrganId, organId)
                .inIfPresent(PostDO::getId, ids)
                .inIfPresent(PostDO::getStatus, statuses));
    }

    default PageResult<PostDO> selectPage(PostPageReqVO reqVO) {
        LambdaQueryWrapperX<PostDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        assert loginUser != null;
        Boolean isSupAdmin = loginUser.getIsSupAdmin();
        if(isSupAdmin && !Objects.isNull(reqVO.getOrganId())) {
            lambdaQueryWrapperX.eqIfPresent(PostDO::getOrganId, reqVO.getOrganId());
        }else{
            lambdaQueryWrapperX.eqIfPresent(PostDO::getOrganId, loginUser.getOrganId());
        }

        lambdaQueryWrapperX.likeIfPresent(PostDO::getCode, reqVO.getCode())
                .likeIfPresent(PostDO::getName, reqVO.getName())
                .eqIfPresent(PostDO::getStatus, reqVO.getStatus());


        if (reqVO.getOrder() == null) {
            lambdaQueryWrapperX.orderByDesc(PostDO::getSort);
        } else {
            if (reqVO.getOrder().equals("ascend")) {
                switch (reqVO.getField()) {
                    case "name" -> lambdaQueryWrapperX.orderByAsc(PostDO::getName);
                    case "code" -> lambdaQueryWrapperX.orderByAsc(PostDO::getCode);
                    case "sort" -> lambdaQueryWrapperX.orderByAsc(PostDO::getSort);
                    case "status" -> lambdaQueryWrapperX.orderByAsc(PostDO::getStatus);
                    case "create_time" -> lambdaQueryWrapperX.orderByAsc(PostDO::getCreateTime);
                    default -> {
                        // default不排序
                    }
                }
            } else if (reqVO.getOrder().equals("descend")) {
                switch (reqVO.getField()) {
                    case "name" -> lambdaQueryWrapperX.orderByDesc(PostDO::getName);
                    case "code" -> lambdaQueryWrapperX.orderByDesc(PostDO::getCode);
                    case "sort" -> lambdaQueryWrapperX.orderByDesc(PostDO::getSort);
                    case "status" -> lambdaQueryWrapperX.orderByDesc(PostDO::getStatus);
                    case "create_time" -> lambdaQueryWrapperX.orderByDesc(PostDO::getCreateTime);
                    default -> {
                        // default不排序
                    }
                }
            }
        }

        return selectPage(reqVO, lambdaQueryWrapperX);
    }

    default PostDO selectByName(String name) {
        return selectOne(PostDO::getName, name, PostDO::getOrganId, OrganContextHolder.getOrganId());
    }

    default PostDO selectByCode(String code) {
        return selectOne(PostDO::getCode, code, PostDO::getOrganId, OrganContextHolder.getOrganId());
    }

}
