package com.jeeplus.assess.proofread.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeeplus.assess.proofread.domain.ProofreadDetail;
import com.jeeplus.assess.proofread.domain.ProofreadInfo;
import com.jeeplus.assess.proofread.mapper.ProofreadDetailMapper;
import com.jeeplus.assess.proofread.mapper.ProofreadInfoMapper;
import com.jeeplus.assess.proofread.service.dto.ProofreadInfoDto;
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 org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

@Service
public class ProofreadInfoService {

    @Resource
    private ProofreadInfoMapper infoMapper;

    @Resource
    private ProofreadDetailMapper detailMapper;

    public String save(ProofreadInfoDto dto) {
        if (StringUtils.isNotEmpty(dto.getProjectId())) {
            LambdaQueryWrapper<ProofreadInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ProofreadInfo::getProjectId, dto.getProjectId());
            wrapper.eq(ProofreadInfo::getDelFlag, 0);
            wrapper.eq(ProofreadInfo::getType, dto.getType());
            ProofreadInfo proofreadInfo = infoMapper.selectOne(wrapper);
            if (proofreadInfo != null) {
                update(dto);
            }else {
                add(dto);
            }
        }
        return "操作成功";
    }

    public String add(ProofreadInfoDto dto) {
        //生成主键id值
        String id = UUID.randomUUID().toString().replace("-", "");
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        //保存基本信息
        ProofreadInfo info = new ProofreadInfo();
        BeanUtils.copyProperties(dto, info);
        info.setId(id);
        info.setCreateById(userDTO.getId());
        info.setCreateTime(new Date());
        info.setUpdateById(userDTO.getId());
        info.setUpdateTime(new Date());
        infoMapper.insert(info);
        //明细表相关信息
        if (CollectionUtil.isNotEmpty(dto.getDetails())) {
            for (ProofreadDetail detail:dto.getDetails()) {
                String detailId = UUID.randomUUID().toString().replace("-", "");
                detail.setId(detailId);
                detail.setInfoId(id);
                detail.setCreateById(userDTO.getId());
                detail.setCreateTime(new Date());
                detail.setUpdateById(userDTO.getId());
                detail.setUpdateTime(new Date());
                detailMapper.insert(detail);
            }
        }
        return dto.getProjectId();
    }

    public String update(ProofreadInfoDto dto) {
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        //修改基本信息
        ProofreadInfo info = new ProofreadInfo();
        BeanUtils.copyProperties(dto, info);
        info.setUpdateById(userDTO.getId());
        info.setUpdateTime(new Date());
        LambdaQueryWrapper<ProofreadInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProofreadInfo::getProjectId, dto.getProjectId());
        wrapper.eq(ProofreadInfo::getType, dto.getType());
        infoMapper.update(info, wrapper);
        //明细表相关信息修改
        if (CollectionUtil.isNotEmpty(dto.getDetails())) {
            for (ProofreadDetail detail:dto.getDetails()) {
                detail.setUpdateById(userDTO.getId());
                detail.setUpdateTime(new Date());
                detailMapper.updateById(detail);
            }
        }
        return dto.getProjectId();
    }

    public ProofreadInfoDto findById(String id, String type) {
        //查询基本信息
        ProofreadInfoDto dto = infoMapper.findByProjectId(id, type);
        if (dto != null) {
            //查询扩展信息
            List<ProofreadDetail> details = detailMapper.findByInfoId(dto.getId());
            dto.setDetails(details);
        }
        return dto;
    }

    public List<ProofreadDetail> list(String type) {
        return detailMapper.findList(type);
    }
}
