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.CwProjectReportSignAndIssue;
import com.jeeplus.finance.projectReport.domain.CwProjectReviewNew;
import com.jeeplus.finance.projectReport.domain.CwProjectReviewNewTeam;
import com.jeeplus.finance.projectReport.mapper.CwProjectReportSignAndIssueMapper;
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.http.ResponseEntity;
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.List;
import java.util.UUID;

/**
 * @author 王强
 * @version 1.0
 * @date 2024-01-25 14:00
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Component
public class CwProjectReportSignAndIssueService extends ServiceImpl<CwProjectReportSignAndIssueMapper, CwProjectReportSignAndIssue> {

    @Resource
    private CwProjectReportSignAndIssueMapper signAndIssueMapper;

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

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

            CwProjectReportSignAndIssue signAndIssue = signAndIssueMapper.selectById(reportData.getSignAndIssueId());
            if (signAndIssue != null){
                reportData.setCreateBy(reportData.getCreateBy());
                return update(reportData,signAndIssue);
            }
        }
        return add(reportData);

    }

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

        signAndIssueMapper.updateById(review);

        return review.getId();
    }

    public String add(CwProjectReportData dto) throws Exception{


        //生成主键id值
        String id = UUID.randomUUID().toString().replace("-", "");
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));
        //保存基本信息
        CwProjectReportSignAndIssue info = new CwProjectReportSignAndIssue();
        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());

        signAndIssueMapper.insert(info);

        return info.getId();
    }

    public ResponseEntity deleteById(String id) {
        signAndIssueMapper.deleteById(id);
        return ResponseEntity.ok ("删除成功");
    }
}
