package com.fs.app.controller;

import com.alibaba.fastjson.JSON;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.StringUtils;
import com.fs.course.mapper.FsCourseWatchLogMapper;
import com.fs.course.param.FsCourseWatchLogByFinishTimeParam;
import com.fs.qw.domain.QwCompany;
import com.fs.qw.domain.QwUser;
import com.fs.qw.mapper.QwCompanyMapper;
import com.fs.qw.mapper.QwUserMapper;
import com.fs.qw.param.QwExternalContactAddTagParam;
import com.fs.qw.service.IQwExternalContactService;
import com.fs.qw.service.IQwUserService;
import com.fs.qw.vo.QwSopRuleTimeVO;
import com.fs.qwApi.param.QwSendMsgParam;
import com.fs.qwApi.service.QwApiService;
import com.fs.sop.domain.SopUserLogs;
import com.fs.sop.domain.SopUserLogsInfo;
import com.fs.sop.mapper.QwSopMapper;
import com.fs.sop.mapper.SopUserLogsInfoMapper;
import com.fs.sop.params.QwSopAutoByTags;
import com.fs.sop.params.SopUserLogsParamByDate;
import com.fs.sop.service.IQwSopLogsService;
import com.fs.sop.service.ISopUserLogsService;
import com.fs.voice.utils.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.codehaus.jettison.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
@RequestMapping(value="/app/test")
public class testCourseController {

    @Autowired
    RedisCache redisCache;

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Autowired
    private IQwSopLogsService qwSopLogsService;

    @Autowired
    private QwSopMapper qwSopMapper;

    @Autowired
    private FsCourseWatchLogMapper fsCourseWatchLogMapper;

    @Autowired
    private ISopUserLogsService sopUserLogsService;

    @Autowired
    private SopUserLogsInfoMapper sopUserLogsInfoMapper;

    @Autowired
    private IQwExternalContactService contactService;

    @Autowired
    private IQwUserService iQwUserService;

    @Autowired
    private TestCourseService testService;

    @Autowired
    QwUserMapper qwUserMapper;

    @Autowired
    QwCompanyMapper qwCompanyMapper;

    @Autowired
    QwApiService qwApiService;

    @GetMapping("/test1")
    public R test1() {

        String msg="<font color=\"warning\">您的云主机，企业微信【已经退出登录】！！望悉知~</font>";

        QwUser qwUserByAppKey = qwUserMapper.selectQwUserByAppKey("6563");

        QwSendMsgParam sendMsgParam = new QwSendMsgParam();
        QwCompany qwCompany = qwCompanyMapper.selectQwCompanyByCorpId(qwUserByAppKey.getCorpId());
        sendMsgParam.setAgentid(Integer.parseInt(qwCompany.getServerAgentId().trim()));
        sendMsgParam.setTouser(qwUserByAppKey.getQwUserId());

        QwSendMsgParam.Markdown markdown = new QwSendMsgParam.Markdown();
        markdown.setContent(msg);

        sendMsgParam.setMarkdown(markdown);
        sendMsgParam.setMsgtype("markdown");
        qwApiService.sendMsg(sendMsgParam, qwCompany.getCorpId());
//        List<String> combinedTags = new ArrayList<>();
//        combinedTags.add("et7tWFCgAAvIJDB-xSXUDpUIukUwqRrA");
//        combinedTags.add("etfFKfDQAAS4NYg9yWCx6-KCF9V8Az2g");
//        combinedTags.add("etfFKfDQAAIWzaju4DodqIc1ZJAyxJfw");
//
//        //分时段里符合的标签-再用来创建自动SOP
//        QwSopAutoByTags qwSopAutoByTags=new QwSopAutoByTags();
//        qwSopAutoByTags.setQwUserId("8799");
//        qwSopAutoByTags.setCorpId("ww5a88c4f879f204c5");
//        qwSopAutoByTags.setTagsIdsSelectList(combinedTags);
//        qwSopAutoByTags.setSendType(2);
//        List<QwSopRuleTimeVO> qwSopRuleTimeVOS = qwSopMapper.selectQwSopAutoByTagsByForeach(qwSopAutoByTags);

        return  R.ok();
    }

    @GetMapping("/text3")
    public R text3() throws JSONException {
        List<Long> list1 = new ArrayList<>();
        list1.add(7234165L);

        List<String> listTag = new ArrayList<>();
        listTag.add("et7tWFCgAAHZOsxTFXpJ3vaFtmv87CqQ");
        listTag.add(" et7tWFCgAAkco8tquT4V3pLmQR6v6-aQ");
        listTag.add(" et7tWFCgAA2lmDrcj6bqRvEHgOccN0Mw");

        QwExternalContactAddTagParam param=new QwExternalContactAddTagParam();
        param.setUserIds(list1);
        param.setTagIds(listTag);
        param.setCorpId("ww5a88c4f879f204c5");
        contactService.addUserTag(param);

        return  R.ok();
    }

    @GetMapping("/text2")
    public R text2() {


        return  R.ok();
    }

    /**
    * 一键导入授权码
    */
    @PostMapping("/importExcel")
    public R importExcel(@RequestParam("file") MultipartFile file) throws IOException {

        R r = null;


        List<String> appkeyList=new ArrayList<>();
        List<String> appkeyUnNow=new ArrayList<>();

        if (file.isEmpty()) {
            throw new IllegalArgumentException("File is empty!");
        }

        // 创建工作簿对象
        Workbook workbook = new XSSFWorkbook(file.getInputStream());

        // 获取第一个工作表
        Sheet sheet = workbook.getSheetAt(0);

        // 遍历每一行
        for (Row row : sheet) {
            List<String> rowData = new ArrayList<>();

            // 获取A列（第1列）
            Cell cellA = row.getCell(0);
            String ip = getCellValueAsString(cellA);

            // 获取B列（第2列）
            Cell cellB = row.getCell(5);
            String appKey = getCellValueAsString(cellB);


            if (!StringUtil.strIsNullOrEmpty(appKey)
                    && !("UNKNOWN".equals(appKey))
                    && appKey.matches("\\d+")) {  // 只允许纯数字

//                System.out.println(ip + "-----------" + appKey);

                r= iQwUserService.qwBindCloudHostByIp2(appKey, ip,appkeyList,appkeyUnNow);

            }

        }

        // 关闭工作簿
        workbook.close();
        return  r;
    }
    /**
     * 将单元格的值转换为字符串
     */
    private String getCellValueAsString(Cell cell) {
        if (cell == null) {
            return ""; // 如果单元格为空，返回空字符串
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString();
                } else {
                    return String.valueOf(cell.getNumericCellValue());
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            default:
                return "UNKNOWN";
        }
    }
}
