package com.jeeplus.test.cw.contractRegistration.service;

import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.cw.contractRegistration.domain.ContractFile;
import com.jeeplus.test.cw.contractRegistration.domain.ContractFilePaper;
import com.jeeplus.test.cw.contractRegistration.domain.ContractInfo;
import com.jeeplus.test.cw.contractRegistration.mapper.ContractFilePaperMapper;
import com.jeeplus.test.cw.contractRegistration.mapper.ContractInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;

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

/**
 * @author: 王强
 * @create: 2022-11-29 14:21
 **/
@Service
public class ContractFilePaperService {

    @Resource
    private ContractFilePaperMapper mapper;

    @Resource
    private ContractInfoMapper infoMapper;

    public String saveInfo(ContractFilePaper filePaper) throws Exception {

        ContractFilePaper info = mapper.selectFilePaperByContractInfoId(filePaper.getId());
        if (null != info) {
            filePaper.setFileCreateName(filePaper.getCreateBy());
            filePaper.setCreateBy(filePaper.getCreateId());
            infoMapper.updatefiledTypeById(filePaper.getContractInfoId(),filePaper.getFiledPaperType());
            return update(filePaper, info.getId());
        }else {
            return add(filePaper);
        }
    }

    /**
     * 根据id修改status
     */
    public void updateStatusById(ContractFilePaper filePaper) {
        ContractInfo infoById = infoMapper.getInfoById(filePaper.getId());
        if (null != infoById){
            infoMapper.updatefiledPaperTypeById(filePaper.getId(),filePaper.getFiledPaperType());
            mapper.updateStatusById(filePaper.getId(), filePaper.getFiledPaperType());
        } else {
            ContractFilePaper file = mapper.getById(filePaper.getId());
            infoMapper.updatefiledPaperTypeById(file.getContractInfoId(),filePaper.getFiledPaperType());
            mapper.updateStatusById(filePaper.getId(), filePaper.getFiledPaperType());
        }

    }

    public ContractFilePaper findByContractInfoId(String id) {
        return mapper.selectFilePaperByContractInfoId(id);
    }

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

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

}
