package com.ruoyi.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.domain.dto.employmentData.EmploymentDataEditRequest;
import com.ruoyi.web.domain.dto.employmentData.EmploymentDataQueryRequest;
import com.ruoyi.web.domain.dto.employmentData.EmploymentDataAddRequest;
import com.ruoyi.web.domain.entity.EmploymentData;
import com.ruoyi.web.mapper.EmploymentDataMapper;
import com.ruoyi.web.service.EmploymentDataService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class EmploymentDataServiceImpl extends ServiceImpl<EmploymentDataMapper, EmploymentData>
        implements EmploymentDataService {

    @Override
    public Integer addEmploymentData(EmploymentDataAddRequest employmentDataAddRequest) {
        if (employmentDataAddRequest == null) {
            throw new ServiceException("请求参数为空");
        }
        try {
            EmploymentData newEmploymentData = new EmploymentData();
            BeanUtil.copyProperties(employmentDataAddRequest, newEmploymentData);
            // 数据校验
            validEmploymentData(newEmploymentData, BusinessType.INSERT);
            // 保存到数据库
            this.save(newEmploymentData);
            return newEmploymentData.getId();
        } catch (Exception e) {
            log.error("添加就业数据失败", e);
            throw new ServiceException("添加就业数据失败");
        }
    }

    @Override
    public boolean deleteEmploymentData(String ids) {
        if (StrUtil.isBlank(ids)) {
            throw new ServiceException("id不能为空或id异常");
        }

        // 2. 分割ID字符串为List<Integer>
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(String::trim)
                .filter(StrUtil::isNotBlank)
                .map(Long::parseLong)
                .collect(Collectors.toList());

        if (idList.isEmpty()) {
            throw new ServiceException("ID格式异常");
        }

        // 3. 构建删除条件
        QueryWrapper<EmploymentData> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", idList);

        // 4. 执行删除（返回是否删除成功）
        return remove(queryWrapper);
    }

    @Override
    public void editEmploymentData(EmploymentDataEditRequest employmentDataEditRequest) {
        // 判断是否存在
        Integer id = employmentDataEditRequest.getId();
        EmploymentData oldEmploymentData = this.getById(id);
        if (oldEmploymentData == null) {
            throw new ServiceException("没有找到就业数据");
        }
        EmploymentData newEmploymentData = new EmploymentData();
        BeanUtil.copyProperties(employmentDataEditRequest, newEmploymentData);
        newEmploymentData.setUpdateTime(new Date());
        // 数据校验
        validEmploymentData(newEmploymentData, BusinessType.UPDATE);

        // 操作数据库
        boolean result = this.updateById(newEmploymentData);
        if (!result) {
            throw new ServiceException("修改就业数据操作失败");
        }
    }

    /**
     * 校验就业数据
     *
     * @param employmentData
     */
    public void validEmploymentData(EmploymentData employmentData, BusinessType type) {
        if (ObjectUtil.isEmpty(employmentData)) {
            throw new ServiceException("数据为空");
        }
        Integer id = employmentData.getId();
        String months = employmentData.getMonths();
        Integer positionNumber = employmentData.getPositionNumber();
        Integer hireNumber = employmentData.getHireNumber();
        String monthPayLow = employmentData.getMonthPayLow();
        String monthPayHigh = employmentData.getMonthPayHigh();
        String shoragePosition = employmentData.getShoragePosition();
        Integer skillTraining = employmentData.getSkillTraining();

        // 修改数据时，id 不能为空
        if (type != BusinessType.INSERT && ObjectUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        if (StrUtil.isBlank(months)) {
            throw new ServiceException("月份不能为空");
        }
        if (ObjectUtil.isEmpty(positionNumber)) {
            throw new ServiceException("岗位数量不能为空");
        }
        if (ObjectUtil.isEmpty(hireNumber)) {
            throw new ServiceException("录用人数不能为空");
        }
        if (StrUtil.isBlank(monthPayLow)) {
            throw new ServiceException("月薪下限不能为空");
        }
        if (StrUtil.isBlank(monthPayHigh)) {
            throw new ServiceException("月薪上限不能为空");
        }
        if (ObjectUtil.isEmpty(skillTraining)) {
            throw new ServiceException("培训情况不能为空");
        }
    }


    @Override
    public EmploymentData getEmploymentDataById(int id) {
        if (ObjectUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        return this.getById(id);
    }

    @Override
    public Page<EmploymentData> getListEmploymentDataByPage(EmploymentDataQueryRequest employmentDataQueryRequest) {
        QueryWrapper<EmploymentData> queryWrapper = new QueryWrapper<>();
        String months = employmentDataQueryRequest.getMonths();
        long pageNum = employmentDataQueryRequest.getPageNum();
        long pageSize = employmentDataQueryRequest.getPageSize();
        String sortField = employmentDataQueryRequest.getSortField();
        String sortOrder = employmentDataQueryRequest.getSortOrder();

        queryWrapper.eq(StrUtil.isNotBlank(months), "months", months)
                .orderBy(StrUtil.isNotEmpty(sortField), "ascend".equals(sortOrder), sortField);

        // 分页查询
        return this.page(new Page<>(pageNum, pageSize),
                queryWrapper);
    }
}




