package com.jeeplus.finance.invoice.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.finance.invoice.domain.InvoiceConfirmStatus;
import com.jeeplus.finance.invoice.mapper.InvoiceConfirmStatusMapper;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

@Service
public class InvoiceConfirmStatusService {

    @Resource
    private InvoiceConfirmStatusMapper invoiceConfirmStatusMapper;

    public String save(InvoiceConfirmStatus dto) {
        //UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        try {
            String invoiceIds = dto.getInvoiceId();
            String[] split = invoiceIds.split(",");
            for (String invoiceId : split) {
                QueryWrapper<InvoiceConfirmStatus> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("invoice_id", invoiceId);  //  invoice_id
                //queryWrapper.eq("user_id", userDTO.getId());  // user_id
                InvoiceConfirmStatus invoiceConfirmStatus = invoiceConfirmStatusMapper.selectOne(queryWrapper);
                InvoiceConfirmStatus invoiceConfirmStatus1 = new InvoiceConfirmStatus();
                if (invoiceConfirmStatus == null) {
                    invoiceConfirmStatus1.setStatus(dto.getStatus());
                    invoiceConfirmStatus1.setInvoiceId(invoiceId);
                    invoiceConfirmStatus1.setReimbursementId(dto.getReimbursementId());
                    add(invoiceConfirmStatus1);
                } else {
                    invoiceConfirmStatus1.setStatus(dto.getStatus());
                    invoiceConfirmStatus1.setInvoiceId(invoiceId);
                    update(invoiceConfirmStatus1);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "操作完成";
    }

    public String add(InvoiceConfirmStatus dto) {
        // 获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        // 生成id
        String id = UUID.randomUUID().toString().replace("-", "");
        // 保存用户确认状态数据
        InvoiceConfirmStatus detail = new InvoiceConfirmStatus();
        BeanUtils.copyProperties(dto, detail);
        detail.setId(id);
        detail.setCreateById(userDTO.getId());
        detail.setCreateTime(new Date());
        detail.setUpdateById(userDTO.getId());
        detail.setUpdateTime(new Date());
        detail.setDelFlag(0);
        detail.setUserId(userDTO.getId());
        detail.setReimbursementId(dto.getReimbursementId());
        detail.setInvoiceId(dto.getInvoiceId());
        detail.setStatus(dto.getStatus());
        invoiceConfirmStatusMapper.insert(detail);
        return "操作完成";
    }

    public String update(InvoiceConfirmStatus dto) {
        // 获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        // 保存用户确认状态数据
        InvoiceConfirmStatus detail = new InvoiceConfirmStatus();
        BeanUtils.copyProperties(dto, detail);
        detail.setUpdateById(userDTO.getId());
        detail.setUpdateTime(new Date());
        detail.setStatus(dto.getStatus());
        invoiceConfirmStatusMapper.updateById(detail);
        return "操作完成";
    }

    public List<InvoiceConfirmStatus> getListByUser(InvoiceConfirmStatus invoiceConfirmStatus){
        //UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        QueryWrapper<InvoiceConfirmStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reimbursement_id", invoiceConfirmStatus.getReimbursementId());  // 假设你按 invoice_id 查一条记录
        //queryWrapper.eq("user_id", userDTO.getId());  // 假设你按 invoice_id 查一条记录
        List<InvoiceConfirmStatus> invoiceConfirmStatuses = invoiceConfirmStatusMapper.selectList(queryWrapper);
        return invoiceConfirmStatuses;
    }
}
