package com.jeeplus.test.workContract.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.mould.service.SerialnumTplService;
import com.jeeplus.test.oss.domain.WorkAttachment;
import com.jeeplus.test.oss.mapper.OssServiceMapper;
import com.jeeplus.test.workContract.domain.WorkContractFile;
import com.jeeplus.test.workContract.domain.WorkContractInfo;
import com.jeeplus.test.workContract.mapper.WorkContractFileMapper;
import com.jeeplus.test.workContract.mapper.WorkContractInfoMapper;
import com.jeeplus.test.workContract.service.dto.WorkAttachmentDto;
import com.jeeplus.test.workContract.service.dto.WorkContractFileDto;
import com.jeeplus.test.workContract.service.dto.WorkContractInfoDto;
import org.flowable.editor.language.json.converter.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class WorkContractFileService {

    @Resource
    private WorkContractFileMapper workContractFileMapper;

    public String saveInfo(WorkContractFileDto workContractFileDto) throws Exception {
        if (StringUtils.isNotEmpty(workContractFileDto.getId())) {
            WorkContractFile info = workContractFileMapper.selectFileByContractInfoId(workContractFileDto.getId());
            if (info != null) {
                return update(workContractFileDto, info.getId());
            }
        }
        return add(workContractFileDto);
    }

    /**
     * 合同登记新增
     */
    @Transactional(rollbackFor = Exception.class)
    public String add(WorkContractFileDto workContractFileDto) throws Exception{
        //获取当前登录人信息
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        String id = UUID.randomUUID().toString().replace("-", "");
        WorkContractFile file = new WorkContractFile();
        BeanUtils.copyProperties(workContractFileDto, file);
        file.setId(id);
        file.setCreateBy(userDTO.getId());
        file.setCreateDate(new Date());
        file.setUpdateBy(userDTO.getId());
        file.setUpdateDate(new Date());
        workContractFileMapper.insert(file);
        return id;
    }

    /**
     * 合同登记修改
     */
    @Transactional(rollbackFor = Exception.class)
    public String update(WorkContractFileDto workContractFileDto, String id) {
        //获取当前登录人信息
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        WorkContractFile file = new WorkContractFile();
        BeanUtils.copyProperties(workContractFileDto, file);
        file.setId(id);
        file.setUpdateBy(userDTO.getId());
        file.setUpdateDate(new Date());
        workContractFileMapper.updateById(file);
        return file.getId();
    }

    /**
     * 根据id修改status
     */
    public void updateStatusById(WorkContractFileDto workContractFileDto) {
        workContractFileMapper.updateStatusById(workContractFileDto.getId(), workContractFileDto.getFiledType());
    }

    public WorkContractFile findById(String id) {
        return workContractFileMapper.selectById(id);
    }

    public WorkContractFile findByContractInfoId(String id) {
        return workContractFileMapper.selectFileByContractInfoId(id);
    }
}
