package com.sqx.modules.resumes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.resumes.dao.ShieldCompanyDao;
import com.sqx.modules.resumes.entity.ShieldCompany;
import com.sqx.modules.resumes.service.CompanyService;
import com.sqx.modules.resumes.service.ShieldCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class ShieldCompanyServiceImpl extends ServiceImpl<ShieldCompanyDao, ShieldCompany> implements ShieldCompanyService {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private UserService userService;


    @Override
    public Result selectShieldList(Integer page, Integer limit, Long userId, Long companyId) {
        QueryWrapper<ShieldCompany> wrapper = new QueryWrapper<>();
        if (userId != null) {
            wrapper.eq("user_id", userId).eq("shield_type", 1);

        }
        if (companyId != null) {
            wrapper.eq("company_id", companyId).eq("shield_type", 2);
        }
        wrapper.orderByDesc("create_time");
        IPage<ShieldCompany> shieldCompanyPage = baseMapper.selectPage(new Page<>(page, limit), wrapper);
        for (ShieldCompany shieldCompany : shieldCompanyPage.getRecords()) {
            shieldCompany.setCompany(companyService.getById(shieldCompany.getCompanyId()));
            shieldCompany.setUser(userService.selectUserById(shieldCompany.getUserId()));
        }
        return Result.success().put("data", new PageUtils(shieldCompanyPage));
    }

    @Override
    public Result insertShieldCompany(ShieldCompany shieldCompany) {
        int count = baseMapper.selectCount(new QueryWrapper<ShieldCompany>().eq("company_id", shieldCompany.getCompanyId()).eq("user_id", shieldCompany.getUserId()).eq("shield_type", shieldCompany.getShieldType()));
        if(count>0){
            return Result.error("该主体已被屏蔽了！");
        }
        shieldCompany.setCreateTime(DateUtils.format(new Date()));

        return Result.upStatus(baseMapper.insert(shieldCompany));
    }


}
