package com.fs.his.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fs.common.core.domain.R;
import com.fs.common.service.ISmsService;
import com.fs.company.domain.CompanySmsLogs;
import com.fs.company.service.ICompanySmsLogsService;
import com.fs.sms.domain.SmsDeliver;
import com.fs.sms.domain.SmsReport;
import com.fs.sms.domain.TemplateState;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@RestController
@RequestMapping("/his/sms")
public class FSSmsCommonController {
    @Autowired
    private ICompanySmsLogsService smsLogsService;
    @Autowired
    private ISmsService smsService;
    org.slf4j.Logger logger= LoggerFactory.getLogger(getClass());
    @PostMapping(path = "/deliver" )
    public Object deliver(@RequestParam String  deliver) throws Exception {
        logger.info("短信回复状态:"+deliver);
        SmsDeliver smsDeliver = JSON.parseObject(deliver, SmsDeliver.class);
        List<SmsDeliver.DeliveryInfo> delivers = smsDeliver.getDelivers();
        for (SmsDeliver.DeliveryInfo deliveryInfo : delivers) {
            CompanySmsLogs companySmsLogs = smsLogsService.selectCompanySmsLogsByMobile(deliveryInfo.getPhone());

            if (companySmsLogs!=null){
                if (companySmsLogs.getReplyContent()!=null){
                    companySmsLogs.setReplyContent(companySmsLogs.getReplyContent()+","+deliveryInfo.getContent());
                }else {
                    companySmsLogs.setReplyContent(deliveryInfo.getContent());
                }

                smsLogsService.updateCompanySmsLogs(companySmsLogs);
            }
        }
        return JSON.parseObject("{\"status\":\"success\"}");
    }


    @PostMapping(path = "/report" )
    public Object report(@RequestParam String report) throws Exception {
        logger.info("短信通知状态:"+report);
        SmsReport smsReport = JSON.parseObject(report, SmsReport.class);
        List<SmsReport.Report> reports = smsReport.getReports();
        for (SmsReport.Report deliveryInfo : reports) {
            CompanySmsLogs companySmsLogs = smsLogsService.selectCompanySmsLogsByMid(deliveryInfo.getMsgid());
            if (companySmsLogs!=null){
                if (deliveryInfo.getStatus().equals("0")){
                    companySmsLogs.setStatus(1);
                }else {
                    companySmsLogs.setStatus(2);
                }

                smsLogsService.updateCompanySmsLogs(companySmsLogs);
            }
        }
        return JSON.parseObject("{\"status\":\"success\"}");
    }

    @PostMapping(path = "/templateState" )
    public Object templateState(@RequestParam String templateState) throws Exception {
        logger.info("短信模板状态:"+templateState);
        TemplateState state = JSON.parseObject(templateState, TemplateState.class);
        return JSON.parseObject("{\"status\":\"success\"}");
    }



    @PostMapping("/notify")
    public  String notify(@RequestBody String json){
        logger.info("smsNotify");
        return smsService.smsNotify(json);
    }
}
