package com.fs.app.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fs.app.service.WxTaskService;
import com.fs.company.domain.CompanyVoiceRoboticCallLogCallphone;
import com.fs.company.service.ICompanyVoiceRoboticCallLogCallphoneService;
import com.fs.company.service.ICompanyWxAccountService;
import com.fs.company.service.impl.CompanyWxServiceImpl;
import com.fs.company.vo.CdrDetailVo;
import com.fs.crm.service.ICrmCustomerPropertyService;
import com.fs.wxcid.dto.message.SendImageMessageParam;
import com.fs.wxcid.dto.message.SendVideoMessageParam;
import com.fs.wxcid.service.MessageService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@Api("公共接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/app/common")
public class CommonController {

    private final WxTaskService taskService;
    private final ICompanyWxAccountService companyWxAccountService;

    @GetMapping("initAccountNum")
    public void initAccountNum() {
        taskService.initAccountNum();
    }

    @GetMapping("initAccountMsg")
    public void initAccountMsg() {
        taskService.initAccountMsg();
    }

    @GetMapping("addWx")
    public void addWx(Long accountId) {
        taskService.addWx(Collections.singletonList(accountId));
    }

    @GetMapping("isCheckContact")
    public void isCheckContact(String formUser, Long accountId) {
        companyWxAccountService.isCheckContact(formUser, accountId);
    }

    @GetMapping("cellRun")
    public void cellRun() {
        taskService.cellRun();
    }

    @GetMapping("callNextTask")
    public void callNextTask() {
        taskService.callNextTask();
    }

    @GetMapping("addWx4Workflow")
    public void addWx4Workflow(Long accountId) {
        taskService.addWx4Workflow(Collections.singletonList(accountId));
    }

    @GetMapping("cidWorkflowAddWxRun")
    public void cidWorkflowAddWxRun() {
        taskService.cidWorkflowAddWxRun();
    }


    @Autowired
    CompanyWxServiceImpl ccompanyWxService;
    @Autowired
    MessageService messageService;

    @GetMapping("mockAddWxSuccess/{clientId}")
    public void mockAddWxSuccess(@PathVariable("clientId") Long clientId) {
        ccompanyWxService.mockInterface(clientId);
    }

    @GetMapping("sendImg")
    public void mockAddWxSuccess(Long accountId, String imgUrl, String toUser) {
        SendImageMessageParam param = new SendImageMessageParam();
        param.setAccountId(accountId);
        param.setImgUrl(imgUrl);
        param.setToUser(toUser);
        messageService.sendImageMessage(param);
    }

    @GetMapping("sendVideo")
    public void sendVideo(Long accountId, String thumbUrl, String videoUrl, String toUser) {
        SendVideoMessageParam param = new SendVideoMessageParam();
        param.setAccountId(accountId);
        param.setThumbUrl(thumbUrl);
        param.setVideoUrl(videoUrl);
        param.setToUser(toUser);
        messageService.sendVideoMessage(param);
    }

    @PostMapping("/saveCdrTest")
    public String saveCdrTest(@RequestBody CdrDetailVo cdr) throws InstantiationException, IllegalAccessException {
        log.info("cdr=" + cdr);
        return  "success";
    }

    @Autowired
    private ICrmCustomerPropertyService crmCustomerPropertyService;
    @Autowired
    private ICompanyVoiceRoboticCallLogCallphoneService companyVoiceRoboticCallLogCallphoneService;

    @PostMapping("/testProperty")
    public void testProperty() throws JsonProcessingException {
        log.info("开始解析");
        CompanyVoiceRoboticCallLogCallphone byId = companyVoiceRoboticCallLogCallphoneService.getById(251L);
        List<Map<String, Object>> maps = new ObjectMapper().readValue(
                byId.getContentList(),
                new TypeReference<List<Map<String, Object>>>() {
                }
        );
        log.info("解析数据：{}", maps);
        crmCustomerPropertyService.addPropertyByCallLog(byId);
    }

}
