package com.fs.tulin.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fs.tulin.entity.PeriodInfo;
import com.fs.tulin.entity.QwExUserInfo;
import com.fs.tulin.entity.StudentInfo;
import com.fs.tulin.entity.TulinInfoSyncLog;
import com.fs.tulin.mapper.TulinInfoSyncLogMapper;
import com.fs.tulin.service.ITulinInfoSyncLogService;
import com.fs.tulin.utils.ColumnNameEnum;
import com.fs.tulin.utils.PharmacyEnum;
import com.fs.tulin.utils.SyncStudentInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @description: 信息同步Service
 * @author: Guos
 * @time: 2025/10/23 上午11:44
 */
@Slf4j
@Service
@AllArgsConstructor
public class TulinInfoSyncLogServiceImpl implements ITulinInfoSyncLogService {

    @Resource
    private TulinInfoSyncLogMapper tulinInfoSyncLogMapper;

    private final Environment environment;

    private final String PROD_URL = "https://papi.xiangyue.life/api/v1/mp/sync/student?sign=%s&t=%d";

    private final String DEV_URL = "https://api.xiangyue.life/api/v1/mp/sync/student?sign=%s&t=%d";

    private final String PROD_CORPID = "lRz72QOIYBxJW2RBAp2UYe1ckjVpixHL";
    @Override
    public void syncInfo() {
        // 获取当前激活的profile
        String activeProfile = "dev";
        String[] activeProfiles = environment.getActiveProfiles();
        if (activeProfiles.length > 0) {
            activeProfile = activeProfiles[0];
        }
        log.info("当前环境：{}", activeProfile);
        List<QwExUserInfo> qwExUserInfos = tulinInfoSyncLogMapper.selectStudentInfo();
        if(!activeProfile.equals("dev")){
            log.info("线上开始执行数据同步");
            for (QwExUserInfo qwExUserInfo : qwExUserInfos) {
                StudentInfo studentInfo = new StudentInfo();
                BeanUtils.copyProperties(qwExUserInfo, studentInfo);
                studentInfo.setAppid("wx6688e6b9b6fb8700");
                studentInfo.setColumn_name(ColumnNameEnum.GYBNX.getName());
                studentInfo.setPharmacy_id(PharmacyEnum.YJKYSY.getPharmacyId());
                studentInfo.setService_phone("15141712344");
                List<PeriodInfo> periodInfos = tulinInfoSyncLogMapper.selectPeriodInfo(qwExUserInfo.getFs_user_id());
                if(CollectionUtils.isNotEmpty(periodInfos)){
                    studentInfo.setPeriod_name(periodInfos.get(0).getCourseName());
                }
                try {
                    TulinInfoSyncLog tulinInfoSyncLog = SyncStudentInfoService.send(PROD_URL,PROD_CORPID, studentInfo);
                    tulinInfoSyncLog.setFsUserId(qwExUserInfo.getFs_user_id());
                    insert(tulinInfoSyncLog);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        if(activeProfile.equals("dev")) {
            log.info("线下开始执行数据同步");
            for (QwExUserInfo qwExUserInfo : qwExUserInfos) {
                StudentInfo studentInfo = new StudentInfo();
                BeanUtils.copyProperties(qwExUserInfo, studentInfo);
                studentInfo.setAppid("wx6688e6b9b6fb8700");
                studentInfo.setColumn_name(ColumnNameEnum.GYBNX.getName());
                studentInfo.setPharmacy_id(PharmacyEnum.YJKYSY.getPharmacyId());
                studentInfo.setService_phone("15141712344");
                List<PeriodInfo> periodInfos = tulinInfoSyncLogMapper.selectPeriodInfo(qwExUserInfo.getFs_user_id());
                if(CollectionUtils.isNotEmpty(periodInfos)){
                    studentInfo.setPeriod_name(periodInfos.get(0).getCourseName());
                }
                try {
                    TulinInfoSyncLog tulinInfoSyncLog = SyncStudentInfoService.send(PROD_URL,PROD_CORPID, studentInfo);
                    tulinInfoSyncLog.setFsUserId(qwExUserInfo.getFs_user_id());
                    insert(tulinInfoSyncLog);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


    @Override
    public Integer insert(TulinInfoSyncLog tulinInfoSyncLog) {
        TulinInfoSyncLog oldObj = tulinInfoSyncLogMapper.selectByPrimaryKey(tulinInfoSyncLog.getFsUserId());
        if(ObjectUtils.isEmpty(oldObj)){
            return tulinInfoSyncLogMapper.insert(tulinInfoSyncLog);
        }else {
            tulinInfoSyncLog.setUpdateTime(new Date());
            return tulinInfoSyncLogMapper.updateByPrimaryKey(tulinInfoSyncLog);
        }
    }
}
