package com.jeeplus.business.workContractInfo.service;

import cn.hutool.extra.spring.SpringUtil;
import com.jeeplus.business.workContractInfo.domain.JyWorkContractFile;
import com.jeeplus.business.workContractInfo.mapper.JyWorkContractFileMapper;
import com.jeeplus.business.workContractInfo.service.dto.JyWorkContractFileDto;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
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.Date;
import java.util.UUID;

//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;

@Service
public class JyWorkContractFileService {

    @Resource
    private JyWorkContractFileMapper jyWorkContractFileMapper;

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


    /**
     * 合同登记新增
     */
    @Transactional(rollbackFor = Exception.class)
    public String add(JyWorkContractFileDto workContractFileDto) throws Exception{
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        String id = UUID.randomUUID().toString().replace("-", "");
        JyWorkContractFile file = new JyWorkContractFile();
        BeanUtils.copyProperties(workContractFileDto, file);
        file.setId(id);
        file.setCreateById(userDTO.getId());
        file.setCreateTime(new Date());
        file.setUpdateById(userDTO.getId());
        file.setUpdateTime(new Date());
        jyWorkContractFileMapper.insert(file);
        return id;
    }

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

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

    public JyWorkContractFile findById(String id) {
        return jyWorkContractFileMapper.selectById(id);
    }

    public JyWorkContractFile findByContractInfoId(String id) {
        return jyWorkContractFileMapper.selectFileByContractInfoId(id);
    }
}
