package com.ruoyi.web.work.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.work.api.util.AppUtil;
import com.ruoyi.web.work.domain.Column;
import com.ruoyi.web.work.domain.Enterprise;
import com.ruoyi.web.work.domain.Position;
import com.ruoyi.web.work.domain.User;
import com.ruoyi.web.work.domain.dto.PositionDto;
import com.ruoyi.web.work.domain.dto.PositionQueryDto;
import com.ruoyi.web.work.domain.dto.PositionStateDto;
import com.ruoyi.web.work.domain.vo.PositionListVo;
import com.ruoyi.web.work.mapper.PositionMapper;
import com.ruoyi.web.work.service.IColumnService;
import com.ruoyi.web.work.service.IEnterpriseService;
import com.ruoyi.web.work.service.IPositionService;
import com.ruoyi.web.work.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lsw
 * @date 2024-06-07
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements IPositionService {
    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private IColumnService columnService;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private IUserService userService;

    @Override
    public List<Position> selectList(Position position) {
        return positionMapper.selectList(position);
    }

    @Override
    public List<Position> manageList(Position position) {
        position.setUserId(AppUtil.getUser().getId());
        PageUtils.orderBy("id desc");
        return positionMapper.manageList(position);
    }

    @Override
    public List<PositionListVo> indexList(PositionQueryDto dto) {
        if (StringUtils.isNotEmpty(dto.getSalary())) {
            if (dto.getSalary().equals("50k以上")) {
                dto.setMin(50);
                dto.setMax(500);
            } else if (dto.getSalary().equals("不限")) {
                dto.setMin(0);
                dto.setMax(50000);
            } else {
                String salary[] = dto.getSalary().replace("k", "").split("-");
                dto.setMin(Integer.parseInt(salary[0]));
                dto.setMax(Integer.parseInt(salary[1]));
            }
        }
        return positionMapper.indexList(dto);
    }

    @Override
    public AjaxResult detail(Long id) {
        AjaxResult result = new AjaxResult();
        Position position = getById(id);
        if (position == null || position.getState() != 0 || position.getAudit() != 1) {
            return AjaxResult.error("职位不存在或已下架");
        }
        if (position.getType() == 0) {
            Enterprise enterprise = enterpriseService.getById(position.getUserId());
            result.put("enterprise", enterprise);
        }
        result.put("position", position);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult manageAdd(PositionDto dto) {
        if (AppUtil.getUser().getIsAuthentication() != 1) {
            return AjaxResult.error(7878, "发布全职或兼职需要实名认证");
        }
        //如果发布全职需要审核企业信息通过才允许发布
        if (dto.getType() == 0) {
            User user=userService.getById(AppUtil.getUser().getId());
            if (user.getType() != 1) {
                return AjaxResult.error("只有企业用户才能发布全职");
            }
            if (user.getIsEnterprise() != 1) {
                return AjaxResult.error(7979, "请先完成企业认证");
            }
        }
        Position position = new Position();
        BeanUtils.copyProperties(dto, position);
        Column region = columnService.selectRegion(new Column().setTitle(position.getRegionName()).setLevel(3));
        if (region != null) {
            position.setAncestors(region.getAncestors() + "," + region.getId());
        }
        //全职薪资
        if (!position.getSalary().equals("面议") && position.getType() == 0) {
            String salary[] = position.getSalary().replace("k", "").split("-");
            position.setMin(Integer.parseInt(salary[0]));
            position.setMax(Integer.parseInt(salary[1]));
        }
        position.setUserId(AppUtil.getUser().getId());
        position.setState(0);
        position.setAudit(1);
        position.setRecommend(0);
        if (!save(position)) {
            throw new ServiceException("发布职位失败");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult manageEdit(PositionDto dto) {
        Position position = getById(dto.getId());
        if (position == null || !position.getUserId().equals(AppUtil.getUser().getId())) {
            return AjaxResult.error("职位不存在或非法操作");
        }
        if (position.getAudit() == 1) {
            // return AjaxResult.error("该职位已审核通过不允许编辑");
        }
        BeanUtils.copyProperties(dto, position);
        Column region = columnService.selectRegion(new Column().setTitle(position.getRegionName()).setLevel(3));
        if (region != null) {
            position.setAncestors(region.getAncestors() + "," + region.getId());
        }
        //全职薪资
        if (!position.getSalary().equals("面议") && position.getType() == 0) {
            String salary[] = position.getSalary().replace("k", "").split("-");
            position.setMin(Integer.parseInt(salary[0]));
            position.setMax(Integer.parseInt(salary[1]));
        }
        position.setUserId(AppUtil.getUser().getId());
        if (!updateById(position)) {
            throw new ServiceException("编辑职位失败");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult manageDetail(Long id) {
        Position position = getById(id);
        if (position == null || !position.getUserId().equals(AppUtil.getUser().getId())) {
            return AjaxResult.error("职位不存在或非法操作");
        }
        return AjaxResult.success(position);
    }

    @Override
    public AjaxResult manageRemove(Long id) {
        Position position = getById(id);
        if (position == null || !position.getUserId().equals(AppUtil.getUser().getId())) {
            return AjaxResult.error("职位不存在或非法操作");
        }
        if (!removeById(id)) {
            throw new ServiceException("删除职位失败,请联系平台");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult manageState(PositionStateDto dto) {
        Position position = getById(dto.getId());
        if (position == null || !position.getUserId().equals(AppUtil.getUser().getId())) {
            return AjaxResult.error("职位不存在或非法操作");
        }
        position.setState(dto.getState());
        if (!updateById(position)) {
            throw new ServiceException("启用或关闭职位失败,请联系平台");
        }
        return AjaxResult.success();
    }
}
