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.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.User;
import com.ruoyi.web.work.domain.vo.EnterpriseListVo;
import com.ruoyi.web.work.mapper.EnterpriseMapper;
import com.ruoyi.web.work.service.IColumnService;
import com.ruoyi.web.work.service.IEnterpriseService;
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-06-05
 */
@Service
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, Enterprise> implements IEnterpriseService {
    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Autowired
    private IColumnService columnService;

    @Autowired
    private IUserService userService;

    @Override
    public List<Enterprise> selectList(Enterprise enterprise) {
        return enterpriseMapper.selectList(enterprise);
    }

    @Override
    public List<EnterpriseListVo> indexList(Enterprise enterprise) {
        return enterpriseMapper.indexList(enterprise);
    }

    @Override
    public AjaxResult index() {
        Enterprise enterprise = getById(AppUtil.getUser().getId());
        if (enterprise == null) {
            enterprise = new Enterprise();
            enterprise.setId(AppUtil.getUser().getId());
            if (!save(enterprise)) {
                throw new ServiceException("创建企业信息失败");
            }
        }
        return AjaxResult.success(enterprise);
    }

    @Override
    public AjaxResult edit(Enterprise enterprise) {
        Enterprise check = getById(AppUtil.getUser().getId());
        if (check == null || check.getState() == 1) {
            return AjaxResult.error("企业信息不存在或当前无法编辑");
        }
        Column region = columnService.selectRegion(new Column().setTitle(enterprise.getRegionName()).setLevel(3));
        if (region != null) {
            enterprise.setAncestors(region.getAncestors() + "," + region.getId());
        }
        enterprise.setId(AppUtil.getUser().getId());
        enterprise.setState(0);
        if (!updateById(enterprise)) {
            throw new ServiceException("编辑企业信息失败");
        }
        return AjaxResult.success();
    }

    @Transactional
    @Override
    public AjaxResult audit(Enterprise enterprise) {
        if (!updateById(enterprise)) {
            throw new ServiceException("审核企业信息失败");
        }
        User user = new User();
        user.setId(enterprise.getId());
        user.setIsEnterprise(enterprise.getState() == 1 ? 1 : 0);
        if (!userService.updateById(user)) {
            throw new ServiceException("更新用户信息失败");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult detail(Long id) {
        Enterprise enterprise = getById(id);
        if (enterprise == null || enterprise.getState() != 1) {
            return AjaxResult.error("企业信息不存在或未认证");
        }
        return AjaxResult.success(enterprise);
    }
}
