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.SecurityUtils;
import com.ruoyi.web.work.api.util.AppUtil;
import com.ruoyi.web.work.domain.Company;
import com.ruoyi.web.work.domain.Relate;
import com.ruoyi.web.work.domain.User;
import com.ruoyi.web.work.domain.vo.RelateCompanyListVo;
import com.ruoyi.web.work.mapper.RelateMapper;
import com.ruoyi.web.work.service.ICompanyService;
import com.ruoyi.web.work.service.IRelateService;
import com.ruoyi.web.work.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lsw
 * @date 2024-04-18
 */
@Service
public class RelateServiceImpl extends ServiceImpl<RelateMapper, Relate> implements IRelateService {
    @Autowired
    private RelateMapper relateMapper;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    IUserService userService;

    @Override
    public List<Relate> selectList(Relate relate) {
        return relateMapper.selectList(relate);
    }

    @Override
    public List<RelateCompanyListVo> selectRelateCompanyList(Relate relate) {
        return relateMapper.selectRelateCompanyList(relate);
    }

    @Transactional
    @Override
    public AjaxResult add(Relate relate) {
        Company company = companyService.getById(relate.getCompanyId());
        if (company == null || company.getState() == 1) {
            return AjaxResult.error("企业不存在或停用");
        }
        relate.setUserId(AppUtil.getUser().getId());
        relate.setState(0);
        Relate check = relateMapper.check(relate);
        if (check != null) {
            return AjaxResult.error("你已关联该企业");
        }
        save(relate);
        User user = userService.getById(AppUtil.getUser().getId());
        user.setIsCompany(user.getIsCompany() + 1);
        userService.updateById(user);
        return AjaxResult.success();
    }

    @Transactional()
    @Override
    public AjaxResult removeUser(Relate relate) {
        relate.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        if (!relateMapper.remove(relate)) {
            throw new ServiceException("解除关联失败,请联系平台");
        }
        User user = userService.getById(relate.getUserId());
        user.setIsCompany(user.getIsCompany() - 1);
        if (!userService.updateById(user)) {
            throw new ServiceException("更新用户信息失败,请联系平台");
        }
        return AjaxResult.success();
    }

    @Transactional
    @Override
    public AjaxResult remove(Relate relate) {
        relate.setUserId(AppUtil.getUser().getId());
        if (!relateMapper.remove(relate)) {
            throw new ServiceException("解除关联失败,请联系平台");
        }
        User user = userService.getById(AppUtil.getUser().getId());
        user.setIsCompany(user.getIsCompany() - 1);
        if (!userService.updateById(user)) {
            throw new ServiceException("更新用户信息失败,请联系平台");
        }
        return AjaxResult.success();
    }
}
