package com.jeeplus.assess.workContract.service;

import cn.hutool.extra.spring.SpringUtil;
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.assess.workContract.domain.WorkContractFile;
import com.jeeplus.assess.workContract.mapper.WorkContractFileMapper;
import com.jeeplus.assess.workContract.service.dto.WorkContractFileDto;
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.pubmodules.serialNumTpl.service.SerialnumTplService;
//import com.jeeplus.pubmodules.oss.domain.WorkAttachment;
//import com.jeeplus.pubmodules.oss.mapper.OssServiceMapper;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
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 = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        String id = UUID.randomUUID().toString().replace("-", "");
        WorkContractFile file = new WorkContractFile();
        BeanUtils.copyProperties(workContractFileDto, file);
        file.setId(id);
        file.setCreateById(userDTO.getId());
        file.setCreateTime(new Date());
        file.setUpdateById(userDTO.getId());
        file.setUpdateTime(new Date());
        workContractFileMapper.insert(file);
        return id;
    }

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

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


    /**
     * 根据id删除归档
     */
    public int deleteById(WorkContractFileDto workContractFileDto) {
       return workContractFileMapper.removeById(workContractFileDto.getId());
    }



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

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