package io.wdd.server.coreService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.wdd.server.beans.mapper.ProjectInfoDTOMapper;
import io.wdd.server.beans.po.ProjectInfoPO;
import io.wdd.server.beans.request.ProjectQueryEntity;
import io.wdd.server.beans.vo.ProjectInfoVO;
import io.wdd.server.coreService.CoreProjectService;
import io.wdd.server.service.ProjectInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CoreProjectServiceImpl implements CoreProjectService {

    @Resource
    ProjectInfoService projectInfoService;

    @Override
    public List<ProjectInfoPO> projectGetAll() {

        return projectInfoService.list();
    }

    @Override
    public Page<ProjectInfoPO> projectQueryByEntity(ProjectQueryEntity projectQueryEntity) {

        if (projectQueryEntity == null || projectQueryEntity.getProjectName() == null || projectQueryEntity.getProjectProvince() == null) {
            return null;
        }

        if (projectQueryEntity.getPageNumber() == null || projectQueryEntity.getPageSize() == null) {
            projectQueryEntity.setPageNumber(1);
            projectQueryEntity.setPageSize(10);
        }

        Page<ProjectInfoPO> poPage = new Page<>(
                projectQueryEntity.getPageNumber(),
                projectQueryEntity.getPageSize()
        );

        new LambdaQueryChainWrapper<ProjectInfoPO>(projectInfoService.getBaseMapper())
                .likeRight(
                        StringUtils.isNotBlank(projectQueryEntity.getProjectName()),
                        ProjectInfoPO::getProjectName,
                        projectQueryEntity.getProjectName()
                )
                .likeRight(
                        StringUtils.isNotBlank(projectQueryEntity.getProjectProvince()),
                        ProjectInfoPO::getProjectProvince,
                        projectQueryEntity.getProjectProvince()
                )
                .page(poPage);

        return poPage;
    }

    @Override
    public ProjectInfoPO projectGetOne(Long projectId) {

        return projectInfoService.getById(projectId);
    }

    @Override
    public boolean projectCreate(ProjectInfoVO projectInfoVO) {

        if (!checkDataValid(projectInfoVO)) {
            return false;
        }

        ProjectInfoPO projectInfoPO = ProjectInfoDTOMapper.INSTANCE.voToPo(projectInfoVO);

        return projectInfoService.save(projectInfoPO);

    }

    private boolean checkDataValid(ProjectInfoVO projectInfoVO) {

        if (StringUtils.isBlank(projectInfoVO.getProjectName())) {
            return false;
        }

        return !StringUtils.isBlank(projectInfoVO.getProjectNamespace());
    }

    @Override
    public boolean projectUpdate(ProjectInfoPO projectInfoPO) {

        if (!checkDataValid(ProjectInfoDTOMapper.INSTANCE.poToVo(projectInfoPO))) {
            return false;
        }

        return projectInfoService.saveOrUpdate(projectInfoPO);
    }

    @Override
    public boolean projectDelete(Long projectId) {
        return projectInfoService.removeById(projectId);
    }
}
