package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.common.param.SmsSendBatchParam;
import com.fs.common.param.SmsSendParam;
import com.fs.app.utils.JwtUtils;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.common.service.ISmsService;
import com.fs.common.utils.ServletUtils;
import com.fs.company.domain.CompanySms;
import com.fs.company.domain.CompanySmsTemp;
import com.fs.company.param.CompanySmsTempListQueryParam;
import com.fs.company.service.*;
import com.fs.company.vo.CompanySmsTempListQueryVO;
import com.fs.crm.service.ICrmCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api("短信接口")
@RestController
@RequestMapping(value="/app/sms")
public class SmsController extends  AppBaseController {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
    JwtUtils jwtUtils;

	@Autowired
	private ICompanySmsTempService smsTempService;

	@Autowired
	private ICompanySmsService companySmsService;

	@Autowired
	private ICompanySmsLogsService smsLogsService;

	@Autowired
	private ICrmCustomerService crmCustomerService;

	@Autowired
	private ISmsService smsService;

	@Login
	@ApiOperation("获取短信模版列表")
	@GetMapping("/getSmsTempList")
	public R getSmsTempList(){
		CompanySmsTempListQueryParam map=new CompanySmsTempListQueryParam();
		map.setStatus(1);
		map.setIsAudit(1);
		map.setCompanyId(getCompanyId());
		List<CompanySmsTempListQueryVO> smsTemp=smsTempService.selectCompanySmsTempListQuery(map);
		return R.ok().put("data",smsTemp);

	}

	@Login
	@ApiOperation("发送短信")
	@PostMapping("/sendCustomerMsg")
	@RepeatSubmit
	@Transactional
	public  R sendCustomerMsg(@Validated @RequestBody SmsSendParam param){
		param.setCompanyId(getCompanyId());
		param.setCompanyUserId(Long.parseLong(getUserId()));
		return smsService.sendSms(param);
	}

	@Login
	@ApiOperation("发送短信")
	@PostMapping("/sendCustomerBatchMsg")
	@RepeatSubmit
	@Transactional
	public  R sendCustomerBatchMsg(@Validated @RequestBody SmsSendBatchParam param){
		param.setCompanyId(getCompanyId());
		param.setCompanyUserId(Long.parseLong(getUserId()));
		return smsService.sendBatchSms(param);
	}


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


	@Login
	@ApiOperation("获取短信数量")
	@GetMapping("/getCompanySmsCount")
	public R getCompanySmsCount( ) {
		CompanySms companySms=companySmsService.selectCompanySmsByCompanyId(getCompanyId());
		return R.ok().put("data",companySms);
	}

}
