package com.jeeplus.test.proofread.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.proofread.domain.ProofreadDetail;
import com.jeeplus.test.proofread.domain.ProofreadInfo;
import com.jeeplus.test.proofread.mapper.ProofreadDetailMapper;
import com.jeeplus.test.proofread.mapper.ProofreadInfoMapper;
import com.jeeplus.test.proofread.service.dto.ProofreadInfoDto;
import org.flowable.editor.language.json.converter.util.CollectionUtils;
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 = UserUtils.getCurrentUserDTO();
        //保存基本信息
        ProofreadInfo info = new ProofreadInfo();
        BeanUtils.copyProperties(dto, info);
        info.setId(id);
        info.setCreateBy(userDTO.getId());
        info.setCreateDate(new Date());
        info.setUpdateBy(userDTO.getId());
        info.setUpdateDate(new Date());
        infoMapper.insert(info);
        //明细表相关信息
        if (CollectionUtils.isNotEmpty(dto.getDetails())) {
            for (ProofreadDetail detail:dto.getDetails()) {
                String detailId = UUID.randomUUID().toString().replace("-", "");
                detail.setId(detailId);
                detail.setInfoId(id);
                detail.setCreateBy(userDTO.getId());
                detail.setCreateDate(new Date());
                detail.setUpdateBy(userDTO.getId());
                detail.setUpdateDate(new Date());
                detailMapper.insert(detail);
            }
        }
        return dto.getProjectId();
    }

    public String update(ProofreadInfoDto dto) {
        //获取当前登录人信息
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        //修改基本信息
        ProofreadInfo info = new ProofreadInfo();
        BeanUtils.copyProperties(dto, info);
        info.setUpdateBy(userDTO.getId());
        info.setUpdateDate(new Date());
        LambdaQueryWrapper<ProofreadInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProofreadInfo::getProjectId, dto.getProjectId());
        wrapper.eq(ProofreadInfo::getType, dto.getType());
        infoMapper.update(info, wrapper);
        //明细表相关信息修改
        if (CollectionUtils.isNotEmpty(dto.getDetails())) {
            for (ProofreadDetail detail:dto.getDetails()) {
                detail.setUpdateBy(userDTO.getId());
                detail.setUpdateDate(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);
    }
}
