package com.nasc.custom.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.nasc.common.core.text.Convert;
import com.nasc.custom.domain.*;
import com.nasc.custom.mapper.*;
import com.nasc.custom.service.INascRecordService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: Yujianq
 * @Date: 2024/6/4 16:36
 * @Description: 审查事项
 * @return:
 */
@Service
public class NascRecordServiceImpl implements INascRecordService {

    @Autowired
    private NascPrintLogMapper printLogMapper;

    @Autowired
    private NascDealRecordMapper dealRecordMapper;

    @Autowired
    private NascManualVerifyMapper manualVerifyMapper;

    @Autowired
    private NascCertificateMapper certificateMapper;

    @Autowired
    private NascCertificateSendMapper certificateSendMapper;

    @Override
    @Transactional
    public int insertPrintLog(NascPrintLog printLog) {
        return printLogMapper.insertPrintLog(printLog);
    }

    @Override
    @Transactional
    public int addDealRecord(NascDealRecord dealRecord) {
        if (ObjectUtils.isNotEmpty(dealRecord.getId()) && dealRecord.getWithStatus() == 1) {
            dealRecordMapper.updateDealRecordStatus(dealRecord);
            return Convert.toInt(dealRecord.getId());
        }
        JSONArray materials = JSON.parseArray(dealRecord.getMaterials());
        List<Long> manualVerifyList = new ArrayList<>();
        materials.forEach(item -> {
            JSONObject material = JSON.parseObject(item.toString());
            if (material.getIntValue("verification") == 1) {
                Long manualVerifyId = material.getLongValue("manualVerifyId");
                manualVerifyList.add(manualVerifyId);
            }
        });
        if (ObjectUtils.isNotEmpty(dealRecord.getId())) {
            dealRecordMapper.updateDealRecord(dealRecord);
        } else {
            dealRecordMapper.insertDealRecord(dealRecord);
        }
        if (manualVerifyList.size() > 0) {
            manualVerifyMapper.updateManualVerifyDealId(dealRecord.getId(), manualVerifyList);
        }
        return Convert.toInt(dealRecord.getId());
    }

    @Override
    public List<Map<String, Object>> selectDealRecordList(NascDealRecord dealRecord) {
        return dealRecordMapper.selectDealRecordList(dealRecord);
    }

    @Override
    public Map<String, Object> getDealRecordById(Long id) {
        return dealRecordMapper.getDealRecordById(id);
    }

    @Override
    public int updateDealRecordStatus(NascDealRecord dealRecord) {
        return dealRecordMapper.updateDealRecordStatus(dealRecord);
    }

    @Override
    public List<Map<String, Object>> selectPrintLogList(NascPrintLog printLog) {
        return printLogMapper.selectPrintLogList(printLog);
    }

    @Override
    @Transactional
    public int addManualVerify(NascManualVerify manualVerify) {
        int result = manualVerifyMapper.insertManualVerify(manualVerify);
        return result > 0 ? Convert.toInt(manualVerify.getId()) : result;
    }

    @Override
    public List<Map<String, Object>> selectManualVerifyList(String serialNumber, Integer dealStatus) {
        return manualVerifyMapper.selectManualVerifyList(serialNumber, dealStatus);
    }

    @Override
    public int updateManualVerify(NascManualVerify manualVerify) {
        return manualVerifyMapper.updateManualVerify(manualVerify);
    }

    @Override
    public List<NascManualVerify> getManualVerifyByIds(List<Long> ids) {
        return manualVerifyMapper.getManualVerifyByIds(ids);
    }

    @Override
    public int insertCertificate(List<NascCertificate> certificateList) {
        return certificateMapper.insertCertificate(certificateList);
    }

    @Override
    public List<NascCertificate> selectCertificateList(NascCertificate certificate) {
        return certificateMapper.selectCertificateList(certificate);
    }

    @Override
    public List<Map<String, Object>> selectCertificateSendList(NascCertificateSend certificateSend) {
        return certificateSendMapper.selectCertificateSendList(certificateSend);
    }

    @Override
    public int addCertificateSend(List<NascCertificateSend> certificateSendList) {
        return certificateSendMapper.insertCertificateSend(certificateSendList);
    }

    @Override
    public int sendCertificate(List<NascCertificateSend> certificateSendList) {
        return certificateSendMapper.updateCertificateSend(certificateSendList, certificateSendList.get(0).getSendUser());
    }

}
