package com.fs.company.controller.common;

import com.alibaba.fastjson.JSON;
import com.fs.ad.enums.AdUploadType;
import com.fs.common.annotation.DataSource;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.enums.DataSourceType;
import com.fs.common.service.impl.SmsServiceImpl;
import com.fs.company.mapper.*;
import com.fs.company.service.ICompanyService;
import com.fs.company.service.ICompanyUserService;
import com.fs.company.vo.RedPacketMoneyVO;
import com.fs.course.mapper.FsCourseRedPacketLogMapper;
import com.fs.course.service.IFsCourseWatchLogService;
import com.fs.course.service.ITencentCloudCosService;
import com.fs.erp.domain.ErpDeliverys;
import com.fs.erp.domain.ErpOrderQuery;
import com.fs.erp.dto.ErpOrderQueryRequert;
import com.fs.erp.dto.ErpOrderQueryResponse;
import com.fs.erp.service.IErpOrderService;
import com.fs.fastGpt.mapper.FastGptChatSessionMapper;
import com.fs.fastGpt.service.IFastgptEventLogTotalService;
import com.fs.his.config.FsSysConfig;
import com.fs.his.mapper.*;
import com.fs.his.service.*;
import com.fs.his.service.impl.FsPackageOrderServiceImpl;
import com.fs.his.utils.ConfigUtil;
import com.fs.his.utils.qrcode.QRCodeUtils;
import com.fs.hisStore.domain.FsStoreOrderScrm;
import com.fs.im.service.IImService;
import com.fs.im.service.OpenIMService;
import com.fs.qw.service.IQwAppContactWayService;
import com.fs.qw.service.IQwCompanyService;
import com.fs.qw.service.IQwExternalContactTransferLogService;
import com.fs.qw.service.IQwUserService;
import com.fs.qw.vo.AdUploadVo;
import com.fs.qwApi.service.QwApiService;
import com.fs.sop.service.IQwSopTempContentService;
import com.fs.sop.service.IQwSopTempDayService;
import com.fs.sop.service.IQwSopTempRulesService;
import com.fs.sop.service.IQwSopTempService;
import com.fs.system.mapper.SysConfigMapper;
import com.google.zxing.WriterException;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

@RestController
public class Test {
    @Autowired
    private ConfigUtil configUtil;

    @Autowired
    private FsCourseRedPacketLogMapper fsCourseRedPacketLogMapper;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    @Qualifier("erpOrderServiceImpl")
    private IErpOrderService gyOrderService;
    @Autowired
    private FsUserCouponMapper fsUserCouponMapper;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private IFsPrescribeService fsPrescribeService;
    @Autowired
    IFsStoreOrderService fsStoreOrderService;
    @Autowired
    FsStoreOrderMapper fsStoreOrderMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private FsPackageOrderMapper fsPackageOrderMapper;
    @Autowired
    private IFsFollowService fsFollowService;
    @Autowired
    private IFsStoreAfterSalesService fsStoreAfterSalesService;
    //    @Autowired
//    IErpOrderService erpOrderService;
    @Autowired
    FsIntegralOrderMapper integralOrderMapper;
    @Autowired
    IFsInquiryOrderService iFsInquiryOrderService;
    @Autowired
    FsPackageMapper fsPackageMapper;
    @Autowired
    FsStoreProductMapper fsStoreProductMapper;
    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    private CompanyMoneyLogsMapper moneyLogsMapper;
    @Autowired
    FsUserMapper fsUserMapper;
    @Autowired
    IFsUserIntegralLogsService fsUserIntegralLogsService;
    @Autowired
    SmsServiceImpl smsService;
    @Autowired
    FsStoreOrderItemMapper fsStoreOrderItemMapper;

    @Autowired
    FsPatientMapper fsPatientMapper;
    @Autowired
    FsPrescribeDrugMapper fsPrescribeDrugMapper;
    @Autowired
    FsFollowMapper fsFollowMapper;
    @Autowired
    private IImService imService;
    @Autowired
    private FsInquiryOrderMapper inquiryOrderMapper;
    @Autowired
    private CompanyVoiceCallerMapper companyVoiceCallerMapper;
    @Autowired
    private CompanyVoiceLogsMapper companyVoiceLogsMapper;
    @Autowired
    FsPackageOrderServiceImpl packageOrderService;
    @Autowired
    private IFsStoreOrderLogsService fsStoreOrderLogsService;
    org.slf4j.Logger logger= LoggerFactory.getLogger(getClass());
    @Autowired
    IFsDoctorService doctorService;
    @Autowired
    CompanyUserMapper companyUserMapper;
    @Autowired
    FsInquiryOrderMapper fsInquiryOrderMapper;
    @Autowired
    QwApiService qwApiService;
    //每天执行一次
    @Autowired
    IQwAppContactWayService qwAppContactWayService;

    @Autowired
    FastGptChatSessionMapper fastGptChatSessionMapper;

    @Autowired
    IQwExternalContactTransferLogService qwExternalContactTransferLogService;



    @Autowired
    ITencentCloudCosService tencentCloudCosService;


    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    @Qualifier("wdtErpOrderServiceImpl")
    private IErpOrderService wdtOrderService;
    @Autowired
    @Qualifier("hzOMSErpOrderServiceImpl")
    private IErpOrderService hzOMSErpOrderService;
    @Autowired
    @Qualifier("dfOrderServiceImpl")
    private IErpOrderService dfOrderService;

    @Autowired
    @Qualifier("JSTErpOrderServiceImpl")
    private IErpOrderService jSTOrderService;
    @Autowired
    @Qualifier("k9OrderScrmServiceImpl")
    private IErpOrderService k9OrderService;
    @Autowired
    IErpOrderService erpOrderService;
    @GetMapping("test")
    public void fileDownload() {
        deliveryOpScrm();
    }
    private IErpOrderService getErpService() {

        FsSysConfig sysConfig = configUtil.getSysConfig();
        Integer erpOpen = sysConfig.getErpOpen();
        if (erpOpen != null && erpOpen == 1){
            //判断erp类型
            Integer erpType = sysConfig.getErpType();
            if (erpType != null){
                IErpOrderService erpOrderService = null;
                if (erpType == 1){
                    //管易
                    erpOrderService =  gyOrderService;
                } else if (erpType == 2){
                    //旺店通
                    erpOrderService =  wdtOrderService;
                } else if (erpType == 3){
                    //
                    erpOrderService =  hzOMSErpOrderService;
                } else if (erpType == 4){
                    //代服
                    erpOrderService =  dfOrderService;
                }else if(erpType == 5){
                    erpOrderService=jSTOrderService;
                }else if(erpType == 6){
                    erpOrderService=k9OrderService;
                }
                return erpOrderService;


            }
        }
        return null;
    }
    public void deliveryOpScrm()
    {
        System.out.println("deliveryOpScrm");
        IErpOrderService erpOrderService = getErpService();
        List<FsStoreOrderScrm> orders = null;
        if (erpOrderService == gyOrderService){
            orders = fsStoreOrderMapper.selectOmsOrderdeliveryOpScrm();
        } else if (erpOrderService == wdtOrderService || erpOrderService == dfOrderService || erpOrderService == jSTOrderService){
            orders = fsStoreOrderMapper.selectWdtOmsOrderdeliveryOpScrm();
        }
        logger.info("查询到的"+orders.size());
        for(FsStoreOrderScrm order:orders){
            ErpOrderQueryRequert request=new ErpOrderQueryRequert();
            request.setCode(order.getExtendOrderId());
            if (erpOrderService != null){
                ErpOrderQueryResponse response=erpOrderService.getOrder(request);
                if (erpOrderService != dfOrderService){
                    if(response.getOrders()!=null&&response.getOrders().size()>0){
                        for(ErpOrderQuery orderQuery : response.getOrders()){
                            if(orderQuery.getDeliverys()!=null&&orderQuery.getDeliverys().size()>0){
                                for(ErpDeliverys delivery:orderQuery.getDeliverys()){
                                    if(delivery.getDelivery()&& StringUtils.isNotEmpty(delivery.getMail_no())){
                                        //更新商订单状态 删除REDIS
                                        logger.info("查询到的"+delivery);

                                        fsStoreOrderService.deliveryOrderScrm(order.getOrderCode(),delivery.getMail_no(),delivery.getExpress_code(),delivery.getExpress_name());
                                        redisCache.deleteObject("delivery"+":"+order.getExtendOrderId());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}
