package com.sqx.modules.intention.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.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.intention.dao.IntentionDao;
import com.sqx.modules.intention.entity.Intention;
import com.sqx.modules.intention.service.IntentionService;
import com.sqx.modules.resumes.entity.RuleClassify;
import com.sqx.modules.resumes.service.RuleClassifyService;
import com.sqx.modules.resumes.service.RulePostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import weixin.popular.bean.datacube.article.Usershare;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author www.javacoder.top
 * @since 2023-01-13
 */
@Service
public class IntentionServiceImpl extends ServiceImpl<IntentionDao, Intention> implements IntentionService {
    @Autowired
    CommonInfoService commonInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private RuleClassifyService classifyService;

    @Override
    public Result saveUpdate(Intention intention) {
        if (intention.getIsDefault() == 1) {
            Intention updateIntention = new Intention();
            updateIntention.setIsDefault(0);
            baseMapper.update(updateIntention, new QueryWrapper<Intention>().eq("user_id", intention.getUserId()));
        }
        QueryWrapper<Intention> wrapper = new QueryWrapper<Intention>().eq("user_id", intention.getUserId());
        if (intention.getIntentionId() != null) {
            wrapper.ne("intention_id", intention.getIntentionId());
        }
        int count = baseMapper.selectCount(wrapper);
        UserEntity userEntity = userService.getById(intention.getUserId());
        int maxCount = Integer.parseInt(commonInfoService.findOne(325).getValue());
        if (count > maxCount) {
            return Result.error("求职意向最多添加" + maxCount + "个");
        }
        if (intention.getIntentionId() != null) {
            return Result.upStatus(baseMapper.updateById(intention));
        } else {
            intention.setCreateTime(LocalDateTime.now());
            intention.setUserName(userEntity.getUserName());
            RuleClassify ruleClassify = classifyService.getById(intention.getRuleClassifyId());
            RuleClassify twoClassify = classifyService.getOne(new QueryWrapper<RuleClassify>().eq("rule_classify_id", ruleClassify.getParentId()));
            RuleClassify oneClassify = classifyService.getOne(new QueryWrapper<RuleClassify>().eq("rule_classify_id", twoClassify.getParentId()));
            intention.setRuleClassifyId(ruleClassify.getRuleClassifyId());
            intention.setClassifyTwoId(twoClassify.getRuleClassifyId());
            intention.setClassifyOneId(oneClassify.getRuleClassifyId());
            return Result.upStatus(baseMapper.insert(intention));
        }
    }

    @Override
    public IPage<Intention> getIntentionList(Integer page, Integer limit, Intention intention) {
        Page<Intention> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.selectPage(pages, new QueryWrapper<>(intention).orderByDesc("create_time"));
    }
}
