package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.ruoyi.system.common.ApiResult;
import com.ruoyi.system.common.ReturnResult;
import com.ruoyi.system.constant.ApiConstant;
import com.ruoyi.system.mapper.VoucherMapper;
import com.ruoyi.system.model.VoucherQueryRequestModel;
import com.ruoyi.system.model.VoucherQueryResponseModel;
import com.ruoyi.system.model.voucher.VoucherAuxiliary;
import com.ruoyi.system.model.voucher.VoucherBody;
import com.ruoyi.system.model.voucher.VoucherImportModel;
import com.ruoyi.system.service.VoucherQueryService;
import com.ruoyi.system.support.VoucherQuerySupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;

import java.util.List;


/**
 * @Author jiuli
 * @Date 2024/2/26
 */
@Service
public class VoucherQueryServiceImpl implements VoucherQueryService {

    private static final Logger logger = LoggerFactory.getLogger(VoucherQueryServiceImpl.class);

    private VoucherMapper voucherMapper;

    public VoucherQueryServiceImpl( VoucherMapper voucherMapper) {
        this.voucherMapper = voucherMapper;
    }

    @Override
    @Transactional
    public ApiResult save(VoucherQueryRequestModel voucherQueryRequestModel) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ReturnResult result = new ReturnResult();
        //ReturnResult result = VoucherUtils.callVoucherQuery(voucherQueryRequestModel, ApiConstant.VOUCHER_LIST_QUERY);
        JSONArray jsonArray = JSON.parseArray(result.getData().toString());
        List<VoucherQueryResponseModel> voucherQueryResponseModelList = jsonArray.toJavaList(VoucherQueryResponseModel.class);
        //凭证头
        List<VoucherImportModel> voucherImportModelList = VoucherQuerySupport.getVoucherImportModel(voucherQueryResponseModelList);
        //凭证分录信息
        List<VoucherBody> voucherBodyList = VoucherQuerySupport.getVoucherBody(voucherQueryResponseModelList);
        //辅助核算项
        List<VoucherAuxiliary> auxiliaryModelList = VoucherQuerySupport.getAuxiliaryModel(voucherQueryResponseModelList);
        List<String> headIdList = VoucherQuerySupport.getHeadIdList(voucherQueryResponseModelList);
        List<String> bodyIdList = VoucherQuerySupport.getBodyIdList(voucherQueryResponseModelList);
        //删掉id重复的数据
        voucherMapper.deleteHeadList(headIdList);
        voucherMapper.deleteBodyList(headIdList);
        voucherMapper.deleteAxiliarylist(bodyIdList);
        //凭证头
        List<List<VoucherImportModel>> headList = Lists.partition(voucherImportModelList, 1000);
        for (List<VoucherImportModel> voucherImportModels : headList) {
            voucherMapper.saveHeadBatch(voucherImportModels);
        }
        //凭证分录信息
        List<List<VoucherBody>> bodyList = Lists.partition(voucherBodyList, 1000);
        for (List<VoucherBody> voucherBodies : bodyList) {
            voucherMapper.saveBodyBatch(voucherBodies);
        }
        //辅助核算项
        List<List<VoucherAuxiliary>> auxiliaryList = Lists.partition(auxiliaryModelList, 1000);
        for (List<VoucherAuxiliary> voucherAuxiliaries : auxiliaryList) {
            voucherMapper.saveAuxiliaryList(voucherAuxiliaries);
        }
        stopWatch.stop();
        logger.error("消耗时间:" + stopWatch.getLastTaskTimeMillis());
        return new ApiResult(200, "操作成功!");
    }
}
