package org.dromara.main.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.main.domain.MainPostApply;
import org.dromara.main.domain.bo.MainPostApplyBo;
import org.dromara.main.domain.vo.MainPostApplyVo;
import org.dromara.main.mapper.MainPostApplyMapper;
import org.dromara.main.service.IMainPostApplyService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

@RequiredArgsConstructor
@Service
public class MainPostApplyServiceImpl implements IMainPostApplyService {

    private final MainPostApplyMapper baseMapper;

    @Override
    public TableDataInfo<MainPostApplyVo> queryPageList(MainPostApplyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MainPostApply> lqw = buildQueryWrapper(bo);
        Page<MainPostApplyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<MainPostApplyVo> queryList(MainPostApplyBo bo) {
        LambdaQueryWrapper<MainPostApply> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public MainPostApplyVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public Boolean insertByBo(MainPostApplyBo bo) {
        MainPostApply add = MapstructUtils.convert(bo, MainPostApply.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(MainPostApplyBo bo) {
        MainPostApply update = MapstructUtils.convert(bo, MainPostApply.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    private LambdaQueryWrapper<MainPostApply> buildQueryWrapper(MainPostApplyBo bo) {
        LambdaQueryWrapper<MainPostApply> lqw = Wrappers.lambdaQuery();
        lqw.like(ObjectUtil.isNotNull(bo.getApplyNo()), MainPostApply::getApplyNo, bo.getApplyNo());
        lqw.eq(ObjectUtil.isNotNull(bo.getTenantId()), MainPostApply::getTenantId, bo.getTenantId());
        lqw.like(ObjectUtil.isNotNull(bo.getPostName()), MainPostApply::getPostName, bo.getPostName());
        lqw.like(ObjectUtil.isNotNull(bo.getCompanyName()), MainPostApply::getCompanyName, bo.getCompanyName());
        lqw.eq(ObjectUtil.isNotNull(bo.getApplyStatus()), MainPostApply::getApplyStatus, bo.getApplyStatus());
        lqw.eq(ObjectUtil.isNotNull(bo.getPostType()), MainPostApply::getPostType, bo.getPostType());
        lqw.eq(ObjectUtil.isNotNull(bo.getIsUrgent()), MainPostApply::getIsUrgent, bo.getIsUrgent());
        lqw.eq(ObjectUtil.isNotNull(bo.getPostLevel()), MainPostApply::getPostLevel, bo.getPostLevel());
        lqw.eq(ObjectUtil.isNotNull(bo.getWorkCity()), MainPostApply::getWorkCity, bo.getWorkCity());
        lqw.orderByDesc(MainPostApply::getCreateTime);
        return lqw;
    }

    private void validEntityBeforeSave(MainPostApply entity) {
        if (entity.getRegistrationStartDate() != null && entity.getRegistrationEndDate() != null) {
            if (entity.getRegistrationStartDate().after(entity.getRegistrationEndDate())) {
                throw new ServiceException("报名开始时间不能晚于结束时间");
            }
        }

        if (entity.getRecruitNum() != null && entity.getRecruitNum() <= 0) {
            throw new ServiceException("招聘人数必须大于0");
        }

        if (entity.getGradeA() != null && (entity.getGradeA().compareTo(BigDecimal.ZERO) < 0
            || entity.getGradeA().compareTo(new BigDecimal("100")) > 0)) {
            throw new ServiceException("能力A及格分必须在0-100之间");
        }

        if (entity.getGradeB() != null && (entity.getGradeB().compareTo(BigDecimal.ZERO) < 0
            || entity.getGradeB().compareTo(new BigDecimal("100")) > 0)) {
            throw new ServiceException("能力B及格分必须在0-100之间");
        }

        if (entity.getGradeC() != null && (entity.getGradeC().compareTo(BigDecimal.ZERO) < 0
            || entity.getGradeC().compareTo(new BigDecimal("100")) > 0)) {
            throw new ServiceException("能力C及格分必须在0-100之间");
        }
    }
}
