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.CwProjectReportOnlineArchiving;
import com.jeeplus.finance.projectReport.domain.CwProjectReportSignAndIssue;
import com.jeeplus.finance.projectReport.mapper.CwProjectReportOnlineArchivingMapper;
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-26 14:37
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Component
public class CwProjectReportOnlineArchivingService extends ServiceImpl<CwProjectReportOnlineArchivingMapper, CwProjectReportOnlineArchiving> {

    @Resource
    private CwProjectReportOnlineArchivingMapper onlineArchivingMapper;

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

    public CwProjectReportData queryByReportId(String id) {
        CwProjectReportOnlineArchiving signAndIssue = onlineArchivingMapper.selectOne(new QueryWrapper<CwProjectReportOnlineArchiving>().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())){

            CwProjectReportOnlineArchiving onlineArchiving = onlineArchivingMapper.selectById(reportData.getOnlineArchivingId());
            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 ( ));
        //保存基本信息
        CwProjectReportOnlineArchiving info = new CwProjectReportOnlineArchiving();
        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());

        onlineArchivingMapper.insert(info);

        return info.getId();
    }


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

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

        return review.getId();
    }
}
