package com.fs.company.controller;

import com.fs.common.annotation.Log;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.param.SmsSendBatchParam;
import com.fs.common.param.SmsSendParam;
import com.fs.common.param.SmsSendStoreOrderParam;
import com.fs.common.service.ISmsService;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanySms;
import com.fs.company.domain.CompanyUserCard;
import com.fs.company.service.ICompanySmsService;
import com.fs.company.service.ICompanySmsTempService;
import com.fs.company.service.ICompanyUserCardService;
import com.fs.company.vo.CompanySmsListVO;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公司短信Controller
 *
 * @author fs
 * @date 2023-01-09
 */
@RestController
@RequestMapping("/company/companySms")
public class CompanySmsController extends BaseController
{
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ICompanySmsService companySmsService;
    @Autowired
    private ISmsService smsService;

    /**
     * 查询公司短信列表
     */
    @PreAuthorize("@ss.hasPermi('company:companySms:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanySms companySms)
    {
        startPage();
        List<CompanySmsListVO> list = companySmsService.selectCompanySmsList(companySms);
        return getDataTable(list);
    }

    /**
     * 导出公司短信列表
     */
    @PreAuthorize("@ss.hasPermi('company:companySms:export')")
    @Log(title = "公司短信", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanySms companySms)
    {
        List<CompanySmsListVO> list = companySmsService.selectCompanySmsList(companySms);
        ExcelUtil<CompanySmsListVO> util = new ExcelUtil<CompanySmsListVO>(CompanySmsListVO.class);
        return util.exportExcel(list, "companySms");
    }

    /**
     * 获取公司短信详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companySms:query')")
    @GetMapping(value = "/{smsId}")
    public AjaxResult getInfo(@PathVariable("smsId") Long smsId)
    {
        return AjaxResult.success(companySmsService.selectCompanySmsById(smsId));
    }

    /**
     * 新增公司短信
     */
    @PreAuthorize("@ss.hasPermi('company:companySms:add')")
    @Log(title = "公司短信", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanySms companySms)
    {
        return toAjax(companySmsService.insertCompanySms(companySms));
    }

    /**
     * 修改公司短信
     */
    @PreAuthorize("@ss.hasPermi('company:companySms:edit')")
    @Log(title = "公司短信", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanySms companySms)
    {
        return toAjax(companySmsService.updateCompanySms(companySms));
    }

    /**
     * 删除公司短信
     */
    @PreAuthorize("@ss.hasPermi('company:companySms:remove')")
    @Log(title = "公司短信", businessType = BusinessType.DELETE)
	@DeleteMapping("/{smsIds}")
    public AjaxResult remove(@PathVariable Long[] smsIds)
    {
        return toAjax(companySmsService.deleteCompanySmsByIds(smsIds));
    }

    @ApiOperation("发送短信")
    @PostMapping("/sendCustomerMsg")
    @RepeatSubmit
    @Transactional
    public R sendCustomerMsg(@Validated @RequestBody SmsSendParam param){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        param.setCompanyUserId(loginUser.getUser().getUserId());
        return smsService.sendSms(param);
    }

    @ApiOperation("订单发送短信")
    @PostMapping("/sendStoreOrderMsg")
    @RepeatSubmit
    @Transactional
    public R sendStoreOrderMsg(@Validated @RequestBody SmsSendStoreOrderParam param){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        param.setCompanyUserId(loginUser.getUser().getUserId());
        return smsService.sendStoreOrderSms(param);
    }

    @ApiOperation("批量发送短信")
    @PostMapping("/sendCustomerBatchMsg")
    @RepeatSubmit
    @Transactional
    public R sendCustomerBatchMsg(@Validated @RequestBody SmsSendBatchParam param){
        if(param.getCustomerIds().length>500){
            return R.error("批量短信一次最多500条");
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        param.setCompanyUserId(loginUser.getUser().getUserId());
        //return  R.ok();
        return smsService.sendBatchSms(param);
    }


    @GetMapping("/getCompanySmsCount")
    public R getCompanySmsCount( )
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CompanySms companySms=companySmsService.selectCompanySmsByCompanyId(loginUser.getCompany().getCompanyId());
        return R.ok().put("data",companySms);
    }
}
