package org.dromara.payment.taxRecords.service.impl;

import cn.hutool.core.date.DateUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.payment.api.worker.RemoteUserWokerService;
import org.dromara.payment.api.worker.domain.RemoteResultZip;
import org.dromara.payment.api.worker.domain.RemoteUserWorkerZipVo;
import org.dromara.payment.sys.domain.BusDownloads;
import org.dromara.payment.sys.mapper.BusDownloadsMapper;
import org.dromara.payment.taxRecords.domain.MerTaxRecords;
import org.dromara.payment.taxRecords.domain.vo.MerTaxRecordsVo;
import org.dromara.payment.taxRecords.mapper.MerTaxRecordsDetailMapper;
import org.dromara.payment.taxRecords.mapper.MerTaxRecordsMapper;
import org.dromara.payment.worker.domain.UserWorker;
import org.dromara.payment.worker.mapper.UserWorkerMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author sunzexing
 * @version 1.0
 * @title AsyncTaxRecordsService
 * @description
 * @create 2024-05-06 19:37
 */

@Service
public class AsyncTaxRecordsService {


    @Resource
    private MerTaxRecordsMapper merTaxRecordsMapper;


    @Resource
    private MerTaxRecordsDetailMapper  merTaxRecordsDetailMapper;

    @Resource
    private UserWorkerMapper userWorkerMapper;


    @Resource
    private BusDownloadsMapper busDownloadsMapper;


    @DubboReference
    private RemoteUserWokerService remoteUserWokerService;


    /****
     * 根据用工者ID，生成用工着身份证信息
     * @param workIds
     * @throws Exception
     */
    @Async
    public void createSfzZipByUserInfos(List<Long> workIds,Long createBy,Integer busType)throws Exception{

        List< UserWorker> uw = this.userWorkerMapper.selectBatchIds(workIds);
        List<RemoteUserWorkerZipVo>  zipUserVoList = new ArrayList();
        for(UserWorker u : uw){
            RemoteUserWorkerZipVo ruz = new RemoteUserWorkerZipVo();
            ruz.setWorkId(u.getId());
            ruz.setWorkName(u.getName());
            ruz.setCardBack(u.getCardBack());
            ruz.setCardPositive(u.getCardPositive());
            zipUserVoList.add(ruz);
        }
        RemoteResultZip result = this.remoteUserWokerService.createWorkerSfzZip(zipUserVoList);
        if(result != null ){

            BusDownloads  busDownloads = new BusDownloads();
            busDownloads.setOssId(result.getOssId());
            busDownloads.setBusType(busType);
            busDownloads.setCreateBy(createBy);
            busDownloads.setUrl(result.getUrl());
            busDownloads.setCreateTime(new Date());
            this.busDownloadsMapper.insert(busDownloads);
        }
    }


    /***
     * 获取身份证压缩包
     * @return
     */
    @Async
    public void getSfzZip(Long trId)throws Exception{

      List<RemoteUserWorkerZipVo> userList =   merTaxRecordsDetailMapper.selTaxRecordWorkInfo(trId);
      RemoteResultZip result = this.remoteUserWokerService.createWorkerSfzZip(userList);

      if(result != null && StringUtils.isNotEmpty(result.getUrl())){
          MerTaxRecords  merTaxRecords = new MerTaxRecords();
          merTaxRecords.setId(trId);
          merTaxRecords.setSfzZip(result.getUrl());
          this.merTaxRecordsMapper.updateById(merTaxRecords);
      }
    }



    /***
     * 获取合同压缩包
     * @return
     */
    @Async
    public void getHtZip(Long trId)throws Exception{

        MerTaxRecordsVo vo =  this.merTaxRecordsMapper.selectVoById(trId);
        Date startTime = DateUtil.beginOfMonth(DateUtil.parse(vo.getMonth()+"-01")).toJdkDate();
        Date endTime = DateUtil.endOfMonth(DateUtil.parse(vo.getMonth()+"-01")).toJdkDate();

        Map paraMap = new HashMap();
        paraMap.put("merId",vo.getMerId());
        paraMap.put("startTime",startTime);
        paraMap.put("endTime",endTime);
        paraMap.put("trId",trId);

        List<RemoteUserWorkerZipVo> userList =   merTaxRecordsDetailMapper.selTaxRecordWorkHtInfo(paraMap);
        RemoteResultZip result = this.remoteUserWokerService.createWorkerHtZip(userList);

        if( result!= null && StringUtils.isNotEmpty(result.getUrl())){
            MerTaxRecords  merTaxRecords = new MerTaxRecords();
            merTaxRecords.setId(trId);
            merTaxRecords.setHtZip(result.getUrl());
            this.merTaxRecordsMapper.updateById(merTaxRecords);
        }
    }



    /***
     * 获取支付凭证
     * @return
     */
    @Async
    public void getPayCerZip(Long trId)throws Exception{

        MerTaxRecordsVo vo =  this.merTaxRecordsMapper.selectVoById(trId);
        Date startTime = DateUtil.beginOfMonth(DateUtil.parse(vo.getMonth()+"-01")).toJdkDate();
        Date endTime = DateUtil.endOfMonth(DateUtil.parse(vo.getMonth()+"-01")).toJdkDate();

        Map paraMap = new HashMap();
        paraMap.put("merId",vo.getMerId());
        paraMap.put("startTime",startTime);
        paraMap.put("endTime",endTime);
        paraMap.put("trId",trId);

        List<RemoteUserWorkerZipVo> userList =   merTaxRecordsDetailMapper.selTaxRecordWorkPayCerInfo(paraMap);
        RemoteResultZip result =  this.remoteUserWokerService.createWorkerPayCerZip(userList);

        if(result != null &&StringUtils.isNotEmpty(result.getUrl())){
            MerTaxRecords  merTaxRecords = new MerTaxRecords();
            merTaxRecords.setId(trId);
            merTaxRecords.setPayZip(result.getUrl());
            this.merTaxRecordsMapper.updateById(merTaxRecords);
        }
    }

}
