package com.jeeplus.assess.proofread.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
//import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.assess.proofread.domain.ProofreadIssued;
import com.jeeplus.assess.proofread.mapper.ProofreadIssuedMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

@Service
public class ProofreadIssuedService {

    @Resource
    private ProofreadIssuedMapper mapper;

    public String save(ProofreadIssued issued){
        ProofreadIssued byId = findById(issued.getProjectId());
        if (byId != null) {
            update(issued);
            return byId.getId();
        } else {
            return add(issued);
        }
    }

    public String add(ProofreadIssued issued) {
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        String id = issued.getProjectId();
        issued.setId(id);
        issued.setCreateById(userDTO.getId());
        issued.setCreateTime(new Date());
        issued.setUpdateById(userDTO.getId());
        issued.setUpdateTime(new Date());
        issued.setDelFlag(0);
        mapper.insert(issued);
        return id;
    }

    public void update(ProofreadIssued issued) {
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        issued.setUpdateById(userDTO.getId());
        issued.setUpdateTime(new Date());
        LambdaQueryWrapper<ProofreadIssued> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProofreadIssued::getProjectId, issued.getProjectId());
        mapper.update(issued, wrapper);
    }

    public ProofreadIssued findById(String id) {
        ProofreadIssued issued = mapper.findById(id);
        return issued;
    }

    public ProofreadIssued findProjectInfoById(String id) {
        ProofreadIssued issued = mapper.findProjectInfoById(id);
        return issued;
    }

    public void updateStatusById(ProofreadIssued issued) {
        mapper.updateStatusById(issued.getId(), issued.getStatus());
    }
}
