package com.jeeplus.finance.projectReport.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.finance.projectReport.domain.CwProjectReportData;
import com.jeeplus.finance.projectReport.domain.CwProjectReportPaperArchiving;
import com.jeeplus.finance.projectReport.mapper.CwProjectReportPaperArchivingMapper;
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.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author 王强
 * @version 1.0
 * @date 2024-01-31 15:54
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Component
public class CwProjectReportPaperArchivingService extends ServiceImpl<CwProjectReportPaperArchivingMapper, CwProjectReportPaperArchiving> {

    @Resource
    private CwProjectReportPaperArchivingMapper paperArchivingMapper;

    /**
     * 修改状态
     * @param data
     * @return
     */
    public String updateStatusByReportId(CwProjectReportData data) {
        paperArchivingMapper.updateStatusById(data.getId(),data.getStatus());
        return "操作成功";
    }

    public CwProjectReportData queryByReportId(String id) {
        CwProjectReportPaperArchiving signAndIssue = paperArchivingMapper.selectOne(new QueryWrapper<CwProjectReportPaperArchiving>().eq("report_id", id));
        CwProjectReportData data = new CwProjectReportData();
        if (null != signAndIssue) {

            BeanUtils.copyProperties(signAndIssue, data);
        }

        return data;
    }

    /**
     * 保存项目以及其他相关信息
     * @param reportData
     * @return
     * @throws Exception
     */
    public String saveForm(CwProjectReportData reportData) throws Exception{
        if (StringUtils.isNotEmpty(reportData.getId())){

            CwProjectReportPaperArchiving onlineArchiving = paperArchivingMapper.selectById(reportData.getPaperArchivingId());
            if (onlineArchiving != null){
                reportData.setCreateBy(reportData.getCreateBy());
                reportData.setMark(onlineArchiving.getMark());
                return update(reportData,onlineArchiving);
            }
        }
        return add(reportData);

    }

    public String add(CwProjectReportData dto) throws Exception{


        //生成主键id值
        String id = UUID.randomUUID().toString().replace("-", "");
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));
        //保存基本信息
        CwProjectReportPaperArchiving info = new CwProjectReportPaperArchiving();
        BeanUtils.copyProperties(dto, info);

        info.setId(id);
        info.setCreateById(userDTO.getId());
        info.setCreateTime(new Date());
        info.setUpdateById(userDTO.getId());
        info.setUpdateTime(new Date());
        info.setReportId(dto.getId());

        paperArchivingMapper.insert(info);

        return info.getId();
    }


    /**
     * 合同登记修改
     */
    @Transactional(rollbackFor = Exception.class)
    public String update(CwProjectReportData reportData, CwProjectReportPaperArchiving onlineArchiving) throws Exception{
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));
        CwProjectReportPaperArchiving review = new CwProjectReportPaperArchiving();
        BeanUtils.copyProperties(reportData, review);
        review.setUpdateById(userDTO.getId());
        review.setUpdateTime(new Date());
        review.setId(reportData.getPaperArchivingId());

        if (StringUtils.isNotBlank(reportData.getMark())) {
            if (reportData.getMark().equals("1") && reportData.getStatus().equals("5")) {
                //超期归档
                review.setStatus("7");
            }
        }

        paperArchivingMapper.updateById(review);

        return review.getId();
    }
}
