package com.xhpc.tradebill.service.impl;

import com.aliyun.oss.OSSClient;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.tradebill.config.AliOSSProperties;
import com.xhpc.tradebill.domain.XhpcTradebillInternetCheckRecordDomain;
import com.xhpc.tradebill.domain.XhpcTradebillPaymentCheckRecordDomain;
import com.xhpc.tradebill.domain.XhpcTradebillUploadRecordDomain;
import com.xhpc.tradebill.mapper.XhpcTradebillUploadRecordMapper;
import com.xhpc.tradebill.service.CommonService;
import com.xhpc.tradebill.service.InternetBillService;
import com.xhpc.tradebill.service.PaymentBillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class CommonServiceImpl implements CommonService {

    @Resource
    XhpcTradebillUploadRecordMapper uploadRecordMapper;

    @Resource
    PaymentBillService paymentBillService;

    @Resource
    InternetBillService internetBillService;

    @Resource
    AliOSSProperties ossProperties;

    @Override
    public XhpcTradebillUploadRecordDomain ossUpload(XhpcTradebillUploadRecordDomain domain) throws Exception{
        Long userId = SecurityUtils.getUserId();
        domain.setCreateBy(userId);
        domain.setUpdateBy(userId);
        domain.setSource("上传");
        domain.setFileName(domain.getFileName());
        domain.setStatus(0);
        domain.setDelFlag(0);
        domain.setSuccessCount(0);
        domain.setFailCount(0);
        domain.setUncheckCount(0);
        domain.setCheckedCount(0);
        uploadRecordMapper.insertSelective(domain);

        XhpcTradebillUploadRecordDomain resDomain = uploadRecordMapper.selectByUrl(domain.getUrl());
        List<XhpcTradebillPaymentCheckRecordDomain> paymentCheckRecordDomainList = null;
        List<XhpcTradebillInternetCheckRecordDomain> recordDomainList = null;
        Map<String, Integer> totalMap = new HashMap<>();
        int insertCount = 0;
        resDomain.setStatus(2);
        switch (domain.getType()){
            case 1:
            case 2:
                // 微信支付
                paymentCheckRecordDomainList = paymentBillService.getWechatDataList(domain.getUrl());
                insertCount = paymentBillService.insertPaymentCheckList(resDomain.getId(), paymentCheckRecordDomainList);
                totalMap = computePaymentRecord(paymentCheckRecordDomainList);
                break;
            case 3:
            case 4:
                // 支付宝支付
                paymentCheckRecordDomainList = paymentBillService.getAliPayDataList(domain.getUrl());
                insertCount = paymentBillService.insertPaymentCheckList(resDomain.getId(), paymentCheckRecordDomainList);
                totalMap = computePaymentRecord(paymentCheckRecordDomainList);
                break;
            case 5:
                // 恒大
                recordDomainList = internetBillService.getHdInternetDataList(domain.getUrl());
                insertCount = internetBillService.insertInternetCheckList(resDomain.getId(), recordDomainList);
                totalMap = computeInternetRecord(recordDomainList);
                break;
            case 6:
                // 小桔
                recordDomainList = internetBillService.getXjInternetDataList(domain.getUrl());
                insertCount = internetBillService.insertInternetCheckList(resDomain.getId(), recordDomainList);
                totalMap = computeInternetRecord(recordDomainList);
                break;
            case 7:
                // 新电途
                recordDomainList = internetBillService.getXDTInternetDataList(domain.getUrl());
                insertCount = internetBillService.insertInternetCheckList(resDomain.getId(), recordDomainList);
                totalMap = computeInternetRecord(recordDomainList);
                break;
            case 8:
                // 快电
                recordDomainList = internetBillService.getKDInternetDataList(domain.getUrl());
                insertCount = internetBillService.insertInternetCheckList(resDomain.getId(), recordDomainList);
                totalMap = computeInternetRecord(recordDomainList);
                break;
            default:
                resDomain.setStatus(1);
                totalMap.put("successCount", 0);
                totalMap.put("failCount", 0);
                totalMap.put("checkCount", 0);
                totalMap.put("uncheckCount", 0);
                break;
        }
        resDomain.setStatus(1);
        resDomain.setSuccessCount(totalMap.get("successCount"));
        resDomain.setFailCount(totalMap.get("failCount"));
        resDomain.setCheckedCount(totalMap.get("checkCount"));
        resDomain.setUncheckCount(totalMap.get("uncheckCount"));
        uploadRecordMapper.updateByPrimaryKey(resDomain);
       return resDomain;
    }



    @Override
    public XhpcTradebillUploadRecordDomain fileUpload(MultipartFile file, int type) throws Exception{
        XhpcTradebillUploadRecordDomain domain = new XhpcTradebillUploadRecordDomain();
        // 创建OSSClient实例
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
        File dataFile = File.createTempFile("temp_", suffix);

        file.transferTo(dataFile);

        String ossPath =  "bill/" + file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")) + "_" +System.currentTimeMillis() + "." + suffix;
        OSSClient ossClient = new OSSClient(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getSecretKey());
        // 上传文件流
        ossClient.putObject(ossProperties.getBucketName(), ossPath, dataFile);
        ossClient.shutdown();

        domain.setFileName(file.getOriginalFilename());
        domain.setUrl(ossProperties.getDomain() + ossPath);
        domain.setType(type);

        return ossUpload(domain);
    }

    @Override
    public List<XhpcTradebillUploadRecordDomain> getFilePage(Map<String, Object> params){

        return uploadRecordMapper.selectByType(params);
    }


    private Map<String, Integer> computePaymentRecord(List<XhpcTradebillPaymentCheckRecordDomain> domainList){
        Integer successCount = 0;
        Integer failCount = 0;
        Integer checkCount = 0;
        Integer uncheckCount = 0;
        Map<String, Integer> result = new HashMap<>();
        for (XhpcTradebillPaymentCheckRecordDomain domain: domainList){
            if(domain.getStatus() == 2){
                successCount++; checkCount++;
            } else if(domain.getStatus() == 1 || domain.getStatus() == 0){
                failCount++; checkCount++;
            } else {
                uncheckCount++;
            }
        }
        result.put("successCount", successCount);
        result.put("failCount", failCount);
        result.put("checkCount", checkCount);
        result.put("uncheckCount", uncheckCount);
        return result;
    }


    private Map<String, Integer> computeInternetRecord(List<XhpcTradebillInternetCheckRecordDomain> domainList){
        Integer successCount = 0;
        Integer failCount = 0;
        Integer checkCount = 0;
        Integer uncheckCount = 0;
        Map<String, Integer> result = new HashMap<>();
        for (XhpcTradebillInternetCheckRecordDomain domain: domainList){
            if(domain.getStatus() == 2){
                successCount++; checkCount++;
            } else if(domain.getStatus() == 1 || domain.getStatus() == 0){
                failCount++; checkCount++;
            } else {
                uncheckCount++;
            }
        }
        result.put("successCount", successCount);
        result.put("failCount", failCount);
        result.put("checkCount", checkCount);
        result.put("uncheckCount", uncheckCount);
        return result;
    }
}
